var chkDot = true;
var usEmail = true;
function validEmail(eAddr) 
{ 
   var lenSuffix = (usEmail) ? 4: 3;
   var goodAddr = false;
   var ndxAt = ndxDot = 0;

   ndxAt  = eAddr.indexOf("@");
   ndxDot = eAddr.indexOf(".");
   ndxDot2 = eAddr.lastIndexOf(".");

   if ( (ndxDot < 0) || (ndxAt < 0) )
      alert("Your email address lacks '.' or '@'.\n\nThe format is 'you@dom.suf'");  
   else if ( (ndxDot2 - 2) <= ndxAt)
        alert("You may be missing your domain name.\n\nThe format is 'you@dom.suf'");
   
   else if ( eAddr.length < (ndxDot2 + lenSuffix) )
      usEmail =!( confirm("You have fewer than 3 characters as a domain suffix.\nAre you sure that is right?"));
   else
      goodAddr = true;
  
   return (goodAddr);                       
}

function ValidatePhoneNow(fldTheField)
{

var iLoop;
var iLen;
var sTemp = "";
var chDigit;
var sMessage = "";
var sPhoneNo = fldTheField.value;

    // sPhoneNo = sPhoneNo.trim();      // Doesn't work in JScript
    
    // Get all the digits from the phone number
    for (iLoop = 0; iLoop <= sPhoneNo.length - 1; iLoop++)
    {
	chDigit = sPhoneNo.substring(iLoop, iLoop + 1);

        // sTemp will only contain the numbers in the original string
        if (chDigit >= "0" && chDigit <= "9" )
            sTemp = sTemp + sPhoneNo.substring(iLoop, iLoop + 1);
    }
    
    // Format the phone number or display error box
    iLen = sTemp.length;

    if (iLen == 10) 
    {
        sTemp = "(" + sTemp.substring(0, 3) + ") " + 
                      sTemp.substring(3, 6) + "-" + 
                      sTemp.substring(6, 10);
    }
    else if (iLen == 0) 
    {
        if (sPhoneNo.length > 0)
            sMessage = "This should be a valid phone number or blank.";
    }
    else 
    {
        if (iLen > 10) 
	    sMessage = "The phone number has too many digits.";
	else
	    sMessage = "The phone number has too few digits.";
    }
    
    // Set the text box value to our current digits or phone number
    fldTheField.value = sTemp 
   
    if (sMessage != "")
    {
        // Display error message, retain focus on the text box
    	alert("Invalid entry: " + sMessage);
		fldTheField.value="";
        fldTheField.focus();
    }
        
}





function submitt(theform) {

    var msg;
    msg = "";
    var radio_choice = false;
    if (theform.chkemailstatus.value == "Y") {
        document.forms[0].submit();
    }

    if (theform.chkemailstatus.value == "N") {
        document.forms[0].submit();
    }

    if (theform.chkemailstatus.value == "S") {
        if (theform.emailchk.value == "") {
            msg = msg + " Please enter your Email address \n";
            theform.chkemailstatus.value = "A";
        }
        if (msg != "") {
            alert(msg);

            return false;

        } else {
            document.forms[0].submit();
        }

    }
    if (theform.chkemailstatus.value == "A") {


        var ccard_fee = theform.card_fee;

        if (theform.spot_left.value < 1) {
            msg = "Selected course is SOLD OUT, please select a different course";

        }
        else {
            if (theform.fchoice.value == "") {
                msg = msg + " Please select the course you want to enroll \n";
            }
            if (theform.ls_first.value == "") {
                msg = msg + " Please enter your First Name \n";
            }
            if (theform.ls_last.value == "") {
                msg = msg + " Please enter your Last Name \n";
            }
            if (theform.ls_email.value == "") {
                msg = msg + " Please enter your Email address \n";
            }
            if (theform.ls_phone.value == "") {
                msg = msg + " Please enter your Home Phone number \n";
            }
            if (theform.ls_address.value == "") {
                msg = msg + " Please enter your Address \n";
            }
            if (theform.ls_city.value == "") {
                msg = msg + " Please enter your City \n";
            }
            if (theform.ls_zip.value == "") {
                msg = msg + " Please enter your Zip Code \n";
            }
            //		if (theform.sle_age.value == "")
            //		{
            //			msg = msg + " Please Select your Age \n";
            //		}
            if (theform.sle_dob.value != "") {
                if (isDate(theform.sle_dob.value) == false)
                    msg = msg + " Please enter valid Date of Birth \n";
            }
            if (theform.bill_ls_first.value == "") {
                msg = msg + " Please enter your Billing First Name \n";
            }
            if (theform.bill_ls_last.value == "") {
                msg = msg + " Please enter your Billing Last Name \n";
            }
            if (theform.bill_ls_address.value == "") {
                msg = msg + " Please enter your Billing Address \n";
            }
            if (theform.bill_ls_city.value == "") {
                msg = msg + " Please enter your Billing City \n";
            }

            if (theform.bill_ls_zip.value == "") {
                msg = msg + " Please enter your Billing Zip Code \n";
            }

           // for (counter = 0; counter < theform.student.length; counter++) {
           //     if (theform.student[counter].checked) {
           //         if (counter == 0) {
           //             var schitem = document.forms[0].school_name.selectedIndex;
           //             var schvalue = document.forms[0].school_name.options[schitem].value;
           //             if (schvalue == '(none)') {
           //                 msg = msg + " Please select the School \n";
           //             }
           //         }
           //         radio_choice = true;
           //     }
          //  }
            //if (!radio_choice) {
           //     msg = msg + " Please select the Fee \n";
          //  }

            for (counter = 0; counter < theform.card_type.length; counter++) {
                if (theform.card_type[counter].checked) {
                    radio_choice = true;
                }
            }
            if (!radio_choice) {
                msg = msg + " Please select the Credit Card Type\n";
            }
//alert(theform.sel_amt);
            if (theform.sel_amt.value == "") {
                msg = msg + " Please enter your Course Fee\n";
            } else {
                var js_amt;
                js_amt = theform.sel_amt.value;
                if (js_amt < 75 || js_amt > 495) {
                    msg = msg + " Please correct Course Fee\n";
                }

            }
            if (theform.card_no.value == "") {
                msg = msg + " Please enter Credit Card Number \n";
            }
            var tmsg
            var cc_month = theform.card_month.options[theform.card_month.selectedIndex].value;
            var cc_year = theform.card_year.options[theform.card_year.selectedIndex].value;
            tmsg = validateCard(theform.card_no.value, theform.card_type.value, cc_month, cc_year)
            if (tmsg != " ") {
                msg = msg + tmsg
            }
        }
        if (msg != "") {
            alert(msg);
            document.forms[0].cont.value = "edit";
            return false;
        } else {
            //			document.f_signup.arg_action.value = 'S';
            document.forms[0].submit();
            //			return true;
        }
    } // S end if
}


	function conti(ctypee)
	{
		if (ctypee == 1){
			document.forms[0].cont.value = "continue";
		}else if (ctypee == 2){
			document.forms[0].cont.value = "edit";
		}else if (ctypee == 3){
			document.forms[0].cont.value = "enroll";
		}		
	}

function mod10( cardNumber ) { // LUHN Formula for validation of credit card numbers.
	var ar = new Array( cardNumber.length );
	var i = 0,sum = 0;


    	for( i = 0; i < cardNumber.length; ++i ) {
    		ar[i] = parseInt(cardNumber.charAt(i));
    	}
    	for( i = ar.length -2; i >= 0; i-=2 ) { // you have to start from the right, and work back.
    		ar[i] *= 2;							 // every second digit starting with the right most (check digit)
    		if( ar[i] > 9 ) ar[i]-=9;			 // will be doubled, and summed with the skipped digits.
    	}										 // if the double digit is > 9, ADD those individual digits together 


        	for( i = 0; i < ar.length; ++i ) {
        		sum += ar[i];						 // if the sum is divisible by 10 mod10 succeeds
        	}
        	return (((sum%10)==0)?true:false);	 	
    }


        function expired( month, year ) {
		var val;
		var val2
        	var now = new Date();							// this function is designed to be Y2K compliant.
        	var expiresIn = new Date(year,month-1,28,0,0);		// create an expired on date object with valid thru expiration date
//			alert(expiresIn);
//        	expiresIn.setMonth(expiresIn.getMonth()+1);		// adjust the month, to first day, hour, minute & second of expired month
//			alert(expiresIn);
//        	expiresIn.setDate(expiresIn.getDay()-1);		// adjust the month, to first day, hour, minute & second of expired month
//			alert(expiresIn.getTime());
//			alert(now.getTime());
//			alert(expiresIn);
			val  = expiresIn.getTime()
			val2 = now.getTime()
        	if( val  < val2 ) 
			{
				return false;
			}
			else
			{
	        	return true;									// then we get the miliseconds, and do a long integer comparison
			}
    }


        function validateCard(cardNumber,cardType,cardMonth,cardYear) {

        	if( cardNumber.length == 0 ) {						//most of these checks are self explanitory
        		return " Please enter a valid card number.";				
        	}
        	for( var i = 0; i < cardNumber.length; ++i ) {		// make sure the number is all digits.. (by design)
        		var c = cardNumber.charAt(i);


            		if( c < '0' || c > '9' ) {
            			return " Please use only digits. do not use spaces or hyphens.";
            		}
            	}
            	var length = cardNumber.length;			//perform card specific length and prefix tests

                	switch( cardType ) {
                   		case 'Mastercard':
                  			if( length != 16 ) {
                   				return " Please enter a valid MasterCard number."; 
                   			}
                   			var prefix = parseInt( cardNumber.substring(0,2));
                       			if( prefix < 51 || prefix > 55) {
                                  	return " Please enter a valid MasterCard Card number.";
                                }
                                break;
                        case 'Visa':
	                            if( length != 16 && length != 13 ) {
                                	return " Please enter a valid Visa Card number.";
                                }
                                	var prefix = parseInt( cardNumber.substring(0,1));
                                    if( prefix != 4 ) {
                                    	return " Please enter a valid Visa Card number.";
                                    }
                                break;
                                }
                                if( !mod10( cardNumber ) ) { 		// run the check digit algorithm
                                	return " Sorry! this is not a valid credit card number.";
                                }
                                if( !expired( cardMonth, cardYear ) ) {							// check if entered date is already expired.
                                	return " Sorry! The expiration date you have entered would make this card invalid.";
                                }
                                return " "; // at this point card has not been proven to be invalid
                                   }
                                            //-->

var dtCh= "/";
var minYear=1940;
var maxYear=2000;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
//		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
//		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
//		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
//		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
//		alert("Please enter a valid date")
		return false
	}
return true
}


