// create the prototype on the String object

String.prototype.trim = function() {

 // skip leading and trailing whitespace
 // and return everything in between
  var x=this;
  x=x.replace(/^\s*(.*)/, "$1");
  x=x.replace(/(.*?)\s*$/, "$1");
  return x;
}

// function to validate Search PG Request
function validate_search()
{
var docF=document.form1;
if(docF.type.value == 0 && docF.Budget.value == 1 && docF.sharing.value == 0 && docF.city.value == "") {
	alert("Please enter atleast one search criteria.");
	return false;
}

}


// function to validate form of PG Registration
function validate_pg()
{
var docF=document.form2;

var name=docF.name.value;
var length= docF.name.length;

if (docF.name.value.trim() == "")
{
alert("Name cannot be Empty.");
docF.name.focus();
return false;
}

if ((docF.address1.value).trim() == "")
{
alert("Address1 cannot be Empty.");
docF.address1.focus();
return false;
}

if ((docF.city.value).trim() == "")
{
alert("Please specify City.");
docF.city.focus();
return false;
}

if ((docF.pincode.value).trim() == "")
{
alert("Please specify Pincode");
docF.pincode.focus();
return false;
}

if((docF.pincode.value).trim()&&(isNaN(+(docF.pincode.value).trim())||((docF.pincode.value).trim()).length!=6 ))
{
alert("Pincode should contains exactly six numerics.");
docF.pincode.focus();
return false;
}

if (docF.type.value == "")
{
alert("Please specify Type of PG.");
docF.type.focus();
return false;
}

if (docF.sharing.value == "")
{
alert("Please specify Sharing.");
docF.sharing.focus();
return false;
}

if (docF.rent_single.value == "" && docF.rent_sharing.value == "")
{
alert("Please specify Rent.");
docF.rent_single.focus();
return false;
}

if (docF.rent_advance.value == "")
{
alert("Please specify Advance Rental Required.");
docF.rent_advance.focus();
return false;
}

if(((docF.phone.value).trim() == "" || (docF.citycode.value).trim() == "") && ((docF.altphone.value).trim() == "" || (docF.altcity.value).trim() == "") && (docF.mobile.value).trim() == "")
{
alert("Enter atleast one of these - \nPhone No., Alternate Ph. No. or Mobile No.");
docF.citycode.focus();
return false;
}

if(isNaN(+(docF.countrycode.value).trim()) || isNaN(+(docF.citycode.value).trim()) || isNaN(+(docF.phone.value).trim()))
{
alert("Phone No. entered is not valid!");
docF.countrycode.focus();
return false;
}

if(isNaN(+(docF.altcountry.value).trim()) || isNaN(+(docF.altcity.value).trim()) || isNaN(+(docF.altphone.value).trim()))
{
alert("Alternate Phone No. entered is not valid!");
docF.altcountry.focus();
return false;
}

if(isNaN(+(docF.mobile.value).trim()))
{
alert("Invalid Mobile Number!");
docF.mobile.focus();
return false;
}

if(isNaN(+(docF.fax.value).trim()))
{
alert("Invalid Fax Number!");
docF.fax.focus();
return false;
}

if(!docF.terms.checked)
{
alert("Please Agree to Terms and Conditions before Proceeding.");
docF.terms.focus();
return false;
}

}

// function to validate form of User Registration
function validate_user()
{

var docF=document.form1;


var username=(docF.username.value).trim();
var length= username.length;

if (username == '')
{
alert("Username cannot be Empty.");
docF.username.focus();
return false;
}

if (length < 6 || length > 20 )
{
alert("Enter username of length greater than 6 and less than 20 ");
docF.username.focus();
return false;
}

username_pattern = /\W/gi;

if (username.match(username_pattern) != null)
{
alert("Username can contain only Alpha-Numerics and underscore.");
docF.username.focus();
return false;
}

if(docF.password.value != docF.confpass.value)
{
alert("Passwords do not match\n");
docF.confpass.focus();
return false;
}

if(docF.username.value==docF.password.value)
{
alert("Username and Password cannot be same.");
docF.password.focus();
return false;
}

if((docF.password.value).trim() == "")
{
alert("Password cannot be Empty.");
docF.password.focus();
return false;
}
password=docF.password.value;
length=password.length;
if((length) < 6 || (length) > 20)
{
alert("Password should be 6 - 20 characters long");
docF.password.focus();
return false;
}

if ((docF.name.value).trim() == "")
{
alert("Name cannot be Empty");
docF.name.focus();
return false;
}


if ((docF.address1.value).trim() == "")
{
alert("Address cannot be Empty.");
docF.address1.focus();
return false;
}

if (docF.city1.value == "")
{
alert("Please specify City.");
docF.city1.focus();
return false;
}
if ((docF.pincode.value).trim() == "")
{
alert("Please specify Pincode");
docF.pincode.focus();
return false;
}

if((docF.pincode.value).trim()&&(isNaN(+(docF.pincode.value).trim())||((docF.pincode.value).trim()).length!=6 ))
{
alert("Pincode should contains exactly six numerics.");
docF.pincode.focus();
return false;
}

if(!isValidEmailAddress(docF.email1)) return false;

if((docF.email1.value).trim() != (docF.email2.value).trim())
{
alert("Email IDs do not match");
docF.email1.focus();
return false;
}

if(((docF.phone.value).trim() == "" || (docF.citycode.value).trim() == "") && ((docF.altphone.value).trim() == "" || (docF.altcity.value).trim() == "") && (docF.mobile.value).trim() == "")
{
alert("Enter atleast one of these - \nPhone No., Alternate Ph. No. or Mobile No.");
docF.citycode.focus();
return false;
}

if(isNaN(+(docF.countrycode.value).trim()) || isNaN(+(docF.citycode.value).trim()) || isNaN(+(docF.phone.value).trim()))
{
alert("Phone No. entered is not valid!");
docF.countrycode.focus();
return false;
}

if(isNaN(+(docF.altcountry.value).trim()) || isNaN(+(docF.altcity.value).trim()) || isNaN(+(docF.altphone.value).trim()))
{
alert("Alternate Phone No. entered is not valid!");
docF.altcountry.focus();
return false;
}

if(isNaN(+(docF.mobile.value).trim()))
{
alert("Invalid Mobile Number!");
docF.mobile.focus();
return false;
}

if(isNaN(+(docF.fax.value).trim()))
{
alert("Invalid Fax Number!");
docF.fax.focus();
return false;
}

if(!docF.terms.checked)
{
alert("Please Agree to Terms and Conditions before Proceeding.");
docF.terms.focus();
return false;
}

}

// This function is called on changing the selected option of Sharing field on PG Registration.
function onChangeSharing()
{
var docF=document.form2;

if(docF.sharing.value == "1") {
docF.rent_single.disabled = false;
docF.rent_sharing.value = "1";
docF.rent_sharing.disabled = true;
}
if(docF.sharing.value == "2") {
docF.rent_single.value = "1";
docF.rent_single.disabled = true;
docF.rent_sharing.disabled = false;
}
if(docF.sharing.value == "3") {
docF.rent_single.disabled = false;
docF.rent_sharing.disabled = false;
}

}

function isValidEmailAddress(emailAddress) {
    var ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
    var eAddr = emailAddress.value.trim();
    var chkDot = ndxDot2 = true;
    var usEmail = true;

    for(i=0; i < eAddr.length ;i++){
        if(ok.indexOf(eAddr.charAt(i))<0){
            alert('Email field contains invalid character(s)')
            emailAddress.focus();
            emailAddress.select();
            return (false);
        }
    }
    var firstAlphabet = eAddr.charAt(0);

    var lenSuffix = 3;
      var result = false;
      var ndxAt = ndxDot =  0;
      var lastAT = 0;
      ndxAt  = eAddr.indexOf('@');
      lastAT  = eAddr.lastIndexOf('@');
      ndxDot = eAddr.indexOf('.') ;
      ndxDot2 = eAddr.lastIndexOf('.') ;

      if (!(firstAlphabet >= 'a' && firstAlphabet <= 'z') && !(firstAlphabet >= 'A' && firstAlphabet <= 'Z') && !(firstAlphabet >= 0 && firstAlphabet <= 9))
             alert(" E-mail Id should begin with an alphabet/numeric.");
      else if ((ndxDot < 0) || (ndxAt < 0))
         alert("Email address lacks '.' or '@'.\n\nThe format is 'user@dom.suf'");
      else if (lastAT != ndxAt )
         alert("Email address must have only one '@'.\n\nThe format is 'user@dom.suf'");
      else if ( (ndxDot2 - 2) <= ndxAt)    //Modified (ndxDot2 - 3) to (ndxDot2 - 2) as per SPR No.5055 by Shiva Kumar B.S. on 28th Oct 2003
         alert("Email address contains invalid domain name.");
      else if (eAddr.length < ndxDot2 + lenSuffix)
           alert("Email address contains invalid domain name.");
      else
           result=true;

      if(!result){
        emailAddress.focus();
        emailAddress.select();
      }
      return result;
}