var ie = document.all != null;
var mz = !ie && document.getElementById!= null ;// && document.layers == null;
var mac = (navigator.platform.indexOf('Mac') >= 0);

function postStats(){
	if(getCookie('stats') != 'saved'){
		try{
		getResponse('/stats.update/?screenWidth='+screen.width+'&screenHeight='+screen.height);
		expDate = new Date();
		expDate = new Date(expDate.getTime()+1000*60*60*24);
		setCookie('stats', 'saved', expDate);
		}catch(e){}
	}
}

postStats();

function setCookie(name, value, expires){
    document.cookie= name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "");
}

function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1){
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else{
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1){
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain){
    if (getCookie(name)){
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") +           "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


function openPhotoManager() {
	window.open('/photo/profile/', 'drphilphoto', 'height=465,width=720,resize=true');
}

String.prototype.trim = function() {
	var r = /^\s+|\s+$/;
	var a = this.split(/\n/g),
	i=a.length;
	while(i-->0){
		a[i]=a[i].replace(r,'');
	}
	return a.join('\n');
}

function toggle(containerID) {
	var container = document.getElementById(containerID);
	if(container) {
		container.style.display = (container.style.display == 'none') ? 'block' : 'none';
		return(container.style.display);
	}
	return(0);
}

function toggleOn(containerID) {
	var container = document.getElementById(containerID);
	if(container) {
		container.style.display = 'block';
	}
	return(0);
}

function toggleOff(containerID) {
	var container = document.getElementById(containerID);
	if(container) {
		container.style.display = 'none';
	}
	return(0);
}


function toggleInl(containerID) {
	var container = document.getElementById(containerID);
	if(container)
		container.style.display = (container.style.display == 'none') ? 'inline' : 'none';
}

function checkRequired(myElements) {
	for(element in myElements) {
		if(myElements[element].value.trim() == '') {
			alert('Error: [' + ((myElements[element].title) ? myElements[element].title : myElements[element].name) + '] is a required field.');
			if(myElements[element].type != 'hidden')
				myElements[element].focus();
			return(false);
		}
	}
	return(true);
}

// don't put unnesessary scripts here

function showSmallPop(url,name) {
	alert('deprecated!');
	options = "toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,width=470,height=550";
	newwindow=window.open(url,name, options);
	newwindow.document.close();
	window.name='phil_main';
}

function slideshow(myID,myXML) {
	var href = window.location.href.split('/');
	var showID = 0;
	if( (href[3] == 'shows') &&
		(href[4] == 'show') ) {
		showID = href[5];
	}

	var pos = 0;
	var extra = (window.location.href.indexOf('preview=1') > 0) ? '&preview=1' : '';
	pos = window.location.href.indexOf('versionID=');
	var extra2 = (pos > 0) ? ('&versionID=' + ((window.location.href.substring(pos + 10, pos + 11)))) : '';

	window.location.href = '/slideshows/slideshow/' + myID +'/?id=' + myID + '&showID=' + showID + extra + extra2;
}

function tip(myID,myXML) {
	slideshow(myID,myXML, 'isTip');
}

function poll(myID) {
	var myOptions = 'toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,width=470,height=550';
	newwindow = window.open('/polls/poll/' + myID + '/', 'poll', myOptions);
}

function popup(URL, w, h, options) {
	if(!options) options = 'toolbar=0,menubar=0,status=1,locationbar=no,scrollbars=0,resizable=yes,top=100,left=100';
	if(!w) w = 505;
	if(!h) h= 485;
	aWindow = window.open(URL, 'newWin', 'width='+w+',height='+h+','+options);
}	

function drphilstore(){
	window.open("http://www.drphilstore.com/", "DrPhilStore", "");
}

function tapesandtranscripts(){
	popup('http://tapesandtranscripts.drphil.com', 530, 490, 'toolbar=0,menubar=0,status=1,locationbar=no,scrollbars=1,resizable=yes,top=100,left=100');
}

function validateEmail(str){
	var emailRegEx = /^[a-z0-9\._&-]+@+[a-z0-9\._-]+\.+[a-z]{2,6}$/;
//	var emailRegEx = /^[a-zA-Z0-9][\w\._-]@[a-zA-Z0-9][\w\._-]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	return (emailRegEx.test(str.toLowerCase()));
}

function countDown(tick) {
	if (tick == 0) {
		if(confirm('You are about to be logged out due to inactivity. Clicking OK will keep you logged in. Clicking Cancel or not responding will log you out.')) {
			countDown(5400);
		} else {
			location.href='/members/logout/';
		}
		return;
	}
	var time = "Time remaining till you are automatically logged out. ";
	var minute = Math.floor(tick / 60);
	if (minute < 10) {
		time += "0";
	}
	time += minute + ":";
	var second = tick % 60;
	if (second < 10) {
		time += "0";
	}
	time += second;
//	window.status = time;
	--tick;
	var command = "countDown(" + tick + ")";
	window.setTimeout(command,1000);
}

function SymError(err){
//	alert(err); 
 	return true;
}

//window.onerror = SymError;

//-----------------------------------------------------------
function startList() {
	if (document.getElementById) {
		navRoot = document.getElementById('menu');
		for (i = 0; i < navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName == 'LI') {
				node.onmouseover = function() {
					this.className += ' over';
				}
				node.onmouseout = function() {
					this.className = this.className.replace(' over', '');
					//setTimeout('HideAllOver();', 500);
				}
			}
		}
	}
}

function HideAllOver() {
	var E = GetElementsByClassName(document.getElementById('Menu'), 'LI', 'base over');
	for (var i = 0; i < E.length; i++) {
		E[i].className = E[i].className.replace(' over', '');
		return;
	}
}
//-----------------------------------------------------------

function GetElementsByClassName(MyContainer, MyTagName, MyClassName) {
	var MyElements = new Array();
	if (MyContainer) {
		var MyTags = MyContainer.getElementsByTagName(MyTagName);
		for (var w = 0; w < MyTags.length; w++) {
			if(MyTags[w].className.match(new RegExp("(^|\\s)" + MyClassName + "(\\s|$)")))
				MyElements.push(MyTags[w]);
		}
	}
	return MyElements;
}

// Debug ----------------------------------------------------
function Debug(MyObject) {
	var MyHeader  = '//VPI_JS_Debug-----------------------------------------------' + "\n";
		MyHeader += (MyObject['tagName'] ? 'Tag Name: ' + MyObject['tagName'] + "\n" : '');
		MyHeader += 'Type: ' + (typeof MyObject) + "\n";
	var MyFooter  = "\n\n" + '//----------------------------------------------------------------';
	var z = 0;
	var y = 0;
	var MySplit = 'Page [' + ++y + ']' + "\n" + '//----------------------------------------------------------------' + "\n\n";
	var MyMessage = '';
	for(var i in MyObject) {
		MyMessage += '[' + i + '] -> ' + MyObject[i] + "\n";
		if(z++ > 15) {
			alert(MyHeader + MySplit + MyMessage + MyFooter);
			z = 0;
			MySplit = 'Page [' + ++y + ']' + "\n" + '//----------------------------------------------------------------' + "\n\n";
			MyMessage = '';
		}
	}
	alert(MyHeader + MySplit + MyMessage + MyFooter);
}
//-----------------------------------------------------------

function PopVideo( MyURL ) {
	var Height = 350;
	var Width = 400;

	if(Height > screen.height)
		Height = screen.height;
	if(Width > screen.width)
		Width = screen.width;

	var MyOptions = 'toolbar=no,status=no,menubar=no,scrollbars=no,resizable=yes,width='+Width+',height='+Height;

	var background = (arguments[1] != null) ? 'background=' + arguments[1] : '';
	background = ((MyURL) ? '&' : '?') + background;

	NewWindow = window.open('/videos/' + ( ( MyURL ) ? '?Url=' + MyURL : '' ) + background, 'video', MyOptions);

	if(Height > screen.height) {
		NewWindow.myMaxHeight = Height - 160;
	} else {
		NewWindow.myMaxHeight = null;
	}
}

//------------------------------------------------------[TG]
function Populate(Data) {
	return false;

	var Data = eval('(' + unescape(Data.substr(0, Data.indexOf('=&onLoad=', ''))) + ')');

	return;

	alert(Data);
	Debug(Data);

	if (!Data['id']) return;

	if (Data['birthday'])
		Data['birthday'] = Data['birthday'].split('-');

	document.getElementById('30').value = Data['firstname'];
	document.getElementById('31').value = Data['lastname'];

	var Year = document.getElementById('birthYear');
	for (var i = 0; i < Year.options.length; i++) {
		if (Year.options[i].value == parseInt(Data['birthday'][0])) {
			Year.selectedIndex = i;
			break;
		}
	}
	var Month = document.getElementById('birthMonth');
	for (var i = 0; i < Month.options.length; i++) {
		if (Month.options[i].value == parseInt(Data['birthday'][1])) {
			Month.selectedIndex = i;
			break;
		}
	}
	var Day = document.getElementById('birthDay');
	for (var i = 0; i < Day.options.length; i++) {
		if (Day.options[i].value == parseInt(Data['birthday'][2])) {
			Day.selectedIndex = i;
			break;
		}
	}

	document.getElementById('33').value = Data['email'];

	document.getElementById('34').value = Data['address1'] + ' ' + Data['address2'];
	document.getElementById('35').value = Data['city'];

//	document.getElementById('36').value = Data['state']; // State

	document.getElementById('38').value = Data['zipcode'];
	document.getElementById('40').value = Data['phone'];
	document.getElementById('48').value = Data['cell'];

}
//------------------------------------------------------[TG]