/***********CODE FOR CALCULATOR**********/
arrFormValidation = new Array();
idx=0;

function CheckNumeric()
{
   // Get ASCII value of key that user pressed
   var key = window.event.keyCode;

   // Was key that was pressed a numeric character (0-9)?
   if ( key > 47 && key < 58 )
      return; // if so, do nothing
   else
      window.event.returnValue = null; // otherwise, discard character
}

function formatCurrency(num) {
		num = num.toString().replace(/\$|\,/g,'');
		percent_selected = document.form1.select1.selectedIndex;
		percent = document.form1.select1.options[percent_selected].value ;
		this_result = ((num * percent) - num) ;
		//normal_result = num * 1.02
		num = this_result ;
    return format_price(num);
	}

function format_price(num){
if (isNaN(num)) {
			num = "0";
			return num;
		} else {
			sign = (num == (num = Math.abs(num)));
			num = Math.floor(num*100+0.50000000001);
			cents = num%100;
			num = Math.floor(num/100).toString();
			if (cents<10)
				cents = "0" + cents;
			for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
			num = num.substring(0,num.length-(4*i+3))+','+
			num.substring(num.length-(4*i+3));
			if (parseInt(cents) == 0) {
				return (((sign)?'':'-') + '$' + num);
			} else {
				return (((sign)?'':'-') + '$' + num + '.' + cents);
			}
		}

}
function calctradition_com(num){
	num = num.toString().replace(/\$|\,/g,'');
	//num=num * 1.06;
		percent_selected = document.form1.select1.selectedIndex;
		percent = document.form1.select1.options[percent_selected].value ;
		num = ((num * percent) - num) ;
		//num = ((num * 1.06) - num) ;
	return format_price(num);
}

function our_sol(num){
num = num.toString().replace(/\$|\,/g,'');
num=(299+(num * 1.02) - num);
return format_price(num);
}

function your_saving(num){
	num = num.toString().replace(/\$|\,/g,'');
	
	percent_selected = document.form1.select1.selectedIndex;
	percent = document.form1.select1.options[percent_selected].value ;
	num1 = ((num * percent) - num) ;
	//num1=((num * 1.06) - num);
	num2=(299 + ((num * 1.02)-num));
	num=(num1-num2);
return format_price(num);
}

	function calcSavings() {
		price = formatCurrency(document.form1.text1.value)
		if (price == "0") {
			result = ""
			document.form1.text1.value=""
			alert("Please enter home value that is greater than $0.")
		}
		//document.form1.text2.value = price
		num=document.form1.text1.value;
		num = num.toString().replace(/\$|\,/g,'');
		document.form1.text1.value=format_price(num);
		document.form1.homeprice.value=format_price(num);  // C
		document.form1.tradition_com.value=calctradition_com(document.form1.text1.value); //D
		document.form1.our_sol.value=our_sol(document.form1.text1.value); //E
		document.form1.savings.value=your_saving(document.form1.text1.value);//F
		document.form1.text2.value = your_saving(document.form1.text1.value);//G		
		
		document.form1.text1.focus;
	}
	function checkIt(event)
	{
		var charCode

		if (navigator.appName == "Netscape")
		{
			charCode = event.which
		}
		else
		{
			charCode = event.keyCode
		}
		if (charCode == "13")
		{
			document.form1.text2.focus()
		}
	}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}


/**************CODE FOR NUMBER FORMAT FOR ZIP CODE***************/
function FormatNumber(num, format, shortformat)
{
 if(format==null)
 {
     format = "(###) ###-####";   // Telephone w/ Area Code

 }

 if(shortformat==null)
 {
    var shortformat = "";
 }

 var validchars = "0123456789";
 var tempstring = "";
 var returnstring = "";
 var extension = "";
 var tempstringpointer = 0;
 var returnstringpointer = 0;
 count = 0;

 // Get the length so we can go through and remove all non-numeric characters
 var length = num.value.length;


 // We are only concerned with the format of the phone number - extensions can be left alone.
 if (length > format.length)
 {
  length = format.length;
 };

 // scroll through what the user has typed
 for (var x=0; x<length; x++)
 {
  if (validchars.indexOf(num.value.charAt(x))!=-1)
  {
  tempstring = tempstring + num.value.charAt(x);
  };
 };
 // We should now have just the #'s - extract the extension if needed
 if (num.value.length > format.length)
 {
  length = format.length;
  extension = num.value.substr(format.length, (num.value.length-format.length));
 };

 // if we have fewer characters than our short format, we'll default to the short version.
 for (x=0; x<shortformat.length;x++)
 {
  if (shortformat.substr(x, 1)=="#")
  {
   count++;
  };
 }
 if (tempstring.length <= count)
 {
  format = shortformat;
 };


 //Loop through the format string and insert the numbers where we find a # sign
 for (x=0; x<format.length;x++)
 {
  if (tempstringpointer <= tempstring.length)
  {
   if (format.substr(x, 1)=="#")
   {
    returnstring = returnstring + tempstring.substr(tempstringpointer, 1);
    tempstringpointer++;
   }else{
    returnstring = returnstring + format.substr(x, 1);
   }
  }

 }

 // We have gone through the entire format, let's add the extension back on.
  returnstring = returnstring + extension;

 //we're done - let's return our value to the field.
 num.value = returnstring;
}


/************* CODE FOR VALIDATE THE USER INPUTS  ***************/
	function verify()
	{
	  var s;
	   s=document.from_submit.phone.value;

	    if(document.from_submit.firstname.value == "")
		{
			alert("MISSING: FIRST NAME.");
			document.from_submit.firstname.focus();
			return false;
		}
		if(document.from_submit.lastname.value == "")
		{
			alert("MISSING: LAST NAME.");
			document.from_submit.lastname.focus();
			return false;
		}

		if(document.from_submit.email.value == "")
		{
			alert("MISSING: EMAIL ADDRESS.");
			document.from_submit.email.focus();
			return false;
		}
		if(!check_email(document.from_submit.email.value))
		{
			alert("Invalid Email Address!");
			document.from_submit.email.value = ""
			document.from_submit.email.focus();
			return false;
		}

		if(document.from_submit.companyname.value == "")
		{
			alert("MISSING: COMPANY NAME on your Real Estate License.");
			document.from_submit.companyname.focus();
			return false;
		}
		if(document.from_submit.licensenumber.value == "")
		{
			alert("MISSING: LICENSE NUMBER In Good Standing.");
			document.from_submit.licensenumber.focus();
			return false;
		}
		if(document.from_submit.MLS_systems.value == "")
		{
			alert("MISSING: MLS SYSTEM where you have membership.");
			document.from_submit.MLS_systems.focus();
			return false;
		}
		if(document.from_submit.website.value == "")
		{
			alert("MISSING: WEBSITE ADDRESS or Office's Website Address.");
			document.from_submit.website.focus();
			return false;
		}
		if(document.from_submit.phone.value == "")
		{
			alert("MISSING: CONTACT PHONE NUMBER.");
			document.from_submit.phone.focus();
			return false;
		}
		 //alert(s.length);
		if(s.length != 12)
		{  ///alert(s.length);
			alert("Invalid Format (xxx-xxx-xxxx).");
			document.from_submit.phone.focus();
			return false;
		}
		
		if(document.from_submit.zip.value == "")
		{
			alert("You Must Enter A Valid Zip Code.");
			document.from_submit.zip.focus();
			return false;
		}
		return true;
	}


	function check_email(e)
	{
		  //var field = form.email; // email field
		  //var str = field.value; // email string
		  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
		  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
		  if (!reg1.test(e) && reg2.test(e)) { // if syntax is valid

			return true;
		  }
		  else
		{
			  return false;
		}
	}


function checkAll()
  {
      var lgn = document.from_submit.length;
      for(var i = 0; i < lgn; i++)
      {
         if(document.from_submit[i].type == "checkbox" )
         {
            document.from_submit[i].checked = true

          }
       }
  }
function uncheckAll()
  {
      var lgn = document.from_submit.length;
      for(var i = 0; i < lgn; i++)
      {
         if(document.from_submit[i].type == "checkbox"  )
         {
            document.from_submit[i].checked = false

          }
       }
  }

function check(){
if(document.from_submit.select1.checked == true){
checkAll();
document.from_submit.select1.checked =  true
}
else{
uncheckAll();
document.from_submit.select1.checked = false
}
}

function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
} 
