// General purpose scripts. Browswer sniffing, etc.

//<Script Language="JavaScript">

/*****************************************
	Open link in new window. Gets focused, positioned, and re-used.
	Pass in URL, Name, width, height
	NOTE: The generic popup is spec'd at 400x550 px. 
		- Pass in 440x550 to allow for scrollbar and page margin.)
		- For product info popup, pass in 485x550 .
*****************************************/
function OpenNewWindow(openURL,WindowName,width,height) 
{
    var newwindow = window.open(openURL,WindowName,'toolbar=no,location=no,status=no,menubar=no,resizable=yes,width='+width+',height='+height+',scrollbars=yes,top=30,left=30');
    newwindow.focus();
}

function OpenProgramGuide(openURL,WindowName,width,height) 
{
    var newwindow = window.open(openURL,WindowName,'toolbar=yes,location=no,status=no,menubar=yes,resizable=yes,width='+width+',height='+height+',scrollbars=yes,top=30,left=30');
    newwindow.focus();
}

// NOTE: Remove the else branch after localization is implemented. It simply prevents showing a broken page.
function validateZIP(zfield) 
{
		var zvalue = zfield.value;
		var zerocount = 0;
		if (zvalue.length!=5) {
			alert("Please enter a 5-digit ZIP Code.");
			zfield.focus();
			return false;
		}
		for (var i=0; i < zvalue.length; i++) {
			temp = "" + zvalue.substring(i, i+1);
			if (temp == "0") zerocount++;
			if (zerocount == 5) {
				alert("Please enter a valid 5-digit ZIP Code.");
				zfield.select();
				return false;
			}
		}
		var valid = "0123456789"
		var ok = "yes";
		var temp;
		for (var i=0; i < zvalue.length; i++) {
			temp = "" + zvalue.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") ok = "no";
		}
		if (ok == "no") {
			alert("Please enter a valid 5-digit ZIP Code.");
			zfield.focus();
			return false;
		}
	
		return true;
}

// Morph of validateZIP to include the distance field on the Payment Center page.
function validatePC(zfield,dfield) 
{
		var zvalue = zfield.value;
		var dvalue = dfield.value;
		var valid = "0123456789"; // numeric entries only
		var temp, temp2;
		
		// Begin with zip validation
		var zerocount = 0;
		
		// zip code field is not empty and length == 5 characters 
		if (zvalue.length!=5) {
			alert("Please enter a 5-digit ZIP Code.");
			zfield.focus();
			return false;
		}
		
		// zip code is all numeric values
		for (var i=0; i < zvalue.length; i++) {
			temp = "" + zvalue.substring(i, i+1);
			if (valid.indexOf(temp) == "-1")
			{
				alert("Please enter a valid 5-digit ZIP Code.");
				zfield.select();
				return false;
			}
		}
		
		// zip code is not all zeros
		for (var i=0; i < zvalue.length; i++) {
			temp = "" + zvalue.substring(i, i+1);
			if (temp == "0") zerocount++;
			if (zerocount == 5) {
				alert("Please enter a valid 5-digit ZIP Code.");
				zfield.select();
				return false;
			}
		}
		
		// Begin distance validation
		var DistOK = true;
		var reject = "dist1";
		
		// Distance field is empty
		if (dvalue == "")
		{
			DistOK = false;
			reject = "dist1";
		}
		
		// Distance contains alpha characters
		for (var i=0; i < dvalue.length; i++) {
			temp2 = "" + dvalue.substring(i, i+1);
			if (valid.indexOf(temp2) == "-1") 
			{
				DistOK = false;
				reject = "dist1";
			}
		}
		
		// Distance field is larger than 50
		if ((dvalue > 50) || (dvalue == 0))
		{
			DistOK = false;
			reject = "dist2";
		}
		
		// Choose an error message
		if (!DistOK) {
		
			if (reject == "dist1")
			{
				alert("Please enter a valid Distance.");
				dfield.focus();
				return false;
			}
			else
			{
				alert("Please enter a Distance of 50 miles or less and greater than 0.");
				dfield.focus();
				return false;
			}
		}
	
		return true;
}




// TODO Call email validation. Remove final else branch.
// Pass in two required fields: email and last name.
function validateMoveForm(oEmailFld, oLastnameFld) 
{
	var sEmail = oEmailFld.value;
	var sLastname = oLastnameFld.value;
	var bFocus = false;
	var sMessage = "Please enter:\n";
	
	if (sEmail == "")
	{
		sMessage += "- Your e-mail address\n";
		if (bFocus == false)
		{	
			bFocus = true;
			oEmailFld.focus();
		}
	}
	else
	{	
		// TODO Validate email
	}
	
	if (sLastname == "")
	{
		sMessage += "- Your last name\n";
		if (bFocus == false)
		{	
			bFocus = true;
			oLastnameFld.focus();
		}
	}
	if (bFocus == true)
	{
		alert(sMessage);
		return false;
	}
	
	// TODO Remove this branch when ready
	else
	{
		window.navigate('/FutureFunctionality.html');
		return false;	
	}
}


// Require a term in search box. 
// Pass in the field name.
function validateFaqSearch(sField) 
{
	var sTerm = sField.value;
  	if (sField.value == "") {
    	alert("Please enter at least one word to search for.");
    	sField.focus();
    	return false;
  	}
  	
  	// TODO Implement search and remove this branch.
	else
	{
		window.navigate('/FutureFunctionality.html');
		return false;
	}
  
  	return true;
}

// Validate state and region on Customer Service localization
function validateRegion(oStateFld, oRegionFld) 
{
	var bFocus = false;
	if (oStateFld.value == -1)
	{
		if (bFocus == false)
		{
			alert('Please select your state.');
			bFocus = true;
			oStateFld.focus();
			return false;
		}
	}
	if (oRegionFld.value == -1)
	{
		if (bFocus == false)
		{
			alert('Please select your region.');
			bFocus = true;
			oRegionFld.focus();
			return false;
		}
	}
	
	return;
}


// Validate payment center ZIP and search radius in Payment Center search box
// TODO Implement this function
function ValidatePaymentCenter(oZip, oRadius) 
{
	//alert("Searching for your nearest payment center...");
	
}


/*****************************************
	Set size of text box depending on NN/IE to maintain layout.
	Pass field name, size for NN and larger size for IE.
*****************************************/
function SetBoxSize(oName, iSmall, iLarge) 
{		
	if (document.all)					// IE
		oName.size = iLarge;
	else if (document.getElementById)	// Netscape 6+
		oName.size = iSmall;
	else if (document.layers)			// Netscape <6
		oName.size = iSmall;
	else
		oName.size = iSmall;			// Default
}


/*******************************************
	Handle resize for CLU floating legend
*******************************************/
function CheckSize()
{
	//alert("CheckSize: " + window.innerHeight);
	
	var sBrowser = GetBrowser();
	if (sBrowser != "Msie")
	{
		//debug alert("CheckSize, Not IE");
		
		var Floater = document.getElementById("FloatingDiv");
		
		var FloaterPosition = Floater.style.top;
		//alert(FloaterPosition);
		
		var ScrolledAmount = document.body.scrollTop;
		var WindowSize = window.innerHeight;
		
		var ChromeY = 50;	// Cushion amount		
		var Net = WindowSize + ChromeY - ScrolledAmount;
		
		// Keep above footer (for arbitrary size "BottomPadding"; legend could be taller)
		var BottomPadding = 100;
		var LowLimit = WindowSize - BottomPadding;		
		
		if (WindowSize < 320)
		{	
			if (ScrolledAmount < 281)
			{
				Floater.style.top = Net + "px";
			}
			else
			{ 	
				Floater.style.top = "0px";
			}				
		}
		else
		{
			if (ScrolledAmount < 281)
			{
				Floater.style.top = "290px";
			}
			else
				Floater.style.top = "0px";
		}
		
		// Space above footer
		if (FloaterPosition > LowLimit)
		{
			Floater.style.top = LowLimit + "px";
		}
		
		
	}
}


/********************************************
	Browser sniff
*********************************************/	
function GetBrowser()
{
	var sAgent =navigator.userAgent.toLowerCase();
	var sBrowser;

	if (sAgent.indexOf('msie') != -1)
	{	
		sBrowser = "Msie";
	}
	else if (sAgent.indexOf('netscape') != -1)
	{
		sBrowser = "Netscape";
		if(sAgent.indexOf('netscape/7') != -1)
			sBrowser = "NetscapeSevenX";
	}
	else if (sAgent.indexOf('firefox') != -1)
	{
		sBrowser = "Firefox";
	}
	else if (sAgent.indexOf('gecko') != -1)
	{
		sBrowser = "Gecko";
	}
	else if (sAgent.indexOf('safari') != -1)
	{
		sBrowser = "Safari";
	}
	else
		sBrowser = "na";		// Other. Not IE, Netscape, Firefox, Gecko, Safari.
		
	return sBrowser;
}


function SetFloatingDiv()
{
	var sBrowser = GetBrowser();
	
	if (sBrowser == "Msie")					// IE
	{
		document.body.onscroll = Float;
	}
	else if(sBrowser == "NetscapeSevenX")	// Netscape 7x
	{
		// debug 	alert("Browser: Netscape 7x");			
		FloatNetscape();
		return;
	}
	else if(sBrowser == "Firefox")			// Firefox
	{
		// debug 		alert("Browser: Firefox");
		FloatFirefox();		
		return;
	}
	else if(sBrowser == "Gecko")			// catch non-NN, non-FF Geckos
	{
		// TODO Calling the Firefox function for now
		FloatFirefox();		
		return;
	}
	else if(sBrowser == "Safari")			// Safari
	{
		// TODO Calling the Firefox function for now
		FloatFirefox();		
		return;
	}
	else
		return;
	
}

// IE version
function Float()
{
	var Floater = document.all["FloatingDiv"];
	
	//alert("new " + document.body.scrollTop);
	var StartY = 295;
	var ChromeY = 50;	// Cushion amount

	// Amount that window has scrolled
	var ScrolledY = document.body.scrollTop;
	
	// New "floated" position
	var New = ScrolledY + ChromeY;
		
	// Prevent from moving above start position
	if (Floater.style.posTop < StartY || ScrolledY < StartY)
		Floater.style.posTop = StartY;
	else
		Floater.style.posTop = New;
}

function FloatFirefox()
{
	//		alert("Called FloatFirefox");
	var Floater = document.getElementById("FloatingDiv");
	
	Floater.style.position = "fixed";
	Floater.style.top = "290px";
	Floater.style.left = "590px";
}

function FloatNetscape()
{
	// debug 	alert ("win: " + window.innerHeight + " scroll: " + document.body.scrollTop);
	var Floater = document.getElementById("FloatingDiv");
	
	Floater.style.position = "fixed";
	Floater.style.top = "290px";
	Floater.style.left = "590px";
}


















// TODO - Remove this because it is used in Product Details and needs to be removed from there.
function clearZipValue()
{
	return;
}





























