/*------------------------------------------------------------------------------------------------------
email validator
input 	: email string
Returns	: true if email id is valid else retuen false
------------------------------------------------------------------------------------------------------*/
function checkemail(pstrEmail){
	var emailOK = false;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	
	if (filter.test(pstrEmail)) {
		emailOK = true;
	}
	return (emailOK);
}


/*------------------------------------------------------------------------------------------------------
email length validation (minmim 6 characters required)
input 	: email string
Returns	: true if email id is valid else retuen false
------------------------------------------------------------------------------------------------------*/
function checkspaces(pstrMail){

    var emailOK = true;
    var val=pstrMail;
    var vspace=trim(val);

    if((vspace=="")||(vspace.length<6)){
	    emailOK = false;
    }
    
    return emailOK;
}

function trim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ") { temp = ""; }
   return temp;
}

/*------------------------------------------------------------------------------------------------------
Function to remove all spaces in a field value
input 	: text object
Returns	: returns empty string if the field has no value
------------------------------------------------------------------------------------------------------*/
function spacetrim(obj){
	var pstr = obj.value;
	var pstr1 = "";
//alert(pstr.length);
	if (pstr != null && pstr!="") {
		while (pstr !=null && pstr != ""){
			pstr1 = pstr.replace(" ","");
			if (pstr1==pstr){
				break;
			} else {pstr=pstr1;}
		}
		if (pstr=="") { obj.value = pstr;}
	} else { pstr="";}
	
	return(pstr);
}

/*------------------------------------------------------------------------------------------------------
Function to remove all ' and " in a field value
input 	: text object
Returns	: none
------------------------------------------------------------------------------------------------------*/
function quotetrim(obj){
 if (obj!= null) {
	var pstr = obj.value;
         pstr=pstr.replace(/'/g,"");
         pstr=pstr.replace(/"/g,'');
         obj.value = pstr;
    }
}
/*------------------------------------------------------------------------------------------------------
Functions for validating phone number (min 8 chars required)
input 	: phone object to ValidatePhone function.
Returns	: returns true of false.
------------------------------------------------------------------------------------------------------*/
// 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 = 8;

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(obj){
	var Phone=obj;
	
	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
 }
 
/*------------------------------------------------------------------------------------------------------
 Functions for validating Pincode (min 6 numbers)
 input 	: phone object to ValidatePhone function.
 Returns	: returns true of false.
------------------------------------------------------------------------------------------------------*/
function ValidatePin(obj){
	var Pin = spacetrim(obj);	//remove all spaces
	lreturn = false;
	
	if (isInteger(Pin)){
		if (Pin.length<6){
			alert("Pincode must be 6 digits..");
		} else { 
			lreturn=true;
		}
	} else {
		alert("Pincode can not contain non numerals..");
	}
	
	if (!lreturn) {
		obj.value="";
		obj.focus();
	}
	
	return lreturn;
 }
 
/*------------------------------------------------------------------------------------------------------
  Functions to chceck blank values
  input 	: display name of the field and the object
  Returns	: returns true of false.
------------------------------------------------------------------------------------------------------*/
function chkBlank(strFldName, obj){
	if ((obj == null) || (spacetrim(obj) == "")) {
		alert("You must enter "+ strFldName + "...");
		obj.focus();
		return false;
	} else { 
		return true; 
	}
}

