function _CF_onError(form_object, input_object, object_value, error_message)
    {
	alert(error_message);
       	return false;	
    }


function _CF_hasValue(obj, obj_type)
    {
    if (obj_type == "TEXT" || obj_type == "PASSWORD")
	{
    	if (obj.value.length == 0) 
      		return false;
    	else 
      		return true;
    	}
    else if (obj_type == "SELECT")
	{
        for (i=0; i < obj.length; i++)
	    	{
		if (obj.options[i].selected)
			return true;
		}

       	return false;	
	}
    else if (obj_type == "SINGLE_VALUE_RADIO" || obj_type == "SINGLE_VALUE_CHECKBOX")
	{

		if (obj.checked)
			return true;
		else
       		return false;	
	}
    else if (obj_type == "RADIO" || obj_type == "CHECKBOX")
	{

        for (i=0; i < obj.length; i++)
	    	{
		if (obj[i].checked)
			return true;
		}

       	return false;	
	}
	}


function _CF_checkinteger(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
	return _CF_checknumber(object_value);
    else
	return false;
    }


function _CF_numberrange(object_value, min_value, max_value)
    {
    // check minimum
    if (min_value != null)
	{
        if (object_value < min_value)
		return false;
	}

    // check maximum
    if (max_value != null)
	{
	if (object_value > max_value)
		return false;
	}
	
    //All tests passed, so...
    return true;
    }


function _CF_checknumber(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true
    }


function _CF_checkrange(object_value, min_value, max_value)
    {
    //if value is in range then return true else return false

    if (object_value.length == 0)
        return true;


    if (!_CF_checknumber(object_value))
	{
	return false;
	}
    else
	{
	return (_CF_numberrange((eval(object_value)), min_value, max_value));
	}
	
    //All tests passed, so...
    return true;
    }

function _CF_checkphoneIntl(object_value) 
{    
	if (object_value.length == 0)
        return true;
	
	// Declaring required variables
	var digits = "0123456789";
	// non-digit characters which are allowed in phone numbers
	var phoneNumberDelimiters = "()-xX";
	// 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 = 3;
	
	s=stripCharsInBag(object_value,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

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 ValidateForm(){
	var Phone=document.frmSample.txtPhone
	
	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 _CF_checkphone(object_value)
    {
    if (object_value.length == 0)
        return true;
		
    if (object_value.length != 12)
        return false;

	// check if first 3 characters represent a valid area code
    if (!_CF_checknumber(object_value.substring(0,3)))
		return false;
    else
	if (!_CF_numberrange((eval(object_value.substring(0,3))), 100, 1000))
		return false;

	// check if area code/exchange separator is either a'-' or ' '
	if (object_value.charAt(3) != "-" && object_value.charAt(3) != " ")
        return false

	// check if  characters 5 - 7 represent a valid exchange
    if (!_CF_checknumber(object_value.substring(4,7)))
		return false;
    else
	if (!_CF_numberrange((eval(object_value.substring(4,7))), 100, 1000))
		return false;
	
	// check if exchange/number separator is either a'-' or ' '
	if (object_value.charAt(7) != "-" && object_value.charAt(7) != " ")
        return false;

	// make sure last for digits are a valid integer
	if (object_value.charAt(8) == "-" || object_value.charAt(8) == "+")
        return false;
	else
	{
		return (_CF_checkinteger(object_value.substring(8,12)));
	}
    }


//Check Email for valid address

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		if (str.indexOf(at)==-1){
		    return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}

function _CF_hasEmailAddress(){
	var emailID=document.contactYipes.email
	
	if ((emailID.value==null)||(emailID.value=="")){
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }

//End Check Email for valid address

function  _CF_checkcontactYipes(_CF_this)

    {

    if  (!_CF_hasValue(_CF_this.company, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.company, _CF_this.company.value, "Company is a required field.  Please check and make sure it has been completed."))

            {

            return false; 

            }

        }


 
    if  (!_CF_hasValue(_CF_this.first_name, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.first_name, _CF_this.first_name.value, "First name is a required field.  Please check and make sure it has been completed."))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.last_name, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.last_name, _CF_this.last_name.value, "Last name is a required field.  Please check and make sure it has been completed."))

            {

            return false; 

            }

        }
        

    if  (!_CF_hasValue(_CF_this.lead_source, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.lead_source, _CF_this.lead_source.value, "How did you hear about us? Please let us know."))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.email, "TEXT" )) 

       {

        if  (!_CF_onError(_CF_this, _CF_this.email, _CF_this.email.value, "E-mail address is a required field.  Please check and make sure it has been completed."))

            {

            return false; 

            }

        }
        
    if  (!_CF_hasEmailAddress(_CF_this.email, "TEXT" )) 

       {

        if  (!_CF_onError(_CF_this, _CF_this.email, _CF_this.email.value, "Please check and make sure you have a valid e-mail address."))

            {

            return false; 

            }

        }        




    if  (!_CF_hasValue(_CF_this.phone, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.phone, _CF_this.phone.value, "Please enter a valid phone number."))

            {

            return false; 

            }

        }
    if  (!_CF_hasValue(_CF_this.country, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.country, _CF_this.country.value, "Please enter your country."))

            {

            return false; 

            }

        }
        
      
          if  (!_CF_hasValue(_CF_this.industry, "TEXT" )) 
      
              {
      
              if  (!_CF_onError(_CF_this, _CF_this.industry, _CF_this.industry.value, "Please enter your industry."))
      
                  {
      
                  return false; 
      
                  }
      
        }

    if  (!_CF_checkphoneIntl(_CF_this.phone.value))

        {

        if  (!_CF_onError(_CF_this, _CF_this.phone, _CF_this.phone.value, "Please enter a valid Phone number"))

            {

            return false; 

            }

        }

       

    return true;

    }

