/**************************************************
This is the global JS file for C&G Technologies
**************************************************/

var phraseCounter = 0;

function topRightRotator() {
	var phraseArray = new Array("Quality","Experience","Support");
	var targetDiv = document.getElementById("topRightRotation");
	targetDiv.innerHTML = phraseArray[phraseCounter];
	if(phraseCounter < (phraseArray.length-1)) {
		phraseCounter++;
	} else {
		phraseCounter = 0;
	}
	setTimeout("topRightRotator()", 2000);
}


function setSelectedTopNav() {
	try {
		if(selectedNavItem == "index.php") {
			document.getElementById("top_nav_home").className += " selectedTopNav";
		} else {
			document.getElementById("top_nav_"+selectedNavItem).className += " selectedTopNav";
		}
	} catch(err) {}
}

/* FLASH 'CLICK TO ACTIVATE' FIX */
function flashFixit() {
	 theObjects = document.getElementsByTagName("object");
	for (var i = 0; i < theObjects.length; i++) {
		theObjects[i].outerHTML = theObjects[i].outerHTML;
	}
}
function openProductPop(pageURL) {
	window.open(pageURL,'details','height=500,width=650,resizable,scrollbars,left=50,top=50');
}


















// global vars
var validationErrorTitle = "Please complete the following information to continue:\n\n";
var validationErrorBody = "";
var postValidateFocus = "";

/*******************************************************************
Sets the first field with an error to be focused after validation.
*******************************************************************/
function setFirstInvalid(inputObj) {
	if(!validationErrorMessage)	{postValidateFocus = document.getElementById(inputObj);}
}

// This function checks for a 10-digit phone number
// This function rips out commonly used formatting characters (spaces, commas, parentheses, dashes, or plusses) which are not part of the actual phone number
function validatePhone(fieldID)	{
	var fldObj = document.getElementById(fieldID);
	var phoneNumber = fldObj.value.replace(/ /g,"");
	var phoneNumber = phoneNumber.replace(/-/g,"");
	var phoneNumber = phoneNumber.replace(/\(/g,"");
	var phoneNumber = phoneNumber.replace(/\)/g,"");
	var phoneNumber = phoneNumber.replace(/\+/g,"");
	
	if(phoneNumber.search(/^\d{10}$/) == -1) {
		return false;
	}
}

// This function checks for a 5-digit zip code
function validateZip(fieldID)	{
	var fldObj = document.getElementById(fieldID);
	if(fldObj.value.search(/^\d{5}$/) == -1) {
		return false;
	}
}

// This function checks for a valid email address
function validateEmail(fieldID)	{
	var fldObj = document.getElementById(fieldID);
	if(fldObj.value.search(/^\w+((-\w+)|(\.\w+)|(\+\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9][A-Za-z0-9]+$/) == -1) {
		return false;
	}
}

// This function checks for a whole number
function validateNumerical(fieldID)	{
	var fldObj = document.getElementById(fieldID);
	if(fldObj.value.search(/^[0-9]+$/) == -1) {
		return false;
	}
}

// overall validation function
function validateRequestForm(formObject) {
	validationErrorMessage = "";
	postValidateFocus = "";
	// validate information //

	if(!formObject.cust_first_name.value) {setFirstInvalid("cust_first_name"); validationErrorMessage += "Enter your first name.\n";}
	if(!formObject.cust_last_name.value) {setFirstInvalid("cust_last_name"); validationErrorMessage += "Enter your last name.\n";}
//	cust_company
	if(formObject.cust_email.value) {
		if(validateEmail("cust_email") == false) {setFirstInvalid("cust_email"); validationErrorMessage += "Enter a valid e-mail address.\n";}
	}
	if(!formObject.cust_address.value) {setFirstInvalid("cust_address"); validationErrorMessage += "Enter your address.\n";}
	if(!formObject.cust_city.value) {setFirstInvalid("cust_city"); validationErrorMessage += "Enter your city.\n";}
//	cust_state
//	cust_zip
	if(!formObject.cust_country.value) {setFirstInvalid("cust_country"); validationErrorMessage += "Enter your country.\n";}
	if(!formObject.cust_phone.value) {setFirstInvalid("cust_phone"); validationErrorMessage += "Enter a phone number.\n";}
	
	// display error message //
	if(validationErrorMessage) {
		alert(validationErrorTitle+validationErrorMessage);
		postValidateFocus.setAttribute('autocomplete','off'); 
		postValidateFocus.focus();
		return false;
	}
	
	//everything works, so...
	formObject.send.value = true;
	return true;
}