var form_Check=false;
var validation_msg_validation_head = "Validation errors";
var validation_exc_mandatory = "is mandatory";
var validation_exc_number_format = "number format incorrect";
var validation_exc_date_format = "date format incorrect";
var validation_exc_length = "length wrong";
var validation_exc_range = "out of range";
var validation_exc_regexp = "error on validation rules";
var validation_exc_mask_format = "error on mask rule";
var validation_msg_component="";

// =======================================================================
// GLOBAL VARIABLES
// =======================================================================

var validation_form;
var formName;
var validation_widgets  = new Array(); // WIDGET'S DATATYPE
var validation_widgetsI18N = new Array(); // WIDGET I18N descrizione nome campo
var validation_required = new Array(); // IS REQUIRED?
var validation_format   = new Array(); // SPECIAL FORMAT
var validation_user     = new Array(); // CUSTOM USER FUNCTION
var validation_length   = new Array(); // LENGTH
var validation_range    = new Array(); // RANGE
var validation_regexp   = new Array(); // REGEXP
var validation_accept 	= new Array(); // ACCEPT KEYS
var validation_pad	 	= new Array(); // AUTOMATI

var server_side_form  = new Array();
var server_side_errors = new Array();

var validation_messages = new Array(); // VALIDATION ERROR MESSAGES
var validation_mask_messages = new Array(); 
var validation_datatype_messages = new Array();
var validation_docheck   = new Array(); // WHICH FORMS TO CHECK

var errorsWithIdsAsKey   = new Array(); //TO MAP WIDGET'S ID'S TO ERROR MESSAGES
var errors = new Array();
// Validator Object
var valid = new Object();
var phone;
// REGEX Elements

// ArrivalDepartureNumber 
valid.ADN1 = /^[0-9]{3}$/;
valid.ADN2 = /^[0-9]{8}$/;

valid.ph1 = /^[0-9]{0,4}$/;
valid.ph2 = /^[0-9]{0,11}$/;
valid.ph3 = /^[0-9]{0,20}$/;
valid.newphone1 = /^\d{3}$/;
valid.newphone2 = /^\d{7,10}$/;
valid.newphone3 = /^[\-\(\)\[\]\/\\\.\_\#]{0,20}[0-9]([\s\-\(\)\[\]\/\\\.\_\#0-9]){0,20}$/;

// matches all digits
valid.digits = /^[0-9]+$/;

valid.cin = /^[0-9]+$/;

valid.creditcardnumber = /^[0-9]+$/;
//Height and Weight
valid.height=/^\d{3}(\.{1}(\d{1})+)*$/;
valid.weight=/^\d{3}(\.{1}(\d{1})+)*$/;

// matches everything except digits
valid.Alphabets = /^[a-zA-Z]+[\s|\-]?[a-zA-Z]+[\s|\-]?[a-zA-Z]*$/;
valid.LastNameAlphabets = /^[a-zA-Z]+[\s|\-|\'|\"|\`]?[a-zA-Z]+[\s|\-]?[a-zA-Z]*$/;

valid.AccountID = /^[0-9]+$/;

//Name
//valid.Name=/^([a-zA-z]|[\s|\,|\.])*$/;
valid.Name=/^([a-zA-Z0-9\&\s|\,|\.|\-|\']{0,24}[a-zA-Z][a-zA-Z0-9\&\s|\,|\.|\-|\']{0,24})$/;

//Middle Intial
valid.intial=/^[a-zA-z]+$/;

// AlphaNumeric
//valid.alphanumeric = /^([a-zA-Z]|[0-9])+[\s{1}|\-{1}]?([a-zA-Z]|[0-9])*$/;
valid.alphanumeric = /^[\s0-9a-zA-Z-]*$/;
//Street
valid.street=/^([a-zA-Z]|[0-9]|\.|\s|\-|\'|\,|\"|\&|\#|\/|\\|\(|\))+$/;

// matches zip codes
valid.zipCode = /^\d{5,10}$/;

// Province
valid.province=/^[a-zA-Z]+(\s{1}|-{1})?[a-zA-Z]*$/;

//city

valid.city=/^([a-zA-z]|[\s|\,])*$/

// matches $17.23 or $14,281,545.45 or ...
valid.Currency = /\$\d{1,3}(,\d{3})*\.\d{2}/;

// matches 5:04 or 12:34 but not 75:83
valid.Time = /^([1-9]|1[0-2]):[0-5]\d$/;

//matches email
//valid.emailAddress = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
valid.emailAddress = /^[\w_\.\-]+@[\w.\-]+\.[a-zA-Z]{2,4}$/;


//matches phone ###-###-#######
// valid.phoneNumber = /^\(?\d{3}\)?\s|-\d{3}-\d{4}$/;
valid.phoneNumberCountryCode = /^\d{3}$/;
valid.phoneNumberCityCode = /^\d{3}$/;
valid.phoneNumber = /^\d{7}$/;

// International Phone Number
valid.phoneNumberInternational = /^\d(\d|-){7,20}$/;

// IP Address
valid.ipAddress = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;

// Date xx/xx/xxxx
valid.Date = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;

valid.dateDay = /^\d{1,2}$/;

valid.dateYear = /^\d{4}$/;

// State Abbreviation
valid.State = /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i;

// Social Security Number
valid.SSN1 = /^\d{3}$/;
valid.SSN2 = /^\d{2}$/;
valid.SSN3 = /^\d{4}$/;

// Anumber
//valid.Anumber = /^\d{7,9}$/;
valid.Anumber=/^(^[a|A]{1}|-{1})*\d{3}(-{1}|\s{1})*\d{3}(-{1}|\s{1})*(\d{1}|\d{2}|\d{3})$/
valid.password = /^\w{6,11}$/;
function setDoCheck(iWidget,doIt) {
   validation_docheck[iWidget] = doIt;
}

function setRequired(iWidget,isRequired) {
   validation_required[iWidget] = isRequired;
}
function getWidgetValue( iName )
{

  var element;
// = document.getElementsByName( iName );
  if(document.all)
  {
	var multiChoice = new Array();
	element = document.getElementById(iName);
	var counter = 0;
        var tNames;
        tNames=document.getElementsByTagName("input");
        for (var i = 0; i < tNames.length; i++)
        {
           if(tNames[i].getAttribute('name') != null && tNames[i].getAttribute('name') != undefined && tNames[i].getAttribute('name') != '' && tNames[i].getAttribute('name') == iName)
           {
		if(tNames[i].getAttribute('type') == 'radio')
		{
		  multiChoice.push([counter , tNames[i]]);
		  counter++;
		}
		else
		{
		if(element == null || element == undefined || element == '') 	
                  element = tNames[i];
		}
           }
        }
        tNames=document.getElementsByTagName("select");
        for (var i = 0; i < tNames.length; i++)
        {
           if(tNames[i].getAttribute('name') != null && tNames[i].getAttribute('name') != undefined && tNames[i].getAttribute('name') != '' && tNames[i].getAttribute('name') == iName)
           {
                element = tNames[i];
           }
        }
        tNames=document.getElementsByTagName("textarea");

        for (var i = 0; i < tNames.length; i++)
        {
           if(tNames[i].getAttribute('name') != null && tNames[i].getAttribute('name') != undefined && tNames[i].getAttribute('name') != '' && tNames[i].getAttribute('name') == iName)
           {
                element = tNames[i];
           }
        }
       if((element != null && element.type != null && element.type != undefined && element.type == "radio") || (multiChoice != null && multiChoice != undefined && multiChoice != ''))
       {
	if(multiChoice != null && multiChoice != undefined && multiChoice != '')
	{
          for (var i = 0; i < multiChoice.length; i++)
          {
            if (multiChoice[i][1].checked) {
                return multiChoice[i][1].value;
            }
          }
       }
      }
      else if (element != null && element.type != null && element.type != undefined && element.type == "checkbox")
      {
       var element= document.getElementsByName( iName );
       for (var i = 0; i < element.length; i++) {
         if (element[i].checked) {
             return element[i].value;
         }
       }
	
      }
      else
      {
	return element.value;
      }

}
else
{

element = document.getElementsByName( iName );
  if(element[0] != null && element[0].type != null && element[0].type != undefined && element[0].type == "radio"){
        for (var i = 0; i < element.length; i++) {
            if (element[i].checked) {
                return element[i].value;
            }
        }
  }
  else if (element[0] != null && element[0].type != null && element[0].type != undefined && element[0].type == "checkbox")
  {
       for (var i = 0; i < element.length; i++) {         
        if (element[i].checked) {
            return element[i].value;
         }
       }

  }
  else
  {
        var field = document.forms[formName].elements[ iName ];

        if( field != null )
            return field.value;

  }

}
  return null;
}

function setWidgetValue( iName , value )
{
  var field = validation_form.elements[ iName ];
  if( field != null )
    field.value = value;
}

function isBlank(val)
{
	if(val==null)
	{
		return true;
	}
	for(var i=0;i<val.length;i++)
	{
		if((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r"))
		{
			return false;
		}
	}
	return true;
}

function isDigit(num)
{
	if(num.length>1)
	{
		return false;
	}
	var string="1234567890";
	if(string.indexOf(num)!=-1)
	{
		return true;
	}
return false;
}

function isInteger(val)
{
if(isBlank(val))
	{
		return false;
	}
for(var i=0;i<val.length;i++)
{
	if(!isDigit(val.charAt(i)))
	{
		return false;
	}
}
return true;
}

function isFloat( iValue )
{
  if( iValue == null || iValue == "" )
    return true;

  //TODO TEST ONLY THE TRUE CURRENCY FORMAT AND NOT BOTH
  //var reFloatF1 = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
  //var reFloatF2 = /^((\d+(\,\d*)?)|((\d*\,)?\d+))$/;
  //return reFloatF1.test( iValue ) || reFloatF2.test( iValue );
  //TODO works only with italian number pattern; extend to other locales
  var newReFloat = /^([\d\.]*(\,\d*)?)$/;
  return newReFloat.test(iValue);
}        
function validation_preRegister(iWidget,iMaskErrorMessage,iDataTypeErrorMessage)
{
if(iMaskErrorMessage != '' && iMaskErrorMessage != null)
validation_mask_messages[iWidget]=iMaskErrorMessage;
if(iDataTypeErrorMessage != '' && iDataTypeErrorMessage != null)
validation_datatype_messages[iWidget]=iDataTypeErrorMessage;
}
function validation_register( iWidget, iWidgetI18N, iRequired, iDatatype, iFormat, iUserFunction,iLength, iRange, iRegexp, iAccept, iErrorMessage) {
//  var applyValidation= getApplyValidation();
//  if(applyValidation=="false")
//        return;


  validation_widgets[iWidget] = iDatatype;

  validation_widgetsI18N[iWidget] = iWidgetI18N;
  
  // REQUIRED
  if( iRequired )
    validation_required[iWidget] = true;

  // FORMAT
  if( iFormat )
    validation_format[iWidget] = iFormat;

  // USER FUNCTION
  if( iUserFunction )
    validation_user[iWidget] = iUserFunction;

  // LENGTH
  if( iLength )
    validation_length[iWidget] = iLength;

  // RANGE
  if( iRange )
    validation_range[iWidget] = iRange;
    
  // REGEXP
  if( iRegexp ){
    validation_regexp[iWidget] = iRegexp;
  }	
    
  if ( iAccept ) {
  	validation_accept[iWidget] = iAccept;
  } 
  
  if ( iErrorMessage ) {
	validation_msg_component = iErrorMessage;
  	validation_messages[iWidget] = iErrorMessage;
  }
}

function validation_precheck_widget( iWidget )
{
  if( !iWidget || iWidget == null )
    return;
  var value = getWidgetValue( iWidget );
  var ret = null;
  // LENGTH
  var length = validation_length[iWidget];
  if( length != null && value != null )
  {
    var min   = length[0];
    var max   = length[1];
    var exact = length[2];

	var maxval = (max && max != null && value.length > max) ? max : 999999;
	if (exact && exact != null && exact < maxval) maxval = exact;
	if (maxval && maxval != null && value.length > maxval) {
		if (window.confirm(validation_widgetsI18N[widget] + validation_msg_trunk)) {
			value = value.substring(0,maxval);
			setWidgetValue(iWidget, value);
			ret = validation_msg_recheck;
		}
	} else if( min && min   != null && value.length < min) {
	
		var pad = validation_pad[iWidget];
		if (pad != null && value && value != null && value != '') {
			var side = pad[0];
			var val = pad[1];
			if (!side || side == null || side == '') side = 'left';
			if (val && val != null && val != '') {
				while (value.length < min) {
					if (side == 'left') {
						value = val + value;
					} else {
						value = value + val;
					}
				}
				if (maxval && maxval != null && value.length > maxval) {
					value = value.substring(0,maxval);
				}
				setWidgetValue(iWidget, value);
				ret = validation_msg_padrecheck;				
			}
		}
	}
  }
  return ret;
}
var creditcardcheck = 0;
var cin = 0;
var datecompare=false;
function validation_check_widget( iWidget )
{
  var value = getWidgetValue( iWidget );
  var datatype = validation_widgets[iWidget];
	
  if(iWidget.substring(iWidget.lastIndexOf('.')+1,iWidget.length) == 'ssn1')
  {
     ssn = 0; 
  }

  if(iWidget.substring(iWidget.lastIndexOf('.')+1,iWidget.length) == 'adn1')
  {
     adn = 0; 
  }


  if( datatype == "creditcardcheck" )
  {
        cardNumberiWidget = "CARDNUM";
        var cardTypeValue = getWidgetValue(iWidget);
        var cardNumberValue = getWidgetValue(cardNumberiWidget);
        var creditcardStatus;
        if ((cardNumberValue != null && cardNumberValue != '' && cardNumberValue != undefined) && (cardTypeValue != null && cardTypeValue != '' && cardTypeValue != undefined && cardTypeValue != 'UNKNOWN' ))
        {
         creditcardStatus = doCreditCardCheck(getWidgetValue(cardNumberiWidget),getWidgetValue(iWidget));
        if (creditcardStatus == false)
        {
	    //errorsWithIdsAsKey.push( [iWidget, "error on validation rules" ] );
         errorsWithIdsAsKey.push( [cardNumberiWidget, "error on validation rules" ] );
	  errors.push( [cardNumberiWidget,"", validation_datatype_messages[ iWidget ] ] );
	  creditcardcheck = 1; 
	  return validation_exc_number_format;
        }
	}
  }

  if( datatype == "cvvcheck" )
  {
	var cinname = iWidget;
	var cinvalue = getWidgetValue(cinname)
	var cinlength = cinvalue.length;
	if(cinname == 'CIN')
	{
	  var ctname = 'cardType';
	  var ctvalue = getWidgetValue(ctname);
          
	  if((ctvalue == 'ccamericanexp' || ctvalue == 'ccjcb') && cinvalue!='' &&  cinlength<4)
	  {
	    errorsWithIdsAsKey.push( [iWidget, "error on validation rules" ] );	
	    errors.push( [ iWidget,"",validation_datatype_messages[iWidget]]  );
cin = 1;
	  }
	  if(ctvalue == 'ccvisa' && cinvalue!='' && (cinlength < 3 || cinlength > 3 ))
	  {
	    errorsWithIdsAsKey.push( [iWidget, "error on validation rules" ] );
       	    errors.push( [ iWidget,"", validation_datatype_messages[iWidget]] ); 
cin = 1;
	  }
	  if(ctvalue == 'ccdiscover' && cinvalue!='' && (cinlength < 3 || cinlength > 3 ))
	  {
            errorsWithIdsAsKey.push( [iWidget, "error on validation rules" ] );
            errors.push( [ iWidget,"", validation_datatype_messages[iWidget]] ); 
cin = 1;
 	  }
	  if(ctvalue == 'ccmaster' && cinvalue!='' && (cinlength < 3 || cinlength > 3 ))
	  {
            errorsWithIdsAsKey.push( [iWidget, "error on validation rules" ] );
            errors.push( [ iWidget,"", validation_datatype_messages[iWidget]] ); 
cin = 1;
	  }
  }

  }

  if( datatype == "emailcheck" )
      {
	  confirmemailiWidget = "customer.confirmemail";
	  var emailValue = getWidgetValue(iWidget);
	  var confirmemailValue = getWidgetValue(confirmemailiWidget);
	  var emailStatus;
	  if ((emailValue != null && emailValue != '' && emailValue != undefined) && (confirmemailValue != null && confirmemailValue != '' && confirmemailValue != undefined ))
	      {
		  emailStatus = doEmailCheck(emailValue,confirmemailValue);
		  if ((emailStatus == false) && (emailValue.search(valid[validation_regexp[iWidget]]) != -1)  && (confirmemailValue.search(valid[validation_regexp[confirmemailiWidget]]) != -1))
	      {
		  errorsWithIdsAsKey.push( [iWidget, "error on validation rules" ] );
		  errorsWithIdsAsKey.push( [confirmemailiWidget, "error on validation rules" ] );
		  errors.push( [confirmemailiWidget, "", "The Email addresses you entered do not match." ] );
		  return validation_exc_number_format;
	      }


	      }

      }

  if( datatype == "newphone" )
      {
	  var iwidget = iWidget.substring(0,iWidget.lastIndexOf("."));
	   //iwidget = iwidget.substring(0,iwidget.lastIndexOf("."));
 	  var phone1 = iwidget+".phone1";
	  var phone2 = iwidget+".phone2";
	  var phone3 = iwidget+".phone3";
	  var country = iwidget+".country.countrymenu";
	  var phone1value = getWidgetValue(iwidget+".phone1");
	  var phone2value = getWidgetValue(iwidget+".phone2");
	  var phone3value = getWidgetValue(iwidget+".phone3");
	  var countryvalue = getWidgetValue(iwidget+".countrymenu");
	  if((countryvalue == "1" || countryvalue == " 1"))
	      {
		  if((!phone1value) && (!phone2value) ) 
		      {
			  errorsWithIdsAsKey.push( [phone1, "error on validation rules" ] );
			  errors.push( [ phone1,"", "Please enter your area code and telephone number"] );
		      }
		  else if((!phone1value) && (phone2value) ) 
		      {
			  errorsWithIdsAsKey.push( [phone1, "error on validation rules" ] );
			  errors.push( [ phone1,"", "Please enter the area code"] );
		      }
		  else if((phone1value) && (!phone2value) ) 
		      {
			  errorsWithIdsAsKey.push( [phone2, "error on validation rules" ] );
			  errors.push( [ phone2,"", "Please enter the phone number"] );
		      }
		  else
		      {
			  if((phone1value.search(valid[validation_regexp[phone1]]) == -1) && (phone2value.search(valid[validation_regexp[phone2]]) != -1)) 
			      {
				  if(phone1value.search(/^[0-9]*$/) == -1)
				      {	   
					  errorsWithIdsAsKey.push( [phone2, "error on validation rules" ] );
					  errors.push( [ phone1,"", "Please enter numbers only."] );

				      }
				  else
				      {
					  errorsWithIdsAsKey.push( [phone2, "error on validation rules" ] );
					  errors.push( [ phone2,"", "area code you entered is incorrect"] );
				      }
			      }
			  else if((phone1value.search(valid[validation_regexp[phone1]]) != -1) && (phone2value.search(valid[validation_regexp[phone2]]) == -1)) 
			      {
				  if(phone2value.search(/^[0-9]*$/) == -1)
				      {
					  errorsWithIdsAsKey.push( [phone1, "error on validation rules" ] );
					  errors.push( [ phone1,"", "Please enter numbers only."] );
				      }
				  else
				      {
					  errorsWithIdsAsKey.push( [phone1, "error on validation rules" ] );
					  errors.push( [ phone1,"", "phone number you entered is incorrect"] );
				      }
			      }
			  else if((phone1value.search(valid[validation_regexp[phone1]]) == -1) && (phone2value.search(valid[validation_regexp[phone2]]) == -1)) 
			      {
				  if((phone1value.search(/^[0-9]*$/) == -1) && (phone2value.search(/^[0-9]*$/) == -1))
				      {
					  errorsWithIdsAsKey.push( [phone2, "error on validation rules" ] );
					  errors.push( [ phone1,"", "Please enter numbers only."] );
				      }
				  else
				      {
					  errorsWithIdsAsKey.push( [phone2, "error on validation rules" ] );
					  errors.push( [ phone1,"", "Telephone number you entered is incorrect"] );
				      }
			      }
		      }
	      }

	  else
	      {
		  if(!phone3value)
		      {
			  errorsWithIdsAsKey.push( [phone1, "error on validation rules" ] );
			  errors.push( [ phone1,"", "Please enter your Telephone number"] );
		      }
		  else if((phone3value) && (phone3value.search(valid[validation_regexp[phone3]]) == -1))
		      {
			  errorsWithIdsAsKey.push( [phone1, "error on validation rules" ] );
			  errors.push( [ phone1,"", "Please enter numbers only."] );
		      }
	      }
      }


  
  if( datatype == "expirydate" )
  {
        yeariWidget = "year";
	monthiWidget = "month";
	monthyeariWidget = "monthyear";
        var yearValue = getWidgetValue(yeariWidget);
        var monthValue = getWidgetValue(iWidget);
        var monthyearValue = getWidgetValue(monthyeariWidget);
        var expirydateStatus;
        if (yearValue == '' && monthValue != '') 
        {
	  errorsWithIdsAsKey.push( [yeariWidget, "error on validation rules" ] );
	  errors.push( [ yeariWidget,"", "Please select year" ] );
	}
        else if (yearValue != '' && monthValue == '') 
        {
	  errorsWithIdsAsKey.push( [monthiWidget, "error on validation rules" ] );
	  errors.push( [ monthiWidget,"", "Please select month" ] );
	}
        else if (yearValue == '' && monthValue == '') 
        {
	  errorsWithIdsAsKey.push( [iWidget, "error on validation rules" ] );
	  errors.push( [ iWidget,"",validation_mask_messages[iWidget]]);
	}
	else if ((yearValue != null && yearValue != '' && yearValue != undefined) || (monthValue != null && monthValue != '' && monthValue != undefined ) || (monthyearValue != null && monthyearValue != '' && monthyearValue != undefined ))
        {
         expirydateStatus = doExpiryDateCheck(getWidgetValue(iWidget),getWidgetValue(yeariWidget),getWidgetValue(monthyeariWidget));
        
        if (!expirydateStatus)
        {
	  //pushErrorsId(iWidget, 'error on validation rules');
	  errorsWithIdsAsKey.push( [iWidget, "error on validation rules" ] );
	  errors.push( [ iWidget,"", validation_datatype_messages[ iWidget ] ] );
	  return validation_exc_number_format;
        }
	}
        else if (yearValue != null && yearValue != '' && yearValue != undefined)
        {
          return null;
        }

  }
  var tempno=0;
if( datatype == "PH")
{

var iwidget = iWidget.substring(0,(iWidget.length - 1));
if(document.all)
    {
if((document.getElementById("phone1").value =='') && (document.getElementById("phone2").value !=''))
{
		  errorsWithIdsAsKey.push( [iwidget+"1", "error on validation rules" ] );
		  errors.push( [ iwidget+"1","Please enter first part of your phone number." ] );
}
 if((document.getElementById("phone1").value !='') && (document.getElementById("phone2").value ==''))
{
		  errorsWithIdsAsKey.push( [iwidget+"2", "error on validation rules" ] );
		  errors.push( [ iwidget+"2","Please enter second part of your phone number." ] );

}
if((document.getElementById("phone1").value !='') && (document.getElementById("phone2").value !=''))
{
if((document.getElementById("phone2").value ==''))
{

		  errorsWithIdsAsKey.push( [iwidget+"2", "error on validation rules" ] );
		  errors.push( [ iwidget+"2","Please enter second part of your phone number." ] );
}
//else if((document.getElementById("phone1").value =='') && (document.getElementById("phone2").value !=''))
else if((document.getElementById("phone1").value ==''))
{

		  errors.push( [ iwidget+"2","Please enter first part of your phone ." ] );
}
    var test1 = document.getElementById("phone1").value;
    var test2 = document.getElementById("phone2").value;
    if((test1.search(valid[validation_regexp[iwidget+"1"]]) == -1) && (test2.search(valid[validation_regexp[iwidget+"2"]]) != -1) )
	{
		  errorsWithIdsAsKey.push( [iwidget+"2", "error on validation rules" ] );
		  errors.push( [ iwidget+"2","The first part of the phone number is not valid" ] );
	}
    else if((test1.search(valid[validation_regexp[iwidget+"1"]]) != -1) && (test2.search(valid[validation_regexp[iwidget+"2"]]) == -1))
	{
		  errors.push( [ iwidget+"2","The second part of the phone number is not valid" ] );
	}
    else if((test1.search(valid[validation_regexp[iwidget+"1"]]) == -1) && (test2.search(valid[validation_regexp[iwidget+"2"]]) == -1))
	{
		  errors.push( [ iwidget+"2","The phone number you have entered is not valid." ] );
	}
}

    }
else
    {
if((document.getElementById("phone1").value !='') && (document.getElementById("phone2").value ==''))
{
		  errorsWithIdsAsKey.push( [iwidget+"2", "error on validation rules" ] );
		  errors.push( [ iwidget+"2","Please enter second part of your phone number." ] );
}
if((document.getElementById("phone1").value =='') && (document.getElementById("phone2").value !=''))
{
		  errorsWithIdsAsKey.push( [iwidget+"2", "error on validation rules" ] );
		  errors.push( [ iwidget+"2","Please enter first part of your phone number." ] );
}

if((document.getElementById("phone1").value !='') && (document.getElementById("phone2").value !=''))
{
    var test1 = document.getElementById("phone1").value;
    var test2 = document.getElementById("phone2").value;
    if((test1.search(valid[validation_regexp[iwidget+"1"]]) == -1) && (test2.search(valid[validation_regexp[iwidget+"2"]]) != -1) )
	{
		  errorsWithIdsAsKey.push( [iwidget+"2", "error on validation rules" ] );
		  errors.push( [ iwidget+"2","The first part of the phone number is not valid" ] );
	}
    else if((test1.search(valid[validation_regexp[iwidget+"1"]]) != -1) && (test2.search(valid[validation_regexp[iwidget+"2"]]) == -1))
	{
		  errorsWithIdsAsKey.push( [iwidget+"2", "error on validation rules" ] );
		  errors.push( [ iwidget+"2","The second part of the phone number is not valid" ] );
	}
    else if((test1.search(valid[validation_regexp[iwidget+"1"]]) == -1) && (test2.search(valid[validation_regexp[iwidget+"2"]]) == -1))
	{
		  errorsWithIdsAsKey.push( [iwidget+"2", "error on validation rules" ] );
		  errors.push( [ iwidget+"2","The phone number you have entered is not valid." ] );
	}
}

    }    

}

  // LENGTH
  var length = validation_length[iWidget];

  var regexp = validation_regexp[iWidget];
  if( regexp != null && value != null )
  {
      msg = validation_exc_regexp;
      if(regexp != null) {
	      var regexpResult = value.search( valid[regexp] );
	      if( regexpResult == -1 && (value != null && value != '' && value != undefined))
	      {
                if(regexp == "Alphabets" )
                {
		   errorsWithIdsAsKey.push( [iWidget, msg ] );
		   errors.push( [ iWidget,"", validation_mask_messages[ iWidget ] ] );
                 return msg;
                }

                else if(regexp == "emailAddress" )
                {
			    errorsWithIdsAsKey.push( [iWidget, msg ] );
			    errors.push( [ iWidget,"", validation_mask_messages[ iWidget ] ] );
			    return msg;
                }
		else if(regexp == "creditcardnumber")
		{
		  if(creditcardcheck == 0) 
		  {
		  errorsWithIdsAsKey.push( [iWidget, msg ] );
		  errors.push( [ iWidget,"", validation_mask_messages[ iWidget ] ] );
                  return msg;				
		  }
		}
		else if(regexp == "cin")
		{
		  if(cin == 0) 
		  {
		  errorsWithIdsAsKey.push( [iWidget, msg ] );
		  errors.push( [ iWidget,"", validation_mask_messages[ iWidget ] ] );
                  return msg;				
		  }
		}
		else if(regexp == "ph1" || regexp == "ph2" || regexp == "ph3")
		{
		  errorsWithIdsAsKey.push( [iWidget, msg ] );
		  errors.push( [ iWidget,"", validation_mask_messages[ iWidget ] ] );
                  return msg;				
		}
		else
		{
		  errorsWithIdsAsKey.push( [iWidget, msg ] );
		  errors.push( [ iWidget,"", validation_mask_messages[ iWidget ] ] );
                  return msg;
		}
	      }	
      }
  }

 var dateSubString = iWidget.substring(iWidget.lastIndexOf('.')+1,iWidget.length);
  if((!value || value == undefined || value == null || value == '' || value == "UNKNOWN")) {

     if( validation_required[iWidget] || validation_required[iWidget] != null )

     {


	var iwidget=iWidget.substring(0,(iWidget.length - 1));


        if(validation_required[iWidget] == true)
        {
         return validation_exc_mandatory;
        }
        else
        {
          return null;
        }


     }

  }  
  // USER FUNCTION (CALL IT WITH ITSELF AS ARGUMENT)
  var userFunction = validation_user[iWidget];
  if( userFunction != null )
  {
    return eval( userFunction + "(" + iWidget + ")" );
  }
if(validation_required[iWidget] != true)
  return null;
}

function validation_check_form( iForm )
{
 
  validation_form = iForm != null ? iForm : document.forms[formName];

  var msg;

  //Call the listener method to set/unset veto on field validation?
  if (typeof(preInitCheck) == 'function') preInitCheck();
  for( widget in validation_widgets) {

    if(document.getElementById(widget) != null || document.getElementsByName(widget)[0] != null) {
	
  	if (!validation_docheck[widget] || validation_docheck[widget]=='' || validation_docheck[widget] == 'true') {
	    msg = validation_precheck_widget( widget );
	    if( msg != null ) {
			errorsWithIdsAsKey.push( [widget, msg ] );
			errors.push([ widget, msg ]);
	    }
	 }  
	}
  }

  for(widget in validation_widgets) {
	var temp = getParentSectionDiv(widget);
	var temp_widget;
	var sectionDiv = document.getElementById(temp);
	if(sectionDiv != null && sectionDiv.style.display !="none"){
	 var tName = document.getElementsByName(widget)[0];
	 if(document.all && (tName == null || tName == undefined))
	 {
	
        var tNames;
	
	tName = document.getElementById(widget);
	if(tName == null || tNmae == undefined || tNmae == '')
        {
        tNames=document.getElementsByTagName("input");
        for (var i = 0; i < tNames.length; i++)
        {
           if(tNames[i].getAttribute('name') != null && tNames[i].getAttribute('name') != undefined && tNames[i].getAttribute('name') != '' && tNames[i].getAttribute('name') == widget)
           {
                tName = tNames[i];
           }
        }
        tNames=document.getElementsByTagName("select");
        for (var i = 0; i < tNames.length; i++)
        {
           if(tNames[i].getAttribute('name') != null && tNames[i].getAttribute('name') != undefined && tNames[i].getAttribute('name') != '' && tNames[i].getAttribute('name') == widget)
           {
                tName = tNames[i];
           }
        }
        tNames=document.getElementsByTagName("textarea");

        for (var i = 0; i < tNames.length; i++)
        {
           if(tNames[i].getAttribute('name') != null && tNames[i].getAttribute('name') != undefined && tNames[i].getAttribute('name') != '' && tNames[i].getAttribute('name') == widget)
           {
                tName = tNames[i];
           }
        }
	}
       }
	 if((document.getElementById(widget) != null && document.getElementById(widget) != undefined) || (tName != null && tName != undefined))
	 {
          if(document.getElementById(widget) != null && document.getElementById(widget) != undefined)
          {
                temp_widget = document.getElementById(widget);
          }	  
	  else if(document.getElementsByName(widget)[0] != null && document.getElementsByName(widget)[0] != undefined)
	  {
		temp_widget = document.getElementsByName(widget)[0];
          }
          if(temp_widget.disabled != true) 
            {   
            if (!validation_docheck[widget] || validation_docheck[widget]=='' || validation_docheck[widget] == 'true') {
	    msg = validation_check_widget( widget );
	    if( msg != null){
		if (msg == validation_exc_mandatory)
                {
                    errorsWithIdsAsKey.push( [widget, msg ] );
                    errors.push( [widget, validation_messages[ widget ] ] );
                }
            }			
		
            } 
	   
	 }
	}
	}
  }
  
  //Call the listener method to add message errors by other external validations
  if (typeof(postInitCheck) == 'function') {
  	errors = postInitCheck(errors);
  }
  
  return errors;
}

// =======================================================================
// Display all validation errors found. The array containing errors can be
// generated by calling validation_check_form(). If you want to add other errors to
// display, just update the array returned from validation_check_form() with yours 
// before calling this function.
//
// Usage:
function pushErrorsId(iWidget, msg)
{
   var check = "false";
   for(var i=0; i < errorsWithIdsAsKey.length; ++i)
   {
	var widget = errorsWithIdsAsKey[i][0];
	if(iWidget == widget)//errorsWithIdsAsKey[i][0])
	{
	    check = "true";
	    break;
	}
   }
      if(check == "false")	
      {
         errorsWithIdsAsKey.push( [iWidget, msg ] );
      }
}
// This is for tree actions, set form_Check var and use while highlighting and header err div 
// return accordingly
function formCheck(arg)
{
form_Check = true;
if(form_submit(arg))
return true;
else
return false;
}
 function form_submit(arg)
{

   formName = arg;
   validation_form = document.forms[formName];
   var form = document.forms[formName];
   errors = new Array();
   errorsWithIdsAsKey=new Array();
   refreshErrorDivs();

   refreshStyles(form);

   
   if( !validation_show_errors( validation_check_form( form ) ) ) 
   {
      return false;	
   }
   else
   {
	if(document.getElementById("payment") != null || document.getElementsByName("payment")[0] != undefined) 
	{
        if(document.getElementById("ADDRESS1") != null && document.getElementById("ADDRESS1") != undefined)
        {
            var streetNumber = document.getElementById("ADDRESS1").value;
            var regExp = new RegExp("[0-9]+", "g");
            var tempStreetNumber ="";
            tempStreetNumber = streetNumber.match(regExp);
            if(tempStreetNumber != null && tempStreetNumber != undefined)
            {
		if(document.getElementById("addrNum") != null || document.getElementById("addrNum") != undefined)
                {
                    document.getElementById("addrNum").value = tempStreetNumber[0];
	        }
	        else if(document.getElementsByName("addrNum")[0] != null || document.getElementsByName("addrNum")[0] != undefined)
                {
		    document.getElementsByName("addrNum")[0].value = tempStreetNumber[0];
		}
            }
            else
            {
                document.forms[0].addrNum.value ="";
            }
        }	
	iwidget="payment";
	var result;
	if(validation_mask_messages[iwidget] != null && validation_mask_messages[iwidget] !="" && validation_mask_messages[iwidget] != undefined)
        result = confirm (validation_mask_messages[iwidget]);
	else
	result = confirm ("Your credit card processing may take some time, so kindly be patient.");
        if(!result){
              return false;

         }


	}

        if(document.getElementById("secure") != null || document.getElementsByName("secure")[0] != undefined)
        {
            var hostAction = document.forms[formName].action;
            if(hostAction.indexOf("http") < 0)
            {
            var hostName= window.location.hostname;
            if(hostName.indexOf("www") >=0)
               hostName = hostName.replace("www","secure");
            else if(hostName.indexOf("secure") < 0)
                hostName = "secure." + hostName;
            var pathName = window.location.pathname;
            pathName = pathName.substring(0,pathName.lastIndexOf("/"))
            document.forms[formName].action = "https:" + "//" + hostName + pathName + "/" + hostAction;
            }
            else
            {
              if(hostAction.indexOf("//www") >= 0)
              {
                 hostAction = hostAction.replace("//www","//secure");
                 hostAction = hostAction.replace("http:","https:");
                 document.forms[formName].action= hostAction;
              }
              else if(hostAction.indexOf("//secure") <= 0)
              {
                hostAction = hostAction.replace("//","//secure.");
                hostAction = hostAction.replace("http:","https:");
                document.forms[formName].action= hostAction;
              }
            }
       }
      if(!form_Check)
      {
         form.submit();
      }
      else
      {
	return true;
      }
   }
 }


//
// param: iErrors It contains couples of field-name/error message
// 	 === MODIFIED TO DISPLAY ERROR MESSAGES ON DIV'S - ANIL 2006/03/16
// return: true if there are no errors, otherwise false
// =======================================================================
function validation_show_errors( iErrors )
{
  if( iErrors == null || iErrors.length == 0 )
    return true;
  var commoniWidjet="common";

  var msgCommon="";
  var msg="";
  var msgOnDiv="";
  var specificMsg="";
  var specificContent="";
  var sectionNameSubscript="errors-section-1";
  var msgCtr = 0;
  var specificMsgCtr = 0;
  for( var i = 0; i < errorsWithIdsAsKey.length; ++i )
  {
	
   var firstElement = document.getElementsByName(errorsWithIdsAsKey[i][0])[0];
   
    if(firstElement == null || firstElement =='')
    {
	
	firstElement = document.getElementById(errorsWithIdsAsKey[i][0]);	   
	if(document.all && (firstElement == undefined || firstElement == '' ))
	{





        if(document.all)
        {

        var tNames;
	tName = document.getElementById(widget);
	if(tName == null || tName == undefined || tName == '')
	{
        tNames=document.getElementsByTagName("input");
        for (var j = 0; j < tNames.length; j++)
        {
           if(tNames[j].getAttribute('name') != null && tNames[j].getAttribute('name') != undefined && tNames[j].getAttribute('name') != '' && tNames[j].getAttribute('name') == errorsWithIdsAsKey[i][0])
           {
                firstElement = tNames[j];
           }
        }
        tNames=document.getElementsByTagName("select");
        for (var j = 0; j < tNames.length; j++)
        {
           if(tNames[j].getAttribute('name') != null && tNames[j].getAttribute('name') != undefined && tNames[j].getAttribute('name') != '' && tNames[j].getAttribute('name') == errorsWithIdsAsKey[i][0])
           {
                firstElement = tNames[j];
           }
        }
        tNames=document.getElementsByTagName("textarea");

        for (var j = 0; j < tNames.length; j++)
        {
           if(tNames[j].getAttribute('name') != null && tNames[j].getAttribute('name') != undefined && tNames[j].getAttribute('name') != '' && tNames[j].getAttribute('name') == errorsWithIdsAsKey[i][0])
           {
                firstElement = tNames[j];
           }
        }
        }
     }








	}
        
    }
   if(firstElement == null)
   {
	return true;
   }
//    var widget = errorsWithIdsAsKey[i][0];
    specificMsg = "";
    msg = "";
    for(j=0;j< errors.length;++j)
    {
    if(errorsWithIdsAsKey[i][0] == errors[j][0])
    {
    if(errors[j][1] != undefined && errors[j][1] != '' && errors[j][1] != null)
    {
       //msgCtr++;
       msg = errors[j][1];
    }
    if(errors[j][2] != undefined && errors[j][2] != '' && errors[j][2] != null)
    {
       //specificMsgCtr++;
       specificMsg = errors[j][2];
    }
    }
    }
    if(!form_Check)
    {
  	 highlight(firstElement,msg,specificMsg);
    }
 
}

    if(!form_Check){
    if(validation_mask_messages["common"] != null && validation_mask_messages["common"] != undefined)
    {
      document.getElementById("errors-page").innerHTML= validation_mask_messages["common"];
    }
    else
    {
      document.getElementById("errors-page").innerHTML= "<b>Please complete the fields highlighted below.</b>";
    }
    }
    document.getElementById("errors-page").className = "page-errors";
    ShowContent('errors-page');
    
    sectionName=sectionNameSubscript;

  return false;
}

//param:tagName
//function to retreive the immediate parent DIV with "id" attribute starting with "errors-section-xxx"
function getParentDiv(idValue){

        if(document.getElementById(idValue)!=null && document.getElementById(idValue).parentNode.tagName=="DIV" )
        {
		if(document.getElementById(idValue).parentNode.getAttribute("id") != null && document.getElementById(idValue).parentNode.getAttribute("id") != undefined && document.getElementById(idValue).parentNode.getAttribute("id") != '')
		{
                var id=document.getElementById(idValue).parentNode.getAttribute("id");
		
                if(id.substring(0,14)=="errors-section")
		{
                    return id;
                }
		}
        }

        var node;
        if(document.all)
        {

        var tNames;
        tNames=document.getElementsByTagName("input");
        for (var i = 0; i < tNames.length; i++)
        {
         if(tNames[i].getAttribute('name') != null && tNames[i].getAttribute('name') != undefined && tNames[i].getAttribute('name') != '' && tNames[i].getAttribute('name') == idValue)
           {
                node = tNames[i];
           }
        }
        tNames=document.getElementsByTagName("select");
        for (var i = 0; i < tNames.length; i++)
        {
         if(tNames[i].getAttribute('name') != null && tNames[i].getAttribute('name') != undefined && tNames[i].getAttribute('name') != '' && tNames[i].getAttribute('name') == idValue)
           {
                node = tNames[i];
           }
        }
        tNames=document.getElementsByTagName("textarea");

        for (var i = 0; i < tNames.length; i++)
        {
         if(tNames[i].getAttribute('name') != null && tNames[i].getAttribute('name') != undefined && tNames[i].getAttribute('name') != '' && tNames[i].getAttribute('name') == idValue)
           {
                node = tNames[i];
           }
        }
        }
        else
        {
            node = document.getElementsByName(idValue)[0];
        }

                node = document.getElementsByName(idValue)[0];
	        if (node == null && node == undefined)
		{
			node = document.getElementById(idValue);
		}
                while(node != null && node != undefined)
                {
                     if(node.tagName == "DIV" && node.getAttribute("id") != null && node.getAttribute("id").substring(0,14)=="errors-section")
                                break;

                        if(node.previousSibling !=null)
                        {
                                node = node.previousSibling;
                        }
                        else
                        {
                                node = node.parentNode;
                        }
                }
                if(node != null && node != undefined) {
                var id=node.getAttribute("id");
                if(id.substring(0,14)=="errors-section")
                        return id;
                }
                else
                        return "errors-page";
        
}

function getParentTAG(idValue,tag,userClass) 
{
	var cname;
	
	if(idValue != null && idValue != undefined)
	{
	if(idValue.parentNode.getAttribute('class') != null && idValue.parentNode.getAttribute('class') != undefined)
	{
	  cname = idValue.parentNode.className;
	}
        if(idValue != null && idValue.parentNode.tagName == tag && cname != null && cname != undefined && cname.indexOf(userClass) >= 0) {
                return idValue.parentNode;
	}
        else
        {
		node = idValue.parentNode;
                while(node != null && node != undefined)
                {
		      var cNameTemp = node.className;
                      if(node.tagName == tag && cNameTemp != null && cNameTemp != undefined && cNameTemp.indexOf(userClass) >= 0 )
		        break;
                        else
                        {
                                node = node.parentNode;
                        }
                }
                if(node != null && node != undefined) {
		cname = node.className;
		}
                if(node != null && node != undefined)
		{
		// && node.tagName == tag && cName != null && cName != undefined && cName.indexOf("label") > 0)
		
                   return node;
                }
                else
                        return null;
        }
       
    }
}



//param:tagName
//function to retreive the immediate parent DIV with "id" attribute starting with "errors-section-xxx"
function getParentSectionDiv(idValue)
{
        if(document.getElementById(idValue)!=null && document.getElementById(idValue).parentNode.tagName=="DIV")
        {
		if(document.getElementById(idValue).parentNode.getAttribute("id") != null && document.getElementById(idValue).parentNode.getAttribute("id") != undefined && document.getElementById(idValue).parentNode.getAttribute("id") != '')
		{
                var id=document.getElementById(idValue).parentNode.getAttribute("id");
				if(id.substring(0,7)=="section"){
                 	       	    return id;
                		}
		}
        }

	var node;
	if(document.all)
	{

	var tNames;
	tNames=document.getElementsByTagName("input");
        for (var i = 0; i < tNames.length; i++)
        {
           if(tNames[i].getAttribute('name') != null && tNames[i].getAttribute('name') != undefined && tNames[i].getAttribute('name') != '' && tNames[i].getAttribute('name') == idValue)
           {
                node = tNames[i];
           }
        }
	tNames=document.getElementsByTagName("select");
        for (var i = 0; i < tNames.length; i++)
        {
           if(tNames[i].getAttribute('name') != null && tNames[i].getAttribute('name') != undefined && tNames[i].getAttribute('name') != '' && tNames[i].getAttribute('name') == idValue)
           {
                node = tNames[i];
           }
        }
        tNames=document.getElementsByTagName("textarea");

	for (var i = 0; i < tNames.length; i++)
	{
	   if(tNames[i].getAttribute('name') != null && tNames[i].getAttribute('name') != undefined && tNames[i].getAttribute('name') != '' && tNames[i].getAttribute('name') == idValue)
	   {
		node = tNames[i];
	   }
	}
	}
	else
	{
	    node = document.getElementsByName(idValue)[0];
	}

	if (node == null || node == undefined)
	{
		node = document.getElementById(idValue);
	} 	
	while(node != null && node != undefined)
        {
              if(node.tagName == "DIV" && node.getAttribute("id") != null && node.getAttribute("id").substring(0,7)=="section")
	      {
                   break;
	      }
              else
              {
                   node = node.parentNode;
              }         

            //   if(node.previousSibling !=null)
              //          {
                //                node = node.previousSibling;
                  //      }
                    //    else
                    //    {
                    //            node = node.parentNode;
                    //    }
                }
                if(node != null && node != undefined) {
                var id=node.getAttribute("id");
                if(id.substring(0,7)=="section")		
                        return id;
                }
                else
                        return "errors-page";
        
}


function validation_execute( iForm )
{
  if(!form_Check)
    validation_form.submit();

  if( !validation_show_errors( validation_check_form( iForm ) ) )
      return;
  else
    return true;
}

function checkdate(objName)
{
	var datefield = objName;
	if (chkdate(objName) == false)
	 {
		datefield.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function chkdate(strDate)
{
	var strDatestyle = "US"; //United States date style
	//var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	//var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	//strDate = datefield.value;
	if (strDate.length < 1)
	{
		return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++)
	{
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1)
		{
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3)
			{
				err = 1;
				return false;
			}
			else
			{
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}	
			booFound = true;
		   }
	}
	if (booFound == false)
	{
		if (strDate.length>5)
		{
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
	}
	// US style
	if (strDatestyle == "US")
	{
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday))
	{
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth))
	{
		for (i = 0;i<12;i++)
		{
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase())
			{
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth))
		{
			err = 3;
			return false;
		}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear) || (intYear < 1900 || intYear > 2100))
	{
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1)
	{
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1))
	{
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1))
	{
		err = 7;
		return false;
	}
	if (intMonth == 2)
	{
		if (intday < 1)
		{
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true)
		{
			if (intday > 29)
			{
				err = 9;
				return false;
			}
		}
		else
		{
			if (intday > 28)
			{
				err = 10;
				return false;
			}
		}	
	}
	return true;
}

function doEmailCheck(email,confirmemail)
{
      if(email != confirmemail)

           return false;
      else
           return null;
}


function doCreditCardCheck(cc,type)
{

    var cctype=type;
    var ccno=cc;
    // Check if it is a master Card
    if(cctype=="ccmaster")
    {
        firstdig = cc.substring(0,1);
        seconddig = cc.substring(1,2);
        if((cc.length == 16) && (firstdig == 5) && (seconddig >= 1) && (seconddig <= 5))
           return null;
    }

    // Check if it is a Visa Card
    if(cctype=="ccvisa")
    {
       if (((cc.length == 16) || (cc.length == 13)) &&(cc.substring(0,1) == 4))
           return null;
    }

    // Check if it is a American Express Card
    if(cctype=="ccamericanexp")
    {
         firstdig = cc.substring(0,1);
         seconddig = cc.substring(1,2);
         if ((cc.length == 15) && (firstdig == 3) &&((seconddig == 4) || (seconddig == 7)))
             return null;
    }

    // Check if it is a Discover Card
    if(cctype=="ccdiscover")
    {
        first4digs = cc.substring(0,4);
	first2digs = cc.substring(0,2);
        if ((cc.length == 16) && ((first4digs == "6011") || (first2digs == "65")))
            return null;
    }
    // Check if it is a Diners Club Card
    if(cctype=="ccdinnersclub")
    {
        firstdig = cc.substring(0,1);
        seconddig = cc.substring(1,2);
        if (((cc.length == 14) && (firstdig == 3) && (seconddig == 6)) || ((cc.length == 16) && (firstdig == 5) && (seconddig == 5)))
            return null;
    }
     
    // Check if it is a Solo Card
    if(cctype=="ccsolo")
    {
        firstdig = cc.substring(0,1);
        first4digs = cc.substring(0,4);
        if ((((cc.length == 16) || (cc.length == 18) || (cc.length == 19)) && ((first4digs == "6334") || (first4digs == "6767") || (first4digs == "5020"))) || ((cc.length == 16) && (firstdig == 6)))
            return null;
    }

    // Check if it is a JCB
    if(cctype=="ccjcb")
    {
        first2digs = cc.substring(0,2);
        first4digs = cc.substring(0,4);
        if ((cc.length == 15 && (first4digs == "1800" || first4digs == "2131")) || (cc.length == 16 && first2digs == "35"))
            return null;
    }
    // Check if it is a Discover Card
    if(cctype=="ccothers")
    {
        if(isInteger(ccno))
           return null;
    }

    return false;
  }

function doExpiryDateCheck(month,year,monthyear)
{
    var strMonthArray = new Array(12);
    var intMonth;	
    strMonthArray[0] = "01";
    strMonthArray[1] = "02";
    strMonthArray[2] = "03";
    strMonthArray[3] = "04";
    strMonthArray[4] = "05";
    strMonthArray[5] = "06";
    strMonthArray[6] = "07";
    strMonthArray[7] = "08";
    strMonthArray[8] = "09";
    strMonthArray[9] = "10";
    strMonthArray[10] = "11";
    strMonthArray[11] = "12";
    for (i = 0;i<12;i++)
    {
        if (month.toUpperCase() == strMonthArray[i].toUpperCase())
        {
            intMonth = i+1;
            break;
         }
    }

    monthyear = monthyear.split(/_/);
    mon = monthyear[0];
	
    if(((monthyear[1] - year == 0) && (intMonth - monthyear[0] >= 0)) || (monthyear[1] - year < 0))
    {
        return true;
    }	
    else
        return false;

}

function refreshStyles(form) {
    var fields = form.elements;
    for(var i=0;i<fields.length;i++){
        var parentElement = getParentTAG(fields[i],"DIV","set");
        if(parentElement != null)
        {
           var cName=parentElement.className;
           if(cName.indexOf("error") > 0)
           {
              cName=cName.substring(0,cName.lastIndexOf(" "));
              parentElement.className = cName;
           }
        }
	if(parentElement == null)
	{
        parentElement = getParentTAG(fields[i],"P","set");
        if(parentElement != null)
        {
          var cName=parentElement.className;
           if(cName.indexOf("error") > 0)
           {
              cName=cName.substring(0,cName.lastIndexOf(" "));
              parentElement.className = cName;
           }
        }
	}

    }
}

function refreshErrorDivs() {
        var pList= document.getElementsByTagName("P");
        for(var i=0;i < pList.length;i++) {
		var pClass = pList[i].getAttribute("class");
                if(pList[i].getAttribute("class") != null && pList[i].getAttribute("class") != '' && pList[i].getAttribute("class") != undefined && pList[i].getAttribute("class") == "error"){
			var parentElement = getParentTAG(pList[i],"DIV","element");
                        parentElement.removeChild(pList[i]);
			pList= document.getElementsByTagName("P");
			i=0;
                }
if(pList[i].getAttribute("class") == null)
{
  if(pList[i].getAttribute("className") != null && pList[i].getAttribute("className") != '' && pList[i].getAttribute("className") != undefined && pList[i].getAttribute("className") == "error"){
                        var parentElement = getParentTAG(pList[i],"DIV","element");
                        parentElement.removeChild(pList[i]);
                        pList= document.getElementsByTagName("P");
                        i=0;
                }
    }	
  }
}




function highlight(firstElement,msg,specificMsg)
{
        var parentElement = getParentTAG(firstElement,"DIV","set");
        if(parentElement != null)
      	{
            parentElement.className = parentElement.className + " error";
      	}
	if(parentElement != null)
	{
	if(specificMsg != null && specificMsg != '' && specificMsg != undefined)
	{
		var pElement = document.createElement("P");
		var parentElement = getParentTAG(firstElement,"DIV","element");
		parentElement.appendChild(pElement);
		pElement.innerHTML= specificMsg;
		pElement.setAttribute((document.all ? 'className':'class'), "error");
		specificMsg="";

	}
	else if(msg != null && msg != '' && msg != undefined)
	{
                var pElement = document.createElement("P");
                var parentElement = getParentTAG(firstElement,"DIV","element");
                parentElement.appendChild(pElement);
		pElement.innerHTML= msg;
                pElement.setAttribute((document.all ? 'className':'class'), "error");
		msg="";
	}
	}
}

// Script for ServerSide error display
var errorid;
function serverSideErrorDisplay(formId,errMsg)
{
      var errorDivId;
        var sectionName="errors-section-1";
        var node = document.getElementById(formId);

        if(node == null)
        {
                node = document.getElementsByName(formId)[0];
        }
        if(node != null && node.tagName=="FORM" )
        {
            getErrorDiv(node);
        }
        errorDivId = errorid;

        if(errorDivId == null)
        {
                //document.getElementById("errors-section");
                document.getElementById("errors-page").className = "page-errors";
                document.getElementById("errors-page").innerHTML=errMsg;
                ShowContent("errors-page");
        }
        else {
                document.getElementById(errorDivId).className = "section-errors";
                document.getElementById(errorDivId).innerHTML=errMsg;
                ShowContent(errorDivId);
        }
}

function getErrorDiv(node)
{
        var nodelist=node.childNodes;

        if (nodelist != null)
        {
        var i;
        for(i=0;i < nodelist.length ;i++)
        {
                if(nodelist[i].tagName == 'DIV')
                {
                   nodeid=nodelist[i].getAttribute("id");
                   if(nodeid.substring(0,14) == "errors-section")
                   {
                        errorid = nodeid;
                   }
                }
                getErrorDiv(nodelist[i]);
        }
        }
        return errorid;
}

 


