/*****************************
GLOBAL VARIABLES
*****************************/
var lastid = 0;
var original_price = 0;
/*****************************
SEARCH FUNCTIONS
*****************************/
function changeSort(sortvalue) {
	document.getElementById('sortfield').value = sortvalue;
	document.searchform.submit();
	return false;
}
function changePage(newpage) {
	document.getElementById('pagefield').value = newpage;
	document.searchform.submit();
	return false;
}
/*****************************
AJAX FUNCTION
*****************************/
function GetXmlHttpObject() {
	var xmlHttp = null;
	try {
		// Firefox, Opera, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// IE
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	if (xmlHttp == null) {
		alert("Your browser is not compatible with AJAX.");
		return;
	}
	return xmlHttp;
}
/*****************************
ACCOUNT ALTS MULTIPLIER FUNCTION
*****************************/
function multiplierChanged(eid) {
	document.getElementById('multdiv_' + eid).innerHTML = "<img src='http://www.gamepal.com/images/ajax-loader.gif'>";
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = multiplierStateChanged;
	lastid = eid;
	var url = "/multiplier.php";
	url += '?eid='+ eid;
	url += '&value=' + document.getElementById('multiplier_' + eid).value;
	url += '&sid='+Math.random();
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}
function multiplierStateChanged() {
	if (xmlHttp.readyState == 4) {
		document.getElementById('multdiv_' + lastid).innerHTML = xmlHttp.responseText;
	}
}
/*****************************
COUPON FUNCTIONS
*****************************/
function fetchCoupon() {

	if (original_price == 0) {
	// Store original price
		original_price = document.getElementById('amount').value;
		document.getElementById('coupon').value = '';
		//document.getElementById('cart_form').action = 'https://www.paypal.com/cgi-bin/webscr';
	} else {
	// Restore original price, blank out coupon value, reset action URL
		document.getElementById('amount').value = original_price;
		document.getElementById('coupon').value = '';
		//document.getElementById('cart_form').action = 'https://www.paypal.com/cgi-bin/webscr';
	}
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = couponFetched;
	var url = "fetch_coupon.php";
	var couponcode = document.getElementById('coupon_code').value;
	url += '?code='+ couponcode;
	url += '&sid='+Math.random();
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}
function fetchCouponPowerLevel() {
	if (original_price == 0) {
	// Store original price
		original_price = document.getElementById('totalcost').value;
		document.getElementById('coupon').value = '';
//		document.getElementById('cart_form').action = 'https://www.paypal.com/cgi-bin/webscr';
	} else {
	// Restore original price, blank out coupon value, reset action URL
		document.getElementById('totalcost').value = original_price;
		document.getElementById('coupon').value = '';
//		document.getElementById('cart_form').action = 'https://www.paypal.com/cgi-bin/webscr';
	}
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = couponFetched;
	var url = "fetch_coupon_powerlevel.php";
	var couponcode = document.getElementById('coupon_code').value;
	url += '?code='+ couponcode;
	url += '&sid='+Math.random();
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}

function applyCoupon_Powerlevel(max_usage) {
	var coupon_usage = document.getElementById('coupon_usage').value;
	var amount = document.getElementById('totalcost').value;
	var coupon_code = document.getElementById('coupon_code').value;
	if ( (amount - coupon_usage) < 0 ) {
		alert("Your coupon amount ($"+coupon_usage+") cannot be higher than your purchase price ($"+amount+")!");
	} else if ( (max_usage - coupon_usage) < 0) {
		alert("You don't have enough money on your coupon to use this much!");
	} else {
		var new_amount = Math.round( (amount - coupon_usage)*100 )/100;
		if (new_amount == 0) {
			new_amount = '0.00';
//			document.getElementById('cart_form').action = 'receipt.php';
		}
		document.getElementById('totalcost').value = new_amount;
		document.getElementById('coupon').value = coupon_code;
		document.getElementById('CouponContent').innerHTML = '<b>Coupon applied succesfully.</b><br><br>Your new total: <s>$'+amount+'</s>&nbsp; &nbsp;<b>$'+new_amount+'</b>';
	}
	return false;
}
function applyPercentageCoupon_Powerlevel(percentage) {
	var amount = document.getElementById('totalcost').value;
	var discount = percentage/100 * amount;
	var new_amount = Math.round( (amount - discount)*100 )/100;
	document.getElementById('totalcost').value = new_amount;
	document.getElementById('coupon').value = coupon_code;
	document.getElementById('CouponContent').innerHTML = '<b>Coupon applied succesfully.</b><br><br>Your new total: <s>$'+amount+'</s>&nbsp; &nbsp;<b>$'+new_amount+'</b>';
	return false;
}


/*****
Functions for cart (Account)
****/
function couponFetched() {
	if (xmlHttp.readyState == 4) {
		document.getElementById('HiddenCouponRow').style.display = '';
		document.getElementById('CouponContent').innerHTML = xmlHttp.responseText;
	}
}
function applyCoupon(max_usage) {
	var coupon_usage = document.getElementById('coupon_usage').value;
	var amount = document.getElementById('amount').value;
	var coupon_code = document.getElementById('coupon_code').value;
	if ( (amount - coupon_usage) < 0 ) {
		alert("Your coupon amount ($"+coupon_usage+") cannot be higher than your purchase price ($"+amount+")!");
	} else if ( (max_usage - coupon_usage) < 0) {
		alert("You don't have enough money on your coupon to use this much!");
	} else {
		var new_amount = Math.round( (amount - coupon_usage)*100 )/100;
		if (new_amount == 0) {
			new_amount = '0.00';
			document.getElementById('cart_form').action = 'receipt.php';
		}
		document.getElementById('amount').value = new_amount;
		document.getElementById('coupon').value = coupon_code;
		document.getElementById('CouponContent').innerHTML = '<b>Coupon applied succesfully.</b><br><br>Your new total: <s>$'+amount+'</s>&nbsp; &nbsp;<b>$'+new_amount+'</b>';
	}
	return false;
}
function applyPercentageCoupon(percentage) {
	var amount = document.getElementById('amount').value;
	var discount = percentage/100 * amount;
	var new_amount = Math.round( (amount - discount)*100 )/100;
	document.getElementById('amount').value = new_amount;
	document.getElementById('coupon').value = coupon_code;
	document.getElementById('CouponContent').innerHTML = '<b>Coupon applied succesfully.</b><br><br>Your new total: <s>$'+amount+'</s>&nbsp; &nbsp;<b>$'+new_amount+'</b>';
	return false;
}
/*****************************
CART FUNCTIONS
*****************************/
function buildCustomField() {
					if ((document.cart_form.amount.value <= 0)) {
			alert("Your transaction must be at least 1 cent as we require you to process an order through a payment solution even if you are using a gift certificate!");
			return false;
		}

	tela = document.cart_form.night_phone_a.value;
	telb = document.cart_form.night_phone_b.value;
	telc = document.cart_form.night_phone_c.value;
	ip = document.cart_form.pp_ip.value;
	coupon = document.cart_form.coupon.value;
	custom = document.getElementById("pp_custom");
	custom.value = ip + ";" + tela + telb + telc + ";" + coupon;
	return true;
}