/*****************************
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('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.php";
	var couponcode = document.getElementById('coupon_code').value;
	url += '?code='+ couponcode;
	url += '&sid='+Math.random();
	//alert(url);
	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 = couponFetchedPowerLevel;
	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('payment_cart_form').totalcost.value = new_amount;
	
		document.getElementById('totalcost').value = new_amount;
		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_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;
}
function couponFetchedPowerLevel() {
	if (xmlHttp.readyState == 4) {
		document.getElementById('HiddenCouponRow').style.display = '';
		document.getElementById('CouponContent').innerHTML = xmlHttp.responseText;
	}
}


/*****
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('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) {
alert("Notice - You are required to checkout at least $0.01 using one of the payment methods below even if your gift certificate is greater than your order. ");
//			new_amount = '0.00';			
//			document.getElementById('payment_cart_form').action = 'receipt.php';
		}
		else
		{
//		alert(coupon_usage);

		document.getElementById('coupon_cart_form').totalcost.value = new_amount;
		document.getElementById('coupon_cart_form').coupon.value = new_amount;
		document.getElementById('coupon_cart_form').coupon_discount_1.value = coupon_usage;

		document.getElementById('payment_cart_form').totalcost.value = new_amount;
		document.getElementById('payment_cart_form').coupon_discount.value = coupon_usage;
		document.getElementById('payment_cart_form').coupon_code.value = coupon_code

//document.getElementById('coupon_discount').value = coupon_usage;

		document.getElementById('coupon').value = coupon_code;
		document.getElementById('totalcost').value = new_amount;

		document.getElementById('CouponContent').innerHTML = '<b>Coupon applied succesfully.</b><br><br>Your new total: <s>$'+amount+'</s>&nbsp; &nbsp;<b>$'+new_amount+'</b>';
		document.getElementById('CartCouponTotal').innerHTML = '<b>Total: $'+new_amount+'</b>';

//document.getElementById('CartTotalContent').innerHTML = '<tr class=CartColored><td colspan=8 align=left><b>Total: $'+new_amount+'></b></td></tr>';
//document.getElementById('TotalCoupContent').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 coupon_usage = document.getElementById('coupon_usage').value;
	var amount = document.getElementById('totalcost').value;
	var discount = percentage/100 * amount;
	var new_amount = Math.round( (amount - discount)*100 )/100;
	var coupon_code = document.getElementById('coupon_code').value;

//alert(amount);
//alert(new_amount);
	document.getElementById('coupon_cart_form').totalcost.value = new_amount;
	document.getElementById('coupon_cart_form').coupon.value = new_amount;
	document.getElementById('coupon_cart_form').coupon_discount_1.value = discount;

	document.getElementById('payment_cart_form').totalcost.value = new_amount;
	document.getElementById('payment_cart_form').coupon_discount.value = discount;
	document.getElementById('payment_cart_form').coupon_code.value = coupon_code
	
	document.getElementById('coupon').value = coupon_code;
	document.getElementById('totalcost').value = new_amount;
	document.getElementById('CouponContent').innerHTML = '<b>Coupon applied succesfully.</b><br><br><b>Your new total: <s>$'+amount+'</s>&nbsp; &nbsp;</b><b>$'+new_amount+'</b>';

	document.getElementById('CartCouponTotal').innerHTML = '<b>Total: $'+new_amount+'</b>';


	return false;
}
/*****************************
CART FUNCTIONS
    $ip = $custom_field_array[0];
	$phone = $custom_field_array[1];
	$coupon = $custom_field_array[2];
    $additional_information = $custom_field_array[3];
    $cart_session = $custom_field_array[4];
*****************************/
function buildCustomField() {
	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;
	additional_information = document.cart_form.additional_information.value;
	cart_session = document.cart_form.cart_session.value;
      affid = document.cart_form.affid.value;
      link = document.cart_form.link.value;
      time = document.cart_form.time.value;
      from_cookie = document.cart_form.from_cookie.value;
	
	custom = document.getElementById("pp_custom");

/*****************************
    $ip = $custom_field_array[0];
	$phone = $custom_field_array[1];
	$coupon = $custom_field_array[2];
    $additional_information = $custom_field_array[3];
    $cart_session = $custom_field_array[4];
        $affid = $custom_field_array[5];
        $link = $custom_field_array[6];
        $time = $custom_field_array[7];
        $from_cookie = $custom_field_array[8];
*****************************/
	
	custom.value = ip + ";" + tela + telb + telc + ";" + 
                     coupon + ";" + cart_session + ";" + 
                     additional_information + ";" +
                     affid + ";" +
                     link + ";" +
                     time + ";" +
                     from_cookie;
                     
	//alert(custom.value);
	return true;
}

