// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


// COOKIE FUNCTIONS

function setCookie(c_name,value,expiredays) {
  hostname = location.hostname;
  var hostnameLoc = hostname.indexOf(".hughesnet.com");
  var hostused = hostname.substring(hostnameLoc + 1, hostname.length);
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	";domain="+hostused+
	";path=/"+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)	{
	if (document.cookie.length>0)	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1) { 
	    c_start=c_start + c_name.length+1; 
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    } 
	  }
	return "";
}

function deleteCookie(c_name) {
	document.cookie = c_name + '=;path=/;expires=Thu, 01-Jan-70 00:00:01 GMT;';
}

function getParam( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}




////////////////////////////////////////////////////////////////////////////
// 
//  BEGIN CONTACT FORM VALIDATION FUNCTIONS
//
//
// 




function fademsg(fieldid,oldcolor){
		setTimeout('document.getElementById("' + fieldid + '").style.color = "#ff0000";',1200);
		setTimeout('document.getElementById("' + fieldid + '").style.color = "#ff2222";',1300);
		setTimeout('document.getElementById("' + fieldid + '").style.color = "#ff4444";',1400);
		setTimeout('document.getElementById("' + fieldid + '").style.color = "#ff6666";',1500);
		setTimeout('document.getElementById("' + fieldid + '").style.color = "#ff8888";',1600);
		setTimeout('document.getElementById("' + fieldid + '").style.color = "#ffbbbb";',1700);
		setTimeout('document.getElementById("' + fieldid + '").style.color = "#ffdddd";',1800);
		setTimeout('document.getElementById("' + fieldid + '").style.color = "#ffffff";',1900);
		setTimeout('document.getElementById("' + fieldid + '").style.color = "'+oldcolor+'";',2000);
}// end function fademsg






gotfocus=false; // ewwwww, a global

// used to limit the size of a textbox (html element with no 
// maxlength attribute of its own).  Used in contact form.
// most of the code here is just to do fancy styles as the
// user gets near the limit
function textCounter(field, countfield, maxlimit) {

	fieldElm = document.getElementById(field)
 
	countfieldElm = document.getElementById(countfield)
	 
	
	
	if (maxlimit > 100){
		gettingClose = maxlimit * .94
		gettingRealClose =  maxlimit * .96
		gettingTooClose =  maxlimit * .98
	} else {
		gettingClose = maxlimit - 15
		gettingRealClose =  maxlimit - 9
		gettingTooClose =  maxlimit - 4
	}
		 
	if (fieldElm.value.length > gettingTooClose){
		countfieldElm.className = "remLenLimit3"
	} else if (fieldElm.value.length > gettingRealClose){
		countfieldElm.className = "remLenLimit2"
	} else if (fieldElm.value.length > gettingClose){
		countfieldElm.className = "remLenLimit1"
	} else {
		countfieldElm.className = "remLenGood"
	}
	
	if (fieldElm.value.length > maxlimit){
		fieldElm.value = fieldElm.value.substring(0, maxlimit);
	} else { 
		countfieldElm.innerHTML = maxlimit - fieldElm.value.length;
	}
} // end of function textCounter





// used to outline error fields in red.  Also adds to the "errmsg"
// string and puts focus to the element in question, unless another
// element has already been focused this round (so that focus is
// always given to the error field highest up on the page)
function markfield(errmsg,fieldid){
	var daform = document.getElementById('new_inquiry');
	if (gotfocus == false){
		gotfocus = true
		daform[fieldid].focus();
	}
	daform[fieldid].style.border = "1px solid red";
	return(errmsg + "\n\n")
}


function simpletest(){
	alert('got here...');
}
// goes through and checks fields for compliance with requirements.  calls markfield
// for any field that fails, and if there are any that fail it also pops a js alert
// box with the corrections needed and cancels form submission
function check_contact_form() {
 
	gotfocus=false;
	var errmsg = "";
	var daform = document.getElementById('new_inquiry');
	
	// clear any red borders we've put up
	daform['inquiry_first_name'].style.border = "";
	daform['inquiry_last_name'].style.border = "";
	daform['inquiry_company'].style.border = "";
	daform['inquiry_city'].style.border = "";
	daform['inquiry_state'].style.border = "";
	daform['inquiry_phone'].style.border = "";
	daform['inquiry_email'].style.border = "";
	daform['inquiry_number_of_locations'].style.border = "";
	//daform['inquiry_business_sector'].style.border = "";
	daform['inquiry_solution'].style.border = "";
	daform['inquiry_industry'].style.border = "";
	daform['inquiry_lead_source'].style.border = "";
	 
	
	// comments is no longer required
	//daform['inquiry_comments'].style.border = "";
	
	if (daform.inquiry_first_name.value==""){
		errmsg += markfield("You must provide your first name.","inquiry_first_name");
	}
	
	if (daform.inquiry_last_name.value==""){
		errmsg += markfield("You must provide your last name.","inquiry_last_name");
	}
	
	if (daform.inquiry_company.value==""){
		errmsg += markfield("You must provide your company name.","inquiry_company");
	}	
	
	if (daform.inquiry_city.value==""){
		errmsg += markfield("You must provide your city.","inquiry_city");
	}
	
	if (daform.inquiry_state.value ==""){
		errmsg += markfield("Please select your state.","inquiry_state");
	}

	// strip phone number to digits
	//stripphone=daform.inquiry_phone.value.replace(/[^0-9]/g,"")
	
	// make sure it's enough digits to be a us number with area code
	if (!daform.inquiry_phone.value.match(/\d{3}\-\d{3}\-\d{4}/)) {
		errmsg += markfield("Phone number should be in the format XXX-XXX-XXXX.","inquiry_phone");
	}

	// 	if (!daform.inquiry_email.value.match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)){
	if (!daform.inquiry_email.value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/i)){
		
		
		
		
		errmsg += markfield("Please enter a valid email address.","inquiry_email");
	}
	
	if (daform.inquiry_number_of_locations.value ==""){
		errmsg += markfield("Please select your number of locations.","inquiry_number_of_locations");
	}

//	if (daform.inquiry_business_sector.value ==""){
//		errmsg += markfield("Please select your business sector.","inquiry_business_sector");
//	}
	
	if (daform.inquiry_solution.value ==""){
		errmsg += markfield("Please select your solution.","inquiry_solution");
	}
	
	if (daform.inquiry_industry.value ==""){
		errmsg += markfield("Please select your industry.","inquiry_industry");
	}
	
	if (daform.inquiry_lead_source.value ==""){
		errmsg += markfield("Please select your lead source.","inquiry_lead_source");
	}
	
	// comments is no longer a required field
	//if (daform.inquiry_comments.value==""){
	//	errmsg += markfield("You must enter a comment.","inquiry_comments");
	//}
	
	if (daform.inquiry_comments.value.length > 500){
		errmsg += markfield("Please limit your comment to 500 characters.","inquiry_comments");
	}

	if (errmsg){
		// scroll up a bit to show title of element with focus
		window.scrollBy(0,-25);
		
		// pop the error messages
		alert(errmsg);
	} else {
		// form is good, send it
		daform.submit();
	}
	
}// end of function check_contact_form

// 
//  END CONTACT FORM VALIDATION FUNCTIONS
//
//
////////////////////////////////////////////////////////////////////////////









