// JavaScript Document

/*	NOTES	-----
	- onload function for biz profiles is located in bcomments.php - only 1/page worx
	- "ctest" is name of master overlay
*/

//	os & browser detection:	----------------------------------------------------------------

var agt = navigator.userAgent.toLowerCase();
var is_PC = navigator.appVersion.indexOf("Win")!=-1;
var is_MAC = navigator.appVersion.indexOf("Mac")!=-1;
var is_Lin = navigator.appVersion.indexOf("Linux")!=-1;
var is_IE = (agt.indexOf("msie") != -1) && (agt.indexOf("opera") < 0);
var is_IE6 = (agt.indexOf("msie 6") != -1) && (agt.indexOf("opera") < 0);
var is_IE7 = (agt.indexOf("msie 7") != -1) && (agt.indexOf("opera") < 0);
var is_IE8 = (agt.indexOf("msie 8") != -1) && (agt.indexOf("opera") < 0);
var is_IE9 = (agt.indexOf("msie 9") != -1) && (agt.indexOf("opera") < 0);
var is_FF = (agt.indexOf("mozilla") != -1) && (agt.indexOf("msie") < 0) && (agt.indexOf("chrome") < 0) && (agt.indexOf("safari") < 0);
var is_FF4 = (agt.indexOf("firefox/5") != -1) || (agt.indexOf("firefox/6") != -1) || (agt.indexOf("firefox/7") != -1) || (agt.indexOf("firefox/8") != -1) && (agt.indexOf("windows nt 6") != -1) && (agt.indexOf("msie") < 0) && (agt.indexOf("chrome") < 0) && (agt.indexOf("safari") < 0); // FF5 on win7
var is_SAF = (agt.indexOf("safari") != -1) && (agt.indexOf("msie") < 0) && (agt.indexOf("chrome") < 0);
var is_Chrome = (agt.indexOf('chrome') != -1);
var is_AOL = (agt.indexOf('aol') != -1);

//	miscellaneous:	------------------------------------------------------------------------

var loader = false;
//var petedit = false;
var searching = false;
window.onload = function() {
	bgImageRelocate();
	document.getElementById('etest').className = 'hidem';
	if (is_IE6 || is_IE7) {	// fix body overflow for IE6 & 7
		//document.body.style.overflowX = 'visible';
		//document.body.style.overflowY = 'visible';
		document.body.style.overflow = 'visible';
	}
	if (loader == true) {
		prepareSlideshow();
		commentsHeight();
		func1();
	}
/*	if (petedit == true) {
		fillpettype();
	} */
	//if (searching == true) {
		fillCountry();
	//}
}

function openCloseBoard() {	// message boards
	if (document.getElementById('mboard').className == 'showm')	{
		document.getElementById('mboard').className = 'hidem';
		//document.getElementById('board').style.backgroundImage = 'url(images/top_comments+.jpg)';
	} else if (document.getElementById('mboard').className == 'hidem') {
		document.getElementById('mboard').className = 'showm';
		//document.getElementById('board').style.backgroundImage = 'url(images/top_comments-.jpg)';
	}
}

function textCounter(field,cntfield,maxlimit) {	// input text char counter
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	else // otherwise, update 'characters left' counter
		cntfield.value = maxlimit - field.value.length;
}

function setOpacity(value) {
	this.style.opacity = value/10;
	this.style.filter = 'alpha(opacity=' + value*10 + ')';
}

//	overlay shit:	------------------------------------------------------------------------

function pageHeight() {return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;}
function pageWidth() {return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;}

function ctestResize() {
	document.getElementById('ctest').style.width = pageWidth() + 'px';
	document.getElementById('ctest').style.height = pageHeight() + 'px';
}

function overlayCenter(oc) {
	document.getElementById(oc).style.top = Math.round((pageHeight() - document.getElementById(oc).offsetHeight) / 2) + 'px';
	document.getElementById(oc).style.left = Math.round((pageWidth() - document.getElementById(oc).offsetWidth) / 2)-10 + 'px';
}

window.onresize = function() {		// global repositioning on-resize
	var currheight;
	var currwidth;
	if(currheight != pageHeight() || currwidth != pageWidth()) {
		bgImageRelocate();
		ctestResize();
		if (pageName == 'sendecard') { overlayCenter('addfrend'); }
		else if (pageName == 'ecard') { overlayCenter('previewtbl'); overlayCenter('freecardtbl'); }
		else if (pageName == 'giveback') { overlayCenter('charitytbl'); }
		else if (pageName == 'albumspage') {
			for (var pno=1; pno<=photoNum; pno++) {
				overlayCenter('albumstbl'+pno);
				// need to figure a way to do this for 'album-section' pages as well
			}
		}
		if (pageNameLog == 'login') { overlayCenter('forgotpasstbl'); }
	}
	currheight = pageHeight();
	currwidth = pageWidth();
}
////
function hideSelect() { // IE6 hide select boxes
	if (is_IE6) {
		var svn=document.getElementsByTagName("SELECT");
		for (var dkii=0; dkii<svn.length; dkii++) {
			document.getElementsByTagName('select')[dkii].className='hidesel';
		}
	}
}
function showSelect() { // IE6 show select boxes
	if (is_IE6) {
		var svn=document.getElementsByTagName("SELECT");
		for (var dkii=0; dkii<svn.length; dkii++) {
			document.getElementsByTagName('select')[dkii].className='showsel';
		}
	}
}
////
function showMsg5(Pn) { //	eCard Pick-up show
	document.getElementById('epick').className = 'showm';
	document.ecpicker.ECPcode.focus(); document.ecpicker.ECPcode.value = '';
	if (is_IE7) {} else { document.getElementById('xclose1').style.margin = document.getElementById('epick').offsetHeight / 2 - 9 + 'px'; document.getElementById('xclose1').style.cssFloat = 'right'; document.getElementById('xclose1').style.styleFloat = 'right'; }
	if (document.getElementById('forgotpass') != null && document.getElementById('forgotpass').className == 'showm') hideforgotPass();
}
function hideMsg5(Pn) {	// eCard Pick-up hide
	document.getElementById('epick').className = 'hidem';
}
////
function showFreePop() { //	annoying free popup show
	ctestResize();
	document.getElementById('ctest').className = 'showm';
	document.getElementById('freepop').className = 'showm';
	overlayCenter('freepoptbl');
	hideSelect();
}
function hideFreePop(Pn) {	// annoying free popup hide
	document.getElementById('freepop').className = 'hidem';
	document.getElementById('ctest').className = 'hidem';
	showSelect();
}
////
function showFreecard() {
	ctestResize();
	document.getElementById('ctest').className = 'showm';
	document.getElementById('freecard').className = 'showm';
	overlayCenter('freecardtbl');
	hideSelect();
}
function hideFreecard() {
	document.getElementById('freecard').className = 'hidem';
	document.getElementById('ctest').className = 'hidem';
	showSelect();
}
////
function showNext(em) {
	document.getElementById('D'+em).className = 'showm';
	overlayCenter('albumstbl'+em);
	var hd=em-1;
	document.getElementById('D'+hd).className = 'hidem';
}
function showPrev(em) {
	document.getElementById('D'+em).className = 'showm';
	overlayCenter('albumstbl'+em);
	var hd=em+1;
	document.getElementById('D'+hd).className = 'hidem';
}
////
function hideMsg2(em) {		// albums hide
	document.getElementById('D'+em).className = 'hidem';
	document.getElementById('ctest').className = 'hidem';
	showSelect();
}
function showMsg2(em) {		// albums show
	ctestResize();
	document.getElementById('ctest').className = 'showm';
	document.getElementById('D'+em).className = 'showm';
	overlayCenter('albumstbl'+em);
	hideSelect();
}
////
function hideChar() {		// charity hide
	document.getElementById('charity').className = 'hidem';
	document.getElementById('ctest').className = 'hidem';
	showSelect();
}
function showChar(a,b,c,d,e) {		// charity show
	// a = logo; b = title; c = desc; d = website
	//ctestResize();
	document.getElementById('ctest').className = 'showm';
	document.getElementById('charity').className = 'showm';
	//overlayCenter('charitytbl');
	hideSelect();
	var logop='image2.php/'+a+'?width=60&height=60&image=/charlogos/'+a;
	document.getElementById('mlogo').src = logop;
	//document.getElementById('mtitle').innerHTML = b;
	document.getElementById('mdesc').innerHTML = c;
	document.getElementById('mnote').innerHTML = d;
	document.getElementById('murl').innerHTML = b;
	document.getElementById('murl').href = e;
}
////
function hideAddFriend() {
	document.getElementById('btest').className = 'hidem';
	document.getElementById('ctest').className = 'hidem';
	showSelect();
}
function showAddFriend() {
	hideSelect();
	ctestResize();
	document.getElementById('ctest').className = 'showm';
	document.getElementById('btest').className = 'showm';
	overlayCenter('addfrend');
	document.getElementById('Fname').focus();
}
////
function hidePv() {		// eCard previewer
	document.getElementById('previewer').className = 'hidem';
	document.getElementById('ctest').className = 'hidem';
	showSelect();
}
function showPv(detype,dmt,fb) {	// eCard previewer
	ctestResize();
	document.getElementById('ctest').className = 'showm';
	document.getElementById('previewer').className = 'showm';
	overlayCenter('previewtbl');
	if(dmt=='W') {
		if(detype==1) {
			document.getElementById('prebttns').className = 'hidem';
			document.getElementById('Dkfree').className = 'showm';
		} else {
			document.getElementById('Dkfree').className = 'hidem';
			document.getElementById('prebttns').className = 'showm';
		}
	} else {
		if(dmt=='F'|| fb=='Y') {
			if(detype==1) {
				document.getElementById('preloggedinFF').className = 'hidem';
				document.getElementById('preloggedin').className = 'showm';
			} else if(detype==0) {
				document.getElementById('preloggedin').className = 'hidem';
				document.getElementById('preloggedinFF').className = 'showm';
			}
		} else {
			document.getElementById('preloggedinFF').className = 'hidem';
			document.getElementById('preloggedin').className = 'showm';
		}
	}
	hideSelect();
}
////
function showTut() {
	ctestResize();
	document.getElementById('ctest').className = 'showm';
	document.getElementById('tut').className = 'showm';
	overlayCenter('tuttbl');
	document.ecpicker.ECPcode.focus();
	hideSelect();
	if(Pn==999) {
		for(var ccc=1;ccc<32;ccc++) {			// to hide new message box  in calendar
			if(document.getElementById('dtest'+ccc).className == 'showm') {
				document.getElementById('dtest'+ccc).className = 'hidem';
			}
		}
		for (cc=0; cc<datewithmess.length; cc++) {
			if(document.getElementById('btest'+datewithmess[cc]).className == 'showm') {
				document.getElementById('btest'+datewithmess[cc]).className = 'hidem';
			}
		}
	}
	if(Pn==888) {
		document.getElementById('dmtest').className = 'hidem';
	}
}
function hideTut() {
	document.getElementById('tut').className = 'hidem';
	document.getElementById('ctest').className = 'hidem';
	showSelect();
}

function bgImageRelocate() {
	var widepage = document.getElementById('tutpage');
	if (document.body.clientWidth <= 982) {
		if (!widepage)
			document.body.style.backgroundPosition = "166px";
		else
			document.body.style.backgroundPosition = "0";
	} else {
		if (is_SAF || is_Chrome || is_IE6) {
			if (!widepage)
				document.body.style.backgroundPosition = Math.floor(document.body.clientWidth/2) - 325 + 'px';
			else
				document.body.style.backgroundPosition = Math.floor(document.body.clientWidth/2) - 491 + 'px';
		} else {
			document.body.style.backgroundPosition = "center";
		}
	}
}

function commentsHeight() { // max-height for IE6
	//alert(document.getElementById('bcomments').offsetHeight);
	if (is_IE6) {
		if (document.getElementById('bcomments').offsetHeight > 300) {
			document.getElementById('dcomments').style.height='300px';
		}
		//alert(document.getElementById('bcomments').offsetHeight);
		//alert(document.getElementById('dcomments').style.height);
	}
}

//	swap image loader:	------------------------------------------------------------

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

MM_preloadImages();

//	Ram's form checkers:	----------------------------------------------------------

function fregcheck() { // free ecard sign-up
	frvalid = true;
	//var tt = false;
	//var rg = false;
	//var ff = true;
	//ff = fcheck();
	if (document.getElementById('FFname').value=='') {
		frvalid = false;
		alert('Please enter your first name');
	} else if(document.getElementById('FLname').value=='') {
		frvalid = false;
		alert('Please enter your last name');
	} else if(document.getElementById('FPass').value=='') {
		frvalid = false;
		alert('Please enter a password');
	} else if(document.getElementById('FEmail').value!=document.getElementById('FREmail').value) {
		frvalid = false;
		alert('\'Email\' and \'Confirm Email\' do not match');
	} else if(document.getElementById('FPass').value!=document.getElementById('FRPass').value) {
		frvalid = false;
		alert('\'Password\' and \'Confirm Password\' do not match');
	} else if(document.getElementById('FTC').checked!=true) {
		frvalid = false;
		alert('Please confirm that you have read, understood, and agree to the Terms and Conditions.');
	}
	return frvalid;
}

function createObject() {
	var xmlHttp = null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		//Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}			

function chkfreeemail() {
	var xmlHttp = createObject();
	if (xmlHttp == null) {
		alert('Browser doest not support HTTP Request');
		return;
	}
	var nocache = Math.random();
	//var qur ="bizprofilephoto.php?Fno="+filenamestr+'&nocache='+nocache; 
	var mchk=true;
	var mcode;
	mcode= document.fecardregform.FEmail.value;
	//alert(mcode);

	var qur ="remailcheck.php?CE="+mcode+'&nocache='+nocache; 
	xmlHttp.open('GET', qur,true);
	xmlHttp.send(null); 
	xmlHttp.onreadystatechange = function()	{
		if (xmlHttp.readyState == 0) {
			document.fecardregform.freeemailchk.value = '';
		}
		if (xmlHttp.readyState == 4) {
			document.fecardregform.freeemailchk.value = xmlHttp.responseText;
			frechk();
		}
	}
}

function frechk() {
	var efrechk = false;
	if(document.fecardregform.freeemailchk.value == 'F') {
		efrechk = false;
		alert('This email ID already exists. Please use a different email ID.');
	} else {
		efrechk = fregcheck();
		if(efrechk == true) {
			document.fecardregform.submit();
		}
	}
	return efrechk;
}

function frchk() {
	//alert("hello"+document.fecardregform.FEmail.value);
	var ffchk = false;
	if(document.fecardregform.FEmail.value == '')	{
		ffchk = false;
		alert('Please enter Email address');
	} else {
		chkfreeemail();
	}
	return ffchk;
}

//	eCard functions	-------------------------------------------------
function createObject() {
	var xmlHttp=null;
	try {	// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {	//Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function setSessionPEFN(filenamestr,defree,mt,fb) {
	var xmlHttp = createObject();
	if (xmlHttp==null) {
		alert("Browser doest not support HTTP Request");
		return;
	}
	var nocache = Math.random();
	//alert(filenamestr);
	var qur ="setPEFN.php?ecn="+filenamestr+'&nocache='+nocache;
	xmlHttp.open('GET', qur,true);
	xmlHttp.send(null); 
	xmlHttp.onreadystatechange = function()	{
		if (xmlHttp.readyState == 4 ) {
			if(defree==1) {
				showPv(1,mt,fb);
			} else {
				showPv(0,mt,fb);
				//setTimeout('showPv('+0,mt,fb+')', 500);
			}
		}
	}
}
