var delayed=0;

function checkFields() {
var missingInfo = "";
var form = document.colorRequestForm;
var valid = "1234567890()-. ";
var errorMsg;
var toFocus = 0;
var focusHere;
var badphone = false;


// check for first name
if (form.FirstName.value == "") {
missingInfo += "\n     *  First Name";
	if (!toFocus) {
	focusHere = form.FirstName;
    toFocus=1;
   	}
}

// check for last name
if (form.LastName.value == "") {
missingInfo += "\n     *  Last Name";
	if (!toFocus) {
	focusHere = form.LastName;
    toFocus=1;
   	}
}

// check for address
if (form.Address.value == "") {
missingInfo += "\n     *  Address";
	if (!toFocus) {
	focusHere = form.Address;
    toFocus=1;
   	}
}

// check for city
if (form.City.value == "") {
missingInfo += "\n     *  City";
	if (!toFocus) {
	focusHere = form.City;
    toFocus=1;
   	}
}

// check for state
if (form.State.value == "") {
missingInfo += "\n     *  State";
	if (!toFocus) {
	focusHere = form.State;
    toFocus=1;
   	}
}

// check for postal code
if (form.PostalCode.value == "") {
missingInfo += "\n     *  Postal Code";
	if (!toFocus) {
	focusHere = form.PostalCode;
    toFocus=1;
   	}
}

// check for minimum length postal code
if (form.PostalCode.value != "" &&
    form.PostalCode.value.length < 5) {
missingInfo += "\n     *  Postal Code (minimum 5 characters)";
	if (!toFocus) {
	focusHere = form.PostalCode;
    toFocus=1;
   	}
}

// check for phone
//if (form.Phone.value == "") {
//missingInfo += "\n     *  Phone Number";
//	if (!toFocus) {
//	focusHere = form.Phone;
//    toFocus=1;
//   	}
//}

// check if phone number has anything other than "1234567890()-. "
// spaces, dashes and dots are OK

for (var i=0; i<form.Phone.value.length; i++) {
	if (form.Phone.value != "" &&
		valid.indexOf(form.Phone.value.charAt(i)) == "-1") {
  badphone = true;
  break;
	}
}
if (badphone) {
missingInfo += "\n     -  Phone Number (accepts numbers only)";
	if (!toFocus) {
	focusHere = form.Phone;
    toFocus=1;
   	}
}

// check for email address
if (form.Email.value == "") {
missingInfo += "\n     *  Email address";
	if (!toFocus) {
	focusHere = form.Email;
    toFocus=1;
   	}
}

// check if email address has "@" and "."
if 	(form.Email.value != "") {
	if (form.Email.value.indexOf ('@') == -1 ||
		form.Email.value.indexOf ('.') == -1  ) {
	missingInfo += "\n     -  Email address (requires a '@' and '.' in the address)";
		if (!toFocus) {
		focusHere = form.Email;
    	toFocus=1;
   		}
	}
}



// Error message alert
if (missingInfo != "") {
errorMsg = "Please enter valid data in the following required fields:\n" + missingInfo +
"\n___________________________________" +
"\n\nPlease re-enter and submit again. Thankyou.";
alert(errorMsg);
 if (toFocus) {
 focusHere.focus();
 focusHere.select();
 }
return false;
}
else {
insitePost(form);  //**** added code to save to insite
//delay the script to give the code a chance to send to insite
setTimeout("delayme()",2000);
var times=0;
while (delayed==0 && times<99999) {
	 times++;
}
return true;
}
 
}

//****  this function will delay the submit to give the trackign code a chance to run
function delayme() {
	delayed++;
}

//****  this function will need to be updated to capture the fields on their form
function insitePost(form) {
	// create url to send the information to insite metrics
	url = "http://www.insitemetrics.com/imv2/uRMJ/uniformv2.php";
	url += "?actk=aw8l3-2u88w85915";    //the action key defined to Insite
	url += "&imReferrerField=" + escape(document.location);      
	//add or remove any fields that you went sent to insite metrics
	url += "&Name=" + escape(form.FirstName.value + " " + form.LastName.value);
	url += "&Address=" + escape(form.Address.value);
	url += "&City=" + escape(form.City.value);
	url += "&State=" + escape(form.State.value);
	url += "&Country=" + escape(form.Country.value);
	url += "&PostalCode=" + escape(form.PostalCode.value);
	url += "&Phone=" + escape(form.Phone.value);
	url += "&Email=" + escape(form.Email.value);
	url += "&Comments=" + escape(form.Comments.value);   
	// use an image to call the url
	img = new Image(0,0);
	img.src = url;
}



function fPop(url){
  var popWindow;
  var popWindow = window.open(url,"popup_calendar","scrollbars=yes,resizable=yes,width=300,toolbar=no,status=yes,menubar=no,location=no,directories=no");
  //popWindow.moveTo(150,0)
}