/*************************************************************/
/* Copyright (C) 2005 Capturing Design. All rights reserved. */
/* http://www.capturingdesign.com                            */
/*************************************************************/
/* Production Code:  Form Validation                         */
/* Highly specific, Not re-usable                            */
/* Last Updated : April  29, 2005                            */ 
/* ***********************************************************/
/*
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
*/
function isEmpty(inputStr) {if (inputStr == "" || inputStr == null) {return true;}return false;} 

var sY = ""; var sM = ""; var sD = ""; var sH = ""; var sMin = ""; var sSec = ""; 
function DT(d) {var td = new Array(7); td[0] = "Sunday";td[1] = "Monday";td[2] = "Tuesday";td[3] = "Wednesday"; td[4] = "Thursday"; td[5] = "Friday"; td[6] = "Saturday"; return td[d];}
function MT(m) {var tM = new Array(12);tM[0] = "January";tM[1] = "February";tM[2] = "March";tM[3] = "April";tM[4] = "May";tM[5] = "June";tM[6] = "July";tM[7] = "August";tM[8] = "September";tM[9] = "October";tM[10] = "November";tM[11] = "December"; return tM[m];}
function gSD() {var now = new Date();sD = now.getDay(); sM = now.getMonth(); sY = now.getYear();sH = now.getHours(); sMin = now.getMinutes(); sSec = now.getSeconds(); var dt = DT(now.getDay()) + "  "; dt += MT(now.getMonth()) + " ";dt += now.getDate() + ", ";dt += now.getYear() + "  "; dt += " at " + now.getHours() + ":" + now.getMinutes() + ":"  + now.getSeconds();return dt;}
function nSD() {var now = new Date();sD = now.getDate();sM = now.getMonth()+1; sY = now.getYear();var dt = sY + "-" + sM + "-" + sD; return dt;}

function validatelogin(f){
    var Dt = gSD();
	f.userid.value = trim(f.userid.value);
	if (isEmpty(f.userid.value)) {
	      alert ("Please enter your UserID and try again!");
		  return false;
	}
	f.pw.value = trim(f.pw.value);
	if (isEmpty(f.pw.value)) {
	      alert ("Please enter your Password and try again!");
		  return false;
	}
		
    f.sDate_h.value = Dt; 
	return true;
}

function validatecontact(f){
    f.name.value = trim(f.name.value);
	if (isEmpty(f.name.value)) {
	      alert ("Please enter your name and try again!");
		  return false;
	}
    f.email.value = trim(f.email.value);
    if (!isEmail(f.email.value)) {
	      alert ("Please enter a valid email and try again!");
		  return false;
	}
    if (isEmpty(f.message.value)) {
	      alert ("Please enter your message and try again!");
		  return false;
	}
	
    f.sDate_h.value = gSD(); 
	return true;
}
function validateAppraisal(f){
    f.name.value = trim(f.name.value);
	if (isEmpty(f.name.value)) {
	      alert ("Please enter your name and try again!");
		  return false;
	}
    f.email.value = trim(f.email.value);
    if (!isEmail(f.email.value)) {
	      alert ("Please enter a valid email and try again!");
		  return false;
	}
    
	f.phone.value = trim(f.phone.value);
	if (isEmpty(f.phone.value)) {
	      alert ("Please enter your phone number and try again!");
		  return false;
	}
	if (isEmpty(f.message.value)) {
	      alert ("Please enter your message and try again!");
		  return false;
	}
	f.address.value = trim(f.address.value);
	if (isEmpty(f.address.value)) {
	      alert ("Please enter your address and try again!");
		  return false;
	}
	f.age.value = trim(f.age.value);
	if (isEmpty(f.age.value)) {
	      alert ("Please enter the age of your property  and try again!");
		  return false;
	}
	f.lotsize.value = trim(f.lotsize.value);
	if (isEmpty(f.lotsize.value)) {
	      alert ("Please enter the lot size of your property  and try again!");
		  return false;
	}
	
    f.sDate_h.value = gSD(); 
	f.nDate_h.value = nSD(); 
	return true;
}

function validatePassword(f){
   f.oldpw.value=trim(f.oldpw.value);
   if (isEmpty(f.oldpw.value)){
        alert ("Please enter your old password.");
        return false;
   }
   
   f.oldpw.value=trim(f.oldpw.value);
   if (isEmpty(f.oldpw.value)){
        alert ("Please enter your old password.");
        return false;
   }
    f.newpw.value=trim(f.newpw.value);
   if (isEmpty(f.newpw.value)){
        alert ("Please enter your new password.");
        return false;
   }
   f.confirmpw.value=trim(f.confirmpw.value);
   if (isEmpty(f.confirmpw.value)){
        alert ("Please enter your confirm password.");
        return false;
   }
   if (f.confirmpw.value != f.newpw.value) {
       alert ("Please enter identical new and confirm password fields.");
       return false;
   }
  f.pDate_h.value = gSD();
   return true;
}

function validateSubmit(f){
    f.pDate_h.value = gSD();
    f.edit_h.value = "1";
    return true;
}

function validateAddListing(f){
  
	/*alert ("In validateAddListing");*/
	f.pDate_h.value = gSD();
    f.edit_h.value = "1";
    f.listdate.value = trim(f.listdate.value);
	
	if (!isListDate(f.listdate.value)){
	   alert ("The ListingDate " + f.listdate.value + " is invalid.\nPlease enter a valid listing date in the format yyyy-mm-dd and try again.");
        return false;
	}
	
	if (isEmpty(f.listprice.value)){
	  alert ("Please enter valid listing price and try again.");
        return false;
	}
	var price = parseFloat(trim(f.listprice.value));
	/* 
	f.listprice.value = price.toFixed(2);
	alert (f.listprice.value);
	*/
	f.listprice.value = FormatNum(f.listprice.value,2);
	/*alert (f.listprice.value);*/
	if (isEmpty(f.propertytype.value)){
	  alert ("Please select valid property type and try again.");
        return false;
	}
	
		
	return true;
}

function validateAddRealtor(f){
  
	f.pDate_h.value = gSD();
    f.edit_h.value = "1";
 
	if (isEmpty(f.realtorname.value)){
	  alert ("Please enter valid realtor name and try again.");
        return false;
	}
	if (isEmpty(f.realtoraddress.value)){
	  alert ("Please enter valid realtor address and try again.");
        return false;
	}
	
	if (isEmpty(f.realtorphone.value)){
	  alert ("Please enter valid realtor phone and try again.");
        return false;
	}
	/*Optional */
	/*
	if (isEmpty(f.realtorfax.value)){
	  alert ("Please enter valid realtor fax and try again.");
        return false;
	}
	*/
	if (isEmpty(f.realtoremail.value)){
	  alert ("Please enter valid realtor email and try again.");
        return false;
	}
	if (isEmpty(f.maxlists.value)){
	  alert ("Please enter MaxLists and try again.");
        return false;
	}
	return true;
}



function validateSold(f){
   f.saleprice.value=trim(f.saleprice.value);
   if (isEmpty(f.saleprice.value) ){
        alert ("Please enter sale price.");
        return false;
   }
   f.saledate.value=trim(f.saledate.value);
   if (isEmpty(f.saledate.value) || f.saledate.value.indexOf("-") <=0 ){
        alert ("Please enter sale date in the format yyyy-mm-dd.");
        return false;
   }
    f.pDate_h.value = gSD();
    f.edit_h.value = "1";
    return true;
}
function trim(str) {
    /* Removes leading and trailing spaces from input string */
    var temp = str;
    var sReg = /^(\s*)([\W\w]*)(\b\s*$)/;
    if (sReg.test(temp)) { 
        temp = temp.replace(sReg,'$2');
    }
    return temp;
}
function isEmail(s) {
     var rex=true;
     if (window.RegExp){ 
	      st="a";
	      ex=new RegExp(st);
	      if (st.match(ex)) {
		      r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
		      r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
		      b = (!r1.test(s) && r2.test(s));
	      }else{
		      rex=false;
	      }
     }else{	
          rex=false;
     }

     if(!rex) {b = (s.indexOf("@")>0 && s.indexOf(".")>0 && s!="");}
	      return (b);
} 

function isListDate(s){
	 /* alert ("in isListDate");*/
	 DateReg = "^[0-9]{4}[\-]{1}[0-9]{2}[\-]{1}[0-9]{2}$"
	 var regex = new RegExp(DateReg);
	 var ret;
	 if (regex.test(s)){
	     ret = true;
		 /*
                 var yyyy = parseInt(s.substring(0,5 )); 
		 if (yyyy > 2015 || yyyy < 2004 ){ret = false;}
		 var mm = parseInt(s.substring(5,7 ));
		 if (mm >12 || mm <1 ){ret = false;}
	         var dd = parseInt(s.substring(8, 10));
		 alert (("Year : " + yyyy + ("month : "+ mm + "day : "+ dd);
		 if (dd >31 || dd <1 ){ret = false;}
                 */	 
	 }else{
	     ret = false; 
	 }
	 return ret;
}

function FormatNum(s, decplaces){
   var str = (Math.round(parseFloat(s)*Math.pow(10,decplaces))).toString();
   while(str.length <= decplaces) {
      str = "0" + str; 
   }
   var decpoint = str.length - decplaces;
   return str.substring(0,decpoint)+"."+str.substring(decpoint,str.length);
}

function isNumber(s) {
   for (var i = 0; i < s.length; i++) {
      var c = s.substring(i, i + 1);
        if (c < "0" || c > "9") {
           return false;
        }
   }
   return true;
}

/* added for Arabic keyboard   June 4, 2006*/
var txtfield = 2;
function setOutput(letter){
     if (txtfield == 1) {contact.name.value = contact.name.value + letter;}
	 else{contact.message.value = contact.message.value + letter;}
}
function settxtfield(s){
  txtfield = s;
}