<!--Filenme:regForm_valid.js-->
<!--Date : 13/03/03-->
<!--Created by: Ellie Teh-->
<!--Purpose :registration form validation-->

var invalid = " "; // Invalid character is a space

function FNameOK()
{
  	if (document.myForm.FName.value == "")
   {
             alert("Enter Full Name, please");
             document.myForm.FName.focus()
             return false;
   }
}

function FICOK(){
	if (document.myForm.FIC.value == ""){
		alert('Enter IC No, please')
		document.myForm.FIC.focus();
  		return false;
	}
}


function CheckInt(string,length) {
	var valid="0123456789"

    if (string.length < length) {
       return false;
    }
    
    for (var i=0; i<length; i++) {
        if (valid.indexOf(string.charAt(i)) < 0) {
            return false;
        }
    }

    return true;
}

function FDOBOK(){
	if ((document.myForm.mDOB.value == "NS")&&(document.myForm.dDOB.value == "NS")&&(document.myForm.yDOB.value == "")){
		alert("Enter Date Of Birth, Please")	
		document.myForm.dDOB.focus();
		return false;
	}
	else{
		if (CheckDate(document.myForm.dDOB.value,document.myForm.mDOB.value,document.myForm.yDOB.value)== false)
		{
			alert('invalid date');
			document.myForm.dDOB.focus();
			return false;
		}
    }
             return true;
}

function PartnerDOBOK(){
	if ((document.myForm.mPartner_DOB.value == "NS")&&(document.myForm.dPartner_DOB.value == "NS")&&(document.myForm.yPartner_DOB.value == "")){
		return true;
	}
	else{
		if (CheckDate(document.myForm.dPartner_DOB.value,document.myForm.mPartner_DOB.value,document.myForm.yPartner_DOB.value)== false)
		{
			alert('invalid date');
			document.myForm.dPartner_DOB.focus();
			return false;
		}
    }
             return true;
}

function CheckDate(dd,mm,yyyy)
	{		
		var FIELDVALUE;
		FIELDVALUE = dd+"/"+mm+"/"+yyyy
		var normalyear = new Array(99,31, 28, 31, 30, 31, 30, 
								   31, 31, 30, 31, 30, 31);
			specialyear = new Array(99,31, 29, 31, 30, 31, 30, 
									31, 31, 30, 31, 30, 31);

		if (FIELDVALUE.length != 10 || isNaN(Date.parse(FIELDVALUE)) || FIELDVALUE.charAt(2)!='/' || FIELDVALUE.charAt(5)!='/')
		   {
		   return(false);
		   }
		dd = parseFloat(FIELDVALUE.substring(0,2))
		mm = parseFloat(FIELDVALUE.substring(3,5))
		yyyy=parseFloat(FIELDVALUE.substring(6,10))
		yearind=yyyy/4
		daylimit = (Math.ceil(yearind) > yearind) ? normalyear[mm] : specialyear[mm]
		if (yyyy < 1900 || mm<1 || mm>12 || dd<1 || dd>daylimit)
		   {
			   return(false);
		   }
		return(true);
	}


function FSexOK()
{
	if ((document.myForm.FSex[0].checked == false)&&(document.myForm.FSex[1].checked == false)) {
		alert('Select Gender, please');
       return false;
   }
   return true;
}

function FNationalOK()
{
  	if (document.myForm.FNational.value == "NS")
   {
             alert("Select Nationality, please");
             document.myForm.FNational.focus()
             return false;
   }
}

function FAddr1OK()
{
  	if (document.myForm.FAddr1.value == "")
   {
             alert("Enter Address 1, please");
             document.myForm.FAddr1.focus()
             return false;
   }
}

function FTerritoryOK()
{
  	if (document.myForm.FTerritory.value == "NS")
   {
             alert("Select Territory, please");
             document.myForm.FTerritory.focus()
             return false;
   }
}

function FTelOK()
{
  	if (document.myForm.FTelH.value == "")
   {
             alert("Enter Tel No(H), please");
             document.myForm.FTelH.focus()
             return false;
   }
}


function isEmail(emailStr) {

    if (emailStr.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}

function FEmailOK() {

var emailStr = document.myForm.FEmail.value

	 if (emailStr == "")
	{
	   	alert("Enter Email Address, please")
      	document.myForm.FEmail.focus()
		return false;
	}

    if (isEmail(emailStr) == false) {
        alert("Invalid email address");
        document.myForm.FEmail.focus()
        return false;
    }
    	return true;
}
         
function FBankerOK()
{
  	if (document.myForm.FBanker.value == "NS")
   {
             alert("Select Bank Name, please");
             document.myForm.FBanker.focus()
             return false;
   }
   else if (document.myForm.FBanker.value == "")
   {
		if (document.myForm.FACNo.value != "")
		{
			 alert("Select Bank Name, please");
             document.myForm.FBanker.focus()
             return false;
		}	
   }
   else if (document.myForm.FBanker.value != "")
   {
		if (document.myForm.FACNo.value == "")
		{
			 alert("Insert account no, please");
             document.myForm.FACNo.focus()
             return false;
		}	
   }
}

function FPaymentOK()
{
  	if (document.myForm.FPaymentID.value == "NS")
   {
             alert("Select Payment Type, please");
             document.myForm.FPaymentID.focus()
             return false;
   }
  	if (CheckInt(document.myForm.FPayment1.value, 4) == false)
   {
             alert("Enter Credit Card No, please");
             document.myForm.FPayment1.focus()
             return false;
   }
  	if (CheckInt(document.myForm.FPayment2.value, 4) == false)
   {
             alert("Enter Credit Card No, please");
             document.myForm.FPayment2.focus()
             return false;
   }
  	if (CheckInt(document.myForm.FPayment3.value, 4) == false)
   {
             alert("Enter Credit Card No, please");
             document.myForm.FPayment3.focus()
             return false;
   }
  	if (CheckInt(document.myForm.FPayment4.value, 4) == false)
   {
             alert("Enter Credit Card No, please");
             document.myForm.FPayment4.focus()
             return false;
   }
  
   	if (document.myForm.mExpiry.value == "NS")
   {
             alert("Select Expiry Date, please");
             document.myForm.mExpiry.focus()
             return false;
   }
   	if (document.myForm.yExpiry.value == "NS")
   {
             alert("Select Expiry Date, please");
             document.myForm.yExpiry.focus()
             return false;
   } 
   if (CheckInt(document.myForm.FSecurityCode.value, 3) == false)
   {
             alert("Enter Security Code, please");
             document.myForm.FSecurityCode.focus()
             return false;
   }
  	if (document.myForm.FCCName.value == "")
   {
             alert("Enter Name On Credit Card, please");
             document.myForm.FCCName.focus()
             return false;
   }
  	if (document.myForm.FShipAdd1.value == "")
   {
             alert("Enter Shipping address 1, please");
             document.myForm.FShipAdd1.focus()
             return false;
   }
}
       
function LoginInfoOK()
{
	if (document.myForm.FPassword.value == "")
   {
             alert("Enter Password, please");
             document.myForm.FPassword.focus()
             return false;
   }
   if (document.myForm.ConPassword.value == "")
   {
             alert("Re-type Password, please");
             document.myForm.ConPassword.focus()
             return false;
   }
}

function FPasswordOK()
{
	if (document.myForm.FPassword.value.length<6)
		{
			alert("Password must be at least six (6) characters long.")
			document.myForm.FPassword.focus();
          return false;
		}

	// check for spaces
	if (document.myForm.FPassword.value.indexOf(invalid) > -1) 
		{
			alert("Sorry, spaces are not allowed.");
			return false;
		}
	else
		{

			if (document.myForm.FPassword.value != document.myForm.ConPassword.value)
       		  {
		             alert("Your passwords did not match, please try again.");
       		      document.myForm.FPassword.focus();
		             return false;
       		  }
       }
		    return true;
}

function ChangePWOK()
{
	if (document.myForm.OldPassword.value == "")
   {
             alert("Enter Current Password, please");
             document.myForm.OldPassword.focus()
             return false;
   }

	if (LoginInfoOK() == false)
  	{
  		return false;
  	}

	if (FPasswordOK() == false)
  	{
  		return false;
  	}
  	document.myForm.submit();
  	return false;
}

function PInfoOK()
{
  	if (FNationalOK() == false)
  	{
  		return false;
  	}
  	//if (FEmailOK() == false)
  	//{
  		//return false;
  	//}
  	if (FAddr1OK() == false)
  	{
  		return false;
  	}
  	if (FTerritoryOK() == false)
  	{
  		return false;
  	}

  	if (FTelOK() == false)
  	{
  		return false;
  	}

  	if (PartnerDOBOK() == false)
  	{
  		return false;
  	}
  	document.myForm.submit();
  	return false;
}

function BInfoOK()
{
  	if (FBankerOK() == false)
  	{
  		return false;
  	}
  	document.myForm.submit();
  	return false;
}

function subForm()
{
    if (FNameOK() == false)
  	{
  		return false;
  	}
  	if (FICOK() == false)
  	{
  		return false;
  	}
  	if (FDOBOK() == false)
  	{
  		return false;
  	}
    if (FSexOK() == false)
  	{
  		return false;
  	}
  	if (FNationalOK() == false)
  	{
  		return false;
  	}
  	if (FEmailOK() == false)
  	{
  		return false;
  	}
  	if (FAddr1OK() == false)
  	{
  		return false;
  	}
  	if (FTerritoryOK() == false)
  	{
  		return false;
  	}

  	if (FTelOK() == false)
  	{
  		return false;
  	}

  	if (PartnerDOBOK() == false)
  	{
  		return false;
  	}
  	//8/4/2009 comment by ida as bank info is not compulsory
  	/*if (FBankerOK() == false)
  	{
  		return false;
  	}*/
	if (LoginInfoOK() == false)
  	{
  		return false;
  	}
	if (FPasswordOK() == false)
  	{
  		return false;
  	}
  	if (FPaymentOK() == false)
  	{
  		return false;
  	}
  	document.myForm.submit();
  	return false;
}




/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: David Leppek :: https://www.azcode.com/Mod10

Basically, the alorithum takes each digit, from right to left and muliplies each second
digit by two. If the multiple is two-digits long (i.e.: 6 * 2 = 12) the two digits of
the multiple are then added together for a new number (1 + 2 = 3). You then add up the 
string of numbers, both unaltered and new values and get a total sum. This sum is then
divided by 10 and the remainder should be zero if it is a valid credit card. Hense the
name Mod 10 or Modulus 10. */
function Mod10(ccNumb) {  // v2.0
var valid = "0123456789"  // Valid digits in a credit card number
var len = ccNumb.length;  // The length of the submitted cc number
var iCCN = parseInt(ccNumb);  // integer of ccNumb
var sCCN = ccNumb.toString();  // string of ccNumb
sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
var iTotal = 0;  // integer total set at zero
var bNum = true;  // by default assume it is a number
var bResult = false;  // by default assume it is NOT a valid cc
var temp;  // temp variable for parsing string
var calc;  // used for calculation of each digit

// Determine if the ccNumb is in fact all numbers
for (var j=0; j<len; j++) {
  temp = "" + sCCN.substring(j, j+1);
  if (valid.indexOf(temp) == "-1"){bNum = false;}
}

// if it is NOT a number, you can either alert to the fact, or just pass a failure
if(!bNum){
  /*alert("Not a Number");*/bResult = false;
}

// Determine if it is the proper length 
if((len == 0)&&(bResult)){  // nothing, field is blank AND passed above # check
  bResult = false;
} else{  // ccNumb is a number and the proper length - let's see if it is a valid card number
  if(len >= 15){  // 15 or 16 for Amex or V/MC
    for(var i=len;i>0;i--){  // LOOP throught the digits of the card
      calc = parseInt(iCCN) % 10;  // right most digit
      calc = parseInt(calc);  // assure it is an integer
      iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
      i--;  // decrement the count - move to the next digit in the card
      iCCN = iCCN / 10;                               // subtracts right most digit from ccNumb
      calc = parseInt(iCCN) % 10 ;    // NEXT right most digit
      calc = calc *2;                                 // multiply the digit by two
      // Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
      // I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
      switch(calc){
        case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
        case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
        case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
        case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
        case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
        default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
      }                                               
    iCCN = iCCN / 10;  // subtracts right most digit from ccNum
    iTotal += calc;  // running total of the card number as we loop
  }  // END OF LOOP
  if ((iTotal%10)==0){  // check to see if the sum Mod 10 is zero
    bResult = true;  // This IS (or could be) a valid credit card number.
  } else {
    bResult = false;  // This could NOT be a valid credit card number
    }
  }
}
// change alert to on-page display or other indication as needed.
if(bResult) {
//  alert("This IS a valid Credit Card Number!");
}
if(!bResult){
 // alert("This is NOT a valid Credit Card Number!");
}
  return bResult; // Return the results
}