//@BEGINVERSIONINFO
//@APPVERSION: 1.0
//@FILENAME: stValidateForm.asp 
//@DESCRIPTION: Checks for form input errors
//@ENDVERSIONINFO

var submitcount = 0;

function valMember(form){
	return stCheck(form);
}

function valMe(form){
	if (submitcount > 0){
		open("PleaseWait.htm", "", "height=100, width=400, top=400, left=300, menubar=no, resizeable=no, status=no, titlebar=no, toolbar=no");
		return false;
	}
	if (stCheck(form)){
		submitcount++;
		return true;
	} else {
		return false;
	}
}

function specialCase(e, form) {
	if ((e.name == "CardName")||(e.name == "CardNumber")||(e.name == "CardExpiryMonth")||(e.name == "CardExpiryYear")) {
		if (((form.CardName.value.length <= 0)||(form.CardNumber.value.length <= 0)||(form.CardExpiryMonth.value.length <= 0)||(form.CardExpiryYear.value.length <= 0))
		 && ((form.CardName.value.length > 0)||(form.CardNumber.value.length > 0)||(form.CardExpiryMonth.value.length > 0)||(form.CardExpiryYear.value.length > 0))) {
			return "Please enter all Credit Card Information.";
		}
		if ((form.CardName.value.length > 0)&&(form.CardNumber.value.length > 0)&&(form.CardExpiryMonth.value.length > 0)&&(form.CardExpiryYear.value.length > 0)) {
			if (!isCardDateValid(form.CardExpiryYear.value, form.CardExpiryMonth.value)) {
				return "The Credit Card has Expired.";
			}
			if (isCardNumValid(form.CardNumber.value)) {
				return "The Credit Card Number is an invalid format.";
			}
		}
	}
	return "";
}

function stripChar(sValue, sChar) {
	var i, tempChar, buildString;
	buildString = ""
	for (var i=0; i<sValue.length; i++) {
		tempChar = sValue.charAt(i);
		if (tempChar != sChar) {
			buildString = buildString + tempChar;
		}
	}
	return buildString;
}

function isCardDateValid(year, month) {
	var dateCheck, now;
	if (year.length == 2) {
		if (parseInt(year) < 50) {
			year = "20" + year;
		}
	}
	now = new Date();
	dateCheck = new Date(year, month);
	if (now > dateCheck) {
		return false;
	}
	else {
		return true;
	}
}

function isCardNumValid(num) {
	var num1, num2, tempNum;
	if (!isNumber(num)) {
		return true;
	}
	num1 = ""
	if (!(num.length%2==0)) {
		for(var j=0; j < num.length; j++) {
			if ((j+1)%2==0){
				tempNum = 2 * num.charAt(j);
			}
			else {
				tempNum = 1 * num.charAt(j);
			}
			num1 = num1 + tempNum.toString();
		}
	}
	else{
		for(var j=0; j < num.length; j++){
			if ((j+1)%2==0){
				tempNum = 1 * num.charAt(j);
			}
			else{
				tempNum = 2 * num.charAt(j);
			}
			num1 = num1 + tempNum.toString();
		}
	}
	num2 = 0;
	for (var j = 0; j < num1.length; j++) {
		num2 = num2 + parseInt(num1.charAt(j));
	}
	if (num2%10==0) {
		return false;
	}
	else {
		return true;
	}
}

function isNumber(value) {
	for (var i=0; i < value.length; i++) {
		a = parseInt(value.charAt(i));
		if (isNaN(a)) {
			return false;			
			break;
		}
	}
	return true;
}

function stCheck(form) {
	var e, title, empty_fields, char_check, invalid_card, month, year, invalid_date, eMail, invalid_eMail 
	var iQuantity, quantity_check, checkSpecial, tempError, special_Error, msg, upperLine, lowerLine
	var num, invalid_phoneNumber, passwd_mismatch, invalid_Year, invalid_Length
	msg = "";
	empty_fields = "";
	char_check = "";
	special_Error = "";
	tempError = "";
	num = form.length
	for (var i = 0; i < form.length; i++) {
		e = form.elements[i]
		if ((e.title == null)||(e.title == "")) {
			title = e.name;
		}
		else {
			title = e.title;
		}
		if ((e.type == "text" || e.type == "textarea" || e.type == "password") && !e.special && !e.disabled) {
			if (e.value.length <= 0 && !e.optional && e.name.indexOf("homeport_other_type") == -1) {
				empty_fields += "\n            " + title;
				continue;
			}

			if (e.number) {
				num = e.value;
				num = stripChar(num, ".");
				num = stripChar(num, ",");
				num = stripChar(num, "-");
				num = stripChar(num, "(");
				num = stripChar(num, ")");
				if (!isNumber(num)) {
					char_check += "\n             " + title;
					continue;
				}
			}

			if (title == "Vessel Year") {
				num = e.value;
				num = stripChar(num, "'");
				num = stripChar(num, ",");
				if (!isNumber(num)) {
					char_check += "\n             " + title;
					continue;
				}
				if (e.value.length != 4) {
					invalid_Year = true;
					continue;
				}
			}

			if (title == "Vessel Length") {
				num = e.value;
				if (!isNumber(num)) {
					invalid_Length = true;
					continue;
				}
			}

			if (e.creditCardNumber) {
				e.value = stripChar(e.value, " "); 
				e.value = stripChar(e.value, "-"); 
				invalid_card = isCardNumValid(e.value);
			}

			if ((e.creditCardExpMonth)||(e.creditCardExpYear)) {
				if (e.creditCardExpMonth) {
					month = e.value;
					month = stripChar(month, " ")
					if (!isNumber(month)) {
						invalid_date = true;
						month = null;
					}
				}
				if (e.creditCardExpYear) {
					year = e.value;
					year = stripChar(year, " ")
					if (!isNumber(year)) {
						invalid_date = true;
						year = null;
					}
				}
				if ((month != null) && (year != null)) {
					if(!isCardDateValid(year, month)) {
						invalid_date = true;
					}	
				}
			}
			if (e.eMail) {
				eMail = e.value;
				if ((eMail.indexOf("@") != -1) && (eMail.indexOf(".") != -1)) {
					invalid_eMail = false;
				}
				else {
					invalid_eMail = true;
				}
			}
            if (e.name == "txtEmail") {
				eMail = e.value;
				if ((eMail.indexOf("@") != -1) && (eMail.indexOf(".") != -1)) {
					invalid_eMail = false;
				}
				else {
				  
					invalid_eMail = true;
				}
			}	

			if (e.phoneNumber) {
				num = e.value;
				num = stripChar(num, " ");
				num = stripChar(num, "-");
				num = stripChar(num, "+");
				if (num.length < 10) {
					invalid_phoneNumber = true;
				}	
			}

			if (e.homeportOtherType && form.homeport.value=="other" && e.value=="") {
				empty_fields += "\n            " + title;
				continue;
			}

			if (e.homeportMarinaName && form.homeport.value=="marina" && e.value=="") {
				empty_fields += "\n            " + title;
				continue;
			}
  		}
		if (e.creditCardType && e.value == "Select Type") {
			empty_fields += "\n            " + title;
			continue;
		}

		if (e.creditCardExpMonth && e.value == "Select Month") {
			empty_fields += "\n            " + title;
			continue;
		}

		if (e.creditCardExpYear && e.value == "Select Year") {
			empty_fields += "\n            " + title;
			continue;
		}

 		if (e.quantityBox) {
			iQuantity = e.value;
			if (!isNumber(iQuantity)) {
				quantity_check = true;
			}
			if (parseInt(iQuantity) < 0) {
				quantity_check = true;
			}
			if ((iQuantity) < 1) {
				quantity_check = true;
			}

		}
		if (e.password) {
			if (form.Password.value != form.Password2.value) {
					passwd_mismatch = true;
			}
		}
		if (e.special) {
			checkSpecial = specialCase(e, form);
			if (tempError != checkSpecial) {
				special_Error = special_Error + checkSpecial
			}
			tempError = checkSpecial;
		}
		if (e.type == "select-one" && !e.optional) {
			if (e.value == "" || e.value == "none") {
				empty_fields += "\n            " + title;
				continue;
			}
		}
	}
	
	if (!empty_fields && !char_check && !special_Error && !invalid_card && !invalid_date && !invalid_eMail && !quantity_check && !invalid_phoneNumber && !passwd_mismatch) {return true}
	
	msg = "The form was not submited due to the following error(s).\n";
	
	upperLine = "\n_________________________________________________________\n\n";
	lowerLine = "_________________________________________________________\n";
	
	if (empty_fields) {
		msg += upperLine;
		msg += "The following field(s) must be filled in:\n";
		msg += lowerLine;
		msg += empty_fields;
	}
	if (char_check) {
		msg += upperLine;
		msg += "The following field(s) need a numeric value:\n";
		msg += lowerLine;
		msg += char_check;
	}
	if (quantity_check) {
		msg += upperLine;
		msg += "Please enter a Positive Integer.\n"
		msg += lowerLine;
	}
	if (invalid_card) {
		msg += upperLine;
		msg += "The Credit Card Number is invalid.\n";
		msg += lowerLine;
	}
	if (invalid_date) {
		msg += upperLine;
		msg += "The Credit Card has expired.\n";
		msg += lowerLine;
	}
	if (invalid_eMail) {
		msg += upperLine;
		msg += "The Email Address is in an invalid format.\n";
		msg += lowerLine;
	}
	if (invalid_phoneNumber) {
		msg += upperLine;
		msg += "Please enter valid phone number(s) with area code.\n";
		msg += lowerLine;
	}
	if (invalid_Year) {
		msg += upperLine;
		msg += "Please enter a valid four-digit Vessel Year (e.g. 1998).\n";
		msg += lowerLine;
	}
	if (invalid_Length) {
		msg += upperLine;
		msg += "Please enter the Vessel Length in full feet, numbers only (e.g. 25).\n";
		msg += lowerLine;
	}
	if (special_Error) {
		msg += upperLine;
		msg += special_Error + "\n";
		msg += lowerLine;
	}
	if (passwd_mismatch) {
		msg += upperLine;
		msg += "Your passwords did not match. Please enter them again.\n";
		msg += lowerLine;
	}		
	alert(msg);
	return false;
}	

//********************************************************************
// Credit Card Number Validator Written By: Lokesh
//                                   Date : 04/08/2004
// Uses Linh mod-10 formula to determine the validity and then
// calls different functions to check what category the number belongs
// to.
//********************************************************************

var invalid = "Invalid";
//********************************************************************
// Check the Linh Mod-10 validity

function CheckCreditCardNum(num)
{
  var len=num.length;
  var first;
  var sum=0;
  var mul=1;
  var i;
  var numAtI;
  var tempSum;
  var theway;
  var txtcard;
  theway = "";
  for(i=(len-1); i >= 0 ;--i)
  {
   numAtI = parseInt(num.charAt(i));
   tempSum = numAtI * mul;
   theway += numAtI +" x " + mul +"=" + tempSum;
   if(tempSum > 9)
   {
    first = tempSum % 10;
    theway += " => "+ 1 +" + " + first +"=";
    tempSum =  1 +  first;
    theway += tempSum +" ";
   }
   theway += "\n";
   sum += tempSum;

   if (mul == 1)
     mul++;
   else
     mul--;
  }
  theway += "----------\nSum:   "+ sum +"\n";


//*********************************************************
// Comment this line if you want to see the calculation

 
 theway = "Checking for Card Vallidity\n";
  if(sum % 10)
  {
   if (len == 15)
   {
   	txtcard = CheckFor15(num,0);
	if ( txtcard == invalid )
	{
	   alert(theway +"The number: "+ num +" is not valid!! ");
           return false;
        }
   }
   else
   {
    alert(theway +"The number: "+ num +" is not valid!! ");
    return false;
   }
  }

  {
    switch(len)
    {
	case 13:
	   txtcard = CheckFor13(num);
	   break;
	case 14:
	   txtcard = CheckFor14(num);
	   break;
	case 15:
	   txtcard = CheckFor15(num,1);
	   break;
	case 16:
	   txtcard = CheckFor16(num);
	   break;
	default:
	   txtcard = invalid;
    }
    if(txtcard == invalid)
    {
	   alert(theway +"The number: "+ num +" is not valid!! ");
   	   return false;
    }
    else
    theway+= "The number: "+ num +" is valid!! \nand it looks like it is: "+ txtcard;
  }
  alert(theway);
  return true;
} 

//***********************************************************************************
// Checks for Visa..
function CheckFor13(number)
{
  if(parseInt(number.charAt(0)) == 4 )
	return "Visa";
  return invalid;
}

//************************************************************************************
// Check for Diners Club
function CheckFor14(number)
{
  if( parseInt(number.charAt(0)) != 3 )
	return invalid;
  if( (parseInt(number.charAt(1)) == 6) || (parseInt(number.charAt(1)) == 8) )
	return "Diners Club/Carte Blanche"
  if( !(parseInt(number.charAt(1)) ))
	if( (parseInt(number.charAt(2)) >= 0) && (parseInt(number.charAt(2)) <= 5))
		return "Diners Club/Carte Blanche"
  return invalid;
}

//************************************************************************************
// Check for American Express, enRoute, JCB
function CheckFor15(number, chec)
{
    if ( (number.charAt(0) == 3) && ( (number.charAt(1) == 4)||(number.charAt(1) == 7) ) && chec)
	return "American Express";
    var FirstFour =  parseInt(number.charAt(0))*1000;
	FirstFour += parseInt(number.charAt(1))*100;
	FirstFour += parseInt(number.charAt(2))*10;
	FirstFour += parseInt(number.charAt(3));
    //alert(FirstFour);
    if( (FirstFour == 2014) || (FirstFour == 2149) )
	return "enRoute";

    if( ((FirstFour == 2131) || (FirstFour == 1800)) && chec )
	return "JCB";

    return invalid;
}

//************************************************************************************
// Check for Visa, MasterCard, Discover or JCB
function CheckFor16(number)
{
  var a = parseInt(number.charAt(0));
  var b = parseInt(number.charAt(1));
  //alert (a);
  switch (a)
  {
    case 5:
        if((b > 0)&& (b < 6))
		return "MasterCard";
	else
		return invalid;
	break;
   case 4:
	return "Visa";
	break;
   case 6:
	if ( (b == 0) && ( parseInt(number.charAt(2)) == 1) && ( parseInt(number.charAt(3)) == 1))
  		return "Discover";
	else
		return invalid;
	break;
   case 3:
	return "JCB";
	break;
   default:
	return invalid;
	break;
  }
}

