
// <!--


function doFormError(message)
{
   alert(message);
   return false;
}
	
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 checkInternationalPhone(strPhone)
{
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidatePhone(Phone){
		if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter your Phone Number")
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }

function eCheck(str)
{
    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    var error = false;

    if(str.indexOf(at)==-1)
    {
        error = true;

        return false;
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
    {
          error = true;
          return false;
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
    {
          error = true;
          return false;
    }

    if (str.indexOf(at,(lat+1))!=-1)
    {
          error = true;
          return false;
    }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
    {
          error = true;
          return false;
    }

    if (str.indexOf(dot,(lat+2))==-1)
    {
          error = true;
          return false;
    }

    if (str.indexOf(" ")!=-1)
    {
          error = true;
          return false;
    }

    return (error == false) ? null : 'Invalid Email Address';
}
	
function returnValidate(formName)
{
   var theForm;


   if(document.forms)
   {
       theForm = document.forms[formName];
   }



   var textInputs = new Array('firstname', 'lastname', 'First_Cust_Name', 'Cust_Name', 'DonateName', 'DonateAddress', 'DonateCity', 'ownerfirstname', 'ownerlastname', 'Name', 'Name2');
   var emails = new Array('email', 'Email', 'Cust_Email', 'DonateEmailAddress', 'owneremail');
   var phones = new Array('contact_phone', 'Cust_DayPh', 'DonateDaytimePhone', 'ownerhomephone', 'DayPhone', 'home_phone');

   if(theForm.offer_type)
   {
       //if the offer form value offer_type is not set immediately throw an error//
       if(theForm.offer_type.value == '')
       {
           doFormError('The Offer Type is required!\nPlease select Purchase or Rent.');
           return false;
       }
   }

      for(var i = 0; i < theForm.elements.length; i++)
      {
		for(var el = 0; el < textInputs.length; el++)
		{
         if((textInputs[el] == theForm.elements[i].name.toString()) && (theForm.elements[i].value !== ''))
		 {
			for (var char = 0; char < theForm.elements[i].value.length; char++)
			{   
				// Check that current character isn't whitespace.
				var c = theForm.elements[i].value.charAt(char);
				if (!isNaN(c))
				{
			  		doFormError('FirstName and LastName Cannot Contain Numbers.');
					return false;
				}
			}
		 }
		 if((textInputs[el] == theForm.elements[i].name.toString()) && (theForm.elements[i].value == ''))
		 {

			  theForm.elements[i].focus();
			  	doFormError('All Required Fields Must Be Completed And With Valid Data.\nPlease Complete The Required Fields And Click Submit Again.');
              theForm.elements[i].style.backgroundColor = '#ffffdf';

              return false;
		  }
		  
		}

		for(var el = 0; el < emails.length; el++)
		{
		  if((emails[el] == theForm.elements[i].name.toString()) && (eCheck(theForm.elements[i].value) !== null) )
          {
              theForm.elements[i].focus();
              doFormError('Email Address Is Incomplete Or Invalid.');
              theForm.elements[i].style.backgroundColor = '#ffffdf';

              return false;
          }
		}
		for(var el = 0; el < phones.length; el++)
		{
          if(phones[el] == theForm.elements[i].name.toString())
          {
			  var HomePhone = ValidatePhone(theForm.elements[i]);
			  if(HomePhone == false)
			  {
				  theForm.elements[i].style.backgroundColor = '#ffffdf';
				  return false;
              }
		  }
		}
        if((theForm.elements[i].name == 'Offer') && (isNaN(theForm.elements[i].value) == true || theForm.elements[i].value == '' || theForm.elements[i].value == '0' ))
        {
              theForm.elements[i].focus();
              doFormError('Please Include An Offer Amount.');
              theForm.elements[i].style.backgroundColor = '#ffffdf';

              return false;
        }
      }
	  if(theForm.ShowResortID)
      {
      	 var resort = theForm.ShowResortID.value;
       	 if(resort == null || resort == '' || isNaN(resort))
       	 {
           doFormError('Resort Name Is Required To Continue!');
           return false;
         }
   }
}

if(document.getElementById("OfferForm2") !== null)
{
	document.forms["OfferForm2"].onsubmit = function(){
		 return returnValidate("OfferForm2");
	}
	
	document.OfferForm2.Name.focus();
	
	function processSubmit()
	{
		document.OfferForm2.submitbutton.value="Processing...";
		document.OfferForm2.submitbutton.disabled="disabled";
		return true;
	}
}

if(document.getElementById("customcare") !== null)
{
	document.forms["customcare"].onsubmit = function(){
		 return returnValidate("customcare");
	}
}

if(document.getElementById("SellForm"))
{
	document.forms["SellForm"].onsubmit = function(){
		return returnValidate("SellForm");
	}
}	



// -->


