function $(id) {
    return document.getElementById(id);
}
function ajax() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}
function formatCurrency(amount) {
    var i = parseFloat(amount);
    if(isNaN(i)) { i = 0.00; }
    var minus = "";
    if(i <0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if(s.indexOf('.') <0) { s += '.00'; }
    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}
function getPrice() {
	promo = $('promo').value;
	numboothsindex = $('numbooths').selectedIndex;
	numbooths = $('numbooths')[numboothsindex].value;
	if($('corner').checked == true) {
		corner = 1;	
	} else {
		corner = 0;	
	}
	
	var ajx = new ajax;
	ajx.open('get', 'script/regjax.php?action=regprice&promo=' + promo + '&numbooths=' + numbooths + '&corner=' + corner, true);
	ajx.onreadystatechange = function() {
	   if(ajx.readyState == 4) {
			response = ajx.responseText.split('|');
			if(response[1] == 0) {
				$('promoMsg').innerHTML = '';
			} else if(response[1] == 1) {
				$('promoMsg').innerHTML = 'Good Promo Code :)';
			} else if(response[1] == 2) {
				$('promoMsg').innerHTML = 'Bad Promo Code :(';
			}
			$('regPrice').innerHTML = '$' + formatCurrency(response[0]);
			$('amount').value = response[0];
	   }
	}
	ajx.send(null);
	//alert(promo + ' | ' + numbooths + ' | ' + corner);
}
function checkForm() {
	if($('company').value == '') {
		alert("Your Company Name is Missing");
		$('company').focus();
	} else if($('first_name').value == '') {
		alert("Your First Name is Missing");	
		$('first_name').focus();
	} else if($('last_name').value == '') {
		alert("Your Last Name is Missing");	
		$('last_name').focus();
	} else if($('address').value == '') {
		alert("Your Address is Missing");
		$('address').focus();
	} else if($('city').value == '') {
		alert("Your City is Missing");
		$('city').focus();	
	} else if($('zip').value == '') {
		alert("Your Zip Code is Missing");
		$('zip').focus();	
	} else if($('phone').value == '') {
		alert("Your Phone Number is Missing");
		$('phone').focus();	
	} else if($('email').value == '') {
		alert("Your Email is Missing");
		$('email').focus();	
	} else if($('product').value == '') {
		alert("Your Product Description is Missing");
		$('product').focus();
	} else {
		toCheckout();
	}
	
}
function toCheckout() {
	getPrice();
	
	company = $('company').value;
	first_name = $('first_name').value;
	last_name = $('last_name').value;
	address = $('address').value;
	address2 = $('address2').value;
	city = $('city').value;
	stateindex = $('state').selectedIndex;
	state = $('state')[stateindex].value;
	zip = $('zip').value;
	phone = $('phone').value;
	email = $('email').value;
	website = $('website').value;
	numboothsindex = $('numbooths').selectedIndex;
	numbooths = $('numbooths')[numboothsindex].value;
	if($('corner').checked == true) {
		corner = 1;	
	} else {
		corner = 0;	
	}
	pref1 = $('pref1').value;
	pref2 = $('pref2').value;
	product = $('product').value;
	promo = $('promo').value;
	amount = $('amount').value;
	transid = $('transaction_id').value;
	
	var ajx = new ajax;
	var params = 'action=tocheckout&company=' + company + '&first_name=' + first_name + '&last_name=' + last_name + '&address=' + address + '&address2=' + address2 + '&city=' + city + '&state=' + state + '&zip=' + zip + '&phone=' + phone + '&email=' + email + '&website=' + website + '&numbooths=' + numbooths + '&corner=' + corner + '&pref1=' + pref1 + '&pref2=' + pref2 + '&product=' + product + '&promo=' + promo + '&amount=' + amount + '&transid=' + transid; 
	ajx.open('POST', 'script/regjax.php',true);
	ajx.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajx.setRequestHeader("Content-length", params.length);
	ajx.setRequestHeader("Connection", "close");
	ajx.onreadystatechange = function() {
		if(ajx.readyState == 4){
			//alert(ajx.responseText);
			document.regForm.submit();
		}
	}
	ajx.send(params);
}
