/**
 * @author platypii
 */

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

String.prototype.equalsIgnoreCase=myEqualsIgnoreCase;
String.prototype.equals=myEquals;

function myEquals(arg){
        return (this.toString()==arg.toString());
}

function myEqualsIgnoreCase(arg)
{               
        return (new String(this.toLowerCase())==(new
String(arg)).toLowerCase());
}

function topNavRollOver(obj, state) {
  document.getElementById(obj).className = (state == 'on') ? 'topnavtd-highlight' : 'topnavtd';
}

function isIE(){
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		return true;
	}
	else{
		return false;
	}
}

function whatIEVersion(){
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		temp=navigator.appVersion.split("MSIE")
		return parseFloat(temp[1])
	}
	else{
		return 0;
	}
}

function toggleBox(divID, totalDivs) // 1 visible, 0 hidden
{
    if(whatIEVersion()==0 || whatIEVersion()>4)
    {
		var showDiv = document.getElementById('d' + divID);
		//var showTDLink = document.getElementById('t' + divID);
		var aSrc = document.getElementById('a' + divID);
		var frame = document.getElementById('calcFrame');
        showDiv.className = 'header';
		//showTDLink.className = 'scriptLinkoff';
		if(isIE()){
			frames['calcFrame'].location.href = aSrc.href;
		}
		else{
			calcFrame.src = aSrc.href;
		}
	   	for (var i=1; i<=totalDivs; i++){
	  		if(i!=divID){
			    var showDiv = document.getElementById('d' + i);
				var showTDLink = document.getElementById('t' + i);
        		showDiv.className = 'headeroff';
				//showTDLink.className = 'scriptLink';
			}
	   }
    }
    else {
        document.all['d' + divID].className = 'header';
		//document.all['t' + divID].className = 'scriptLinkoff';
		frames['calcFrame'].location.href = document.all['a' + divID].href;
		//document.all['calcFrame'].src = document.all['a' + divID].href;
	   	for (var i=1; i<=totalDivs; i++){
	  		if(i!=divID){
        		document.all['d' + i].className = 'headeroff';
				//document.all['t' + i].className = 'scriptLink';
			}
	   }
    }
}

function clickclear(thisfield, defaulttext) {
    if (thisfield.value == defaulttext) {
    thisfield.value = "";
    }
}

function clickrecall(thisfield, defaulttext) {
    if (thisfield.value == "") {
    thisfield.value = defaulttext;
    }
}

var good;
function checkEmailAddress(field){
         var goodEmail=field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
         if (goodEmail){
             good = true
         }
         else{
              alert('Please enter a valid e-mail address.');
              field.focus();
              field.select();
              good=false;
         }
}
var goodName;
function checkName(field, defaultField){
         if (field.value.trim().length>0 && !field.value.equals(defaultField.value)){
		 	//document.write("val: " + field.value + "<br>");
			//document.write("eq: " + field.value.equals(defaultField.value) + "<br>");
			//document.write("len: " + field.value.trim().length + "<br>");			
            goodName = true
         }
         else{
              alert('Please enter a valid name.');
              field.focus();
              field.select();
              goodName=false;
         }
}

function checkPhone(field, defaultField){		
         if (field.value.trim().length==0 || field.value.toString().equals(defaultField.value)){
			field.value = '';
         }
		//document.write("val: " + field.value + "<br>");
		//document.write("eq: " + field.value.trim().length==0 && field.value.toString().equals(defaultField.value) + "<br>");
}

function submitForm(){
         good=false;
		 goodName=false;
         checkEmailAddress(document.emailForm.emailInput);
         if (good){
		 	checkName(document.emailForm.nameInput, document.emailForm.nameDefault);
                     if (goodName){
					 	  checkPhone(document.emailForm.phoneInput, document.emailForm.phoneDefault);
						  document.emailForm.submit();
					}
		}
}
