/*****************************************************************************************************
* Easy Javascript Validation
* Copyright 2006, All Rights reserved by Daniel Boorn
* In any form element add the following form attributes to validate
* required ="yes"
* validate = { "int", "float", "text", "email" }
* message = "Error Message for Element"
* Example: <input type="text" validate="int" message="Please enter valid zip code" name="zip">
*
* It is required that you add the following to any submit button
*     onClick="validate(this.form); return document.formSubmit;   alert(error);"
******************************************************************************************************/

	function validate(form){
		var error = "";
		//for each form element
		for(var i=0; i<form.length; i++){
			var element = form[i];
			//if required
			if(element.getAttribute("required") == "yes"){
				//if form element if empty
				if(!valid(element.value,element.getAttribute("validate"),element,element.getAttribute("minlength")))
				{
					element.style.border="1px solid #FF0000"
					error += element.getAttribute("message") + "\r\n";}
				else{
					element.style.border="1px solid #009900"
				}
			}
		}
		if(error != ""){
		    error  = rtrim(error);
		    if ( error.substring(error.length-1,error.length) ==","){
		        error = error.substring(0,error.length-1);
		    };
			
			if ( error.substring(0,1) ==","){
		        error = error.substring(1,error.length);
		    };
			
			
			document.getElementById('writetolayer').style.display	= "block";
			document.getElementById('form').style.marginTop	= "60px";
			document.getElementById('writetolayer').innerHTML = "Please fill the following fields: " + error;
			//javascript:scroll(0,0);
			return false;
		}
		else
		{
			document.getElementById('writetolayer').style.display	= "none";
			document.getElementById('form').style.marginTop	= "15px";
			
			document.getElementById('writetolayer').innerHTML = "";
			return true;
		}
	}	
	
	function valid(value,type,element,minlen){
		if(trim(value,"") == "")
			return false;
			
		switch(type){
			case "int":
				if(isNaN(parseInt(value)))
					return false;
				break;
			case "float":
				if(isNaN(parseFloat(value)))
					return false;
				break;
			case "email":
	   			  return checkEmail(value);
  				  break;
			case "checked":
				if(!element.checked)
					return false;
				break;
			case "phone":
				return ValidatePhone(element);
				break;
				
			default://string
				break;
		}

		if (minlen !=""){
			if (value.length<minlen){
				return false;
			}
		}	
		return true;
	}	
	
	
function checkEmail(email) {
  var str = new String(email);
  var isOK = true;
  rExp = /[!\"£$%\^&*()-+=<>,\'#?\\|¬`\/\[\]]/
  if( rExp.test(str) )
    isOK = false;
  if( str.indexOf('.') == -1 || str.indexOf('@') == -1 )
    isOK = false;
  if( str.slice(str.lastIndexOf('.')+1,str.length).length < 2 )
    isOK = false;
  if( str.slice(0,str.indexOf('@')).length < 1 )
    isOK = false;
  if( str.slice(str.indexOf('@')+1,str.lastIndexOf('.')).length < 1 )
    isOK = false;

  if( !isOK )
    return false;

  return true;
}
	
function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}




/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
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 trimPhone(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
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){
	var bracket=3
	strPhone=trimPhone(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	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;
 }
