// JScript source code to calculate invoice
var i_checks;
var i_country=1;
var	cost;
var disc;
var total=0;
//------------- Section For changes
var costCAD	  = 399;
var costUSD	  = 249;
var coupon	  = new Array("qty4rebate", "qty5rebate", "578A7001", "578A7002", "578A7003", "578A7004", "578A7005", "578A7006", "578A7007", "578A7008", "578A7009", "578A7010", "578A7011", "578A7012", "578A7013", "578A7014", "578A7015", "578A7016", "578A7017", "578A7018", "578A7019", "578A7020", "578A7021", "578A7022", "578A7023", "578A7024", "578A7025", "578A7026", "578A7027", "578A7028", "578A7029", "578A7030", "578A7031", "578A7032", "578A7033", "578A7034", "578A7035", "578A7036", "578A7037", "578A7038", "578A7039", "578A7040", "578A7041", "578A7042", "578A7043", "578A7044", "578A7045", "578A7046", "578A7047", "578A7048", "578A7049", "578A7050", "578A7051", "578A7052", "578A7053", "578A7054", "578A7055", "578A7056", "578A7057", "578A7058", "578A7059", "578A7060", "578A7061", "578A7062", "578A7063", "578A7064", "578A7065", "578A7066", "578A7067", "578A7068", "578A7069", "578A7070", "578A7071", "578A7072", "578A7073", "578A7074", "578A7075", "578A7076", "578A7077", "578A7078", "578A7079", "578A7080", "578A7081", "578A7082", "578A7083", "578A7084", "578A7085", "578A7086", "578A7087", "578A7088", "578A7089", "578A7090", "578A7091", "578A7092", "578A7093", "578A7094", "578A7095", "578A7096", "578A7097", "578A7098", "578A7099", "578A7100", "578A7101", "578A7102", "578A7103", "578A7104", "578A7105", "578A7106", "578A7107", "578A7108", "578A7109", "578A7110", "578A7111", "578A7112", "578A7113", "578A7114", "578A7115", "578A7116", "578A7117", "578A7118", "578A7119", "578A7120", "578A7121", "578A7122", "578A7123", "578A7124", "578A7125", "578A7126", "578A7127", "578A7128", "578A7129", "578A7130", "578A7131", "578A7132", "578A7133", "578A7134", "578A7135", "578A7136", "578A7137", "578A7138", "578A7139", "578A7140", "578A7141", "578A7142", "578A7143", "578A7144", "578A7145", "578A7146", "578A7147", "578A7148", "578A7149", "578A7150", "578A7151", "578A7152", "578A7153", "578A7154", "578A7155", "578A7156", "578A7157", "578A7158", "578A7159", "578A7160", "578A7161", "578A7162", "578A7163", "578A7164", "578A7165", "578A7166", "578A7167", "578A7168", "578A7169", "578A7170", "578A7171", "578A7172", "578A7173", "578A7174", "578A7175", "578A7176", "578A7177", "578A7178", "578A7179", "578A7180", "578A7181", "578A7182"
); //add COUPON CODES here. Make sure number of CODES equals to number of values below

var valuesCAD = new Array("100", "250", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30", "30"); //Add discount values corresponding each COUPON CODE in CAD

var valuesUSD = new Array("67", "100", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20", "20"); //Add discount values corresponding each COUPON CODE in US

var values	  = new Array(5); //5 if number of Coupon codes here
var discEB	  = "no"; //put "no" if you want remove discount
var discCAD   = 30; //Comment it out in beginner if you want use percentage
var discUSD   = 20; //Comment it out in beginner if you want use percentage
//var discCAD   = costCAD*7.5/100; //Take off comments in beginner if you want use percentage
//var discUSD   = costUSD*7.5/100; //Take off comments in beginner if you want use percentage
//------------- End of section
var coup_d =0;

function calculate(theform){		
	
	i_checks =0;
	if (theform.item_name1.checked) i_checks++;
	if (theform.item_name2.checked) i_checks++;
	if (theform.item_name3.checked) i_checks++;	
	if (theform.item_name4.checked) i_checks++;	
	if (theform.item_name5.checked) i_checks++;

	  if(theform.Country[0].checked) i_country = 1;
	  if(theform.Country[1].checked) i_country = 2;
	  if(theform.Country[2].checked) i_country = 3;
	  if(theform.Country[3].checked) i_country = 2;
	
	
	if (i_country == 1){		
		cost = costCAD;
		disc = discCAD;
		values = valuesCAD; 
	}
	else if (i_country == 3){		
		cost = costCAD;
		disc = discCAD;
		values = valuesCAD; 
	}
	else if (i_country == 2){
		cost = costUSD;
		disc = discUSD;
		values = valuesUSD;
	}
	
	if (i_checks<1){		
		return false;
	} else{

		theform.item_price.value = cost;
		theform.item_desc.value  = disc;
		if (discEB=="yes"){
			theform.item_desc_eb.value  = disc;
		}
		else{
			theform.item_desc_eb.value  = 0;
		}
		
		for (i=0; i<coupon.length;i++){			
			if (theform.coupon_number.value==coupon[i]){
				coup_d = values[i];
				theform.disc_coupon.value = coup_d;
				//alert("You have discount: "+coup_d+"$ for a coupon "+theform.coupon_number.value);
				break;
			}
			else{
				theform.disc_coupon.value="0";
				//return false;
			}
		}
		if (theform.coupon_number.value.length>0 && coup_d==0){
			alert("Wrong coupon number. Please enter again");
		}	
		
	
		total = (i_checks*cost-(i_checks-1)*disc-coup_d);		

		if (theform.stc_number.value.length==5){
			total = total-disc;
			theform.disc_stc.value=disc;
		}
		else{
			theform.disc_stc.value=0;
		}
		
		if (theform.acm_number.value.length==7){
			theform.disc_acm.value = disc;
			total = total-disc;
		}
		else{
			theform.disc_acm.value = 0;
		}
		
		if (i_country==1){
			total = total * 1.07;
		}

		theform.country_code.value = i_country;
		
		//alert("Total is:" + total);
	}
}


function validateAndSubmit(theform) {		
	doIt(theform);	
	if (i_checks==0){
		alert("No workshops have been selected");
		return false;
	}

	if (theform.coupon_number.value.length>0 && coup_d==0){				
		return false;
	}
	return checkMemberSTC(5, theform);
	return checkMemberACM(7, theform);
	
}

function checkMemberSTC(len, theform){
	if(theform.stc_number.value.length == 0){
		return true;
	}
	else if (theform.stc_number.value.length!=len || (theform.stc_number.value.length>0 && theform.stc_number.value.length<len) || theform.stc_number.value.length>len){
		alert("Invalid membership number");
		return false;
	}
}
function checkMemberACM(len, theform){
	if(theform.acm_number.value.length == 0){
		return true;
	}
	else if (theform.acm_number.value.length!=len || (theform.acm_number.value.length>0 && theform.acm_number.value.length<len) || theform.acm_number.value.length>len){
		alert("Invalid membership number");
		return false;
	}
	
}

function doIt(theform)
{
  var _f=theform;
  var _m="";
  var _obj=null;

  _obj=_f.Country;
  for(var i=0;i<_obj.length;i++)
    if(_obj[i].checked)
	  _m+="Selected: "+_obj[i].name+"="+_obj[i].value+"\n";	  

  
  if(_f.Country[0].checked) theform.country_code.value = 1;
  if(_f.Country[1].checked) theform.country_code.value = 2;
  if(_f.Country[2].checked) theform.country_code.value = 3;
  if(_f.Country[3].checked) theform.country_code.value = 2;
}

