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

////////////////////////////////////////////////////////////////////////////
//
// SET AND READ CAMPAIGN-BASED COOKIES
//
var utm_source_param = getParam('utm_source'); 
if (utm_source_param) { setCookie('utm_source',utm_source_param,30); }
var utm_source_cookie = getCookie('utm_source');

// 1. set the default phone number for this microsite
phone_number_by_subdomain = '800-961-1900';


// 2. try to set phone number based on persistent campaign source cookie
switch(utm_source_cookie){

  case 'google':
    phone_number_by_subdomain = '800-429-6134';
    break;    

  case 'businesscom':
    phone_number_by_subdomain = '800-429-6135';
    break;    

  case 'yahoo':
    phone_number_by_subdomain = '800-429-6059';
    break;  

  case 'msn':
	phone_number_by_subdomain = '866-771-5718';
	break; 
	
  case 'marchex':
	phone_number_by_subdomain = '800-429-6097';
	break; 
	
  case 'superpages':
	phone_number_by_subdomain = '877-919-7446';
	break;

}

// 3. over-ride phone numbers for specific pages


// 3. set hidden form field to track campaign source via subdomain

hidden_subdomain_field_from_cookie = '<input type="hidden" id="inquiry_subdomain" name="inquiry[subdomain]" value="microsite" />'

if (utm_source_cookie) {
  hidden_subdomain_field_from_cookie = '<input type="hidden" id="inquiry_subdomain" name="inquiry[subdomain]" value="'+utm_source_cookie+'" />'
}




// END DYNAMICALLY GENERATED CAMPAIGN LOGIC

//  END OFFER MANAGER INCLUSION
//
//
////////////////////////////////////////////////////////////////////////////


// 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
//
//
// 


// 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


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









