

// show/hide captcha element
function showhideElement(showhide)
{
  var el = document.getElementById('captcha') ? document.getElementById('captcha') : null;
  var hr = document.getElementById('hr') ? document.getElementById('hr') : null;
  if (showhide == 0) {
    if (el) { el.style.display = 'none';}
    if (hr) { hr.style.display = 'none';}
  } else{
    if (el) { el.style.display = '';}
    if (hr) { hr.style.display = '';}
  }
}

// show or hide an element 
function toggleElement(e)
{
  if (document.getElementById(e)) {
    var el = document.getElementById(e);
    if (el.style.display == 'block') { 
      el.style.display = 'none';
    } else {
      el.style.display = 'block';
    }
  }
}

// FUNCTION TO RUN THRU PHONE BOXES
var bAutotab=true;
var done_tabs = {}; // sorry can't stand no backtabbing
function AutoTab(input, maxLength, next_id){
	var tn=input.value;
	var currentLength = tn.length;	
	next = document.getElementById(next_id);
	if(currentLength== maxLength) {
		//alert(next.innerHTML)
		if(next.value=="" && !(done_tabs[next_id])) {
			done_tabs[next_id] = true;
			next.focus();
		   	next.select();
		}
	}
}

function stopAutoTab(){bAutotab=false;}

// PHONENUMBER VALIDATION

function msg(fld,     // id of element ( or element ) to display message in 
             msgtype, // class to give element ("warn" or "error")
             message) // string to display
{
	//alert('display "' + message + '" in ' + fld );
	dispmessage = message;

	var elem;
	//if ( fld instanceof HTMLElement ) {
	if ( fld.nodeType == 1 /*Node.ELEMENT_NODE*/ ) {
		elem = fld;
	} else {
		elem = document.getElementById(fld);
	}

	//elem.firstChild.nodeValue = dispmessage;
	if (dispmessage=='') {
		elem.innerHTML=''
	} else {
		elem.innerHTML='<img src="/pc/_images/alert.gif"/>&nbsp;'+dispmessage
	}
	elem.className = msgtype;   // set the CSS class to adjust appearance of message
}

function validatePN(piid,form)
{

	ermsg="errorMsg";
	areacode="areacode";
	prefix="prefix";
	final="final";
	//alert("Got a message of " + ermsg );
	if (piid != undefined) {
		areacode='areacode'+piid;
		prefix='prefix'+piid;
		final='final'+piid;
		ermsg='errorMsg'+piid;
	}
	//alert("Got a message of " + ermsg );
	
	zip = document.getElementById('zip');
	if (zip != null ) {
		zipVal=zip.value;
		if ( (zipVal == null) || (zipVal=="") ){
			msg(ermsg,"error","Please enter your Zip Code")
			zip.value=""
			zip.focus()
			return false
		} else if ( checkZipCode(zipVal) == false ) {
			msg(ermsg,"error","Please enter a valid US Zip Code")
			zip.value=""
			zip.focus()
			return false
		}
	}


	areacode = document.getElementById(areacode)
	prefix = document.getElementById(prefix)
	final = document.getElementById(final)

	areacodeVal=areacode.value
	prefixVal=prefix.value
	finalVal=final.value
	pn = areacodeVal+prefixVal+finalVal

	//alert("XXX" + pn + '' + piid)
	document.getElementById(ermsg).style.display='block';
	if ((pn==null)||(pn=="")){
		//alert("Please Enter your Phone Number")
		msg(ermsg,"error","Please Enter your Phone Number")
		areacode.focus()
		return false
	}
	if (checkInternationalPhone(pn)==false){
		//alert("Please Enter a Valid Phone Number")
		msg(ermsg,"error","Please Enter a Valid Phone Number")
		areacode.value=""
		prefix.value=""
		final.value=""
		areacode.focus()
		return false
	}

	// Check for US area codes
	if ( checkSupportedAreaCode(pn)==false  ) {
		//alert('Sorry, can only send messages to US area codes.')
   	//alert('Sorry, can only send messages to US area codes.\n\tex. 917-555-0000')
		//msg(errorMsg,"error","Sorry, can only send messages to US area codes.\n\tex. 917-555-0000")
		msg(ermsg,"error","The phone number you entered is invalid. Please enter a valid US mobile number.")
                areacode.value=""
                prefix.value=""
                final.value=""
		areacode.focus()
		return false 
	}

	
	//document.getElementById('errorMsg').style.display = 'none';
	msg(ermsg,"error","");
	return true;

}

function validatePhonenumber()
{
	//alert("XXX")
	if ( validatePN("") == true ) {
		//alert("XxX")
		f.submit();
	} else {
		//alert(")))")
		return false;
	}
	return true
}



// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkZipCode(field) {
        var valid = "0123456789-";
        var hyphencount = 0;

	//alert("check " + field)

        if (field.length!=5 && field.length!=10) {
                //alert("Please enter your 5 digit or 5 digit+4 zip code.");
                return false;
        }

        for (var i=0; i < field.length; i++) {
                temp = "" + field.substring(i, i+1);
                if (temp == "-") hyphencount++;
                if (valid.indexOf(temp) == "-1") {
                        //alert("Invalid characters in your zip code.  Please try again.");
                        return false;
                }
                if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
                        //alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
                        return false;
                }
        }
        return true;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}



var supportedAreaCodes = new Array(
"201","202","203","204","205","206","207","208","209","210","212","213","214","215","216","217","218","219","224","225",
"226","227","228","229","231","234","239","240","242","246","248","250","251","252","253","254","256","260","262","264",
"267","268","269","270","276","278","281","283","284","289","301","302","303","304","305","306","307","308","309","310",
"312","313","314","315","316","317","318","319","320","321","323","325","330","331","334","336","337","339","340","341",
"345","347","351","352","356","360","361","369","380","385","386","401","402","403","404","405","406","407","408","409",
"410","412","413","414","415","416","417","418","419","423","424","425","430","432","434","435","438","440","441","442",
"443","445","450","456","464","469","470","473","475","478","479","480","484","500","501","502","503","504","505","506",
"507","508","509","510","512","513","514","515","516","517","518","519","520","530","533","540","541","551","557","559",
"561","562","563","564","567","570","571","573","574","575","580","585","586","600","601","602","603","604","605","606",
"607","608","609","610","612","613","614","615","616","617","618","619","620","623","626","627","628","630","631","636",
"641","646","647","649","650","651","657","659","660","661","662","664","667","669","670","671","678","679","682","684",
"689","700","701","702","703","704","705","706","707","708","709","710","712","713","714","715","716","717","718","719",
"720","724","727","731","732","734","737","740","747","752","754","757","758","760","762","763","764","765","767","769",
"770","772","773","774","775","778","780","781","784","785","786","787","800","801","802","803","804","805","806","807",
"808","809","810","812","813","814","815","816","817","818","819","822","828","829","830","831","832","833","835","843",
"844","845","847","848","850","855","856","857","858","859","860","862","863","864","865","866","867","868","869","870",
"872","876","877","878","880","881","882","888","900","901","902","903","904","905","906","907","908","909","910","912",
"913","914","915","916","917","918","919","920","925","928","931","935","936","937","939","940","941","947","949","951",
"952","954","956","959","970","971","972","973","975","978","979","980","984","985","989","555"
	);

function checkSupportedAreaCode(phoneNumber) {
	var prefix = phoneNumber.substring(0,3)
	//alert(prefix + ' check against ' + supportedAreaCodes.length )
	for (var j = 0; j < supportedAreaCodes.length; j++) {
		if ( prefix == supportedAreaCodes[j] ) {
			return true;
		}
	}
	return false
	
}


// End Phone number validation



// Validate Email
function validateEmail(piid) {
	ermsg="email_errorMsg";
	email="email_field";
	if (piid != undefined) {
		email='email_field'+piid;
		ermsg='email_errorMsg'+piid;
	}
	email = document.getElementById(email);
	email = email.value;

	if ((email==null)||(email=="")){
		msg(ermsg,"error","Please Enter an Email");
		email.focus();
		return false;
	}
	if ( checkEmail(email)==false ){
		msg(ermsg,"error","Please Enter a Valid Email");
		email.value="";
		email.focus();
		return false;
	}
	msg(ermsg,"error","");
	return true;
}

function checkEmail(address) {

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(address) == false) {
		return false;
	}
	return true
}


// End Validate Email
