// This variable is used to keep track of the pagename so popups can be limited to only default page  -SAD
var PageName = GetPageName();

function checkUSPhone(area, pre, code) {
	var errMsg = "Please enter a valid 10 digit phone number.";

	var phoneStr = "" + area + pre + code;
	if (phoneStr.length != 10) {
		alert(errMsg);
		return false;
	}

	var phoneNum = Number(phoneStr);
	if (phoneNum == NaN) {
		alert(errMsg);
		return false;
	}
	if (!(phoneNum > 0)) {
		alert(errMsg);
		return false;
	}

	var areaNum = Number(area);
	if (!(areaNum > 0)) {
		alert(errMsg);
		return false;
	}

	return true;						
}

function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
	   return false;
	return true;

}
			
			
function autotab(currentLength, maxLength, nextInput) 
{
	if (currentLength == maxLength) nextInput.focus();
}

var popup = 0;
var host = document.location.host;
var refer = document.referrer;
var surveyWindow;
var supportSite = 0;

function fOnLoad() {
	if(!navigator.cookieEnabled){
		//alert("cookies disabled!");
	}

//	<!--fShopathomecatalog();-->
	
	var survey2 = ReadCookie('survey2');
	if(survey2 != '1' && survey2 != '2' && PageName == 'default') {
		surveyWindow = window.open('/us/survey_start.asp?from=exit','survey','width=780,height=600,resizable=yes,menubar=no,toolbar=no,scrollbars=yes');
		//surveyWindow.focus()
	}

}

function fOnLoad_homepage() {
	if(!navigator.cookieEnabled){
		//alert("cookies disabled!");
	}

	fShopathomecatalog();
}

function fShopathomecatalog() {

	shopAtHomeCatalog = ReadCookie('fp_sahc');
	if(shopAtHomeCatalog != '1' && PageName == 'default') {
		sahcWindow = window.open('/us/shop_at_home_catalog.asp','sahc','width=780,height=600,resizable=yes,menubar=no,toolbar=no,scrollbars=yes');
		sahcWindow.blur();
	}
}

function fOnUnload(){
	var survey2_5 = ReadCookie('survey2_5');
	if(survey2_5 == '1' && PageName == 'default') {
		//alert(supportSite);
		surveyWindow = window.open('/us/survey_start.asp?from=exit&supportSite='+supportSite,'survey','width=780,height=600,resizable=yes,menubar=no,toolbar=no,scrollbars=yes');
		surveyWindow.blur();
	}
	
}

function checkParentLocation() {
	//alert(parent.document.URL);
	//alert(window.parent.window.location.hostname);
	//alert(window.opener.document.location);
	try {
		test = window.opener.location.hostname;
		
	} 
	catch (e) {
		//alert(supportSite);
		if (!supportSite){
			window.location = 'http://survey.mattel.com/ConsRel/s_m_com/s_m_com.htm';
			window.focus();
		}
	}
}
function fOnSurveyLoad(){
	//alert(window.parent.window.location.hostname);
	setTimeout('checkParentLocation()', '1000');
}

function openSurvey(tmpVal)
{
	surveyWindow = window.open(tmpVal,'survey','width=780,height=600,resizable=yes,menubar=no,toolbar=no,scrollbars=yes');
	surveyWindow.focus();
}


function WriteCookie (cookieName, cookieValue, expiry)
{
	var expDate = new Date();
	if(expiry)
	{   expDate.setTime (expDate.getTime() + expiry);
	    document.cookie = cookieName + "=" + escape (cookieValue) + "; expires=" + expDate.toGMTString() + ";path=/;";
	}
	else { document.cookie = cookieName + "=" + escape (cookieValue) + "; path=/;"; }
}

function ReadCookie (CookieName) {
	var CookieString = document.cookie;
	var CookieSet = CookieString.split (';');
	var SetSize = CookieSet.length;
	var CookiePieces
	var ReturnValue = ""
	var x = 0;
	for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++) {
		CookiePieces = CookieSet[x].split ('=');
		if (CookiePieces[0].substring (0,1) == ' ') CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length);
		if (CookiePieces[0] == CookieName) ReturnValue = CookiePieces[1];
	}
	return ReturnValue;
}

function isEmail(val) {
	val = trim(val);

	if ( isEmpty(val) )
		return true;

	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);
	return regex.test(val);
}

// This function is used to keep track of the pagename so popups can be limited to only default page  -SAD
function GetPageName()
{
	var PageName = window.location.href;
//	if (PageName.substring(PageName.length-3, PageName.length) == "us/")
//		PageName == "default";
//	else
	PageName = PageName.split("/").pop().split(".")[0];
	
	return PageName;
}

function fEnterDefaultPage()
{
	var survey2 = ReadCookie('survey2');
	if(survey2 != '1' && survey2 != '2') 
	{
		surveyWindow = window.open('/us/survey_start.asp?from=exit','survey','width=780,height=610,resizable=yes,menubar=no,toolbar=no,scrollbars=yes');
		surveyWindow.focus();
		WriteCookie("fp_sahc", 2, 1*24*60*60*1000);
	}
}

function fLeaveDefaultPage()
{
	shopAtHomeCatalog = ReadCookie('fp_sahc');
	if(shopAtHomeCatalog != '1') 
	{
		sahcWindow = window.open('/us/shop_at_home_catalog.asp','sahc','width=685,height=375,resizable=yes,menubar=no,toolbar=no,scrollbars=yes');
		sahcWindow.blur();
	}
}
