/*
* 		Client side validations Java Script library
*		Version 1.0
*		Creation date 29/11/2005
*		Developer: Yoonas Khan
*
*
*		Description:
*		This library contains client side Java script validation functions
*		used to validate user input before the form is submitted, 
*		most of these function rely on Regular Expressions when performing 
*		their respective validations, these Regular Expressions can be over ridden
*		to provide tailored functionality.
*		see each function's description for further info on each function and its usage.
*
	
*/	
// This functions allow only the character which declared here for the text boxes 
var INTEGER_ALLOWED_CHARS =  /^((-[1-9])?[0-9]*|[-]|[+])$/;
var NUMBERS_ALLOWED_CHARS =  /^((-[1-9])?[0-9]*)$/;

function checkNumbers(fieldRef){
//alert("checkNumbers")
	var fieldValue = String.fromCharCode(event.keyCode);
		//alert(INTEGER_ALLOWED_CHARS.test(fieldValue))
	if (INTEGER_ALLOWED_CHARS.test(fieldValue) ) {
		return true;
	}else{
		return false;
	}
}




function checkInteger(fieldRef){
//alert("checkNumbers")
	var fieldValue = String.fromCharCode(event.keyCode);
		//alert(INTEGER_ALLOWED_CHARS.test(fieldValue))
	if (NUMBERS_ALLOWED_CHARS.test(fieldValue) ) {
		return true;
	}else{
		return false;
	}
}

function trim(str)
{
	//alert('str ' +str);
	var x;
	var ch;
	
	for(x=0;x<str.length;x++)
	{
		ch=str.substr(x,1);
		if(ch==' ' || ch=='\t')
		{
			str=str.substr(x+1,str.length-1);
		}
		else
			break;
	}
	
	for(x=str.length-1;x>=0;x=x-1)
	{
		ch=str.substr(x,1);
		if(ch==' ' || ch=='\t')
		{
			str=str.substr(0,str.length-1);
		}
		else
			break;
	}
	
	return str;
}



	//this function accepts the object as a parameter and display the appropriate error messages if the text box is empty
	function checkempty(objname,errmsg)
	{
		if (objname.value=="")
		{
			alert(errmsg);
			objname.focus();
			return false;
		}
		return true;
	}

	//this function accepts the object as a parameter and display the appropriate error messages if the text box is empty
	//similar to the above function only that it checks for combo box
	function checkcomboempty(objname,errmsg)
	{
		if (trim(objname[objname.selectedIndex].value)=="")
		{
			alert(errmsg);
			objname.focus();
			return false;
		}
		return true;
	}

	function checknumeric(objname,errmsg)
	{
		if (isNaN(trim(objname.value)))
		{
			alert(errmsg);
			objname.focus();
			return false;
		}
		return true;
	}
//Email validation starts from here
	function checkemail(objname,errmsg)
	{
		vvalue=trim(objname.value);
		atPos = vvalue.indexOf('@');
		sppos = vvalue.indexOf(" ");
		dopos = vvalue.indexOf(".");
		if (atPos < 1 || atPos == (vvalue.length - 1) || (sppos != -1)|| (dopos == -1))
		{
			alert(errmsg);
			objname.focus();
			return false;
		}
		return true;
	}
//Email validation ends from here

//telno validation starts from here 
//it checks that the entered value does not contain anything except numeric characters and hyphen
	function checktelno(objname,errmsg)
	{
		var str = trim(objname.value);
		for(x=0;x<str.length;x++)
		{
			ch=str.substr(x,1);
			if ((ch < '0' || ch >'9')&&(ch!='-'))
			{
				//alert(errmsg);
				objname.focus();
				return false;
			}
		}
		return true;
	}
//telno validation ends over here

//maxlength validation starts over here
	function checkmaxlength(objname,maxlength,errmsg)
	{
		var str = objname.value;
		if (str.length>maxlength)
		{
			alert(errmsg);
			objname.focus();
			return false;
		}
		return true;
	}
//maxlength validation ends over here

//minlength validation starts over here
	function checkminlength(objname,minlength,errmsg)
	{
		var str = objname.value;
		if (str.length<minlength)
		{
			alert(errmsg);
			objname.focus();
			return false;
		}
		return true;
	}
//minlength validation ends over here
	function checkinteger(objname,errmsg)
	{
		var str = trim(objname.value);
		for(x=0;x<str.length;x++)
		{
			ch=str.substr(x,1);
			if ((ch < '0' || ch >'9'))
			{
				alert(errmsg);
				objname.focus();
				return false;
			}
		}
		return true;
	}

	
function checkFileds()
{
	var nMaxLength=Lookup.ID.value.length;

	if(isNaN(Lookup.ID.value)){ 
		alert("Invalid data format.\n\nOnly numbers are allowed."); 
		Lookup.ID.focus(); 
		return (false); 
	}
	if(nMaxLength!=11 && nMaxLength!=8 && nMaxLength!=7){
		alert("QID should be either 7/8/11 digits");
		Lookup.ID.focus(); 
		return (false); 
	}
	document.Lookup.QID.value=document.Lookup.ID.value;
	return true;
}


function valDrop(val)
{
if (val == '') return false;
else return true;
} 
	
//*******************************************************************************

// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.

// email

function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "You didn't enter an email address.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\n";
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.";
  
    }
    if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
    } 
return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 6) || (strng.length > 8)) {
       error = "The password is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\n";
    } 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (objnamestrng) {
var error = "";
//if (strng == "") {
 //  error = "You didn't enter a username.\n";
//}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "The username is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
    error = "The username contains illegal characters.\n";
    } 
return error;
}       


// non-empty textbox

function isEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "The mandatory text area has not been filled in.\n"
  }
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;
}
// valid selector from dropdown list

function confirmdropdown(objname,errmsg)
{
	if (objname.selectedIndex == 0)
	{
	alert(errmsg);
	objname.focus();
	return false;
	}
	return true;
}
function checkuser(objname,errmsg1,errmsg2) {
var error = "";
//if (strng == "") {
 //  error = "You didn't enter a username.\n";
//}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((objname.length < 4) || (objname.length > 10)) {
       //error = "The username is the wrong length.\n";
       alert(errmsg1);
    }
    else if (illegalChars.test(objname)) {
    //error = "The username contains illegal characters.\n";
    alert(errmsg2);
    } 
return error;
}       


