// functions for Step2
function ValStep2()
{
	for (i=0; i < document.forms[0].elements.length;i++)
	{
		if (document.forms[0].elements[i].value == "")
		{
			msg = "All of the following fields must be filled in:\n";
			msg += "______________________________________________________\n";
			// build error message notification
			errorMsg = "\nCompany\n";
			errorMsg += "\nFirst Name\n";
			errorMsg += "\nLast Name\n";
			errorMsg += "\nE-mail\n";
			errorMsg += "\nLogin ID\n";
			errorMsg += "\nPassword\n";
			errorMsg += "\nPassword Confirmation\n";
			errorMsg += alert(msg + errorMsg + "\n");
			document.forms[0].elements[i].focus();
			return false;
		}
	}
	
	if (!ValEmail(document.forms[0].Email.value))
		{
			alert("The email address format is not valid.");
			document.forms[0].Email.focus();
			return false;
		}
		
	if (!ValPassword(document.forms[0].Password.value))
		{
			document.forms[0].PasswordConfirm.value = "";
			document.forms[0].Password.focus();
			return false;
		}
	
	if (document.forms[0].Password.value != document.forms[0].PasswordConfirm.value)
	{
		alert("Password and confirmation must match. Please try again.");
		document.forms[0].Password.value = "";
		document.forms[0].PasswordConfirm.value = "";
		document.forms[0].Password.focus();
		return false;
	}
	
	return true;
}

// functions for Step3
function DisableStep3()
{
	document.Form1.txtCatOther.disabled = true;
	document.Form1.txtRefOther.disabled = true;
	document.Form1.txtIDSNRepOther.disabled = true;
	
	if(document.Form1.ddlCategory.options[document.Form1.ddlCategory.selectedIndex].text == "Other") 
	{
		document.Form1.txtCatOther.disabled = false;
	}
	if(document.Form1.ddlReferred.options[document.Form1.ddlReferred.selectedIndex].text == "Other") 
	{
		document.Form1.txtRefOther.disabled = false;
	}
	if(document.Form1.ddlIDSNRep.options[document.Form1.ddlIDSNRep.selectedIndex].text == "Other") 
	{
		document.Form1.txtIDSNRepOther.disabled = false;
	}
}

function ValStep3()
{
	var errorMsg = "";
	
	// validation for Category
	if(document.Form1.ddlCategory.options[document.Form1.ddlCategory.selectedIndex].text == "- SELECT ONE -")
		{
			alert("Please select a Category");
			return false;
		}
	if(document.Form1.ddlCategory.options[document.Form1.ddlCategory.selectedIndex].text == "Other" && document.Form1.txtCatOther.value == "")
		{
			alert("Other Field is required for Category");
			document.Form1.txtCatOther.focus();
			return false;
		}
	
	// validation for Referred By Company or IDSN Representative relationship
	if(document.Form1.ddlReferred.options[document.Form1.ddlReferred.selectedIndex].text == "- SELECT ONE -" && document.Form1.ddlIDSNRep.options[document.Form1.ddlIDSNRep.selectedIndex].text == "- SELECT ONE -")
		{
			alert("Please select a Referred By: either Company OR IDSN Representative");
			return false;
		}
	
	// validation for Referred by Company Other option
	if(document.Form1.ddlReferred.options[document.Form1.ddlReferred.selectedIndex].text == "Other" && document.Form1.txtRefOther.value == "")
		{
			alert("Other Field is required for Referred By: Company");
			document.Form1.txtRefOther.focus();
			return false;
		}
	
	// validation for IDSN Representative Other option
	if(document.Form1.ddlIDSNRep.options[document.Form1.ddlIDSNRep.selectedIndex].text == "Other" && document.Form1.txtIDSNRepOther.value == "")
		{
			alert("Other Field is required for Referred By: IDSN Representative");
			document.Form1.txtIDSNRepOther.focus();
			return false;
		}	
		
	return true;
}

// functions for Step4
function PopulateStep4(val)
{
	if (val.name == 'chkCompany'){
		if (val.checked == true){
			document.Form1.Company.value = document.Form1.hidCompany.value;
			document.Form1.FirstName.value = document.Form1.hidFname.value;
			document.Form1.LastName.value = document.Form1.hidLname.value;
			document.Form1.Email.value = document.Form1.hidEmail.value;
		}
		else
		{
			document.Form1.Company.value = '';
			document.Form1.FirstName.value = '';
			document.Form1.LastName.value = '';
			document.Form1.Email.value = '';
		}
	}
	else
	{
		if (val.checked == true){
			document.Form1.Company2.value = document.Form1.hidCompany.value;
			document.Form1.FirstName2.value = document.Form1.hidFname.value;
			document.Form1.LastName2.value = document.Form1.hidLname.value;
			document.Form1.Email2.value = document.Form1.hidEmail.value;
		}
		else
		{
			document.Form1.Company2.value = '';
			document.Form1.FirstName2.value = '';
			document.Form1.LastName2.value = '';
			document.Form1.Email2.value = '';
		}
	}
}

function ValStep4()
{
	for (i=0; i < document.forms[0].elements.length;i++)
	{
		if(document.forms[0].elements[i].value == "")
			{
				msg = "All of the following fields must be filled in for both Company and Billing:\n";
				msg += "______________________________________________________\n";
				// build error message notification
				errorMsg = "\nCompany\n";
				errorMsg += "\nFirst Name\n";
				errorMsg += "\nLast Name\n";
				errorMsg += "\nE-mail\n";
				errorMsg += alert(msg + errorMsg + "\n");
				document.forms[0].elements[i].focus();
				return false;
			}
		}
	return true;
}

// functions for Step5
function ValStep5()
{
	for (i=0; i < document.forms[0].elements.length;i++)
	{
		if(document.forms[0].elements[i].value == "" && document.forms[0].elements[i].name != "txtccAddress2")
		{
			msg = "All of the following fields must be filled in:\n";
			msg += "______________________________________________________\n";
			// build error message notification
			errorMsg = "\nCredit Card Number\n";
			errorMsg += "\nCredit Card CSC\n";
			errorMsg += "\nName on Card\n";
			errorMsg += "\nBilling Address\n";
			errorMsg += "\nBilling City\n";
			errorMsg += "\nBilling Zip\n";
			errorMsg += "\nBilling Country\n";
			errorMsg += alert(msg + errorMsg + "\n");
			document.forms[0].elements[i].focus();
			return false;
		}
	}
	
	if (! document.forms[0].chkTerms.checked)
	{
		alert("Please indicate that you accept the Terms and Conditions for this service.");
		document.forms[0].chkTerms.focus();
		return false;
	}
		
	
	return true;
}

// helper functions

function CloseWindow() {
		window.close();
}

function OpenWindow(mylink, windowname)
{
	if (! window.focus)
	{
		return true;
	}
	var href;
	if (typeof(mylink) == 'string')
	{
		href=mylink;
	}
	else
	{
		href=mylink.href;
		window.open(href, windowname, 'width=550,height=500,scrollbars=yes');
		return false;
	}
}

function ValPassword(fieldname) {
//Initialise variables
var errorMsg = "";
var space  = " ";
fieldname   = document.forms[0].Password; 
fieldvalue  = fieldname.value; 
fieldlength = fieldvalue.length; 
	
//It must not contain a space
if (fieldvalue.indexOf(space) > -1) {
	errorMsg += "X";
}     
	
//It must be at least 6 characters long.
if (!(fieldlength >= 6)) {
	errorMsg += "X";
}

//If there is a problem with the form then display an error
	if (errorMsg != ""){
		msg = "Please correct the problem(s) with your password.\n";
		msg += "______________________________________________________\n";
		// build error message notification
		errorMsg = "\nPasswords cannot include a space.\n";
		errorMsg += "\nStrong passwords must be at least 6 characters long.\n";
		errorMsg += alert(msg + errorMsg + "\n");
		fieldname.focus();
		return false;
	}
	return true;
}

function ValEmail(src) {
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);
	return regex.test(src);
}

function ValUserName()
{
	if(document.forms[0].txtUserName.value == "")
	{
		alert("Please provide a user name and press Submit.");
		document.forms[0].txtUserName.focus();
		return false;
	}
}

