function copy_to_shipping(name) {
if(document.getElementById("shippingasbilling").value==1) {
  to=document.getElementById(name.name.replace("billing","shipping"));
  if(to) to.value=name.value;
}
}

function Billinginfo(hide) 
{
	var status;
	 if(hide==1) 
	 {
		status=true;
	 } else
	 {
		 status=false;
	 }
	 
	 var sel=document.getElementById("shipping-fname");
	 sel.disabled=status;copy_to_shipping(document.getElementById("billing-fname"));
	 var sel=document.getElementById("shipping-lname");
	 sel.disabled=status;copy_to_shipping(document.getElementById("billing-lname"));
	 var sel=document.getElementById("shipping-address");
	 sel.disabled=status;copy_to_shipping(document.getElementById("billing-address"));
	 var sel=document.getElementById("shipping-city");
	 sel.disabled=status;copy_to_shipping(document.getElementById("billing-city"));
	 var sel=document.getElementById("shipping-zip");
	 sel.disabled=status;copy_to_shipping(document.getElementById("billing-zip"));
	 var sel=document.getElementById("shipping-country");
	 sel.disabled=status;copy_to_shipping(document.getElementById("billing-country"));
	 var sel=document.getElementById("shipping-state");
	 sel.disabled=status;copy_to_shipping(document.getElementById("billing-state"));
}

var coupon=false;
//var coupon=true;
function entered_coupon() {
 coupon=true;  
}

function new_customer(hide) {

 var oldc=document.getElementById("oldcustomer");
 var newc=document.getElementById("newcustomer");

 var sel=document.getElementsByName("newselector");

 for (var i = 0; i < sel.length; i++) {
    sel[i].value=hide;
 }

 if(hide=="1") {
	oldc.style.display="none";
	newc.style.display="inline";
 } else {
	newc.style.display="none";
	oldc.style.display="inline";
 } 
}

var rightNow = new Date();
var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
var temp = jan1.toGMTString();
var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
//document.getElementById("time_zone").value=std_time_offset;

function validate_form() 
{
 if(coupon==true) return true;
 
var errors=0;
return true; // always OK

errors+=  check_inp("email",/[A-Z0-9.-]+\.[A-Z]{2,4}$/i);
errors+=  check_inp("email1",/[A-Z0-9.-]+\.[A-Z]{2,4}$/i);

//errors+=  check_inp("cellphone3",/[0-9]{6,}/);
//errors+=  check_inp("cellphone2",/[0-9]{2,}/);
//errors+=  check_inp("cellphone1",/[0-9]{1,3}/);

errors+=  check_inp("phone3",/[0-9]{6,}/);
errors+=  check_inp("phone2",/[0-9]{2,}/);
errors+=  check_inp("phone1",/[0-9]{1,3}/);


 if(document.getElementById("shippingasbilling").value==0) 
 {
	 errors+=  check_inp_len("shipping-zip");
	 errors+=  check_inp_len("shipping-city");
	 errors+=  check_inp_len("shipping-address");
	 errors+=  check_inp_len("shipping-lname");
	 errors+=  check_inp_len("shipping-fname");
 }


errors+= check_inp_len("billing-zip");
errors+= check_inp_len("billing-city");
errors+= check_inp_len("billing-address");
errors+= check_inp_len("billing-lname");
errors+= check_inp_len("billing-fname");



//alert("form validation..."+errors);
if(errors==0) return true; // there were no errors
  return false; // not submit the form
}

function check_inp(name,regexp) {
 var inp=document.getElementById(name);

 if(!regexp.test(inp.value)) {
	inp.style.backgroundColor="#FFE3E3";inp.focus();return true;
 } else inp.style.backgroundColor="#EFF2FD";
 return false;
}


function check_inp_len(name) {
 var inp=document.getElementById(name);
 if(inp.value.length<2) {
	inp.style.backgroundColor="#FFE3E3";inp.focus();return true;
 } else inp.style.backgroundColor="#EFF2FD";
 return false;
}

function PayMethod(payid) 
{
	$('#payment_systems_description table').hide();
	$('#payment_systems_form table').hide();
	
	$('#'+payid).show();
	$('#'+payid+'_info').show();
	
	return false;
}

function hide_product(prod_nr) {
  var prod_info=document.getElementById("prod_info["+prod_nr+"]");
	prod_info.style.display="none";
  document.getElementById("product_q["+prod_nr+"]").value=0;
document.forms[0].submit();
return false;
// alert(prod_info);
}

function Product_q(input) {

 var prod_reg=/[0-9]+/;
 var prod_nr=prod_reg.exec(input.name);
// alert(input.value);

 var prod_price=document.getElementById("product_price["+prod_nr+"]").value;
//alert(sprintf("%.2f",prod_price*input.value));
 var price_item=document.getElementById("price_item["+prod_nr+"]");
	price_item.innerHTML=sprintf("%.2f",prod_price*input.value);

 entered_coupon();
//alert(price_item); 
}

function str_repeat(i, m) {
	for (var o = []; m > 0; o[--m] = i);
	return o.join('');
}

function sprintf() {
	var i = 0, a, f = arguments[i++], o = [], m, p, c, x, s = '';
	while (f) {
		if (m = /^[^\x25]+/.exec(f)) {
			o.push(m[0]);
		}
		else if (m = /^\x25{2}/.exec(f)) {
			o.push('%');
		}
		else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
			if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) {
				throw('Too few arguments.');
			}
			if (/[^s]/.test(m[7]) && (typeof(a) != 'number')) {
				throw('Expecting number but found ' + typeof(a));
			}
			switch (m[7]) {
				case 'b': a = a.toString(2); break;
				case 'c': a = String.fromCharCode(a); break;
				case 'd': a = parseInt(a); break;
				case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
				case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
				case 'o': a = a.toString(8); break;
				case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
				case 'u': a = Math.abs(a); break;
				case 'x': a = a.toString(16); break;
				case 'X': a = a.toString(16).toUpperCase(); break;
			}
			a = (/[def]/.test(m[7]) && m[2] && a >= 0 ? '+'+ a : a);
			c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
			x = m[5] - String(a).length - s.length;
			p = m[5] ? str_repeat(c, x) : '';
			o.push(s + (m[4] ? a + p : p + a));
		}
		else {
			throw('Huh ?!');
		}
		f = f.substring(m[0].length);
	}
	return o.join('');
}

 PayMethod('visa');

