/*
* 		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(String.fromCharCode(event.keyCode))
	if (INTEGER_ALLOWED_CHARS.test(fieldValue) ) {
		return true;
	}else{
		return false;
	}
}

function checkBlank(fieldRef){
//alert("checkNumbers")
	var fieldValue = String.fromCharCode(event.keyCode);
//		alert(String.fromCharCode(event.keyCode))
	if (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=="")

		{
			document.getElementById('yourname').className = 'textnormalgrey'
			objname.focus();
			return false;
		}
		else
		
		return true;
}
//this function will validate the check boxes is checked or not in a form
function checkChecked(objname1,objname2,objname3,objname4,errmsg)
	{
		if (objname1.checked==false && objname2.checked==false && objname3.checked==false && objname4.checked==false)
		{
			alert(errmsg);
			
			return false;
		}
		return true;
	}
	
function checkThreeText(objname1,objname2,objname3,errmsg)
{
if((objname1.value=="")&&(objname2.value=="")&&(objname3.value==""))

		{
			alert(errmsg);
			objname1.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 checkPresent(dateName){
	theMonth = dateName + "Month";
	theMonth = document.getElementsByName(theMonth)
	theYear = dateName + "Year";
	theYear = document.getElementsByName(theYear)
	if (theMonth[0].selectedIndex == 1)
		{
		theYear[0].className = "hide";
		theYear[0].selectedIndex == 0;
	} else
	 {
		theYear[0].className = "textinside";
		
	}
	
}
	

	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))) { 
       //alert(errmsg);
       error = "Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
         // alert(errmsg1);
          error = "The email address contains illegal characters.\n";
       }
    }
//return true;
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;
}


function checkRadioBut(field,errmsg) 
{
	if (!field[0].checked && !field[1].checked )	 
	
	{
		 alert(errmsg);
      field[0].focus();
		return false;

    }
return true;
}

//validating radio button for yes or no
function checkRadioButton(objname,errmsg)
 {
 
  if (objname.value=="")
    {
       alert(errmsg);
			objname.focus();
			return false;
    }
return true;
}

function checkCheckBox(objname,errmsg)
 {
 
  if(!objname.checked)
    {
       alert(errmsg);
			objname.focus();
			return false;
    }
return true;
}


// 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;
}       


// HISTORY
// ------------------------------------------------------------------
// May 17, 2003: Fixed bug in parseDate() for dates <1970
// March 11, 2003: Added parseDate() function
// March 11, 2003: Added "NNN" formatting option. Doesn't match up
//                 perfectly with SimpleDateFormat formats, but 
//                 backwards-compatability was required.

// ------------------------------------------------------------------
// These functions use the same 'format' strings as the 
// java.text.SimpleDateFormat class, with minor exceptions.
// The format string consists of the following abbreviations:
// 
// Field        | Full Form          | Short Form
// -------------+--------------------+-----------------------
// Year         | yyyy (4 digits)    | yy (2 digits), y (2 or 4 digits)
// Month        | MMM (name or abbr.)| MM (2 digits), M (1 or 2 digits)
//              | NNN (abbr.)        |
// Day of Month | dd (2 digits)      | d (1 or 2 digits)
// Day of Week  | EE (name)          | E (abbr)
// Hour (1-12)  | hh (2 digits)      | h (1 or 2 digits)
// Hour (0-23)  | HH (2 digits)      | H (1 or 2 digits)
// Hour (0-11)  | KK (2 digits)      | K (1 or 2 digits)
// Hour (1-24)  | kk (2 digits)      | k (1 or 2 digits)
// Minute       | mm (2 digits)      | m (1 or 2 digits)
// Second       | ss (2 digits)      | s (1 or 2 digits)
// AM/PM        | a                  |
//
// NOTE THE DIFFERENCE BETWEEN MM and mm! Month=MM, not mm!
// Examples:
//  "MMM d, y" matches: January 01, 2000
//                      Dec 1, 1900
//                      Nov 20, 00
//  "M/d/yy"   matches: 01/20/00
//                      9/2/00
//  "MMM dd, yyyy hh:mm:ssa" matches: "January 01, 2000 12:30:45AM"
// ------------------------------------------------------------------

var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');
function LZ(x) {return(x<0||x>9?"":"0")+x}

// ------------------------------------------------------------------
// isDate ( date_string, format_string )
// Returns true if date string matches format of format string and
// is a valid date. Else returns false.
// It is recommended that you trim whitespace around the value before
// passing it to this function, as whitespace is NOT ignored!
// ------------------------------------------------------------------
function isDate(val,format) {
	var date=getDateFromFormat(val,format);
	if (date==0) { return false; }
	return true;
	}

// -------------------------------------------------------------------
// compareDates(date1,date1format,date2,date2format)
//   Compare two date strings to see which is greater.
//   Returns:
//   1 if date1 is greater than date2
//   0 if date2 is greater than date1 of if they are the same
//  -1 if either of the dates is in an invalid format
// -------------------------------------------------------------------
function compareDates(date1,dateformat1,date2,dateformat2) {
	var d1=getDateFromFormat(date1,dateformat1);
	var d2=getDateFromFormat(date2,dateformat2);
	var sysDate = new Date(); 

	if (d1 == d2) {
		errmsg = "Invalid Dates"
		return errmsg;
		}
	else if (d1 > d2) {
		errmsg = "Invalid Dates"
		return errmsg;
		}
	else if (d1 == sysDate){
		errmsg = "From date cannot be todays date"
		return errmsg;
	}
	return true;
	}

function checkToday(date1,dateformat1) {
	var d1=getDateFromFormat(date1,dateformat1);
	var sysDate = new Date(); 
	if (d1 == sysDate){
		errmsg = "From date cannot be todays date"
		return errmsg;
	}
	return true;

	}
	

function checkBlank(fieldRef){
//alert("checkNumbers")
	var fieldValue = String.fromCharCode(event.keyCode);
//		alert(String.fromCharCode(event.keyCode))
	if (fieldValue !=' ') {
		return true;
	}else{
		return false;
	}
}
//// validating date in the format dd//mm/yyyy
var dtCh= "/";
var minYear=1900;
var maxYear=2008;

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++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date of birth format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month for date of birth")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day for date of birth")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("For date of birth Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date for date of birth")
		return false
	}
return true
}

function ValidateForm(obj1){
	var dt=obj1
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }







