// /purchase/
function iecompattest(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}
function staticbar() {
	var elementName = "priceBox";
	var startY = 270;
	var ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
	var d = document;
	function ml(id){
		var el=d.getElementById(id);
		el.style.visibility="visible"
		if(d.layers)el.style=el;
		el.sP=function(x,y){this.style.left=x+"px";this.style.top=y+"px";};
		//el.x = startX;
		el.y = startY;
		return el;
	}
	window.stayTopLeft=function(){
		var pY = ns ? pageYOffset : iecompattest().scrollTop;
		ftlObj.y += (pY + startY - ftlObj.y)/8;
		ftlObj.sP(ftlObj.x, ftlObj.y);
		setTimeout("stayTopLeft()", 10);
	}
	ftlObj = ml(elementName);
	stayTopLeft();
}

if (window.addEventListener)
window.addEventListener("load", staticbar, false)
else if (window.attachEvent)
window.attachEvent("onload", staticbar)
else if (document.getElementById)
window.onload=staticbar

function updatePrice() {
	var pps = document.myinx_purchase_2.pur_def_pps.value;
	var voicepps = document.myinx_purchase_2.pur_def_voicepps.value;
	var doTick = document.myinx_purchase_2.pur_def_tick.value;
	var doVoice = document.myinx_purchase_2.pur_voice_type.value;
	var loading = document.getElementById('loading');
	var perMonth = document.getElementById('perMonth');
	var perQuarter = document.getElementById('perQuarter');
	var perSemiAnnual = document.getElementById('perSemiAnnual');
	var perAnnual = document.getElementById('perAnnual');
	var initPrice = 0;
	
	$(loading).appear();
	
	var opt_slots = document.myinx_purchase_2.pur_game_slots.options[document.myinx_purchase_2.pur_game_slots.selectedIndex].value;
	var opt_voiceslots = document.myinx_purchase_2.pur_voice_slots.options[document.myinx_purchase_2.pur_voice_slots.selectedIndex].value;
	
	var opt_renewal = document.myinx_purchase_2.pur_renewal.options[document.myinx_purchase_2.pur_renewal.selectedIndex].value;
	var opt_tick = document.myinx_purchase_2.pur_game_tick.options[document.myinx_purchase_2.pur_game_tick.selectedIndex].value;
	var opt_debrand = document.myinx_purchase_2.pur_game_debrand.options[document.myinx_purchase_2.pur_game_debrand.selectedIndex].value;
	var opt_geo = document.myinx_purchase_2.pur_game_geo.options[document.myinx_purchase_2.pur_game_geo.selectedIndex].value;

	initPrice = pps * opt_slots;
	initPrice = initPrice + parseFloat(getOptionCost(opt_debrand)) + parseFloat(getOptionCost(opt_geo));
	
	if (doTick == 1) {
		initPrice = initPrice + parseFloat(getOptionCost(opt_tick));
	}
	if (doVoice == 1) {
		initPrice = initPrice + ((opt_voiceslots - 10) * voicepps);
	} else {
		initPrice = initPrice + (opt_voiceslots * voicepps);
	}
	
	initPrice = (initPrice * parseFloat(getOptionCost(opt_renewal)));
	$(perMonth).hide();
	$(perQuarter).hide();
	$(perSemiAnnual).hide();
	$(perAnnual).hide();
	if (opt_renewal == 13) {
		$(perMonth).show();
	} else if (opt_renewal == 14) {
		$(perQuarter).show();
	} else if (opt_renewal == 15) {
		$(perSemiAnnual).show();
	} else if (opt_renewal == 16) {
		$(perAnnual).show();
	}
	
	var exclVat = document.getElementById("exclVat");
	var vat = document.getElementById("vat");
	var inclVat = document.getElementById("inclVat");
	var currency = document.getElementById("pur_currency");
	
	if (currency.value == "GBP") {
		initPriceIncVat = initPrice * 1.175;
		initPriceVat = initPriceIncVat - initPrice;
		
		exclVat.innerHTML = "&pound;" + initPrice.toFixed(2);
		vat.innerHTML = "&pound;" + initPriceVat.toFixed(2);
		inclVat.innerHTML = "&pound;" + initPriceIncVat.toFixed(2);
	} else if (currency.value == "EUR") {
		initPrice = initPrice * parseFloat(getOptionCost(17));
		initPriceIncVat = initPrice * 1.175;
		initPriceVat = initPriceIncVat - initPrice;
		
		exclVat.innerHTML = "&euro;" + initPrice.toFixed(2);
		vat.innerHTML = "&euro;" + initPriceVat.toFixed(2);
		inclVat.innerHTML = "&euro;" + initPriceIncVat.toFixed(2);
	} else if (currency.value == "USD") {
		initPrice = initPrice * parseFloat(getOptionCost(18));
		initPriceIncVat = initPrice * 1.175;
		initPriceVat = initPriceIncVat - initPrice;
		
		exclVat.innerHTML = "&#36;" + initPrice.toFixed(2);
		vat.innerHTML = "&#36;" + initPriceVat.toFixed(2);
		inclVat.innerHTML = "&#36;" + initPriceIncVat.toFixed(2);
	} else if (currency.value == "DKK") {
		initPrice = initPrice * parseFloat(getOptionCost(19));
		initPriceIncVat = initPrice * 1.175;
		initPriceVat = initPriceIncVat - initPrice;
		
		exclVat.innerHTML = "kr " + initPrice.toFixed(2);
		vat.innerHTML = "kr " + initPriceVat.toFixed(2);
		inclVat.innerHTML = "kr " + initPriceIncVat.toFixed(2);
	}
	
	$(loading).hide();
}

function getOptionCost(optionId) {
	var ajaxCall = new Ajax.Request( '/ajax/myinx/', { method: 'get', parameters: {_a: 'purchase', j: 1, i: optionId}, asynchronous:false });
	return ajaxCall.transport.responseText;
}

function changeGame(game, gameId) {
	var packageId = document.myinx_purchase_2.pur_package.options[document.myinx_purchase_2.pur_package.selectedIndex].value;
	var tickrateOption = document.getElementById('tickrateOption');
	var slotsList = document.getElementById('pur_game_slots');
	var defpps = document.getElementById('pur_def_pps');
	var deftick = document.getElementById('pur_def_tick');
	
	new Ajax.Request('/ajax/myinx/',
	{
		method:'get',
		parameters: {_a: 'purchase', j: 2, i: gameId, h: packageId},
		onSuccess: function(transport) {
			var response = transport.responseText;
			if (response == "invalid") {
				alert("Unfortunately the game you selected is not available under the current package type. Please select a different package to purchase this game.");
				game.selectedIndex = 0;
			} else {
				while (slotsList.firstChild) {
					slotsList.removeChild(slotsList.firstChild);
				}
				
				var bits = response.split("--");
				for (i = parseInt(bits[0]); i <= parseInt(bits[1]); i=i+2) {
					if (i == bits[0]) {
						slotsList.options[slotsList.options.length] = new Option(i + " Players", i);
					} else {
						var price = defpps.value * (i - bits[0]);
						slotsList.options[slotsList.options.length] = new Option(i + " Players (add \u00A3" + price.toFixed(2) + ")", i);
					}
				}
				
				if (bits[2] == 1) {
					$(tickrateOption).show();
					deftick.value = 1;
				} else {
					$(tickrateOption).hide();
					deftick.value = 2;
				}
				
				updatePrice();
			}
		}
	});
}

function changePackage(packageDiv, packageId) {
	var game = document.getElementById('pur_game_id');
	var gameId = game.options[game.selectedIndex].value;
	var defpps = document.getElementById('pur_def_pps');
	var defvoicepps = document.getElementById('pur_def_voicepps');
	var voiceType = document.getElementById('pur_voice_type');
	var voiceIncl = document.getElementById('voiceIncl');
	var voiceExcl = document.getElementById('voiceExcl');
	var slotsList = document.getElementById('pur_voice_slots');
	
	var ajaxCall = new Ajax.Request( '/ajax/myinx/', { method: 'get', parameters: {_a: 'purchase', j: 3, i: packageId}, asynchronous:false });
	
	var pps = parseFloat(ajaxCall.transport.responseText);
	defpps.value = pps;
	
	while (slotsList.firstChild) {
		slotsList.removeChild(slotsList.firstChild);
	}
	
	if (packageId == 1) {
		voiceType.value = 2;
		$(voiceIncl).hide();
		$(voiceExcl).show();
		
		for (i = 0; i <= 100; i=i+2) {
			if (i == 0) {
				slotsList.options[slotsList.options.length] = new Option("No Slots", i);
			} else {
				var price = defvoicepps.value * i;
				slotsList.options[slotsList.options.length] = new Option(i + " Slots (add \u00A3" + price.toFixed(2) + ")", i);
			}
		}
	} else if (packageId == 2) {
		voiceType.value = 1;
		$(voiceIncl).show();
		$(voiceExcl).hide();
		
		for (i = 10; i <= 100; i=i+2) {
			if (i == 10) {
				slotsList.options[slotsList.options.length] = new Option("10 Slots (Free)", i);
			} else {
				var price = defvoicepps.value * (i - 10);
				slotsList.options[slotsList.options.length] = new Option(i + " Slots (add \u00A3" + price.toFixed(2) + ")", i);
			}
		}
	}
	
	changeGame(game, gameId);
}

function redeemVoucher() {
	var voucher = document.getElementById('pur_voucher');
	var redeemBtn = document.getElementById('pur_redeem');
	var loader = document.getElementById('pur_voucher_load');
	var success = document.getElementById('pur_voucher_success');
	
	$(loader).appear();
	$(redeemBtn).hide();
	voucher.readOnly = true;
	
	new Ajax.Request('/ajax/myinx/',
	{
		method:'get',
		parameters: {_a: 'purchase', j: 4, i: voucher.value},
		onSuccess: function(transport) {
			var response = transport.responseText;
			if (response == "invalid") {
				alert("The voucher you have entered is invalid or expired. Please try again.");
				$(loader).hide();
				$(redeemBtn).appear();
				voucher.readOnly = false;
				voucher.value = "";
			} else {
				alert("The voucher has been successfully added.\nPlease note that the price does not include any discounts applied by the voucher. Discounts will be applied on the next page.");
				$(loader).hide();
				$(success).appear();
			}
		}
	});
}

function purchaseStep3Submit() {
	var reg_agreebox = document.getElementById('reg_agreebox');
	
	if (reg_agreebox.checked) {
		document.forms.myinx_purchase_3.submit();
	} else {
		alert("Please tick that you agree to the terms of service and privacy policy.");
	}
}
	

// /register/
var system_check = {email: false, 
                    pass: false, 
					pass2: false, 
                    alias: false,
					name: false,
					reg_agreebox: false};

function check_system() {
  var master_switch = true;
  
  for (k in system_check) {
    if (!system_check[k]) { master_switch = false; break; }
  }

  if ( master_switch ) {
    $('gobutton').disabled = false;
    $('gobutton').value = "Register!";
  } else {
    $('gobutton').disabled = true;
    $('gobutton').value = "Please complete the form above!";
  }

}

function check_email( form ) {
  var ajaxConn = new XHConn();
  ajaxConn.connect('/ajax/myinx/', 'GET', '_a=register&val=email&var='+form.value, fnDone);

  function fnDone(t) {

    if (t.responseText == "1") {
      $('email_good').show();
      $('email_odd').hide();
      $('email_bad').hide();
      system_check['email'] = true;
    } else {
      $('email_good').hide();
      if (t.responseText == "2") { $('email_odd').show(); } else { $('email_odd').hide(); }
      if (t.responseText == "0") { $('email_bad').show(); } else { $('email_bad').hide(); }
      system_check['email'] = false;
    }
    check_system();
  }

  ajaxConn = null;
}

function check_alias( form ) {
 
  var ajaxConn = new XHConn();
  ajaxConn.connect('/ajax/myinx/', 'GET', '_a=register&val=alias&var='+form.value, fnDone);
	
	function fnDone(t) {
	  
	  if (t.responseText == "1") {
		$('alias_good').show();
		$('alias_odd').hide();
		$('alias_bad').hide();
		system_check[ 'alias' ] = true;
	  } else {
		$('alias_good').hide();
		$('alias_odd').hide();
		$('alias_bad').show();
		system_check[ 'alias' ] = false;
	  }
	  check_system();
	}

  ajaxConn = null;
    
}

function check_pass( form ) {
  if ( $('pass_bad') ) { $('pass_bad').hide(); }
  if (form.value == "") {
	$('pass_good').hide();
    $('pass_odd').hide();
    $('pass_bad').show();
    system_check[ 'pass' ] = false;
  } else {
	  if (form.value.length < 5 || form.value.indexOf(' ') > -1) {
	    $('pass_good').hide();
	    $('pass_odd').show();
		$('pass_bad').hide();
	    system_check[ 'pass' ] = false;
	  } else {
	    $('pass_good').show();
	    $('pass_odd').hide();
		$('pass_bad').hide();
	    system_check[ 'pass' ] = true;
	  }
  }
  check_system();
}

function check_pass2( formi ) {
  if ( $('pass2_bad') ) { $('pass2_bad').hide(); }
  if (formi.value == "") {
	$('pass2_good').hide();
    $('pass2_odd').hide();
    $('pass2_bad').show();
    system_check[ 'pass2' ] = false;
  } else {
	  if (formi.value != document.getElementById('reg_pass').value) {
	    $('pass2_good').hide();
	    $('pass2_odd').show();
		$('pass2_bad').hide();
	    system_check[ 'pass2' ] = false;
	  } else {
	    $('pass2_good').show();
	    $('pass2_odd').hide();
		$('pass2_bad').hide();
	    system_check[ 'pass2' ] = true;
	  }
  }
  check_system();
}

function check_name( formi ) {
	var ajaxConn = new XHConn();
	ajaxConn.connect('/ajax/myinx/', 'GET', '_a=register&val=name&var='+formi.value, fnDone);
	
	function fnDone(t) {
	  
	  if (t.responseText == "1") {
		$('name_good').show();
		$('name_odd').hide();
		$('name_bad').hide();
		system_check[ 'name' ] = true;
	  } else {
		$('name_good').hide();
		$('name_odd').show();
		$('name_bad').hide();
		system_check[ 'name' ] = false;
	  }
	  check_system();
	}

	ajaxConn = null;
}

function check_tos( form ) {
  if (form.checked) {
    system_check[ 'reg_agreebox' ] = true;
  } else {
    system_check[ 'reg_agreebox' ] = false;
  }
  check_system();
}

// vcp
function setupForm(val, prefix, max) {
	for (i=1; i <= max; i++) {
		var tempId = document.getElementById(prefix+i);
		$(tempId).hide();
	}
	var placeId = document.getElementById(prefix+val);
	$(placeId).appear();
	$('submit').disabled = false;
}
