/*** 
**********************************************************
JavaScript functions for: com232, com233, com24, com241
Created by Lars Henrik Rotnes
Last modified: 2002-10-10
**********************************************************
**/


/*
CVS Version : $Id: com.js,v 1.23 2003/07/22 14:44:10 fredde Exp $

*/

/**
*Functions in this file
*
*function init()                                                        -> used for initialize fields with drop down boxes for expire date info
*function validateQuantity()    -> Function that validates the quantity field on this form
*function validatePayment()             -> Function that validates payment the field on this form
*function validateFamilyNo()  -> Validates family no field on a form
*printPage()                                                                    -> Funcation that open the print dialog of the browser
**/

/**
*This function populates the year and date fields on templates 
*that needs information about expire date of credit cards
**/
function init(){
        var today = new Date();
        var iYear = today.getFullYear();
        var iMonth = today.getMonth();
        
        //netscape 6 && ie 5>
        if (document.getElementById){
          tmpYear = document.getElementById('expYear');
          tmpDate = document.getElementById('expDate');
        }else if (document.all){ //Explorer 4
          tmpYear = document.all['expYear'];
          tmpDate = document.all['expDate'];
        }
        else{ 
                alert (js_fn_NOT_VALID_BROWSER);
                return false;
        }
        
        for(var i = 0 ; i < 5; i++ ){
                        tmpYear.options[i] = new Option(iYear,iYear);
                        iYear++;
        }
        
        tmpDate.selectedIndex = iMonth;
        tmpYear.selectedIndex = 0;
}




/**
*Function that validates the field on this form
*
*argFieldId, string = the id to the field that will be validated
*argForm = the form to be validated
*optName = string that may contain errormessage
*argLocale string 	=  info about locale, to distinguish between different countries(some country validation requires sepcial logic)
**/
function validateForm(argForm, optName, argLocale){
  	//check expire date and payment method field
	//netscape 6 && ie 5>
	if (document.getElementById){
	  tmpPayment = document.getElementById('payMethod');
	  tmpCardNo     = document.getElementById('creditCardNo');
	}else if (document.all){ //Explorer 4
	 	tmpPayment = document.all['payMethod'];
	 	tmpCardNo       = document.all['creditCardNo'];
	}
 	else{ 
 		alert (js_fn_NOT_VALID_BROWSER);
 		return false;
 	}

//	TO BE UPDATED TO VALIDATE THE ADDRESS INFORMATION IN THE SAME WAY AS EDITPROFILE PAGE DOES.
//	THE VALIDATION WILL ONLY TAKE PLACE IF 
//	BILLING ADDRESS FIELDS EXISTS AND IF SOMETHING IS WRITTEN IN ONE OF THE BILLING ADDRESS FIELDS.


	if ( (argForm.name) && (
		(document.getElementById('name').value != "") || 
		(document.getElementById('address1').value != "") ||
		(document.getElementById('address2').value != "") ||
		(document.getElementById('city').value != "") ||
		(document.getElementById('state').value != "") ||
		(document.getElementById('zip').value != "") ||	
		(document.getElementById('phoneNo').value != "")							
		) ) {

//		UPDATE THIS JAVASCRIPT TO CHECK FOR THE RIGHT ID:S!!! VERIFY THAT THIS IS OK WITH EDITPROFILE TO NOT CAUSE PROBLEMS THERE!!		


		if(!checkAddressForm(argForm, optName, argLocale)){
			return false;	
		}
	}
	
 	//everything is ok
 	return true;
}
//end validateField function






/**
*Function that validates the quantity field on this form
*
*argFieldId, string = the id to the field that will be validated
*optName        string          = Optional, description of field, e.g. Quantity (optional)
*                                                               Not provided -> No alertbox
*argCommand                                     = command to return from function if everything is ok
**/
function validateQuantity(argFieldId, argCommand, optName ){
  //get the quantity field on this form
  
  var sNumber = ""; //variable to store the value of the field
  
  //Netscape 6 and ie 5>
        if (document.getElementById){
         tmp = document.getElementById(argFieldId); 
                 sNumber = tmp.value;
        }else if (document.all){ //Explorer 4
                tmp = document.all[argFieldId];
                sNumber = tmp.value;
 }else{ 
                alert (js_fn_NOT_VALID_BROWSER);
                return false;
 }
 
 //call function in the utility lib
 if( checkQuantity(sNumber, optName) == false ){
    tmp.select();
                tmp.focus();
    return;
 }
 
 return argCommand;
}
//end validateField function

/**
*Function that validates family no field on a form
*
*argFieldId, string = the id to the field that will be validated
*optName        string          = Optional, description of field, e.g. Quantity (optional)
*argLocale string 	=  info about locale, to distinguish between different countries(some country validation requires sepcial logic)
**/
function validateFamilyNo(argFieldId, optName, argLocale){
  //get the quantity field on this form
    
  var sNumber = ""; //variable to store the value of the field
  
  //Netscape 6 and ie 5>
        if (document.getElementById){
         tmp = document.getElementById(argFieldId); 
                 sNumber = tmp.value;
        }else if (document.all){ //Explorer 4
                tmp = document.all[argFieldId];
                sNumber = tmp.value; 
 }else{ 
                alert (js_fn_NOT_VALID_BROWSER);
                return false;
 }
 
 //call function in the utility lib
 if( checkFamilyNo(sNumber, optName, argLocale) == false ){
    tmp.select();
                tmp.focus();
    return false;
 }
 else{
  return true;
 }
 
}
//end validateField function


/**
*Function that validates business card no field on a form
*
*argFieldId, string = the id to the field that will be validated
*optName        string          = Optional, description of field, e.g. Quantity (optional)
*argLocale string 	=  info about locale, to distinguish between different countries(some country validation requires sepcial logic)
**/
function validateBusinessNo(argFieldId, optName, argLocale){
  	//get the quantity field on this form
  	var sNumber = ""; //variable to store the value of the field
	//Netscape 6 and ie 5>
	if (document.getElementById){
	 tmp = document.getElementById(argFieldId); 
	 sNumber = tmp.value;
	}else if (document.all){ //Explorer 4
	  tmp = document.all[argFieldId];
	  sNumber = tmp.value; 
	}else{ 
	 alert (js_fn_NOT_VALID_BROWSER);
	 return false;
	}
 
 	//call function in the utility lib
 	if( checkBusinessCardNo(sNumber, optName, argLocale) == false ){
    	tmp.select();
    	tmp.focus();
    	return false;
 	}
	else{
  		return true;
 	} 
}
//end validateField function



/**
*Mehotod that print the document
*/

var da = (document.all) ? 1 : 0;
var pr = (window.print) ? 1 : 0;
var mac = (navigator.userAgent.indexOf("Mac") != -1); 

if (da && !pr && !mac) with (document) {
 writeln('<OBJECT ID="WB" WIDTH="0" HEIGHT="0" CLASSID="clsid:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>');
 writeln('<' + 'SCRIPT LANGUAGE="VBScript">');
 writeln('Sub window_onunload');
 writeln('  On Error Resume Next');
 writeln('  Set WB = nothing');
 writeln('End Sub');
 writeln('Sub vbPrintPage');
 writeln('  OLECMDID_PRINT = 6');
 writeln('  OLECMDEXECOPT_DONTPROMPTUSER = 2');
 writeln('  OLECMDEXECOPT_PROMPTUSER = 1');
 writeln('  On Error Resume Next');
 writeln('  WB.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER');
 writeln('End Sub');
 writeln('<' + '/SCRIPT>');
}

function printPage() {
 if (pr) // NS4, IE5
   window.print()
 else if (da && !mac) // IE4 (Windows)
   vbPrintPage()
 else // other browsers
   alert(js_print_NOT_SUPPORTED);
 return;
}


//LUHN formula
function check_algo(card)
{
  var cardno = card;
  var cardtotal=0;
  var dnum=0;
  var test=0;

  for ( i = cardno.length; i >= 1 ;  i--)
  {
    test=test+1;
	num = cardno.charAt(i-1);
    if ((test % 2) != 0)
       cardtotal=cardtotal+parseInt(num)
    else {
       dnum=parseInt(num)*2;
       if (dnum >= 10)
          cardtotal=cardtotal+1+dnum-10
       else
          cardtotal=cardtotal+dnum;
    }

  }
  if ((cardtotal % 10) != 0){
       alert(js_pay_INVALID_CARD);
       return (false);
  }else{
       return(true);
  }
}

function check_algo_old(card, length){
	var c = true;
	var lastnum=card.charAt((length-1));
	total=0;
	multifactor=1;
	for (var i = (length-2); i >= 0; i--){			
		if (multifactor == 1)
			multifactor=2;
		else
			multifactor=1;
		
		multitot=parseInt(card.charAt(i)) * multifactor;
		
		if (multitot > 9){
			st_multitot=multitot.toString();
			multitot=parseInt(st_multitot.charAt(0)) +  parseInt(st_multitot.charAt(1));
		}
		total+=multitot;
		
	} // for

	var st_total = total.toString();
	lasttot=10 - parseInt(st_total.charAt((st_total.length-1)));
	if (lasttot != lastnum){
		alert(js_pay_INVALID_CARD);c=false;}
	return c;
}

//array contains numbers for test, this numbers should not be validated
var dibsTest = new Array("5019100000000000", "5019000000000000", "5019000000000001", "5019000000000002", "5019000000000003", "5019000000000004",
							"4571100000000000", "4571000000000000", "4571000000000001", "4571000000000002", "4571000000000003", "4571000000000004",
							"5413100000000000", "5413000000000000", "5413000000000001", "5413000000000002", "5413000000000003", "5413000000000004", 
							"36141000000000", "36140000000000", "36140000000001", "36140000000002", "36140000000003", "36140000000004");


function validate_payment(card){
	//check if card no is a dibs test card, if so bypass validation
	for(var i = 0; i < dibsTest.length; i++ ){
		if(card == dibsTest[i])
			return true;
	}
	
	var test1="49701000000027";
	var test2="49701000000028";
	var b=true;
	
	if (card.length < 10  || card.length > 20){
			alert(js_pay_BETWEEN);
			b=false;
	}

	if (b)
		b=checkNum(card,0,1,1,0000000000000000, 9999999999999999, ' ');

	if (b && card != test1 && card != test2){
		b=check_algo(card);
	}

	return b;
}

function checkCardExpiryDate(inMonth, inYear){
    var toDay = new Date();
    var toDayMonth = "";
    if((toDay.getMonth()+1) < 10 ){
    	toDayMonth = "0" + (toDay.getMonth() + 1);	
   	}else{
   		toDayMonth = new String(toDay.getMonth()+1);
   	}
    
    var expValue = new String(inYear) + new String(inMonth);       
    var todayValue = new String(toDay.getFullYear()) + toDayMonth;
     
    if(parseInt(expValue) < parseInt(todayValue)){
			alert(js_fn_INVALID_EXPDATE);
            return false;
    }	
	return true;
}

