String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}

/* This function adds or subtracts the item from the total.
   But it is generic enough to support any checkBox click.  Just pass in a 
   this reference to the checkbox, the amount to add/subtract and the currency
   symbol and it will do the rest.
*/
function clickCheckbox(checkboxObj,amountToUse,currencySymbol) {
	var totalObj = document.getElementById('total_amount');
	var tempStr = totalObj.innerHTML;
		
	//trim whitespace from the left side
	tempStr = tempStr.ltrim();	

	//remove the currency sign
	var temp2 = tempStr.substr(1);	
	
	if(checkboxObj.checked) {
		//add amount	
		var totalAmount = parseFloat(temp2) + parseFloat(amountToUse);		
	} else {
		//remove the amount from total
		var totalAmount = parseFloat(temp2) - parseFloat(amountToUse);	
	}
	
	totalAmount = totalAmount.toFixed(2);
	
	document.getElementById('total_amount').innerHTML = currencySymbol + totalAmount;		
}


function toggleBillingAddress(radioButtonObj) {
	var billingDiv = document.getElementById('billingDiv');
	
	if(radioButtonObj.value == "no") {
		billingDiv.style.display = 'inline';
	} else {
		billingDiv.style.display = 'none';
	}
}


function onlyNumbers(e,type) { 
	var keynum;
	var keychar;
	var numcheck;
	
	if(window.event) // IE
	{
		keynum = e.keyCode;
	} else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	
	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;    

	switch (keynum) {
		case 8: 	//backspace
		case 9:		//tab
		case 35:	//end
		case 36:	//home
		case 37:	//left arrow
		case 38:	//right arrow
		case 39:	//insert
		case 45:	//delete
		case 46:	//0
		case 48:	//1
		case 49:	//2
		case 50:	//3
		case 51:	//4	
		case 52:	//5
		case 54:	//6
		case 55:	//7
		case 56:	//8
		case 57:	//9
		case 96:	//0
		case 97:	//1
		case 98:	//2
		case 99:	//3
		case 100:	//4	
		case 101:	//5
		case 102:	//6
		case 103:	//7
		case 104:	//8
		case 105:	//9
			result2 = true;		
			break;
		
		case 109: // dash -
			if(type == 'phone') {
				result2 = true;
			} else {
				result2 = false;
			}
			
			break;
		
		default:
			result2 = numcheck.test(keychar);
			break;
	}	

	return result2;
} 


function update_expire() {
	var month = document.getElementById("fields_expmonth");
	var month_value = month.options[month.selectedIndex].value;
	var year = document.getElementById("fields_expyear");
	var year_value = year.options[year.selectedIndex].value;
	
	if((month_value != '') && (year_value != '')) {	
		document.getElementById('cc_expires').value = month_value + year_value;
	} else { 
		document.getElementById('cc_expires').value = '';
	}
}


function SetCountryValue2() {
	el_1 = document.getElementById('state_cus1');
	el_2 = document.getElementById('state_cus2');
	
	var state_value = document.forms['opt_in_form'].fields_state2.value;							
	var objDropdown = document.getElementById('fields_state');
	
	if(state_value != "") {
		found=0;
		
		for(var j=0; j <= objDropdown.length-1; j++) {
			val = document.forms['opt_in_form'].fields_state.options[j].text;
			
			if(val.toLowerCase() == state_value.toLowerCase()) {
				found = 1;
				document.forms['opt_in_form'].fields_state.selectedIndex = j;
			}
		}
		
		if(found == 0) {
			var len = objDropdown.length;
			var objOption = new Option(state_value, state_value);
			objDropdown.options[len] = objOption;
			objOption.selected = len;
		}
	}
}


function copyToState2() {
	var state_value = document.forms['opt_in_form'].fields_state.options[document.forms['opt_in_form'].fields_state.selectedIndex].value;
	document.forms['opt_in_form'].fields_state2.value = state_value;
}


function countryChangeHandler(dropdownObj, doUpdateShipping) {
	var stateDiv1 = document.getElementById('state_cus1');
	var stateDiv2 = document.getElementById('state_cus2');
	
	if(dropdownObj.value == "223") {
		stateDiv1.style.display = 'inline';
		stateDiv2.style.display = 'none';
	} else {
		stateDiv1.style.display = 'none';
		stateDiv2.style.display = 'inline';
	}
	
	if(doUpdateShipping == 1) {
		update_shipping(dropdownObj);
	}
}


function update_shipping(dropdownObj) {
	var shpDropdown = document.getElementById('shipping');
	
	// clear dropdown
	shpDropdown.length = 0;
	
	// if US is selected
	if(dropdownObj.value == "223") {
		var objOption = new Option("Standard ($4.97)", "4.97");
		shpDropdown.options[0] = objOption;
		
		objOption = new Option("Rush ($9.97)", "9.97");
		shpDropdown.options[1] = objOption;
	} else {
		var objOption = new Option("International ($18.97)", "18.97");
		shpDropdown.options[0] = objOption;
	}
	
	SetShippingValue();
}


//this function needs to just mod the total_amount by the shipping change, not overwrite it completly			
function SetShippingValue() {
	var totalObj = document.getElementById('total_amount');
	
	document.getElementById('shipping_price').innerHTML = "$" + document.forms['opt_in_form'].shipping.value;
	
	var total_amount = parseFloat(document.forms['opt_in_form'].custom_product_price.value) + parseFloat(document.forms['opt_in_form'].shipping.value);
	
	total_amount = total_amount.toFixed(2);
	document.getElementById('total_amount').innerHTML = "$"+ total_amount;
}


function isValidEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lldot = str.lastIndexOf(dot);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if(lstr < 8) {
		alert("Invalid Email Address");
		return false;
	}
	
	if(str.indexOf(at)==-1) {
	   alert("Invalid Email Address");
	   return false;
	}

	if(str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==(lstr-1) || str.indexOf(at)==(lstr-3)) {
	   alert("Invalid Email Address");
	   return false;
	}

	if(lldot==lstr-1 || lldot==lstr-2 || (lldot==ldot && lldot < lat)) {
	   alert("Invalid Email Address");
	   return false;
	}
	
	if(str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==(lstr-1)) {
		alert("Invalid Email Address");
		return false;
	}

	if(str.indexOf(at,(lat+1))!=-1) {
		alert("Invalid Email Address");
		return false;
	}

	if(str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
		alert("Invalid Email Address");
		return false;
	}

	if(str.indexOf(dot,(lat+2))==-1) {
		alert("Invalid Email Address");
		return false;
	}
	
	if(str.indexOf(" ")!=-1) {
		alert("Invalid Email Address");
		return false;
	}

	return true;
}


function allValidChars(email) {
  	var parsed = true;
  	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  
  	for (var i=0; i < email.length; i++) {
    	var letter = email.charAt(i).toLowerCase();
    	
		if(validchars.indexOf(letter) != -1) {
      		continue;
		}
    
		parsed = false;
    	break;
  	}
  
  	return parsed;
}


function Key13handler(keyCode, sender) {
	go_next = 0;
	
	if(keyCode == 13) {
		go_next = 1;
	}
	
	if(go_next) {
		nextname = taborder[sender.name];
		objEl = document.forms['opt_in_form'][nextname];
		
		if(objEl != null) {
			objEl.focus();
		}
	}
}


function onPhoneKeyUp(keyCode, sender) {
	go_next = 0;
	
	if((keyCode != 13) && (sender.name == 'phone1') && (sender.value.length > 2)) {
		go_next = 1;
	}
	
	if((keyCode != 13) && (sender.name == 'phone2') && (sender.value.length > 2)) {
		go_next = 1;
	}
	
	if(go_next) {
		nextname = taborder[sender.name];
		objEl = document.forms['opt_in_form'][nextname];
		
		if(objEl != null) {
			objEl.focus();
		}
	}
}