// Swithes out Promo Images for Vegas Offer on Step one of the Order Process and DSL_typical_products page.

function promoImageOrder()
{
	return false;
}

// Opens Popup for DSL promo.  Vegas specific offer.
function promoPopup()
{
	var state = getUSERCookieInfo("state");

	if(state == "NV")
	{
		window.open('/home/local/dsl/popup/REFRESH_p3_promoPopup.html','','width=422,height=395,scrollbars=yes'); return false;
	}
	else
	{
		window.open('/home/local/dsl/popup/REFRESH_p3_promoPopup.html','','width=422,height=395,scrollbars=yes'); return false;
	}

}

function cpeDuplicates()
{
	var usb = document.form1.elements['usb'];
	var nic = document.form1.elements['nic'];

	if(usb.checked && nic.checked)
	{
		alert("Please select either the NIC or USB adapter.\n You will only need to connect with the modem using one device.");
	}
	else
	{
		return false;
	}
		
}

//Function to direct user back to Select Products Page without triggering survey
function changeOrder()
{
	survey = false;
	boolSubmitButton=true;
	window.location.href = "/servlet/DSLDesign?mode=select";
}

//Function to direct user back to Review Selections Page Page without triggering survey
function reviewSelect()
{
	survey = false;
	boolSubmitButton=true;
	window.location.href = "/servlet/DSLDesign?mode=reviewback";
}

//Function to direct user back to Customer Information Page Page without triggering survey
function customerInformation()
{
	survey = false;
	boolSubmitButton=true;
	window.location.href = "/servlet/DSLDesign?mode=reviewback";
}

// Validation and submit for DSL 4.1
function submitProducts()
{
	var exited = 'yes';
	boolSubmitButton=true;
	survey = false; 
	document.form1.submit();
	
}

// Validation and submit for DSL 4.3
function submitCustInfo()
{
	complete = testForm();
	if (complete)
	{
		alert(exited);
		var exited = 'yes';
		survey = false; 
		boolSubmitButton=true;
		alert(exited)
		//alert("AFTER THIS ALERT, THE POPUP WINDOW IS THE LAST CALL BEFORE [document.customerInformation.submit()]...");
		//debugInputs();
		document.customerInformation.submit();
	}
}

var objNewWin = null;

function debugInputs()
{
	objNewWin = window.open("nothing.html", "debug", "Width=400, Height=600, Left=800, Top=10, Scrollbars");
	var to_output = "";

	to_output += "<style type=\"text/css\">\n";
	to_output += "b {font-family:Tahoma; font-size:10pt; font-weight:bolder}\n";
	to_output += "td {font-family:Courier New; font-size:8pt}\n";
	to_output += "</style>\n";
	
	to_output += "<b>Debug Form Fields</b><br>\n";

	to_output += "<table border=\"0\" bordercolor=\"lime\" cellpadding=\"0\" cellspacing=\"2\" width=\"90%\">\n";
	
	for (i=0; i<document.customerInformation.length; i++)
	{
		var currTDColor = "#EEEEEE";
		
		if ((i%2)==1)
		{
			currTDColor = "#FFFFFF";
		}
		
		to_output += "<tr bgcolor=\"" + currTDColor + "\">\n";
		to_output += "<td align=\"left\" valign=\"top\">\n";
	
		to_output += "<i>document.customerInformation[" + i + "]:</i><br>\n";
		to_output += "&nbsp;&nbsp;&nbsp;.name=" + document.customerInformation[i].name + ".<br>\n";
		to_output += "&nbsp;&nbsp;&nbsp;.type=" + document.customerInformation[i].type + ".<br>\n";
		to_output += "&nbsp;&nbsp;&nbsp;.value=" + document.customerInformation[i].value + ".<br>\n";
		if ( (document.customerInformation[i].type == "radio") || (document.customerInformation[i].type == "checkbox") )
		{
			to_output += "&nbsp;&nbsp;&nbsp;.checked=" + document.customerInformation[i].checked + ".<br>\n";
		}
	
		to_output += "</td>\n";
		to_output += "</tr>\n";
	}
	
	to_output += "</table>\n";

	objNewWin.document.write(to_output);
	objNewWin.focus();	
	window.setTimeout("objNewWin.focus()", 500);
}

function checkProInstall()
{
	if(document.form1.Installation.value == 'Professional')
	{
		alert("You do not need to order a NIC or USB adapter with the Professional Installation option.\n The Sprint Technician will have all the items necessary to complete the installation process.\n");
	}
	else
	{
		return false;
	}
}

// Used on Error code pages to validate email address store to DB. Error code_298 for example


function submitDSLemail()
{
	complete = testDSLemail();
	if(complete) {document.form1.submit();}
}


// Tests the forms fields and builds error statement for 4.3
function testForm()
{
	// validate customer information
	firstNameValid = isValidStringLen(document.customerInformation.elements['DslCustomerDO.firstName'].value, 2);
	lastNameValid = isValidStringLen(document.customerInformation.elements['DslCustomerDO.lastName'].value, 2);
	custCodeValid = isValidNumber(document.customerInformation.elements['cust_code'].value, 3, 13);
	ssn4Valid = isValidNumber(document.customerInformation.elements['ssn4'].value, 4, 4);
	emailValid = isValidEmail(document.customerInformation.elements['DslCustomerDO.email'].value);

	// validate service and billing address

	//shippingAddressValid = isValidString(document.customerInformation.elements['DslShippingAddressDO.street1'].value);
	//shippingCityValid = isValidString(document.customerInformation.elements['DslShippingAddressDO.city'].value);
	//shippingZipValid = isValidNumber(document.customerInformation.elements['DslShippingAddressDO.zip'].value, 5, 12);

	// validate contact phone number
	contactNpaValid = isValidNumber(document.customerInformation.elements['DslCustomerDO.contactNpa'].value, 3, 3);
	contactCocValid = isValidNumber(document.customerInformation.elements['DslCustomerDO.contactCoc'].value, 3, 3);
	contactLineValid = isValidNumber(document.customerInformation.elements['DslCustomerDO.contactLine'].value, 4, 4);
	numberValid = (contactNpaValid && contactCocValid && contactLineValid);
	
	var checkBillDelivery = false;
	if (document.customerInformation.elements["DslOrderDO.billDelivery"])
	{
		checkBillDelivery = true;
	}
	
	if (checkBillDelivery)
	{
		billDeliveryOnline = document.customerInformation.elements["DslOrderDO.billDelivery"][0].checked 
		billDeliveryPaper = window.document.customerInformation.elements["DslOrderDO.billDelivery"][1].checked
		billDelivery = (billDeliveryOnline || billDeliveryPaper);
	}
	
	// initialize error message

	accum = 0;
	errorString = "These errors were encountered. Please check these fields to make sure they are correct.\n"
	testError = errorString
	// if any field was invalid then add it to the error message 

	firstNameValid ? accum++ : errorString += "* First Name\n";
	lastNameValid ? accum++ : errorString += "* Last Name\n";
	//ssn4Valid ? accum++ : errorString += "* Last 4 Digits of Social Security Number\n";
	(custCodeValid || ssn4Valid) ? accum++ : errorString += "* Customer Code or Last 4 Digits of Your Social Security Number.\n";
	emailValid ? accum++ : errorString += "* Email address\n";
	//shippingAddressValid ? accum++: errorString += "* Shipping Address\n";
	//shippingCityValid ? accum++: errorString += "* Shipping City\n";
	//shippingZipValid ? accum++: errorString += "* Shipping Zip\n";
	numberValid ? accum++ : errorString += "* Contact Number\n";
	if (checkBillDelivery)
	{
		billDelivery ? accum++ : errorString += "* Bill Delivery Preference\n";
	}

	// append npa,coc,line to cust_code:
	var strAccountID = String(document.customerInformation.elements['DslCustomerDO.contactNpa'].value) + String(document.customerInformation.elements['DslCustomerDO.contactCoc'].value) + String(document.customerInformation.elements['DslCustomerDO.contactLine'].value) + String(document.customerInformation.elements['cust_code'].value);
	document.customerInformation.elements['accountId'].value = strAccountID;
	
	// if any field errors were added to the initial error message then 
	// flag the form as invalid and display the errors

	formCorrect = false
	if(errorString == testError) 
		formCorrect = true;
	else 
	{
		formCorrect = false;
		alert(errorString);
	}
	return formCorrect;
}

// Used on Error code pages to validate email address store to DB. Error code_298 for example
function testDSLemail()

{
	// validate customer email address
	emailValid = isValidEmail(document.form1.elements['email'].value);
	// initialize error message
	accum = 0;
	errorString = "These errors were encountered. Please check these fields to make sure they are correct.\n"
	testError = errorString
	
	// if any field was invalid then add it to the error message 
	emailValid ? accum++ : errorString += "* Email address\n";

	// if any field errors were added to the initial error message then 
	// flag the form as invalid and display the errors
	formCorrect = false

	if(errorString == testError) 
		formCorrect = true;
	else 
	{
		formCorrect = false;
		alert(errorString);
	}
	return formCorrect;
}

//-----------------------------------------------------------------------------------------------

//	Generic form field testing functions

//-----------------------------------------------------------------------------------------------

function isValidStringLen(field, minChar)
//This function will step through a String until it finds a non-whitespace character
//And make sure that the string is min characters long
{
	var acceptable = false;
	for(var i = 0; (i<field.length && !acceptable); i++)
	{
		var c = field.charAt(i);
		if((c != " ") && (c != "\t") && (c != "\n") && (c != "\r"))
		{
			acceptable = true;
		}
	}

	if(field.length < minChar) acceptable = false;
	return acceptable;
}


// Overloaded (kind of) function for Strings not to be tested for particular length
function isValidString(field)
{
	return isValidStringLen(field, 1);
}


function isValidNumber(field, minNum, maxNum)
//This function will test a String to be sure it is all digits and that the correct number of
//	characters have been met as defined in minNum and maxNum.
{
	var acceptable = true;
	//alert("field: " + field + "\nvalue: '" + field.value + "'");
	if((field.length < minNum) || (field.length > maxNum)) return !acceptable;
	for(var i = 0; (i < field.length && acceptable); i++)
	{
		var c = field.charAt(i);
		if(!isDigit(c))
		{
			acceptable = false;
		}
	}
	return acceptable;
}

function isDigit(c)
//This function is seperated from isValidNumber so it can deal with c as an integer.
{
	return ((c <= 9) && (c >= 0));
}

function isValidDropdown(field)

//This function tests Drop Down [Select] boxes to be sure that a value has been selected.
//This function also assumes that an incorrect selection is index 0 (2 spaces)
{
	var acceptable = true;
	if(field.selectedIndex == 0) acceptable = false;
	return acceptable;
}

function isValidEmail(field)
//Tests to be sure that the field contains a valid email address
//Tests for a string in this pattern 'char@char.char'
{
	var acceptable = false;
	at = field.indexOf("@");
	dot = field.lastIndexOf(".");
	if(at > 0 && dot > 3)
	{
		worker = field.split("@");
		username = worker[0];
		worker2 = worker[1].split(".");
		if(worker2.length > 1)
		{
			domain = worker2[0];
			topLevel = worker2[1];
			acceptable = (isValidString(username) && isValidString(domain) && isValidString(topLevel))? true: false;
		}
	}
	return acceptable;
}

