//FUNCTION	: is_username(username,checkempty)
//FUNCTION	: is_password(password,checkempty)
//FUNCTION	: is_re_password(password,re_password,checkempty)
//FUNCTION	: is_firstname(firstname,checkempty)
//FUNCTION	: is_lastname(lastname,checkempty)
//FUNCTION	: is_address(address)
//FUNCTION	: is_country(country)
//FUNCTION	: is_state(state,other_state,country,usa_id)
//FUNCTION	: is_zipcode(zipcode,checkempty)
//FUNCTION	: is_email(email,checkempty)
//FUNCTION	: is_phone(phone,checkempty,msg)
//FUNCTION	: is_fax(fax,checkempty,msg)
//FUNCTION	: isvalidSubdomain(obj,msg)
//FUNCTION	: validatedomain(myobject)
//FUNCTION	: validateURL(myobject)
//FUNCTION  : is_alphabetic (obj,msg)
//FUNCTION  : is_alphanumeric(obj,msg)
//FUNCTION  : is_alphanumericplus(object,str)
//FUNCTION  : empty(obj,msg)
//FUNCTION  : isIntegernum (obj,msg)
//FUNCTION  : is_select(obj,msg)
//FUNCTION  : isDate (year, month, day)
//FUNCTION  : checkDate(s_fname,e_fname,s_mm,s_dd,s_yy,e_mm,e_dd,e_yy,checkFromDate,checkEndDate,currentDate,Compare)
//FUNCTION  : isImage(obj)
/******************************---[ CHECK USERNAME ]---********************************************
Function Name	: is_username.( Standard Username Check )
Purpose			: Check for empty fields value including blank space and check for alphanumeric value.
Input			: username   - form.element-name i.e. (document.form1.username).
				: checkempty - if checkempty=='Y' it will check for empty value.
Return Value	: True or False.
***************************************************************************************************/
function is_username(username,checkempty)
{
	if (checkempty=="Y")
	{
		if (!empty(username,"Please enter the username.")) return false;
	}
	if (trim(username.value)!="" && trim(username.value)!="$1")
	{
		if (!is_alphanumeric(username,"Username cannot have special characters.")) 
		{
			return false;
		}else{
			return true;
		}
	}else{
		return true;
	}
}
/******************************---[ CHECK PASSWORD ]---********************************************
Function Name	: is_password.( Standard Password Check )
Purpose			: Check for empty fields value including length of value.
Input			: password    - form.element-name i.e. (document.form1.password).
				: checkempty - if checkempty=='Y' it will check for empty value.
Return Value	: True or False.
***************************************************************************************************/
function is_password(password,checkempty)
{
	if (checkempty=="Y")
	{
		if (!empty(password,"Please enter the password.")) return false;
	}
	if (trim(password.value)!="" && trim(password.value)!="$1")
	{
		var invalid = "\""; 
		if (password.value.indexOf(invalid) > -1) 
		{
			alert("Password can not have double quotes.");
			password.focus();
			return false;
		}
		var temp=password.value.length;
		if (temp < 6)
		{
			alert("Password can not be less than 6 characters.");
			password.focus();
			return false;
		}
	}else{
		return true;
	}
}
/******************************---[ CHECK RE-PASSWORD ]---********************************************
Function Name	: is_re_password.( Standard Re-Password Check )
Purpose			: Check for empty fields value including match it with password value.
Input			: password		- form.element-name i.e. (document.form1.password).
				: re_password	- form.element-name i.e. (document.form1.re_password).
				: checkempty	- if checkempty=='Y' it will check for empty value.
Return Value	: True or False.
***************************************************************************************************/
function is_re_password(password,re_password,checkempty)
{
	if (checkempty=="Y")
	{
		if (!empty(re_password,"Please retype password.")) return false;
	}
	if (trim(re_password)!="" && trim(re_password)!="$1")
	{
		if(password.value != re_password.value)
		{
			alert("Passwords do not match.")
			re_password.focus();
			return false;
		}
	}else{
		return true;
	}
}
/******************************---[ CHECK FIRST NAME ]---********************************************
Function Name	: is_firstname.( Standard First Name Check )
Purpose			: Check for empty fields value including alphabet check.
Input			: firstname    - form.element-name i.e. (document.form1.firstname).
				: checkempty - if checkempty=='Y' it will check for empty value.
Return Value	: True or False.
****************************************************************************************************/
function is_firstname(firstname,checkempty)
{
	if (checkempty=="Y")
	{
		if (!empty(firstname,"Please enter the first name.")) return false;
	}
	if (trim(firstname.value)!="" && trim(firstname.value)!="$1")
	{
		if(!checkLetters( trim(firstname.value),"First name can have only alphabets (A-Z)." ))
		{	
			firstname.focus();
			return false;
		}
	}else{
		return true;
	}
}
/******************************---[ CHECK LAST NAME ]---********************************************
Function Name	: is_lastname.( Standard Last Name Check )
Purpose			: Check for empty fields value including alphabet check.
Input			: lastname    - form.element-name i.e. (document.form1.lastname).
				: checkempty - if checkempty=='Y' it will check for empty value.
Return Value	: True or False.
***************************************************************************************************/
function is_lastname(lastname,checkempty)
{
	if (checkempty=="Y")
	{
		if (!empty(lastname,"Please enter the last name.")) return false;
	}
	if (trim(lastname.value)!="" && trim(lastname.value)!="$1")
	{
		if(!checkLetters( trim(lastname.value),"Last name can have only alphabets (A-Z)." ))
		{	
			lastname.focus();
			return false;
		}
	}else{
		return true;
	}
}
/******************************---[ CHECK ADDRESS ]---********************************************
Function Name	: is_address.( Standard address Check )
Purpose			: Check for empty fields value.
Input			: address    - form.element-name i.e. (document.form1.address).
Return Value	: True or False.
***************************************************************************************************/
function is_address(address)
{
	if (!empty(address,"Please enter the address.")) return false;
	return true;
}
/******************************---[ CHECK COUNTRY ]---********************************************
Function Name	: is_country.( Standard Country Check )
Purpose			: Check for empty fields value.
Input			: country    - form.element-name i.e. (document.form1.country).
Return Value	: True or False.
***************************************************************************************************/
function is_country(country)
{
	if (country.options[country.selectedIndex].value =="")
	{
		alert("Please select country name.");
		country.focus();
		return false;
	}
	return true;
}
/******************************---[ CHECK SELECTED ]---********************************************
Function Name	: is_select.( Standard Dropdown Check )
Purpose			: Check for empty fields value.
Input			: object    - form.element-name i.e. (document.form1.country).
Return Value	: True or False.
***************************************************************************************************/
function is_select(obj,msg)
{
	if (obj.options[obj.selectedIndex].value =="")
	{
		alert(msg);
		obj.focus();
		return false;
	}
	return true;
}
/******************************---[ CHECK STATE ]---********************************************
Function Name	: is_state.( Standard State Check )
Purpose			: Check for empty fields value.
Input			: state			- form.element-name i.e. (document.form1.state).
				: other_state	- form.element-name i.e. (document.form1.other_state).
				: country		- form.element-name i.e. (document.form1.country).
Return Value	: True or False.
***************************************************************************************************/
function is_state(state,other_state,country,usa_id)
{
	if (country.options[country.selectedIndex].value == usa_id)
	{
		if (state.options[state.selectedIndex].value =="")
		{
			alert("Please select usa state name.");
			state.focus();
			return false;
		}
	}
	else
	{
		if (!empty(other_state,"Please enter the state name.")) return false;
	}
	return true;
}
/******************************---[ CHECK ZIPCODE ]---********************************************
Function Name	: is_zipcode.( Standard ZipCode Check )
Purpose			: Check for empty fields value including alphanumeric and not all alphabetic.
Input			: zipcode		- form.element-name i.e. (document.form1.zipcode).
				: checkempty - if checkempty=='Y' it will check for empty value.
Return Value	: True or False.
***************************************************************************************************/
function is_zipcode(zipcode,checkempty)
{
	if (checkempty=="Y")
	{
		if (!empty(zipcode,"Please enter the zip code.")) return false;
	}
	if (trim(zipcode.value)!="" && trim(zipcode.value)!="$1")
	{
		if (!(onlyAlphaNum(zipcode,"Zip code can have alphanumeric values only.")))  return false;
		if (!(Zipcheck(zipcode,"Zip code can not contain all alphabets.")))  return false;
	}else{
		return true;
	}
}
/******************************---[ CHECK EMAIL ]---********************************************
Function Name	: is_email.( Standard Email Check )
Purpose			: Check for empty fields value including valid email.
Input			: email			- form.element-name i.e. (document.form1.email).

Return Value	: True or False.
***************************************************************************************************/
function is_email(email,checkempty)
{	
	if (checkempty=="Y")
	{
		if (!empty(email,"Please enter the email address.")) return false;
	}
	if (trim(email.value)!="" && trim(email.value)!="$1")
	{
		if (!emailcheck(email,"Please enter valid email address.")) 
			return false;
		else
			return true;
	}
}
/******************************---[ CHECK PHONE ]---********************************************
Function Name	: is_phone.( Standard Phone Check )
Purpose			: Check for empty fields value including valid phone no.
Input			: phone			- form.element-name i.e. (document.form1.phone).

Return Value	: True or False.
***************************************************************************************************/
function is_phone(phone,checkempty,msg)
{
	if (checkempty=="Y")
	{
		if (!empty(phone,"Please enter the phone number.")) return false;
	}
	if (trim(phone.value)!="" && trim(phone.value)!="$1")
	{
		if(!checkphone(phone.value,phone) ) 
		{
			return false ;
		}else{
			var alph_valid=phone.value;
			var sizechar=phone.value.length;
			var alphaCount=0;
		    for (var i=0; i<sizechar; i++) {
		        if (alph_valid.charAt(i) > 0) {
		            alphaCount++
		        }
		    }
		    if (alphaCount==0)
		    {
		    	phone.focus();
		    	alert("Please enter the valid phone number.");
		    	return false;	
		    }			
			return true;
		}
	}else{
		return true;
	}
}
/******************************---[ CHECK FAX ]---********************************************
Function Name	: is_fax.( Standard Fax Check )
Purpose			: Check for empty fields value including valid fax no.
Input			: fax			- form.element-name i.e. (document.form1.fax).

Return Value	: True or False.
***************************************************************************************************/
function is_fax(fax,checkempty,msg)
{
	if (checkempty=="Y")
	{
		if (!empty(fax,msg)) return false;
	}
	if (trim(fax.value)!="" && trim(fax.value)!="$1")
	{
		if(!checkfax(fax.value,fax) ) 
		{
			return false ;
		}else{
			return true;
		}
	}else{
		return true;
	}
}
/***************************************************************************************************
Function Name	: Check Phone.
Purpose			: Check phone for correct forms. i.e.([xxx-xxx-xxxx], [(xxx)xxx-xxxx], [xxxxxxxxxx (10 digited Mobile no)], [xxxxxx (6 digits no)], [xxxxxxx (7 digits no)], [xxxxxxxx (8 digits no)])
***************************************************************************************************/
function checkphone(x,y)
{
					var regexp=/^(\d{3}-\d{3}-\d{4}|\d{10}|\(\d{3}\)\d{3}-\d{4}|\d{6}|\d{7}|\d{8})$/;
						x = trim(x);
						if(!(regexp.test(x)))
						{
							var msg = "";
							msg = "Please enter the correct phone number.";
							msg = msg +  "\n The correct forms are : ";
							msg = msg + "\n xxx-xxx-xxxx";
							msg = msg + "\n (xxx)xxx-xxxx";
							msg = msg + "\n xxxxxxxxxx (10 digited Mobile no)";
							msg = msg + "\n xxxxxx (6 digits no)";
							msg = msg + "\n xxxxxxx (7 digits no)";
							msg = msg + "\n xxxxxxxx (8 digits no)";
							alert(msg);
						    y.focus();
							return false; 
					}
					return true;
}
/***************************************************************************************************
Function Name	: Check Fax.
Purpose			: Check fax for correct forms. i.e.([xxx-xxx-xxxx], [(xxx)xxx-xxxx], [xxxxxxxxxx (10 digited Mobile no)], [xxxxxx (6 digits no)], [xxxxxxx (7 digits no)], [xxxxxxxx (8 digits no)])
***************************************************************************************************/
function checkfax(x,y)
{
					var regexp=/^(\d{3}-\d{3}-\d{4}|\d{10}|\(\d{3}\)\d{3}-\d{4}|\d{6}|\d{7}|\d{8})$/;
						x = trim(x);
						if(!(regexp.test(x)))
						{
							var msg = "";
							msg = "Please enter the correct fax number.";
							msg = msg +  "\n The correct forms are : ";
							msg = msg + "\n xxx-xxx-xxxx";
							msg = msg + "\n (xxx)xxx-xxxx";
							msg = msg + "\n xxxxxxxxxx (10 digited Mobile no)";
							msg = msg + "\n xxxxxx (6 digits no)";
							msg = msg + "\n xxxxxxx (7 digits no)";
							msg = msg + "\n xxxxxxxx (8 digits no)";
							alert(msg);
						    y.focus();
							return false; 
					}
					return true;
}
/***************************************************************************************************
Function Name	: Empty.
Purpose			: Check for empty fields value including blank space.
***************************************************************************************************/
function empty(object,str)
{
	var value=trim(object.value);
	if (value=="$1" || value=="")
	 {
		alert(str);
		object.value="";
		object.focus();
		return false;
	 }
	str1=trim(object.value);
	x=0
	for(var i=0;i<str1.length;i++)
	{	
		if (str1.charAt(i)!=" ") { x=1 }
	
	}
	if (x==0)
	{
		alert(str);
		object.value=""
		object.focus();
		return false;
	}
	str1=trim(object.value);
	x=0
	y=0
	len=str1.length;
	for(var i=0;i<str1.length;i++)
	{	
		if ((str1.charAt(i)=="\r") && (str1.charAt(i+1)=="\n")) { y++; }
	
	}
	if (y==(len/2))
	{
		alert(str);
		object.value=""
		object.focus();
		return false;
	}
	
	 return true;
}
/***************************************************************************************************
Function Name	: is_alphanumeric.
Purpose			: check for alphanumeric, underscore and hyphen in value.
***************************************************************************************************/
function is_alphanumeric(object,str)
{
	var asstr=trim(object.value);

	var alphaCount=0;
	var alph_valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
	var sizechar=asstr.length;

    for (var i=0; i<sizechar; i++) {
        if (alph_valid.indexOf(asstr.charAt(i)) < 0) {
            alphaCount++
        }
    }
    
    if(alphaCount>0)
	{		
		alert(str);
		object.focus();
    	return false;
	}
    else
    	return true;
 
}
/***************************************************************************************************
Function Name	: is_alphanumericplus.
Purpose			: check for alphanumeric, underscore, hyphen, single quote, comma and space in value.
***************************************************************************************************/
function is_alphanumericplus(object,str)
{
	var asstr=trim(object.value);

	var alphaCount=0;
	var alph_valid="@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-,' ";
	var sizechar=asstr.length;

    for (var i=0; i<sizechar; i++) {
        if (alph_valid.indexOf(asstr.charAt(i)) < 0) {
            alphaCount++
        }
    }
    
    if(alphaCount>0)
	{		
		alert(str);
		object.focus();
    	return false;
	}
    else
    	return true;
 
}
/***************************************************************************************************
Function Name	: checkLetters.
Purpose			: check for alphabetic value and single quotes.
***************************************************************************************************/
function checkLetters( str,message )
{
     var i, val;
     var msg = str.split("") ;
	 for (  i = 0 ; i < str.length ; i ++ )
     {
       if ( ( (msg[i] <= "Z") && (msg[i] >= "A") ) || ( (msg[i] <= "z") && (msg[i] >= "a") )|| ( msg[i].indexOf("'")>-1 ));
       else
       {
         val = 1;
         break ;
       }        
     }
     if ( val == 1 )
     {
       alert (message) ;
      return false;
     }
 
	 return true;
}
/***************************************************************************************************
step -1
Function Name	: onlyAlphaNum.
Purpose			: check for only alphanumeric value.
step -2
Function Name	: Zipcheck.
Purpose			: check for not only alphabets in value.
***************************************************************************************************/
function onlyAlphaNum(object,str)
{
 var valid="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 for (var i=0; i<object.value.length; i++)
 {
	  if(valid.indexOf(object.value.charAt(i)) < 0) 
	  {
		alert(str);
		object.focus();
		return false
	  }
 }
 return true
}
/***************************************************************************************************
step -1
Function Name	: onlyAlpha.
Purpose			: check for only alphabets value.
step -2
Function Name	: Zipcheck.
Purpose			: check for not only alphabets in value.
***************************************************************************************************/
function onlyAlpha(object,str)
{
 var valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
 for (var i=0; i<object.value.length; i++)
 {
	  if(valid.indexOf(object.value.charAt(i)) < 0) 
	  {
		alert(str);
		object.focus();
		return false
	  }
 }
 return true
}
/***************************************************************************************************/
function Zipcheck(object,str)
{
 var zip=object.value;
 var allvalid=0;
 var valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 for (var i=0; i<object.value.length; i++)
 {
	 if(valid.indexOf(object.value.charAt(i)) < 0) 
	 {
		 allvalid=1;
	 }
 }
 if (allvalid==0)
 {
	alert(str);
	object.focus();
	object.select();
	return false;
 }
 else
	 return true;
}
/***************************************************************************************************
Function Name	: emailcheck.
Purpose			: check for valid email format.
***************************************************************************************************/
function emailcheck(object,str)
{
	var email=object.value;
	var matcharray=email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z]+)*\.[A-Za-z]+$/) 
	if(matcharray==null){
	alert(str);
	object.focus();
	object.select();
	return false;
	}
	else 
		return true;
}
/***************************************************************************************************
Function Name	: trim.
Purpose			: trim the blank spaces around the value.
***************************************************************************************************/
function trim(str)
{
  return( (""+str).replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') );
}
/***************************************************************************************************
Function Name	: isvalidSubdomain.
Purpose			: check for valid sub-domain for alphanumeric value.
***************************************************************************************************/
function isvalidSubdomain(obj,msg)
{
	var chk="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-0123456789";
	var str=obj.value;
	var allvalid=1;
	for (i = 0;i<str.length;  i++)
	 {
		ch = str.charAt(i);
		if(chk.indexOf(ch)==-1)
		{
			allvalid=0;
		}	
	}	
  if (allvalid==0)
  {
    alert(msg+" cannot have special characters.");
    obj.focus();
	obj.select();
	return false;
  }
return true;
}
/***************************************************************************************************
Function Name	: validatedomain.
Purpose			: check for valid domain i.e. www.yourdomain.com.
***************************************************************************************************/
function validatedomain(myobject)
{
	var strURL = myobject.value;
	if (strURL!="")
	{
		var is_protocol_ok=strURL.indexOf('www');
		var is_dot_ok=strURL.indexOf('.');
		if (((is_protocol_ok==-1) && (is_dot_ok!=-2) || (is_protocol_ok!=-1) && (is_dot_ok==-1)))
		{ 
			 alert("Please enter correct DOMAIN. \n www.test.com");
			 myobject.focus();
			 return false;
		}

	}
	return true;
}
/***************************************************************************************************
Function Name	: validateURL.
Purpose			: check for valid domain i.e. http://www.yourdomain.com.
***************************************************************************************************/
function validateURL(myobject)
{
	var strURL = myobject.value;
	if (strURL!="")
	{
		var is_protocol_ok=strURL.indexOf('http://');
		var is_dot_ok=strURL.indexOf('.');
		if ((is_protocol_ok==-1) || (is_dot_ok==-1))
		{ 
			 alert("Please enter correct URL. \n http://www.test.com");
			 myobject.focus();
			 return false;
		}

	}
	return true;
}
/***************************************************************************************************
Function Name	: is_alphabetic.
Purpose			:   // Search through string's characters one by one
					// until we find a non-alphabetic character.
					// When we do, return false; if we don't, return true.
***************************************************************************************************/
function is_alphabetic (obj,msg)

{   
	var i;
	var as=trim(obj.value);
    for (i = 0; i < as.length; i++)
    {   
        // Check that current character is letter.
        var c = as.charAt(i);

        if (!isLetter(c))
		{
			alert(msg);
			return false;
		}
    }

    // All characters are letters.
    return true;
}
// Returns true if character c is an English letter 
// (A .. Z, a..z).
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}
function isIntegernum (obj,msg)
{   
	var i;
	var ren=true;
	//alert(s+'haaaaaa');
    //if (isEmpty(s)) 
       //if (isInteger.arguments.length == 1) return defaultEmptyOK;
       //else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.
	var as=trim(obj.value);
	if (as.length>=1)
	{
		
		//alert(s+'haaaaaa');
		for (i = 0; i < as.length; i++)
		{   
			// Check that current character is number.
			var c = as.charAt(i);

			if (!isDigit(c))
			{
				alert(msg);
				obj.focus();
				return false;
			}
		
		}

	}
    // All characters are numbers.
   return true;
}

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }


    // All characters are numbers.
    if(s==0)
    	return false;
    else
    	return true;
}
// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9") || (c == "."))
}
// isYear (STRING s [, BOOLEAN emptyOK])
// 
// isYear returns true if string s is a valid 
// Year number.  Must be 2 or 4 digits only.
// 
// For Year 2000 compliance, you are advised
// to use 4-digit year numbers everywhere.
//
// And yes, this function is not Year 10000 compliant, but 
// because I am giving you 8003 years of advance notice,
// I don't feel very guilty about this ...
//
// For B.C. compliance, write your own function. ;->
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isYear (s)
{   
	//alert(s+'isYear');
	if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}

// isMonth (STRING s [, BOOLEAN emptyOK])
// 
// isMonth returns true if string s is a valid 
// month number between 1 and 12.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isMonth (s)
{   
	//alert(s+'isMonth');
	if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}

function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}

// isDay (STRING s [, BOOLEAN emptyOK])
// 
// isDay returns true if string s is a valid 
// day number between 1 and 31.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isDay (s)
{   
	//alert(s+'isDay');
	if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}



// daysInFebruary (INTEGER year)
// 
// Given integer argument year,
// returns number of days in February of that year.

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 );
}



// isDate (STRING year, STRING month, STRING day)
//
// isDate returns true if string arguments year, month, and day 
// form a valid date.
// 

function isDate (year, month, day)
{
	if (parseInt(month)==0)
		month=parseInt(month,10);
	if (parseInt(day)==0)
		day=parseInt(day,10);
  // catch invalid years (not 2- or 4-digit) and invalid months and days.1
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);
	//alert ('step2'+intMonth);
    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 
	//alert ('step3');
    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}
// isNonnegativeInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is an integer >= 0.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
var defaultEmptyOK = false
function makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) daysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}



var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

// isYear (STRING s [, BOOLEAN emptyOK])
// 
// isYear returns true if string s is a valid 
// Year number.  Must be 2 or 4 digits only.
// 
// For Year 2000 compliance, you are advised
// to use 4-digit year numbers everywhere.
//
// And yes, this function is not Year 10000 compliant, but 
// because I am giving you 8003 years of advance notice,
// I don't feel very guilty about this ...
//
// For B.C. compliance, write your own function. ;->
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isYear (s)
{   
	//alert(s+'isYear');
	if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;
	//alert (s+'isNonnegativeInteger');
    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number >= 0

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}
// isSignedInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters are numbers; 
// first character is allowed to be + or - as well.
//
// Does not accept floating point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// EXAMPLE FUNCTION CALL:          RESULT:
// isSignedInteger ("5")           true 
// isSignedInteger ("")            defaultEmptyOK
// isSignedInteger ("-5")          true
// isSignedInteger ("+5")          true
// isSignedInteger ("", false)     false
// isSignedInteger ("", true)      true

function isSignedInteger (s)

{   
	//alert(s+'isSignedInteger');
	if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}
function isEmpty(s)
{   
	//alert(s+'isEmpty');
	return ((s == null) || (s.length == 0))
}

function yearcheck(number) { 
 if (number < 1000)
 {
  number = number + 1900;
 }
 
 return number; 
}

function checkDate(s_fname,e_fname,s_mm,s_dd,s_yy,e_mm,e_dd,e_yy,checkFromDate,checkEndDate,currentDate,compare)
{
	if (checkFromDate=="Y" || checkFromDate=="y")
	{
		if (s_mm.value=="0")
		{
			alert("Please select valid "+s_fname+" month.");
			s_mm.focus();
			return false;
		}
		if (s_dd.value=="0")
		{
			alert("Please select valid "+s_fname+" day.");
			s_dd.focus();
			return false;
		}
		if (s_yy.value=="0")
		{
			alert("Please select valid "+s_fname+" year.");
			s_yy.focus();
			return false;
		}
	}
	if (checkEndDate=="Y" || checkEndDate=="y")
	{
		if (e_mm.value=="0")
		{
			alert("Please select valid "+e_fname+" month.");
			e_mm.focus();
			return false;
		}
		if (e_dd.value=="0")
		{
			alert("Please select valid "+e_fname+" date.");
			e_dd.focus();
			return false;
		}
		if (e_yy.value=="0")
		{
			alert("Please select valid "+e_fname+" year.");
			e_yy.focus();
			return false;
		}
	}
	var month=s_mm[s_mm.selectedIndex].value;
	var day=s_dd[s_dd.selectedIndex].value;
	var year=s_yy[s_yy.selectedIndex].value;
	if (checkFromDate=="Y" || checkFromDate=="y")
	{


		if (!isDate (year, month, day))
		{
			//alert("here iam.........");
			alert ('Please enter valid '+s_fname+' date.');
			s_mm.focus();
			return false;
		}else{
		}
	}
	var month1=e_mm[e_mm.selectedIndex].value;
	var day1=e_dd[e_dd.selectedIndex].value;
	var year1=e_yy[e_yy.selectedIndex].value;	
	if (checkEndDate=="Y" || checkEndDate=="y")
	{
		//alert("Here it is.");



		if (!isDate (year1, month1, day1))
		{
			alert ('Please enter valid '+e_fname+'.');
			e_mm.focus();
			return false;
		}else{
		}
	}

	var now = new Date(); 
	var day2 = now.getDate(); 
	var month2 = now.getMonth()+1;
	var month3 = ((month2 < 10) ? '0' : '')+month2;
	var year2 = yearcheck(now.getYear());
	//alert(year+month+day+">"+year2+month3+day2);
	if (currentDate=="Y" || currentDate=="y")
	{
		if ((year+month+day)>(year2+month3+day2))
		{
			alert (s_fname+" date can not be greater than the current date");
			s_mm.focus();
			return false;
		}
	}
	if (compare=="Y" || compare=="y")
	{
		if ((year+month+day)>(year1+month1+day1))
		{
			//alert(year+month+day);
			alert (e_fname+" date can not be lesser than the "+s_fname+" date");
			s_mm.focus();
			return false;
		}
	}
	return true;
}
function timeDifference(laterdate,earlierdate) {
    var difference = laterdate.getTime() - earlierdate.getTime();

    var daysDifference = Math.floor(difference/1000/60/60/24);
    difference -= daysDifference*1000*60*60*24
    var hoursDifference = Math.floor(difference/1000/60/60);
    difference -= hoursDifference*1000*60*60
    var minutesDifference = Math.floor(difference/1000/60);
    difference -= minutesDifference*1000*60
    var secondsDifference = Math.floor(difference/1000);

    return daysDifference;
}
function is_select1(obj,msg)
{
	if (obj.options[obj.selectedIndex].value =="0")
	{
		alert(msg);
		obj.focus();
		return false;
	}
	return true;
}
function isImage(obj)
{
	var s=obj.value;
	if (s!="")
	{
		array=s.split("\\");
		len=array.length;
		filename=array[len-1];
		array1=filename.split(".");
		if (array1.length == 1)
		{
			alert("Files with extensions .gif, .jpg, .jpeg can only be uploaded.");
			obj.focus();
			return false;									
		}
		if(array1[1].toLowerCase()!="jpg" && array1[1].toLowerCase()!="jpeg" && array1[1].toLowerCase()!="gif")
		{
			alert("Files with extensions .gif, .jpg, .jpeg can only be uploaded.");
			obj.focus();
			return false;
		}
	}
	return true;
}
/********************************************* Vazum Zipcode check ******************************************************
step -1
Function Name	: onlyAlphaNum.
Purpose			: check for only alphanumeric value.
step -2
Function Name	: Zipcheck.
Purpose			: check for not only alphabets in value.
***************************************************************************************************/
function mvzipcode(object)
{
	var objValue = object.value;

	 if (object.value.length == 10)
	 {
		 var valid="0123456789-";
		 for (var i=0; i<object.value.length; i++)
		 {
			if(valid.indexOf(object.value.charAt(i)) < 0) 
			{
				alert("Please enter valid zipcode (xxxxx-xxxx) or (xxxxx).");
				object.focus();
				return false
			}else{
				var is_dot_ok=objValue.indexOf('-');
				if ((object.value.length-is_dot_ok)!=5)
				{
					alert("Please enter valid zipcode (xxxxx-xxxx) or (xxxxx).");
					object.focus();
					return false
				}
			}
		 }
	 }else{
		if (object.value.length !=5)
		{
			alert("Please enter valid zipcode (xxxxx-xxxx) or (xxxxx).");
			object.focus();
			return false
		}
		var valid="0123456789";
		for (var i=0; i<object.value.length; i++)
		{
			if(valid.indexOf(object.value.charAt(i)) < 0) 
			{
				alert("Please enter valid zipcode (xxxxx-xxxx) or (xxxxx).");
				object.focus();
				return false
			}
		}
	 }
	 return true
}