
window.addEvent('domready', function()
{
	vat_countries = Array("AT", "BE", "BG", "CY", "CZ", "DE", "DK", "EE", "EL", "ES", "FI", "FR", "GB", "GR", "HU", "IE", "IT", "LT", "LV", "MT", "NL", "PL", "PT", "RO", "SE", "SI", "SK", "LU");
	reloadVATField();
	liveCheckDataInput();
	showRelatedUnlimited();
});

function reloadPrices(server)
{
	option_os = 0;
	option_1gbit = 0;
	option_network = 0;
	option_ips = 0;
	option_traffic = 0;
	
	if ($("radio_plesk9_10").checked) option_os = $("radio_plesk9_10").value;
	if ($("radio_plesk9_100").checked) option_os = $("radio_plesk9_100").value;
	if ($("radio_plesk9_unl").checked) option_os = $("radio_plesk9_unl").value;
	if ($("radio_plesk10_10").checked) option_os = $("radio_plesk10_10").value;
	if ($("radio_plesk10_100").checked) option_os = $("radio_plesk10_100").value;
	if ($("radio_plesk10_unl").checked) option_os = $("radio_plesk10_unl").value;
	
	if ($("radio_windows_web") != null)
	{
		if ($("radio_windows_web").checked) option_os = $("radio_windows_web").value;
		if ($("radio_windows_standard").checked) option_os = $("radio_windows_standard").value;
		if ($("radio_windows_enterprise").checked) option_os = $("radio_windows_enterprise").value;
	}
	/*if ($("check_1gbit") != null)
		if ($("check_1gbit").checked) option_1gbit = $("check_1gbit").value;*/
	
	if ($("radio_1gbit") != null)
		if ($("radio_1gbit").checked) option_1gbit = $("radio_1gbit").value;
	
	if ($("radio_traffic_5") != null)
	{
		if ($("radio_traffic_5").checked) option_traffic = $("radio_traffic_5").value;
		if ($("radio_traffic_10").checked) option_traffic = $("radio_traffic_10").value;
		if ($("radio_traffic_15").checked) option_traffic = $("radio_traffic_15").value;
		if ($("radio_traffic_20").checked) option_traffic = $("radio_traffic_20").value;
		if ($("radio_traffic_100").checked) option_traffic = $("radio_traffic_100").value;
		if ($("radio_traffic_unlimited_100").checked) option_traffic = $("radio_traffic_unlimited_100").value;
		if ($("radio_traffic_unlimited_1000").checked) option_traffic = $("radio_traffic_unlimited_1000").value;
	}
	
	if ($("radio_ip_single").checked) 
	{
		selected_ip = getSelectedValue("select_server_ips");
		option_ips = selected_ip;
	}
	else
	{
		if ($("radio_ip_vlan_29").checked) option_network = $("radio_ip_vlan_29").value;
		if ($("radio_ip_vlan_28").checked) option_network = $("radio_ip_vlan_28").value;
		if ($("radio_ip_vlan_27").checked) option_network = $("radio_ip_vlan_27").value;
		if ($("radio_ip_vlan_26").checked) option_network = $("radio_ip_vlan_26").value;
		if ($("radio_ip_vlan_25").checked) option_network = $("radio_ip_vlan_25").value;
		if ($("radio_ip_vlan_24").checked) option_network = $("radio_ip_vlan_24").value;
	}
	
	
	req = new Request(
	{
	    url: ajax_hostname+'/ajaxRequest.php',
	    method: 'post',
	    data: 'a=reloadPrices&server='+server+'&option_os='+option_os+'&option_1gbit='+option_1gbit+'&option_ips='+option_ips+'&option_network='+option_network+'&option_traffic='+option_traffic,
	    onSuccess: function(txt)
	    {
			$("configure_pricing").innerHTML = txt;
	    }
	}).send();
}

function showRelatedUnlimited()
{
	if ($("radio_1gbit") != null && $("radio_traffic_unlimited_100") != null)
	{
		if ($("radio_1gbit").checked == true)
		{
			if ($("radio_traffic_unlimited_100").checked == true)
			{
				$("radio_traffic_unlimited_1000").checked = true;
				$("radio_traffic_unlimited_100").checked = false;
			}
			$("li_radio_unlimited_1000").show();
			$("li_radio_unlimited_100").hide();
			$("li_radio_traffic_100tb").show();
		}
		else
		{
			if ($("radio_traffic_unlimited_1000").checked == true)
			{
				$("radio_traffic_unlimited_100").checked = true;
				$("radio_traffic_unlimited_1000").checked = false;
			}
			if ($("radio_traffic_100").checked == true)
			{
				$("radio_traffic_100").checked = false;
				$("radio_traffic_20").checked = true;
			}
			$("li_radio_unlimited_100").show();
			$("li_radio_unlimited_1000").hide();
			$("li_radio_traffic_100tb").hide();
		}
	}
}

function checkForm()
{
	startLoading();
	basic_data = liveCheckDataInput();
	vat_nr = checkVATNumber(function(){submitForm();});
	return false;
}

function liveCheckDataInput()
{
	if ($("firstname") != null)
	{
	    checkout_invoice_data_missing = false;
	
	    if ($("firstname").value.length > 1) validate("firstname");else {unvalidate("firstname");checkout_invoice_data_missing = true;}
	    if ($("surname").value.length > 2) validate("surname");else {unvalidate("surname");checkout_invoice_data_missing = true;}
	    if ($("address").value.length > 2) validate("address");else {unvalidate("address");checkout_invoice_data_missing = true;}
	    if ($("postalcode").value.length > 3) validate("postalcode");else {unvalidate("postalcode");checkout_invoice_data_missing = true;}
	    if ($("city").value.length > 1) validate("city");else {unvalidate("city");checkout_invoice_data_missing = true;}
	    if ($("phone").value.length > 3 && $("phone_prefix").value != "0") validate("phone");else {unvalidate("phone");checkout_invoice_data_missing = true;}
	    if ($("country").value != "0") validate("country");else {unvalidate("country");checkout_invoice_data_missing = true;}
	    if (emailCheck($("email").value) == true) validate("email");else {unvalidate("email");checkout_invoice_data_missing = true;}
	
		if ($("checkbox_conditions").checked == false) checkout_invoice_data_missing = true;
		if ($("checkbox_policy").checked == false) checkout_invoice_data_missing = true;
		
		if (checkout_invoice_data_missing == true) deactivateSubmitButton();
		else activateSubmitButton();
		
	    return !checkout_invoice_data_missing;
	}
}

function checkVATNumber(fncAfterRun)
{
    vat_no = $("vat_no").value;
    countrycode = $("country").get("value");

    if (vat_countries.contains(countrycode) && vat_no != "")
    {
		req = new Request(
		{
		    url: ajax_hostname+'/ajaxRequest.php',
		    method: 'post',
		    data: 'a=checkVAT&vat='+vat_no+'&country='+countrycode,
		    onSuccess: function(txt)
		    {
				if (txt == "1")
				{
				    if (fncAfterRun != null) fncAfterRun();
				}
				else
				{
					alert("You entered an incorrect VAT Number" + txt);
				}
		    }
		}).send();
    }
    else
    {
    	if (fncAfterRun != null) fncAfterRun();
    }
    stopLoading();
}

function submitForm()
{
	$("form_checkout").submit();
}

function startLoading()
{
	$("checkout_loading").removeClass("hide");
	deactivateSubmitButton();
}
function stopLoading()
{
	$("checkout_loading").addClass("hide");
	activateSubmitButton();
}

function activateSubmitButton()
{
	$("form_submit").removeClass("disabled");
	$("form_submit").disabled = false;
}

function deactivateSubmitButton()
{
	$("form_submit").addClass("disabled");
	$("form_submit").disabled = true;
}

function network_active_change()
{
	if (!$('radio_ip_single').checked) { $('select_server_ips').disabled = true; }
}

function network_inactive_change(object)
{
	if (object.checked) { $('select_server_ips').disabled = false; }
}

function emailCheck(email)
{
   reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.]{2,63})+\.([A-Za-z]{2,4})$/;
   if (reg.test(email) == false)
   {
      return false;
   }
   else return true;
}

function validate(id)
{
    $(id+"_check").innerHTML = "<img src='/img/check_yes.png' alt='Check' />";
}
function unvalidate(id)
{
    $(id+"_check").innerHTML = "<img src='/img/check_no.png' alt='Check' />";
}
function resetvalidate(id)
{
    $(id+"_check").innerHTML = "";
}

function reloadVATField()
{
	if ($("country") != null)
	{
	    countrycode = $("country").options[$("country").selectedIndex].value;
	    if (!vat_countries.contains(countrycode))
	    {
			// D.h. NET an der EU drann -> bezillt nie TVA
			$("vat_row").addClass("hide");
			pay_vat = 0;
	    }
	    else
	    {
			$("vat_row").removeClass("hide");
			pay_vat = 1;
	    }
	}
}

function ToggleSiteswitch_dropdown()
{
	$("siteswitch_dropdown").toggle();
}

function getSelectedValue(selectid)
{
	return $(selectid).options[$(selectid).selectedIndex].value;
}
