var strBrowser = navigator.appName;
var intContactsCounter = 0;

var obj_challenge_on = new Image(); obj_challenge_on.src = "/gfx/side_menu/challenge_on.jpg";
var obj_challenge_off = new Image(); obj_challenge_off.src = "/gfx/side_menu/challenge_off.jpg";
var obj_home_on = new Image(); obj_home_on.src = "/gfx/side_menu/home_on.jpg";
var obj_home_off = new Image(); obj_home_off.src = "/gfx/side_menu/home_off.jpg";
//var obj_home_on = new Image(); obj_home_on.src = "/gfx/side_menu/home_on.jpg";
//var obj_home_off = new Image(); obj_home_off.src = "/gfx/side_menu/home_off.jpg";
var obj_leader_on = new Image(); obj_leader_on.src = "/gfx/side_menu/leader_on.jpg";
var obj_leader_off = new Image(); obj_leader_off.src = "/gfx/side_menu/leader_off.jpg";
var obj_prizelist_on = new Image(); obj_prizelist_on.src = "/gfx/side_menu/prizelist_on.jpg";
var obj_prizelist_off = new Image(); obj_prizelist_off.src = "/gfx/side_menu/prizelist_off.jpg";
var obj_quizzes_on = new Image(); obj_quizzes_on.src = "/gfx/side_menu/quizzes_on.jpg";
var obj_quizzes_off = new Image(); obj_quizzes_off.src = "/gfx/side_menu/quizzes_off.jpg";
var obj_rssfeed_on = new Image(); obj_rssfeed_on.src = "/gfx/side_menu/rssfeed_on.jpg";
var obj_rssfeed_off = new Image(); obj_rssfeed_off.src = "/gfx/side_menu/rssfeed_off.jpg";
var obj_winners_on = new Image(); obj_winners_on.src = "/gfx/side_menu/winners_on.jpg";
var obj_winners_off = new Image(); obj_winners_off.src = "/gfx/side_menu/winners_off.jpg";
var obj_register_on = new Image(); obj_register_on.src = "/gfx/side_menu/register_on.jpg";
var obj_register_off = new Image(); obj_register_off.src = "/gfx/side_menu/register_off.jpg";
var obj_account_on = new Image(); obj_account_on.src = "/gfx/side_menu/account_on.jpg";
var obj_account_off = new Image(); obj_account_off.src = "/gfx/side_menu/account_off.jpg";
var obj_widget_on = new Image(); obj_widget_on.src = "/gfx/side_menu/widget_on.jpg";
var obj_widget_off = new Image(); obj_widget_off.src = "/gfx/side_menu/widget_off.jpg";
var obj_hangman_on = new Image(); obj_hangman_on.src = "/gfx/side_menu/hangman_on.jpg";
var obj_hangman_off = new Image(); obj_hangman_off.src = "/gfx/side_menu/hangman_off.jpg";

var obj_tm_home_on = new Image(); obj_tm_home_on.src = "/gfx/top_menu/home_on.jpg";
var obj_tm_home_off = new Image(); obj_tm_home_off.src = "/gfx/top_menu/home_off.jpg";
var obj_tm_quizzes_on = new Image(); obj_tm_quizzes_on.src = "/gfx/top_menu/quizzes_on.jpg";
var obj_tm_quizzes_off = new Image(); obj_tm_quizzes_off.src = "/gfx/top_menu/quizzes_off.jpg";
var obj_tm_encyclopedia_on = new Image(); obj_tm_encyclopedia_on.src = "/gfx/top_menu/encyclopedia_on.jpg";
var obj_tm_encyclopedia_off = new Image(); obj_tm_encyclopedia_off.src = "/gfx/top_menu/encyclopedia_off.jpg";
var obj_tm_dictionary_on = new Image(); obj_tm_dictionary_on.src = "/gfx/top_menu/dictionary_on.jpg";
var obj_tm_dictionary_off = new Image(); obj_tm_dictionary_off.src = "/gfx/top_menu/dictionary_off.jpg";
var obj_tm_onthisday_on = new Image(); obj_tm_onthisday_on.src = "/gfx/top_menu/onthisday_on.jpg";
var obj_tm_onthisday_off = new Image(); obj_tm_onthisday_off.src = "/gfx/top_menu/onthisday_off.jpg";
var obj_tm_biography_on = new Image(); obj_tm_biography_on.src = "/gfx/top_menu/biography_on.jpg";
var obj_tm_biography_off = new Image(); obj_tm_biography_off.src = "/gfx/top_menu/biography_off.jpg";

function returnStyleSheet() {
	var strCSS = "<link rel=\"stylesheet\" href=\"/incl/style.css\" type=\"text/css\" />";
	if(navigator.userAgent.toLowerCase().indexOf('msie') < 0) strCSS = "<link rel=\"stylesheet\" href=\"/incl/styleff.css\" type=\"text/css\" />";
	document.write(strCSS);
}

function mouseOverLeftmenu(paramId) {
	document.getElementById(String(paramId)).src = eval("obj_"+paramId+"_on").src;
}

function mouseOutLeftmenu(paramId, paramLoggedIn) {
	document.getElementById(String(paramId)).src = eval("obj_"+paramId+"_off").src;
	setActiveLeftMenuItem(paramLoggedIn);
}

function mouseOverTopmenu(paramId) {
	document.getElementById(String("tm_"+paramId)).src = eval("obj_tm_"+paramId+"_on").src;
}

function mouseOutTopmenu(paramId) {
	document.getElementById(String("tm_"+paramId)).src = eval("obj_tm_"+paramId+"_off").src;
	setActiveTopMenuItem();
}

function setActiveLeftMenuItem(paramLoggedIn) {
	var strSection = "";
	var strURL = String(location.href);
	if(String(strURL) == "http://www.alltheanswers.com/") strSection = "";	//"home"
	if(String(strURL).indexOf("/quiz/") >-1) strSection = "quizzes";
	if(String(strURL).indexOf("/myaccount/") >-1) strSection = ""; //(String(paramLoggedIn)=="true")?"account":"register";
	if(String(strURL).indexOf("/tipfriends/") >-1) strSection = "challenge";
	if(String(strURL).indexOf("/rss/") >-1) strSection = "rssfeed";
	if(String(strURL).indexOf("/winnerlist/") >-1) strSection = "winners";
	if(String(strURL).indexOf("/prizelist/") >-1) strSection = "prizelist";
	if(String(strURL).indexOf("/leaderboard") >-1) strSection = "leader";
	if(String(strURL).indexOf("/widget") >-1) strSection = "widget";
	if(String(strURL).indexOf("/hangman") >-1) strSection = "hangman";
	if(String(strSection) != "") {
		try {
			document.getElementById(strSection).src = eval("obj_"+strSection+"_on").src;
		} catch (e)	{}
	}
}

function setActiveTopMenuItem() {
	var strURL = String(location.href);
	var strSection = "";
	if(String(strURL).indexOf("/search") >-1) {
		document.getElementById("tm_search").style.background = "url(/gfx/top_menu/search_on.jpg)";
		strSection = "";
	} else {
		document.getElementById("tm_search").style.background = "url(/gfx/top_menu/search_off.jpg)";
	}
	if(String(strURL) == "http://dev.alltheanswers.com/") strSection = "home";
	if(String(strURL).indexOf("/quiz") >-1) strSection = "quizzes";
	if(String(strURL).indexOf("/encyclopedia") >-1) strSection = "encyclopedia";
	if(String(strURL).indexOf("/contentpages2/") >-1) strSection = "encyclopedia";
	if(String(strURL).indexOf("/dictionary") >-1) strSection = "dictionary";
	if(String(strURL).indexOf("/onthisday") >-1) strSection = "onthisday";
	if(String(strURL).indexOf("/biographies") >-1) strSection = "biography";
	if(String(strURL).indexOf("/contentpages/") >-1) strSection = "biography";
	if(strSection != "") document.getElementById("tm_"+strSection).src = eval("obj_tm_"+strSection+"_on").src;
}

function forgotPassword() {
	window.open("/myaccount/forgotpassword.asp","forgotpassword","top=100,left=100,width=975,height=130,scrollbars=0");
}

function verifyNumeric(strInput) {
	if (strInput != '') {
		if (!strInput.match(/[^0-9]/)) return true;
	}
}

function verifyAlphaNumeric(paramInput) {
	var re = new RegExp( /^([a-z0-9-.@_]+)$/i );
	return (re.test(paramInput));
}

function verifyRequestPassword() {
	var strErrorMsg = "";
	var strEmail = document.userform.email.value;
	if(!verifyEmail(strEmail)) {
		strErrorMsg += "Please enter your e-mail address!\n\nIt has to be the one you used, when you created your account!";
	}
	if(String(strErrorMsg) != "") {
		alert(strErrorMsg);
		return false;
	} else {
		return true;
	}
}

function addMSN() {
	window.open("/tipfriends/msncontacts.asp","","top=100,left=100,width=522,height=500,scrollbars=1");
}

function addTip(paramSource) {
	var intNumberOfTips = document.userform.recipientslist.length;
	var stremail = document.userform.addemail.value;
	if (!verifyEmail(stremail)) {
		if(String(paramSource) != "messenger") {
			alert("This e-mail is not valid!");
		}
	} else {
		if(intNumberOfTips > 0) {
			var bAllowTip = true;
			for(var i=0; i<intNumberOfTips; i++) {
				if(stremail == document.userform.recipientslist.options[i].value) {
					bAllowTip = false;
					return false;
				}
			}
			if(bAllowTip) {
				document.userform.recipientslist.options[intNumberOfTips] = new Option(stremail, stremail);
				intContactsCounter++;
				document.userform.addemail.value = "";
				document.userform.addemail.focus();
				document.getElementById("ccontactnum").innerHTML = intContactsCounter;
			}
		} else {
			document.userform.recipientslist.options[intNumberOfTips] = new Option(stremail, stremail);
			intContactsCounter++;
			document.userform.addemail.value = "";
			document.userform.addemail.focus();
			document.getElementById("ccontactnum").innerHTML = intContactsCounter;
			return false;
		}
	}
}

function verifyTips() {
	var strErrorMsg = "";
	var intNumberOfTips = document.userform.recipientslist.length;
	var formSubmitted = false;
	var strTipThem = "";
	if (intNumberOfTips < 1) {
		alert("Please enter the e-mail addresses of the friends, you wish to challenge!");
		return false;
	} else {
		if(formSubmitted) {
			return false;
		} else {
			formSubmitted = true;
			for(var i=0; i<document.userform.recipientslist.length; i++) {
				strTipThem += document.userform.recipientslist.options[i].value + ",";
			}
			strTipThem = strTipThem.substring(0,strTipThem.lastIndexOf(","));
			document.userform.recipients.value = strTipThem;
			return true;
		}
	}
}

function verifyUser() {
	var strErrorMsg = "";

	var strEmail = document.userform.email.value;
	if (!verifyEmail(strEmail)) {
		strErrorMsg += "\n - Please enter your e-mail address!";
	}

	var strPassword = document.userform.password.value;
	var strPassword_2 = document.userform.password_2.value;
	if(String(strPassword) == "") {
		strErrorMsg += "\n - Please enter a password!";
	} else {
		if(!verifyAlphaNumeric(strPassword)) {
			strErrorMsg += "\n - Your password contains invalid characters! (use: a-z 0-9)";
		} else {
			if(String(strPassword) != String(strPassword_2)) {
				strErrorMsg += "\n - The two passwords don't match!";
			}
		}
	}

	var strFirstname = document.userform.firstname.value;
	if(String(strFirstname) == "") {
		strErrorMsg += "\n - Please enter your first name!";
	}
	var strLastname = document.userform.lastname.value;
	if(String(strLastname) == "") {
		strErrorMsg += "\n - Please enter your last name!";
	}
	if(document.userform.title.selectedIndex == 0) {
		strErrorMsg += "\n - Please select your title!";
	}
	if(document.userform.gender.selectedIndex == 0) {
		strErrorMsg += "\n - Please select your gender!";
	}
	var bAdditionalDOBcheck = true;
	if(document.userform.dobday.selectedIndex == 0) {
		bAdditionalDOBcheck = false;
		strErrorMsg += "\n - Please select your DOB: day!";
	}
	if(document.userform.dobmonth.selectedIndex == 0) {
		bAdditionalDOBcheck = false;
		strErrorMsg += "\n - Please select your DOB : dobmonth!";
	}
	if(document.userform.dobyear.selectedIndex == 0) {
		bAdditionalDOBcheck = false;
		strErrorMsg += "\n - Please select your DOB : year!";
	}
	if(!document.userform.terms.checked) {
		strErrorMsg += "\n - Please read and accept the specifications in Privacy Policy and T&C!";
	}
	/*
	if(!document.userform.privacy.checked) {
		strErrorMsg += "\n - Please read and accept the Privacy Policy!";
	}
	*/
	/*
	if((!document.userform.accept_quizzes.checked) && (!document.userform.accept_daily.checked)) {
		strErrorMsg += "\n - Please select your prefered types of e-mails from AllTheAnswers.com!";
	}
	*/
	if(bAdditionalDOBcheck) {
		var strbirthday_day = document.userform.dobday.options[document.userform.dobday.selectedIndex].value;
		var strbirthday_month = document.userform.dobmonth.options[document.userform.dobmonth.selectedIndex].value;
		var strbirthday_year = document.userform.dobyear.options[document.userform.dobyear.selectedIndex].value;
		
		if (!dateCheck(strbirthday_day,strbirthday_month,strbirthday_year)) {
			strErrorMsg += "\n - Please enter a valid DOB!";
		}
	}
	
	if(String(strErrorMsg) != "") {
		alert("You need to correct the following:\n" + strErrorMsg);
		return false;
	} else {
		return true;
	}
}

function verifyDeleteAccount() {
	if(confirm("WARNING!\n\nYou are about to delete your account with AllTheAnswers. Do you want to continue?")) document.location = "/myaccount/profile.asp?src=delete";
}

function preselect(paramForm, paramSource, paramTarget) {
	if(eval('document.'+paramForm+'.'+paramSource+'.checked')) {
		eval('document.'+paramForm+'.'+paramTarget+'.checked = true');
	}
}

function dateCheck(intDay, intMonth, intYear) {
	if (!dayCheck(intDay,intMonth,intYear)) return false;
	if (!monthCheck(intDay,intMonth,intYear)) return false;
	if (!yearCheck(intDay,intMonth,intYear)) return false;
	return true;
}

function dayCheck(intDay, intMonth, intYear) {
	if (!verifyNumeric(intDay)) return false;
	if (intDay < 1 || intDay > 31) return false;
	if (intMonth.match(/4|6|9|11/) && intDay > 30) return false;
	if (intMonth == 2 && intDay > 29) return false;
	if (intYear != 0 && intMonth == 2 && intDay > 28) {
		if (intYear % 100  == 0) {
			if (intYear % 400  != 0) return false;
		} else {
			if (intYear % 4 != 0) return false;
		}
	}
	return true;
}

function monthCheck(intDay, intMonth, intYear) {
	if (!verifyNumeric(intMonth)) return false;
	if (intMonth < 1 || intMonth > 12) return false;
	return true;
}

function yearCheck(intDay, intMonth, intYear) {
	if (!verifyNumeric(intYear)) return false;
	if (intYear < 1900 ) return false;
	return true;
}

function verifyEmail(s) {
	var chrs = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-@_';
	var sLen = s.length; var i=0, c=0, cCnt=0, step=0;
	if (sLen < 6) return false;
	while (i < sLen){
		c=s.charAt(i);
		if (!(chrs.indexOf(c)>=0 || (c=='_' && step<1))) return false;
		if (c=='.') { if (cCnt<1) return false; cCnt=0; }
		if (c=='@') { if (step>0) return false; if (cCnt<1) return false; step++; cCnt=0; }
		cCnt=cCnt+1; i++;
	}	
	if (cCnt < 3 || cCnt > 5 || step==0 || (s.indexOf(".")<0) ) return false;
	return true;
}

function toggleBlockDisplay(paramItem) {
	var bVisible = (document.getElementById("block_"+paramItem).style.display == "none")?true:false;
	if(bVisible) {
		document.getElementById("icon_"+paramItem).src = "/gfx/search_close.gif";
		document.getElementById("block_"+paramItem).style.display = "";
		document.getElementById("spacerblock_"+paramItem).style.display = "";
	} else {
		document.getElementById("icon_"+paramItem).src = "/gfx/search_open.gif";
		document.getElementById("block_"+paramItem).style.display = "none";
		document.getElementById("spacerblock_"+paramItem).style.display = "none";
	}
}

function throwErrorMessage(paramError) {
	switch(String(paramError)) {
		case "10":
			alert("Login failed!\n\nIf you don\'t remember your password,\nyou can have it sent to your e-mail address!");
		break;
		case "20":
			alert("Please login!\n\nIf you don\'t have an account, you can create one for free!\n\nAll you need to enter is your e-mail and a password.\n\nClick Register from the left menu.");
		break;
		case "30":
			alert("It seems that there is already an account with that e-mail address in the system!");
		break;
		/*
		case "100":
			alert("Seems like you've already replied to this question!\n\nMoving on to the next page.");
		break;
		*/
	}
}

function seeAlso(paramID) {
	window.open("/encyclopedia/?ee="+paramID,"learnmore","top=20,left=20,width=995,height=700,scrollbars=1");
}
