//
// Newpassword form validation
//
//
// Login form validation
//

function LoginValidate(theform)
{
if (trim(theform.username.value) == "")
{
alert("Please enter a username");
theform.username.focus();
return (false);
}
if (trim(theform.password.value) == "")
{
alert("Please enter a password");
theform.password.focus();
return (false);
}
return (true);
}

function PartValidate(theform)
{
if (theform.part_reference.value == "")
{
alert("Please enter a part reference");
theform.part_reference.focus();
return (false);
}
if (theform.part_description.value == "")
{
alert("Please enter a description");
theform.part_description.focus();
return (false);
}

if (theform.part_category.value == "")
{
alert("Please enter a category");
theform.part_category.focus();
return (false);
}

if (theform.part_category.value == 0)
{
alert("Please enter a category");
theform.part_category.focus();
return (false);
}

return (true);
}


//
// Make sure a part has been entered
//
function CheckPart(theform)
{
if (trim(theform.criteria.value) == "")
{
alert("Please enter a part reference to use this search");
theform.criteria.focus();
return (false);
}
return (true);
}

function CheckCustomer(theform)
{
if (trim(theform.customer.value) == "")
{
alert("Please enter part of a surname to use this search");
theform.customer.focus();
return (false);
}
return (true);
}



function CheckUserEdit(theform){

if (trim(theform.user_name.value) == "")
{
alert("Please enter a user name");
theform.user_name.focus();
return (false);
}


if (trim(theform.first_name.value) == "")
{
alert("Please enter a first name");
theform.first_name.focus();
return (false);
}

if (trim(theform.surname.value) == "")
{
alert("Please enter a surname");
theform.surname.focus();
return (false);
}

if (theform.newpassword.value != theform.retypepassword.value)
{
alert("Password and retyped password do not match");
theform.newpassword.focus();
return (false);
}


if (theform.last_login_date.value != "")
{
tmpval = theform.last_login_date.value;
if (!(chdate(tmpval))){
	alert("Invalid last login date format");
	theform.last_login_date.focus();
	return (false);
	}
}


return true;

}



//
// Make sure a date is in correct format
//
function CheckSearch(theform)
{
// this needs some thought
// have to have some intelligence to find 
// the active select box and derive the 
// appropriate field from it

return (true);
}

//end of function
function validateSearch(theform)
{
return (true);
}


//
// Registration form validation
//

function RegisterValidate(theform)
{
if (trim(theform.contact_title.value) == "")
{
alert("Please enter a title");
theform.contact_title.focus();
return (false);
}
if (trim(theform.contact_first_name.value) == "")
{
alert("Please enter a first name");
theform.contact_first_name.focus();
return (false);
}
if (trim(theform.contact_surname.value) == "")
{
alert("Please enter a surname");
theform.contact_surname.focus();
return (false);
}
if (trim(theform.contact_phone.value) == "")
{
alert("Please enter a phone number");
theform.contact_phone.focus();
return (false);
}
if (!isPhoneNumber(theform.contact_phone.value))
{
alert("Please enter a numeric phone number");
theform.contact_phone.focus();
return (false);
}

if (trim(theform.contact_address1.value) == "")
{
alert("Please enter an address");
theform.contact_address1.focus();
return (false);
}
if (trim(theform.contact_town.value) == "")
{
alert("Please enter a town");
theform.contact_town.focus();
return (false);
}

if (trim(theform.contact_postcode.value) == "")
{
alert("Please enter a post code");
theform.contact_postcode.focus();
return (false);
}
if (theform.contact_country.value == "0")
{
alert("Please select a country");
theform.contact_country.focus();
return (false);
}

if (theform.contact_email.value == "")
{
alert("Please enter an email address");
theform.contact_email.focus();
return (false);
}
if (!test(theform.contact_email.value) )
{
alert("Please enter a valid email address");
theform.contact_email.focus();
return (false);
}


return (true);
}

function ContactsValidate(theform)
{
if (trim(theform.contact_title.value) == "")
{
alert("Please enter a title");
theform.contact_title.focus();
return (false);
}

if (theform.contact_first_name.value == "")
{
alert("Please enter a first name");
theform.contact_first_name.focus();
return (false);
}

if (theform.contact_surname.value == "")
{
alert("Please enter a surname");
theform.contact_surname.focus();
return (false);
}
if (theform.usergroup.value == "")
{
alert("Please enter a user group");
theform.usergroup.focus();
return (false);
}


if (theform.password.value != theform.retype_password.value)
{
alert("Retyped password does not match");
theform.password.focus();
return (false);
}
/*
if (theform.contact_company.value == "")
{
alert("Please enter a company");
theform.contact_company.focus();
return (false);
}
*/
if (theform.contact_phone.value == "")
{
alert("Please enter a contact telephone");
theform.contact_phone.focus();
return (false);
}
if (!isPhoneNumber(theform.contact_phone.value))
{
alert("Please enter a numeric phone number");
theform.contact_phone.focus();
return (false);
}

if (theform.contact_email.value == "")
{
alert("Please enter an email address");
theform.contact_email.focus();
return (false);
}
if (!test(theform.contact_email.value) )
{
alert("Please enter a valid email address");
theform.contact_email.focus();
return (false);
}


if (theform.contact_address1.value == "")
{
alert("Please enter an address");
theform.contact_address1.focus();
return (false);
}

if (theform.contact_town.value == "")
{
alert("Please enter a town");
theform.contact_town.focus();
return (false);
}
if (theform.contact_country.value == "0")
{
alert("Please select a country");
theform.contact_country.focus();
return (false);
}


return (true);
}


function CheckPersonal(theform)
{
if (trim(theform.contact_title.value) == "")
{
alert("Please enter a title");
theform.contact_title.focus();
return (false);
}

if (trim(theform.contact_first_name.value) == "")
{
alert("Please enter a first name");
theform.contact_first_name.focus();
return (false);
}
if (trim(theform.contact_surname.value) == "")
{
alert("Please enter a surname");
theform.contact_surname.focus();
return (false);
}

if (trim(theform.contact_email.value) == "")
{
alert("Please enter an email address");
theform.contact_email.focus();
return (false);
}
if (!test(theform.contact_email.value) )
{
alert("Please enter a valid email address");
theform.contact_email.focus();
return (false);
}


if (trim(theform.contact_address1.value) == "")
{
alert("Please enter an address");
theform.contact_address1.focus();
return (false);
}
if (trim(theform.contact_town.value) == "")
{
alert("Please enter a town");
theform.contact_town.focus();
return (false);
}

if (trim(theform.contact_postcode.value) == "")
{
alert("Please enter a post code");
theform.contact_postcode.focus();
return (false);
}

if (theform.contact_country.value == "0")
{
alert("Please select a country");
theform.contact_country.focus();
return (false);
}

if (trim(theform.contact_phone.value) == "")
{
alert("Please enter a phone number");
theform.contact_phone.focus();
return (false);
}
if (!isPhoneNumber(theform.contact_phone.value))
{
alert("Please enter a numeric phone number");
theform.contact_phone.focus();
return (false);
}


return (true);
}


function CheckDeliveryDetails(theform)
{

if (trim(theform.delivery_address1.value) == "")
{
alert("Please enter an address");
theform.delivery_address1.focus();
return (false);
}

if (trim(theform.delivery_town.value) == "")
{
alert("Please enter a town");
theform.delivery_town.focus();
return (false);
}
if (trim(theform.delivery_postcode.value) == "")
{
alert("Please enter a postcode");
theform.delivery_postcode.focus();
return (false);
}
if (trim(theform.delivery_country.value) == "0")
{
alert("Please select a country");
theform.delivery_country.focus();
return (false);
}

return true;
}

function CheckPayment(theform)
{
var strip="";
var curdate = new Date();
var st_date =0;
var end_date = 0;
var cur_date = 0;
if (trim(theform.payment_name.value) == "")
{
alert("Please enter a name");
theform.payment_name.focus();
return (false);
}
if (trim(theform.payment_number.value) == "")
{
alert("Please enter a number");
theform.payment_number.focus();
return (false);
}



strip = stripspaces(theform.payment_number.value);
//alert (strip);
if (!isCreditCard(strip))
{
alert("Please enter a valid credit card number");
theform.payment_number.focus();
return (false);
}
else
{
theform.payment_number.value = strip;
}


// now check the start date
st_date = 1+ parseInt(theform.payment_start_month.value) + (parseInt(theform.payment_start_year.value) * 12);
end_date = 1+parseInt(theform.payment_end_month.value) + (parseInt(theform.payment_end_year.value) * 12);
cur_mon = curdate.getMonth();
cur_year = curdate.getFullYear();
cur_date = cur_mon + (cur_year * 12);


if (st_date > cur_date) {
alert ("invalid start date");
theform.payment_start_year.focus();
return (false);
}
if (cur_date > end_date || end_date == st_date){
alert ("invalid expiry date");
theform.payment_end_year.focus();
return (false);
}

if (trim(theform.payment_issue.value) == "" && theform.payment_type.value ==3)
{
alert("Please enter an issue number for Switch");
theform.payment_issue.focus();
return (false);
}


return true;
}


function CheckOrderCatalog(theform)
{
if (theform.pubname.value == "")
{
alert("Please enter a name");
theform.pubname.focus();
return (false);
}
if (theform.email.value == "")
{
alert("Please enter an email address");
theform.email.focus();
return (false);
}
if (theform.address.value == "")
{
alert("Please enter an address");
theform.address.focus();
return (false);
}
return (true);
}

//
// Checkcatalog
//
//make sure something has been entered
//
function CheckCatalog(theform){
//alert ("called");
for (i=0;i<theform.length;i++){
if (theform.elements[i].checked){
return true;
}
}
alert("Please select a catalog");
return (false);

}
// end of function

// CheckBasket
//
//
function CheckBasket(theform){
//alert ("called");
for (k=0;k<theform.length;k++){
if (theform.elements[k].name.substr(0,3)== "qty"){
if (!isNumeric(theform.elements[k].value)){
		alert ("Please enter numeric quantity");
		return false;
	}
}	
}
return true;

}
// end of function



//
// Registration form validation
//

function PDFValidate(theform)
{
if (theform.mailer_name.value == "")
{
alert("Please select a name");
theform.mailer_name.focus();
return (false);
}

if (theform.mailer_description.value == "")
{
alert("Please enter a description");
theform.mailer_description.focus();
return (false);
}

if (theform.mailer_start_date.value == "")
{
alert("Please enter a start date");
theform.mailer_start_date.focus();
return (false);
}

if (theform.mailer_end_date.value == "")
{
alert("Please enter a end date");
theform.mailer_end_date.focus();
return (false);
}
tmpval = theform.mailer_start_date.value;
l1 = tmpval.length;
l2 =  l1-2;
tmpval1 = tmpval.substr(2,l2);
tmpval2 = tmpval.substr(0, l2);

	if (!(chdate(tmpval))){
alert("Invalid start date format");
theform.mailer_start_date.focus();

return false;
}




tmpval = theform.mailer_end_date.value;
l1 = tmpval.length;
l2 =  l1-2;
tmpval1 = tmpval.substr(2,l2);
tmpval2 = tmpval.substr(0, l2);

	if (!(chdate(tmpval))){
alert("Invalid end date format");
theform.mailer_end_date.focus();
return false;
}


return (true);
}


function PageUploadValidate(theform)
{
return true;
}

function MenuValidate(theform)
{
if (trim(theform.menu_text.value) == "")
{
alert("Please enter the menu text");
theform.menu_text.focus();
return (false);
}
if (trim(theform.linktype.value) == "")
{
alert("Please enter a link type");
theform.linktype.focus();
return (false);
}

if (theform.linktype.value == "L" && trim(theform.link.value) == ""){
alert("You must select a link if that is the link type");
theform.link.focus();
return (false);
}

if (theform.linktype.value == "F" && trim(theform.file.value) == ""){
alert("You must select a file if that is the link type");
theform.file.focus();
return (false);
}

if (theform.linktype.value == "D" && trim(theform.data_page.value)==""){
alert("You must select a data page if that is the link type");
theform.data_page.focus();
return (false);
}

if (theform.linktype.value == "N" && trim(theform.news_page.value) == ""){
alert("You must select a news page if that is the link type");
theform.news_page.focus();
return (false);
}

return (true);
}


function PageValidate(theform)
{
if (trim(theform.page_number.value) == "")
{
alert("Please enter a page number");
theform.page_number.focus();
return (false);
}
if (!isNumeric(theform.page_number.value) )
{
alert("Please enter a numeric page number");
theform.page_number.focus();
return (false);
}



if (trim(theform.page_name.value) == "")
{
alert("Please enter a file name");
theform.page_name.focus();
return (false);
}


if (trim(theform.page_title.value) == "")
{
alert("Please enter a page title");
theform.page_title.focus();
return (false);
}

return (true);
}


function EnquiryValidate(theform)
{
//if (trim(theform.Email.value) == "")
//{
//alert("Please enter an email address");
//theform.Email.focus();
//return (false);
//}


return (true);
}

function EnquiryAdminValidate(theform)
{
if (trim(theform.email.value) == "")
{
alert("Please enter an email address");
theform.email.focus();
return (false);
}
if (trim(theform.enquiry_date.value) == "")
{
alert("Please enter an enquiry date");
theform.enquiry_date.focus();
return (false);
}

tmpval = theform.enquiry_date.value;
l1 = tmpval.length;
l2 =  l1-2;
tmpval1 = tmpval.substr(2,l2);
tmpval2 = tmpval.substr(0, l2);
if (!(chdate(tmpval))){
//if (!(chdate(tmpval1) || chdate(tmpval2))){
	alert("Invalid enquiry date format");
	theform.enquiry_date.focus();
	return false;
}



return (true);
}

function CVAdminValidate(theform)
{
if (trim(theform.surname.value) == "")
{
alert("Please enter a surname");
theform.surname.focus();
return (false);
}
if (trim(theform.forenames.value) == "")
{
alert("Please enter a first name");
theform.forenames.focus();
return (false);
}
if (trim(theform.email.value) == "")
{
alert("Please enter an email address");
theform.email.focus();
return (false);
}
if (!test(trim(theform.email.value)) )
{
alert("Please enter avalid email address");
theform.email.focus();
return (false);
}



if (trim(theform.register_date.value) == "")
{
alert("Please enter an register date");
theform.register_date.focus();
return (false);
}

tmpval = theform.register_date.value;
l1 = tmpval.length;
l2 =  l1-2;
tmpval1 = tmpval.substr(2,l2);
tmpval2 = tmpval.substr(0, l2);
if (!(chdate(tmpval))){
//if (!(chdate(tmpval1) || chdate(tmpval2))){
	alert("Invalid register date format");
	theform.register_date.focus();
	return false;
}


return (true);
}





function CheckArea(theform){
//alert (theform.length);
for (k=0;k<theform.length;k++){
if (theform.elements[k].name.substr(0,6)== "number"){
if (!isNumeric(theform.elements[k].value)){
		alert ("Please enter numeric area number");
		return false;
	}
}	
}
return true;

}

function CheckStatusForm(theform){
//alert (theform.length);
for (k=0;k<theform.length;k++){
if (theform.elements[k].name.substr(0,6)== "number"){
if (!isNumeric(theform.elements[k].value)){
		alert ("Please enter numeric status number");
		return false;
	}
}	
}
return true;

}



//This function will validate a number and return 
//True or False. 

function isNumeric(InString)
{
   RefString = "-1234567890.";
   DecimalPoints = 0;
//  alert (InString); 
   if (InString.length == 0)
	{
//    alert(strFieldName + " must not be null.");
    return (true);	
	}   

   for (i=0; i < InString.length; i++)
   {
      TempChar = InString.substring ( i, i+1 );

      if (  TempChar == "." ) { DecimalPoints++; } /* Can only have zero or one decimal points in a number */
      if (( TempChar == "-" ) && ( i != 0 )) { return ( false ); } /* Minus Sign must be first character */

      /* Check if current character is valid */

      if ( RefString.indexOf (TempChar,0) == -1)
      {
         /* character not found in RefString */
 //        alert(strFieldName + " must be numeric.");
         return (false);
      }
   }
   if ( DecimalPoints > 1 )
		{ 
		return ( false );
//		alert(strFieldName + " must be numeric.");
		 }
   return(true);
 }


function isPhoneNumber(InString)
{
   RefString = "-1234567890. ()";
//   DecimalPoints = 0;
//  alert (InString); 
   if (InString.length == 0)
	{
//    alert(strFieldName + " must not be null.");
    return (true);	
	}   

   for (i=0; i < InString.length; i++)
   {
      TempChar = InString.substring ( i, i+1 );

//      if (  TempChar == "." ) { DecimalPoints++; } /* Can only have zero or one decimal points in a number */
//      if (( TempChar == "-" ) && ( i != 0 )) { return ( false ); } /* Minus Sign must be first character */

      /* Check if current character is valid */

      if ( RefString.indexOf (TempChar,0) == -1)
      {
         /* character not found in RefString */
 //        alert(strFieldName + " must be numeric.");
         return (false);
      }
   }
//   if ( DecimalPoints > 1 )
//		{ 
//		return ( false );
//		alert(strFieldName + " must be numeric.");
//		 }
   return(true);
 }



function EmailValidate(theform)
{
if (theform.email_to.value == "")
{
alert("Please select a group");
theform.email_to.focus();
return (false);
}

if (theform.email_subject.value == "")
{
alert("Please enter a subject");
theform.email_subject.focus();
return (false);
}
if (theform.email_message.value == "")
{
alert("Please enter a message");
theform.email_message.focus();
return (false);
}

return (true);
}

function DeleteImageValidate(theform)
{

if (trim(theform.file_name.value) == "")
{
alert("Please select an image name");
theform.file_name.focus();
return (false);
}

return (true);
}



function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also
   // removes consecutive spaces and replaces it with one space.
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function



function NewsValidate(theform)
{
if (trim(theform.news_headline.value) == "")
{
alert("Please enter a headline");
theform.news_headline.focus();
return (false);
}

if (trim(theform.news_text.value) == "")
{
alert("Please enter some news");
theform.news_text.focus();
return (false);
}

if (trim(theform.news_start_date.value) == "")
{
alert("Please enter a start date");
theform.news_start_date.focus();
return (false);
}

//if (trim(theform.news_end_date.value) == "")
//{
//alert("Please enter an end date");
//theform.news_end_date.focus();
//return (false);
//}


if (trim(theform.news_start_date.value) != ""){
	tmpval = theform.news_start_date.value;
	l1 = tmpval.length;
	l2 =  l1-2;
	tmpval1 = tmpval.substr(2,l2);
	tmpval2 = tmpval.substr(0, l2);
	if (!(chdate(tmpval))){
//if (!(chdate(tmpval1) || chdate(tmpval2))){
		alert("Invalid start date format");
		theform.news_start_date.focus();
		return false;
	}
}

if (trim(theform.news_end_date.value) != ""){

	tmpval = theform.news_end_date.value;
	l1 = tmpval.length;
	l2 =  l1-2;
	tmpval1 = tmpval.substr(2,l2);
	tmpval2 = tmpval.substr(0, l2);
	if (!(chdate(tmpval))){
//if (!(chdate(tmpval1) || chdate(tmpval2))){
		alert("Invalid end date format");
		theform.news_end_date.focus();
	return false;
	}
}


return (true);
}


function CheckStatusForm(theform){
//alert (theform.length);
for (k=0;k<theform.length;k++){
if (theform.elements[k].name.substr(0,6)== "number"){
if (!isNumeric(theform.elements[k].value)){
		alert ("Please enter numeric status number");
		return false;
	}
}	
}
return true;

}

//
// CheckOrder validation
//

function CheckOrder(theform)
{
if (theform.pubname.value == "")
{
alert("Please enter a name");
theform.pubname.focus();
return (false);
}
if (theform.email.value == "")
{
alert("Please enter an email address");
theform.email.focus();
return (false);
}
if (theform.address.value == "")
{
alert("Please enter an address");
theform.address.focus();
return (false);
}
return (true);
}



function VacancyValidate(theform)
{
if (trim(theform.vacancy_reference.value) == "")
{
alert("Please enter a reference");
theform.vacancy_reference.focus();
return (false);
}

if (trim(theform.vacancy_title.value) == "")
{
alert("Please enter a title");
theform.vacancy_title.focus();
return (false);
}

if (trim(theform.vacancy_description.value) == "")
{
alert("Please enter a description");
theform.vacancy_description.focus();
return (false);
}


if (trim(theform.vacancy_category.value) == 0)
{
alert("Please enter a category");
theform.vacancy_category.focus();
return (false);
}


if (trim(theform.vacancy_location.value) == 0)
{
alert("Please enter a location");
theform.vacancy_location.focus();
return (false);
}

if (trim(theform.vacancy_posted_date.value) == "")
{
alert("Please enter a posted date");
theform.vacancy_posted_date.focus();
return (false);
}


if (trim(theform.vacancy_posted_date.value) != ""){
	tmpval = theform.vacancy_posted_date.value;
	l1 = tmpval.length;
	l2 =  l1-2;
	tmpval1 = tmpval.substr(2,l2);
	tmpval2 = tmpval.substr(0, l2);
	if (!(chdate(tmpval))){
//if (!(chdate(tmpval1) || chdate(tmpval2))){
		alert("Invalid posted date format");
		theform.vacancy_posted_date.focus();
		return false;
	}
}

if (trim(theform.vacancy_deadline.value) != ""){

	tmpval = theform.vacancy_deadline.value;
	l1 = tmpval.length;
	l2 =  l1-2;
	tmpval1 = tmpval.substr(2,l2);
	tmpval2 = tmpval.substr(0, l2);
	if (!(chdate(tmpval))){
//if (!(chdate(tmpval1) || chdate(tmpval2))){
		alert("Invalid deadline format");
		theform.vacancy_deadline.focus();
	return false;
	}
}

if (trim(theform.vacancy_closed.value) != ""){

	tmpval = theform.vacancy_closed.value;
	l1 = tmpval.length;
	l2 =  l1-2;
	tmpval1 = tmpval.substr(2,l2);
	tmpval2 = tmpval.substr(0, l2);
	if (!(chdate(tmpval))){
//if (!(chdate(tmpval1) || chdate(tmpval2))){
		alert("Invalid closed date format");
		theform.vacancy_closed.focus();
	return false;
	}
}


return (true);
}


function CheckCategory(theform){
// add some validation if we need some

return (true);

}

function CheckCountry(theform){
// add some validation if we need some

return (true);

}


// end of function


// date validation
// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. Instead,
// please just point to my URL to ensure the most up-to-date versions
// of the files. Thanks.
// ===================================================================


// ------------------------------------------------------------------
// These functions use the same 'format' strings as the 
// java.text.SimpleDateFormat class, with minor exceptions.
// The format string consists of the following abbreviations:
// 
// Field        | Full Form          | Short Form
// -------------+--------------------+-----------------------
// Year         | yyyy (4 digits)    | yy (2 digits), y (2 or 4 digits)
// Month        | MMM (name or abbr.)| MM (2 digits), M (1 or 2 digits)
// Day of Month | dd (2 digits)      | d (1 or 2 digits)
// Hour (1-12)  | hh (2 digits)      | h (1 or 2 digits)
// Hour (0-23)  | HH (2 digits)      | H (1 or 2 digits)
// Hour (0-11)  | KK (2 digits)      | K (1 or 2 digits)
// Hour (1-24)  | kk (2 digits)      | k (1 or 2 digits)
// Minute       | mm (2 digits)      | m (1 or 2 digits)
// Second       | ss (2 digits)      | s (1 or 2 digits)
// AM/PM        | a                  |
//
// NOTE THE DIFFERENCE BETWEEN MM and mm! Month=MM, not mm!
// Examples:
//  "MMM d, y" matches: January 01, 2000
//                      Dec 1, 1900
//                      Nov 20, 00
//  "M/d/yy"   matches: 01/20/00
//                      9/2/00
//  "MMM dd, yyyy hh:mm:ssa" matches: "January 01, 2000 12:30:45AM"
// ------------------------------------------------------------------

var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
function LZ(x) {return(x<0||x>9?"":"0")+x}

// ------------------------------------------------------------------
// isDate ( date_string, format_string )
// Returns true if date string matches format of format string and
// is a valid date. Else returns false.
// It is recommended that you trim whitespace around the value before
// passing it to this function, as whitespace is NOT ignored!
// ------------------------------------------------------------------
function isDate(val,format) {
	var date=getDateFromFormat(val,format);
	if (date==0) { return false; }
	return true;
	}

// -------------------------------------------------------------------
// compareDates(date1,date1format,date2,date2format)
//   Compare two date strings to see which is greater.
//   Returns:
//   1 if date1 is greater than date2
//   0 if date2 is greater than date1 of if they are the same
//  -1 if either of the dates is in an invalid format
// -------------------------------------------------------------------
function compareDates(date1,dateformat1,date2,dateformat2) {
	var d1=getDateFromFormat(date1,dateformat1);
	var d2=getDateFromFormat(date2,dateformat2);
	if (d1==0 || d2==0) {
		return -1;
		}
	else if (d1 > d2) {
		return 1;
		}
	return 0;
	}

// ------------------------------------------------------------------
// formatDate (date_object, format)
// Returns a date in the output format specified.
// The format string uses the same abbreviations as in getDateFromFormat()
// ------------------------------------------------------------------
function formatDate(date,format) {
	format=format+"";
	var result="";
	var i_format=0;
	var c="";
	var token="";
	var y=date.getYear()+"";
	var M=date.getMonth()+1;
	var d=date.getDate();
	var H=date.getHours();
	var m=date.getMinutes();
	var s=date.getSeconds();
	var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
	// Convert real date parts into formatted versions
	var value=new Object();
	if (y.length < 4) {y=""+(y-0+1900);}
	value["y"]=""+y;
	value["yyyy"]=y;
	value["yy"]=y.substring(2,4);
	value["M"]=M;
	value["MM"]=LZ(M);
	value["MMM"]=MONTH_NAMES[M-1];
	value["d"]=d;
	value["dd"]=LZ(d);
	value["H"]=H;
	value["HH"]=LZ(H);
	if (H==0){value["h"]=12;}
	else if (H>12){value["h"]=H-12;}
	else {value["h"]=H;}
	value["hh"]=LZ(value["h"]);
	if (H>11){value["K"]=H-12;} else {value["K"]=H;}
	value["k"]=H+1;
	value["KK"]=LZ(value["K"]);
	value["kk"]=LZ(value["k"]);
	if (H > 11) { value["a"]="PM"; }
	else { value["a"]="AM"; }
	value["m"]=m;
	value["mm"]=LZ(m);
	value["s"]=s;
	value["ss"]=LZ(s);
	while (i_format < format.length) {
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
			}
		if (value[token] != null) { result=result + value[token]; }
		else { result=result + token; }
		}
	return result;
	}
	
// ------------------------------------------------------------------
// Utility functions for parsing in getDateFromFormat()
// ------------------------------------------------------------------
function _isInteger(val) {
	var digits="1234567890";
	for (var i=0; i < val.length; i++) {
		if (digits.indexOf(val.charAt(i))==-1) { return false; }
		}
	return true;
	}
function _getInt(str,i,minlength,maxlength) {
	for (var x=maxlength; x>=minlength; x--) {
		var token=str.substring(i,i+x);
		if (token.length < minlength) { return null; }
		if (_isInteger(token)) { return token; }
		}
	return null;
	}
	
// ------------------------------------------------------------------
// getDateFromFormat( date_string , format_string )
//
// This function takes a date string and a format string. It matches
// If the date string matches the format string, it returns the 
// getTime() of the date. If it does not match, it returns 0.
// ------------------------------------------------------------------
function getDateFromFormat(val,format) {
	val=val+"";
	format=format+"";
	var i_val=0;
	var i_format=0;
	var c="";
	var token="";
	var token2="";
	var x,y;
	var now=new Date();
	var year=now.getYear();
	var month=now.getMonth()+1;
	var date=now.getDate();
	var hh=now.getHours();
	var mm=now.getMinutes();
	var ss=now.getSeconds();
	var ampm="";
	
	while (i_format < format.length) {
		// Get next token from format string
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
			}
		// Extract contents of value based on format token
		if (token=="yyyy" || token=="yy" || token=="y") {
			if (token=="yyyy") { x=4;y=4; }
			if (token=="yy")   { x=2;y=2; }
			if (token=="y")    { x=2;y=4; }
			year=_getInt(val,i_val,x,y);
			if (year==null) { return 0; }
			i_val += year.length;
			if (year.length==2) {
				if (year > 70) { year=1900+(year-0); }
				else { year=2000+(year-0); }
				}
			}
		else if (token=="MMM"){
			month=0;
			for (var i=0; i<MONTH_NAMES.length; i++) {
				var month_name=MONTH_NAMES[i];
				if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) {
					month=i+1;
					if (month>12) { month -= 12; }
					i_val += month_name.length;
					break;
					}
				}
			if ((month < 1)||(month>12)){return 0;}
			}
		else if (token=="MM"||token=="M") {
			month=_getInt(val,i_val,token.length,2);
			if(month==null||(month<1)||(month>12)){return 0;}
			i_val+=month.length;}
		else if (token=="dd"||token=="d") {
			date=_getInt(val,i_val,token.length,2);
			if(date==null||(date<1)||(date>31)){return 0;}
			i_val+=date.length;}
		else if (token=="hh"||token=="h") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<1)||(hh>12)){return 0;}
			i_val+=hh.length;}
		else if (token=="HH"||token=="H") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<0)||(hh>23)){return 0;}
			i_val+=hh.length;}
		else if (token=="KK"||token=="K") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<0)||(hh>11)){return 0;}
			i_val+=hh.length;}
		else if (token=="kk"||token=="k") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<1)||(hh>24)){return 0;}
			i_val+=hh.length;hh--;}
		else if (token=="mm"||token=="m") {
			mm=_getInt(val,i_val,token.length,2);
			if(mm==null||(mm<0)||(mm>59)){return 0;}
			i_val+=mm.length;}
		else if (token=="ss"||token=="s") {
			ss=_getInt(val,i_val,token.length,2);
			if(ss==null||(ss<0)||(ss>59)){return 0;}
			i_val+=ss.length;}
		else if (token=="a") {
			if (val.substring(i_val,i_val+2).toLowerCase()=="am") {ampm="AM";}
			else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {ampm="PM";}
			else {return 0;}
			i_val+=2;}
		else {
			if (val.substring(i_val,i_val+token.length)!=token) {return 0;}
			else {i_val+=token.length;}
			}
		}
	// If there are any trailing characters left in the value, it doesn't match
	if (i_val != val.length) { return 0; }
	// Is date valid for month?
	if (month==2) {
		// Check for leap year
		if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
			if (date > 29){ return false; }
			}
		else { if (date > 28) { return false; } }
		}
	if ((month==4)||(month==6)||(month==9)||(month==11)) {
		if (date > 30) { return false; }
		}
	// Correct hours value
	if (hh<12 && ampm=="PM") { hh+=12; }
	else if (hh>11 && ampm=="AM") { hh-=12; }
	var newdate=new Date(year,month-1,date,hh,mm,ss);
	return newdate.getTime();
	}


// chdate



function chdate(objName){
strDate = trim(objName);
// check all of the valid formats
if (isDate(strDate,'d-MMM-yy')){
return true;
}

if (isDate(strDate,'d-MMM-yyyy')){
return true;
}

//if (isDate(strDate,'d/M/y')){
//return true;
//}

if (isDate(strDate,'yyyyMMdd')){
return true;
}

if (isDate(strDate,'d MMM yy')){
return true;
}

if (isDate(strDate,'d MMM yyyy')){
return true;
}


return false;
}


function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
//    the next multiple of 10.

//  document.writeln("<BR>Sum      = ",sum,"<BR>");
//  alert("Sum      = " + sum);

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);
}

function stripspaces(s){
var i;
var retstr="";
spacec = " ";
for (i=0;i<s.length;i++){
c = s.charAt(i);
if (spacec.indexOf(c) == -1) {
	retstr +=c; 
	}
}
return retstr;
}

function test(src) {
     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     return regex.test(src);
  }
