//***************function to check empty textbox***************//

whitespace = "\t \n\r";
function isEmptyString(s)
{
   	var i;
  	if((s == null) || (s.length == 0)) return true;
  	for(i=0;i < s.length;i++)
  	{
  		var currchar = s.charAt(i);
  		if(whitespace.indexOf(currchar) == -1) return false;
  	}
      return true;
}
//----------**end of function to check empty textbox**----------//



//***************function to check numeric value***************//
function isNotNumeric(s)
{
  	if(isNaN(s))
  	{
  		return(true);
  	}
  	return(false);
}
//----------**end of function to check numeric value**----------//



//***************function to validate Email***************//
function isEmail(n)
{
		if ((n==null) || (n.length==0))
		{
			return true;
		}
		if (isEmptyString(n)) return false;
		var i=1;
		var nLength=n.length;
		while((parseInt(i) < parseInt(nLength)) && (n.charAt(parseInt(i)) != '@'))
		{
			i++;
		}
		if ((parseInt(i) >= parseInt(nLength)) || (n.charAt(i)!="@"))
		{
			return false;	
		}	
		else i+=2;
		while((i<nLength) && (n.charAt(i)!="."))
		{
			i++;
		}
		if ((i>=nLength-1) || (n.charAt(i)!="."))
		{
			return false;	
		}	
		else return true;		
}
//----------**end of function to validate Email**----------//


//***************function for confirmation***************//
function confirmThis(message) {
	if (confirm(message))
	{
		return true
	}
	else
	{
		return false
	}
}
//----------**end of function for confirmation**----------//



//******function for validate date in a single textbox******//
function single_textbox_date_validation(textbox)
{	//Checking leap year validation
	var mystring = new String(textbox.value)
 	m=mystring.indexOf("/",0)
	if (parseInt(m)==-1)
 	{
 	m=mystring.indexOf("-",0)
 	}
 	mm=mystring.slice(0,m)
	
	d=mystring.indexOf("/",m+1)
  	if (parseInt(d)==-1)
 	{
  	d=mystring.indexOf("-",m+1)
 	}
	dd=mystring.slice(m+1,d)

	yyyy=mystring.slice(d+1,mystring.length)	
	if ((isNaN(mm))||(mm.length>2)||parseInt(mm)>12||mm<1)
  	{
  	alert("Invalid month");
	textbox.focus();
  	return false;
  	}

	if ((isNaN(dd))||(dd.length>2)||parseInt(dd)>31||dd<1)
  	{
  	alert("Invalid day");
	textbox.focus();
  	return false;
  	}

	if ((isNaN(yyyy))||(yyyy.length>4))
  	{
  	alert("Invalid year");
	textbox.focus();
  	return false;
  	}
	
	var monthInt=parseInt(mm)
	if (monthInt==4)
	{
		var month="April"
	}
	if (monthInt==6)
	{
		var month="June"
	}
	if (monthInt==9)
	{
		var month="September"
	}
	if (monthInt==11)
	{
		var month="November"
	}
	if ((parseInt(yyyy)%400==0)||(!((parseInt(yyyy)%100==0)&&(parseInt(yyyy)%4==0)) && (parseInt(yyyy)%4==0)))
	{	
		//Leap Year
		if ((parseInt(mm)==2)&& (parseInt(dd)>29)) //februry
		{
			alert("Day of Februry month in a leap year should not be greater than 29")
			textbox.focus();
			return false;
		}
	}
	else
	{	
		//Simple Year
		if ((parseInt(mm)==2)&& (parseInt(dd)>28)) //februry
		{
			alert("Day of Februry month in a simple year should not be greater than 28")
			textbox.focus();
			return false;
		}
		
	}
	if (((parseInt(mm)==4) || (parseInt(mm)==6) || (parseInt(mm)==9) || (parseInt(mm)==11)) && (parseInt(dd)>30))
	{	  //month=april		      month=June         month=September	  month=November				
		alert("In " +month+ " month day should not be greater than 30")
		textbox.focus();
		return false;
	}
	
return true;
}
//----------**end of function for validate date in a single textbox**----------//

//***************function to compare two dates***************//
function compareDates(date1,date2)
{	
	//------------date1-------------- 
	var mydate1 = new String(date1);
	mon1=mydate1.indexOf("/",0)
	if (parseInt(mon1)==-1)
 	{
 	mon1=mydate1.indexOf("-",0)
 	}
 	month1=mydate1.slice(0,mon1)
	
	dd1=mydate1.indexOf("/",mon1+1)
  	if (parseInt(dd1)==-1)
 	{
  	dd1=mydate1.indexOf("-",mon1+1)
 	}
	day1=mydate1.slice(mon1+1,dd1)

	yy1=mydate1.slice(dd1+1,mydate1.length)
	
	var yyLength=yy1.length;
	if ((yyLength==2) || (yyLength==1))
	{
		year1=parseInt(yy1)+2000;
	}
	else
	{
		year1=parseInt(yy1);
	}
	//------------------------------- 
	
	
	//------------date2-------------- 
	var mydate2 = new String(date2);
	mon2=mydate2.indexOf("/",0)
	if (parseInt(mon2)==-1)
 	{
 	mon2=mydate2.indexOf("-",0)
 	}
 	month2=mydate2.slice(0,mon2)
	
	dd2=mydate2.indexOf("/",mon2+1)
  	if (parseInt(dd2)==-1)
 	{
  	dd2=mydate2.indexOf("-",mon2+1)
 	}
	day2=mydate2.slice(mon2+1,dd2)

	yy2=mydate2.slice(dd2+1,mydate2.length)
	
	var yyLength2=yy2.length;
	if ((yyLength2==2) || (yyLength2==1))
	{
		year2=parseInt(yy2)+2000;
	}
	else
	{
		year2=parseInt(yy2);
	}
	//------------------------------- 	
	
	if (year1==year2)
	{
		if (month1==month2)
		{
			if (day1==day2)
			{
				return true;
			}
			else if (day1>day2)
			{
				return true;
			}
			else if(day1<day2)
			{
				return false;
			}
		}
		else if (month1>month2)
		{
			return true;
		}
		else if (month1<month2)
		{
			return false;
		}
	}
	else if (year1>year2)
	{
		return true;
	}
	else if (year1<year2)
	{
		return false;
	}	
}
//----------**end of function for comparing two dates**----------//

function checkphone(object_value)	{
	    
	if (object_value.length != 10)
        return false;

	// check if first 3 characters represent a valid area code
    if (!checknumber(object_value.substring(0,3)))
		return false;
    else
	if (!numberrange((eval(object_value.substring(0,3))), 100, 1000))
		return false;


	// check if  characters 5 - 7 represent a valid exchange
    if (!checknumber(object_value.substring(4,7)))
		return false;
    else
		if (!numberrange((eval(object_value.substring(4,7))), 100, 1000))
			return false;

	return (checkinteger(object_value.substring(8,12)));
}

function checknumber(object_value)
    {
    if (object_value.length == 0)
        return true;
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

	check_char = start_format.indexOf(object_value.charAt(0))
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;

	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true
    }

function numberrange(object_value, min_value, max_value) {
    if (min_value != null) 	{
        if (object_value < min_value)
		return false;
	}

    if (max_value != null) 	{
		if (object_value > max_value)
			return false;
	}
	return true;
}
	
	
function checkinteger(object_value) {
	if (object_value.length == 0)
		return true;
			
	var decimal_format = ".";
	var check_char;
		
	check_char = object_value.indexOf(decimal_format)
		
	if (check_char < 1)
		return checknumber(object_value);
	else
		return false;
}
	
function validateSignUp(frmSignup)
{
	if (isEmptyString(document.frmSignup.name.value.toString()))  {
		window.alert("Please enter Agency Name");
		document.frmSignup.name.focus();
		return false;
	}
	if (isEmptyString(document.frmSignup.address1.value.toString())) {
		window.alert("Please enter the agency's address");
		document.frmSignup.address1.focus();
		return false;
	}
	if (isEmptyString(document.frmSignup.city.value.toString())) {
		window.alert("Please enter city");
		document.frmSignup.city.focus();
		document.frmSignup.city.value="";
		return false;
	}
	if (isEmptyString(document.frmSignup.state.value.toString())) {
		window.alert("Please enter state");
		document.frmSignup.state.focus();
		return false;
	}
	if (isEmptyString(document.frmSignup.zip.value.toString()))	{
		window.alert("Please enter zip");
		document.frmSignup.zip.focus();
		document.frmSignup.zip.value="";
		return false;
	}
	if (isEmptyString(document.frmSignup.phone.value.toString())) {
		window.alert("Please enter agency's phone number");
		document.frmSignup.phone.focus();
		document.frmSignup.phone.value="";
		return false;
	}
	if (isEmptyString(document.frmSignup.fax.value.toString()))	{
		window.alert("Please enter agency's fax number");
		document.frmSignup.fax.focus();
		document.frmSignup.fax.value="";
		return false;
	}	
	if (isEmptyString(document.frmSignup.contactName.value.toString()))	{
		window.alert("Please enter the agent's name to use as a contact");
		document.frmSignup.contactName.focus();		
		document.frmSignup.contactName.value="";
		return false;
	}
	if (!isEmail(document.frmSignup.email.value.toString())) {
		window.alert("Please enter a valid Email");
		document.frmSignup.email.focus();
		document.frmSignup.email.value="";
		return false;
	}
	if (isEmptyString(document.frmSignup.password.value.toString())) {
		window.alert("Please enter a password");
		document.frmSignup.password.focus();
		return false;
	}
	if (isEmptyString(document.frmSignup.password_v.value.toString())) {
		window.alert("Please verify your password");
		document.frmSignup.password_v.focus();
		return false;
	}
	if (document.frmSignup.password.value.toString() != document.frmSignup.password_v.value.toString() ){
		window.alert("The password enterd does not match");
		document.frmSignup.password.focus();
		document.frmSignup.password.value="";
		document.frmSignup.password_v.value="";
		return false;
	}
	return true;
}

function validateLogin(frmLogin)
{
	if (isEmptyString(document.frmLogin.a_id.value.toString()))  {
		window.alert("Please enter your agency ID");
		document.frmLogin.a_id.focus();
		return false;
	} else if( !checkphone(document.frmLogin.a_id.value.toString()) ) {
		window.alert("Please enter your 10 digit agency phone number.\nExcluding:   -   .   (   )");
		document.frmLogin.a_id.value="";
		document.frmLogin.a_id.focus();
		return false;
	}
	
	if (isEmptyString(document.frmLogin.a_pw.value.toString())) {
		window.alert("Please enter a valid password.");
		document.frmLogin.a_pw.focus();
		return false;
	}
	return true;
}