function derror(msg,amsg)
{
if (msg==undefined)
alert (amsg);
else
alert(msg);
}
function chk_radio(group)
{
tr=0;

x=group.length;


for (i=0;i<=(x-1);i++)
{

zx=eval(group[i].checked);

if(zx){tr=1;}
}
return tr;
}

function chk_amount(amt) {
if (amt.length<=0) return false;
var valid = "0123456789."
var ok = "yes";
var temp;
for (var i=0; i<amt.length; i++) {
temp = "" + amt.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
return false;
   }else return true;
}


function do_validate(frm_name,field,vtype)
{
vfld=frm_name+"."+field;

  switch (vtype)
	{
		case "date":
			if (!chk_date(vfld))
			{
			x=eval(vfld+"_Day");
			derror(eval("x.error"),"Invalid Entry For Date");

			x.focus();

			return false;
			} else return true;
			break;

	   case "time":	
		if (!chk_time(vfld))
		{
		x=eval(vfld+"_Hour");
	 derror(eval("x.error"),"Invalid Entry in Time ");
		
		x.focus();
		return false;
		} else return true;
		break;
		
		case "req":
		x=eval(vfld);

		xval=x.value;

	  if (xval.length <= 1 )
	   {
	     
	    derror(eval("x.error"),"You Forgot To Fill a Required Field Properly!");
          x.focus();
		    x.select();
	   return false;
	   }
       else return true;
	   break;
	   
	   case "ccard":
	   		x=eval(vfld);
		xval=x.value;

	   if (! _CF_checkcreditcard(xval)) 
	   {
 derror(eval("x.error"),"Invalid Credit Card Number");
	   x.focus();
	   x.select();
	   return false;
	   }
	   else
	   return true; 

	   break
	   
	   		case "list":
		x=eval(vfld);
        xval=eval(vfld+".options["+vfld+".selectedIndex].value");
		  if (xval =="-default-" )
	   {
         derror(eval("x.error"),"You Forgot to Select An Item");
          x.focus();
	   return false;
	   }
 else return true;
	   break;
	   
	     case "zip":

	   	   		x=eval(vfld);
		xval=x.value;
	   if(! _CF_checkzip(xval)) 
	   {
	   alert(x.error);
	   x.focus();
	   x.select();
	   return false;
	   }
	   else
	   return true; 
	   break;	   
	   
	   case "bphone":
	   vfld=frm_name+"."+field;
			
			pex=eval(frm_name+"."+field+"_1");
p1=eval(frm_name+"."+field+"_1.value");
p2=eval(frm_name+"."+field+"_2.value");
p3=eval(frm_name+"."+field+"_3.value");
px = p1+"-"+p2+"-"+p3;
if (p1.length >0  || p2.length > 0 || p3.length > 0)
{
if(!chk_phone(px))
	   	   {
	 derror(eval("pex.error"),"Either fill in correct number or leave it blank");
	   pex.focus();
	   pex.select();
	   return false;
	   }
}
lvd= eval(vfld);
lvd.value=px;
   return true;
   break;
	   	   case "phone":

			pex=eval(frm_name+"."+field+"_1");
p1=eval(frm_name+"."+field+"_1.value");
p2=eval(frm_name+"."+field+"_2.value");
p3=eval(frm_name+"."+field+"_3.value");
px = p1+"-"+p2+"-"+p3;

		if(!chk_phone(px))
	   	   {
	 derror(eval("pex.error"),"Invalid Phone Number");
	   pex.focus();
	   pex.select();
	   return false;
	   }
	   else
	   	   {lvd= eval(vfld);
		   lvd.value=px;
	   return true; }
	   
	   case "email":
	   		x=eval(vfld);
		xval=x.value;
	   if (!chk_mail(xval)) 
	   {
	    derror(eval("x.error"),"Invalid Email Address");
	   x.focus();
	   x.select();
	   return false;
	   }
	   else
	   return true; 
	   
	case "ssn":
	pex=eval(frm_name+"."+field+"_1");
p1=eval(frm_name+"."+field+"_1.value");
p2=eval(frm_name+"."+field+"_2.value");
p3=eval(frm_name+"."+field+"_3.value");
px = p1+"-"+p2+"-"+p3;
		if(!chk_ssn(px))
	   	   {
		       derror(eval("pex.error"),"Invalid Social Security Number");
	   pex.focus();
	   	   pex.select();
	   return false;
	   }
	   else
	   	   {lvd= eval(vfld);
		   lvd.value=px;
	   return true; }
case "amount":
		x=eval(vfld);
		xval=x.value;
		if(!chk_amount(xval))
	   	   {
	 derror(eval("x.error"),"Invalid Amount");
	   x.focus();
	   x.select();
	   return false;
	   }
	   else
	   return true; 
	   
	   case "radio":
	   x=eval(vfld);
        p=x
		if(!chk_radio(x))
	   	   {
		   er=p[0].error;
		    derror(er,"Invalid Choice");
	   p[0].focus();
	   return false;
	   }
	   else
	   return true; 
  
	  	}  //end of swutch
}  // end function

function listat(list,pos){
x=list.split(",");
return x[pos-1];
}

function chk_mail(ad)
{
var pos = ad.indexOf("@") ;
if(pos > -1 )
{
return 1;
}
else {
return 0;
}
}

function chk_date(fld)
{
dy=eval(fld+"_Day.value");

mn=eval(fld+"_Month.value");
yr=eval(fld+"_Year.value");
if(dy >-1 && mn>-1 && yr.length==4 && yr >1900)
return 1;
else return 0;
}
function chk_time(fld)
{
hr=eval(fld+"_Hour.value");

mn=eval(fld+"_Min.value");
if(hr >-1 && mn>-1)
return 1;
else return 0;
}

function chk_ssn(ssn) {
var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
var numDashes = ssn.split('-').length - 1;
if (matchArr == null || numDashes == 1) {

return false;
}
else 
if (parseInt(matchArr[1],10)==0) {
return false;

}
else {
return true;

   }
}
function chk_phone(phone) {
var matchArr = phone.match(/^(\d{3})-?\d{3}-?\d{4}$/);
var numDashes = phone.split('-').length - 1;
if (matchArr == null || numDashes == 1) {

return false;
}
else 
if (parseInt(matchArr[1],10)==0) {
return false;

}
else {
return true;

   }
}

function _CF_checkcreditcard(object_value)
    {
	var white_space = " -";
	var creditcard_string="";
	var check_char;
    if (object_value.length == 0)
        return false;
	// squish out the white space
	for (var i = 0; i < object_value.length; i++)
	{
	check_char = white_space.indexOf(object_value.charAt(i))
		if (check_char < 0)
			creditcard_string += object_value.substring(i, (i + 1));
	}

	// if all white space return error
    if (creditcard_string.length == 0)
        return false;
	 
	 	
	// make sure number is a valid integer
	if (creditcard_string.charAt(0) == "+")
        return false;

	if (!_CF_checkinteger(creditcard_string))
		return false;

    // now check mod10

	var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
	var checkdigit = 0;
	var tempdigit;

	for (var i = 0; i < creditcard_string.length; i++)
	{
		tempdigit = eval(creditcard_string.charAt(i))

		if (doubledigit)
		{
			tempdigit *= 2;
			checkdigit += (tempdigit % 10);

			if ((tempdigit / 10) >= 1.0)
			{
				checkdigit++;
			}

			doubledigit = false;
		}
		else
		{
			checkdigit += tempdigit;
			doubledigit = true;
		}
	}	
	return (checkdigit % 10) == 0 ? true : false;

    }
	
	
	function _CF_checkinteger(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
	return _CF_checknumber(object_value);
    else
	return false;
    }
	
	function _CF_checknumber(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	
    if (object_value.length == 0)
        return false;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	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 _CF_checkzip(object_value)
    {
    if (object_value.length == 0)
        return false;
		
    if (object_value.length != 5 && object_value.length != 10)
        return false;

	// make sure first 5 digits are a valid integer
	if (object_value.charAt(0) == "-" || object_value.charAt(0) == "+")
        return false;

	if (!_CF_checkinteger(object_value.substring(0,5)))
		return false;

	if (object_value.length == 5)
		return true;
	
	// make sure

	// check if separator is either a'-' or ' '
	if (object_value.charAt(5) != "-" && object_value.charAt(5) != " ")
        return false;

	// check if last 4 digits are a valid integer
	if (object_value.charAt(6) == "-" || object_value.charAt(6) == "+")
        return false;

	return (_CF_checkinteger(object_value.substring(6,10)));
    }
	
	function _CF_checkinteger(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return false;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
	return _CF_checknumber(object_value);
    else
	return false;
    }