function showItem(itemname)
{
	document.getElementById(itemname).style.display = 'block';
}

function hideItem(itemname)
{
	document.getElementById(itemname).style.display = 'none';
}

function openpopup(naam, framenaam, breedte, hoogte)
{
	eigenschappen = "'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,width=" + breedte + ",height=" + hoogte +"'";
	window.open(naam, framenaam, eigenschappen);
}

function setItemRed(itemname){
        document.getElementById(itemname).style.color = "#ff0000";
}

function unSetItemRed(itemname){
        document.getElementById(itemname).style.color = "#0055a5";
}

function check_shopall_form() {
	  var ret = 0;

    // Standard form items
    ret += checkFormItem("q_shipping_voorletters");
    ret += checkFormItem("q_shipping_achternaam");
    ret += checkFormItem("q_shipping_straatnaam");
    ret += checkFormItem("q_shipping_huisnummer");
    ret += checkZipItem("q_shipping_postcode", "q_shipping_land");
    ret += checkFormItem("q_shipping_plaatsnaam");
    ret += checkTelItem("q_shipping_telnr_overdag", "q_shipping_land");
    ret += checkTelItem("q_shipping_telnr_savonds", "q_shipping_land");

    // Form items for extra billing info
    
    // Don't invalidate the form if these items aren't shown
    if (document.getElementById("q_anderfactuuradres").checked == true){ 
        ret += checkFormItem("q_billing_voorletters");
        ret += checkFormItem("q_billing_achternaam");
        ret += checkFormItem("q_billing_straatnaam");
        ret += checkFormItem("q_billing_huisnummer");
        ret += checkZipItem("q_billing_postcode", "q_billing_land");
        ret += checkFormItem("q_billing_plaatsnaam");
    }

    // ADSL info

    ret += checkTelItem("q_adsl_phone", "q_shipping_land");
    ret += checkFormItem("q_adsl_name");
    ret += checkDateItem("q_adsl_birthdate");

    // Payment info

    ret += checkFormItem("q_pay_acctnumber");
    ret += checkFormItem("q_pay_acctname");
    ret += checkFormItem("q_pay_acctcity"); 
		
    if (checkBankNr("q_pay_acctnumber")){
        alert("U heeft een ongeldig bank- of gironummer ingevuld");
        return false;
    }	

    if (ret > 0){
        alert("Niet alle verplichte velden zijn ingevuld. Controleer de rode velden.");
        return false;
    }
		
    if (document.getElementById("algemenevoorwaarden").checked != true){
        alert("U dient akkoord te gaan met de algemene voorwaarden van simPC");
        return false;
    }

    document.getElementById("shopall_form").onsubmit = returntrue;
    document.getElementById("shopall_form").submit();
    return true;
}

function cancel_shopall_form() {
	  document.getElementById("shopall_form").annuleer.value = 'annuleer';
		document.getElementById("shopall_form").submit();
}


function check_shop2_form(){
    var ret = 0;

    // Standard form items
    ret += checkFormItem("q_shipping_voorletters");
    ret += checkFormItem("q_shipping_achternaam");
    ret += checkFormItem("q_shipping_straatnaam");
    ret += checkFormItem("q_shipping_huisnummer");
    ret += checkZipItem("q_shipping_postcode", "q_shipping_land");
    ret += checkFormItem("q_shipping_plaatsnaam");
    ret += checkTelItem("q_shipping_telnr_overdag", "q_shipping_land");
    ret += checkTelItem("q_shipping_telnr_savonds", "q_shipping_land");

    // Form items for extra billing info
    
    // Don't invalidate the form if these items aren't shown
    if (document.getElementById("q_anderfactuuradres").checked == true){ 
        ret += checkFormItem("q_billing_voorletters");
        ret += checkFormItem("q_billing_achternaam");
        ret += checkFormItem("q_billing_straatnaam");
        ret += checkFormItem("q_billing_huisnummer");
        ret += checkZipItem("q_billing_postcode", "q_billing_land");
        ret += checkFormItem("q_billing_plaatsnaam");
    }

    // ADSL info

    ret += checkTelItem("q_adsl_phone", "q_shipping_land");
    ret += checkFormItem("q_adsl_name");
    ret += checkDateItem("q_adsl_birthdate");

    // Payment info

    ret += checkFormItem("q_pay_acctnumber");
    ret += checkFormItem("q_pay_acctname");
    ret += checkFormItem("q_pay_acctcity"); 
		
    if (checkBankNr("q_pay_acctnumber")){
        alert("U heeft een ongeldig bank- of gironummer ingevuld");
        return false;
    }		

    if (ret > 0){
        alert("Niet alle verplichte velden zijn ingevuld. Controleer de rode velden.");
        return false;
    }

    document.getElementById("shop2_form").onsubmit = returntrue;
    document.getElementById("shop2_form").submit();
    return true;
}

function checkFormItem(name){
    var item = document.getElementsByName(name);
    
    if (item.length == 0)
        return 0;

    if (item[0].value == "" || item[0].value == null){
        setItemRed(name + "_label");
        return 1;
    } else {
        // Hide because it might be shown after an earler error
        unSetItemRed(name + "_label");
    }

    return 0;
}

function check_shop3_form(){
    if (document.getElementById("algemenevoorwaarden").checked != true){
        alert("U dient akkoord te gaan met de algemene voorwaarden van simPC");
        return false;
    }

    document.getElementById("shop3_form").submit();

}


function openDemoWindow(url){
    window.open(url, "demowindow", "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=1024,height=768");

    return true;
}


function checkADSLform(){
    var pc = document.getElementsByName("pc")[0].value;
    var hn = document.getElementsByName("hn")[0].value;

    if(checkzip_nl(pc) && checkhn(hn)){
        document.getElementById("adslform").submit();
        return true;
    }
    
    alert("Vul a.u.b. uw postcode (vb: 1234AB) en huisnummer zonder toevoegingen in.");
    return false;
    
}

function checkZipItem(field, countryfield){

   
    if (document.getElementsByName(field).length == 0){
        return 0;
    }


    var temp = document.getElementsByName(countryfield)[0];
    var zipcode = document.getElementsByName(field)[0].value;
    var checkzipfunction;
    

    var country = temp[temp.selectedIndex].value;

    if (country == "Nederland"){
        checkzipfunction = checkzip_nl;
    } else if (country == "Belgie"){
        checkzipfunction = checkzip_be;
    } else {
        checkzipfunction = checkzip_generic;
    }

    if (checkzipfunction(zipcode)){
        unSetItemRed(field + "_label");
        return 0;
    }
    setItemRed(field + "_label");
    return 1;
}

function checkTelItem(field, countryfield){
    
    if (document.getElementsByName(field).length == 0){
        return 0;
    }

    var temp = document.getElementsByName(countryfield)[0];
    var telnum = document.getElementsByName(field)[0].value;
    var checktelfunc;

    var country = temp[temp.selectedIndex].value;


    if (country == "Nederland"){
        checktelfunc = checktel_nl;
    } else if (country == "Belgie"){
        checktelfunc = checktel_be;
    } else {
        checktelfunc = checktel_generic;
    }

    if (checktelfunc(telnum)){
        unSetItemRed(field + "_label");
        return 0;
    }
    
    setItemRed(field + "_label");

    return 1;
}
 

function checkzip_nl(zipcode){
	return !!zipcode.match(/^\s*[0-9]{4}\s*[a-eghj-npr-tvwxz]{2}\s*$/i);
}

function checkzip_be(zipcode){
	return !!zipcode.match(/^\s*[0-9]{4}\s*$/i);
}

function checkzip_generic(zipcode){
    // Check if not empty

    if (zipcode == null || zipcode == ""){
        return false;
    }

    return true;
}

function checkhn(hn){
    return !!hn.match(/^[0-9]+$/);
}

function checkform_infopakket(){

    var ret = 0;


    ret += checkFormItem("q_initials");
    ret += checkFormItem("q_surname");
    ret += checkFormItem("q_gender");
    ret += checkFormItem("q_streetname");
    ret += checkFormItem("q_housenumber");
    ret += checkZipItem("q_zipcode", "q_country");
    ret += checkFormItem("q_city");
    ret += checkTelItem("q_phone", "q_country");

    if (ret > 0){
        alert("Niet alle verplichte velden zijn ingevuld. Controleer de rode velden.");
        return false;
    } else {
        // Enable submitting
        //document.getElementById("info_order_form").onsubmit = returntrue;
        document.getElementById("info_order_form").submit();
    }

    return true;
}


function returntrue(){
    return false;
}

function checktel_nl(number){
	// This function will accept the three most common ways of noting a phone number:
	// +31 20 1234567 (standard international)
	// +31 (0)20 1234567 (international with 0)
	// 020 1234567 (standard national)
	// Spaces, hyphens and the like are all removed.
	
	// Sanitize number to national, remove non-numerical characters
    var cleannum = number.replace(/^\+31(\s*\(0\))?/, "0");
	cleannum = cleannum.replace(/\D+/g, "");
	
	// Number must now start with 0 and be ten characters long
	if (cleannum.charAt(0)  != "0" || cleannum.length != 10)
		return false;
	
	// Number can not start with 00, 0800, or 09
	if (cleannum.match(/^0(0|800|9)/))
		return false;
	
	
	return true;
}

function checktel_be (number){
	// Same for nl, but with belgian numbers. Yes, brilliance.
	// the notation with (0) doesn't seem to be used much in belgium, 
    // but we accept it anyway
	var cleannum = number.replace(/^\+32(\s*\(0\))?/, "0");
	cleannum = cleannum.replace(/\D+/g, "");
	
	// Number must now start with 0 and be nine or ten characters long
	if (!(cleannum.charAt(0) == "0" && (cleannum.length == 9 || cleannum.length == 10)))
		return false;
		
	// Number can not start with 00, 0800, or 090[012345]. (09[^0] is for Gent)
	if (cleannum.match(/^0(0|800|90[012345])/))
		return false;

	return true;
}

function checktel_generic(number){
    // Check if not empty

    if (number == null || number == ""){
        return false;
    }

    return true;
}


function getRadioValue(radio){

    for (var i = 0; i <= radio.length; i++)
        if (radio[i].checked)
            return radio[i].value;
}

function checkDateItem(datefield){
   
    if (document.getElementsByName(datefield).length == 0){
        return 0;
    }

    var date = document.getElementsByName(datefield)[0].value;

    if (date.match(/^[0-9]{2}-[0-9]{2}-[12][0-9]{3}$/)){
        unSetItemRed(datefield + "_label");
        return 0;
    } 

    setItemRed(datefield + "_label");
    return 1;
}

function checkBankNr(accfield) {
		var nr = document.getElementsByName(accfield);

		if (nr.length == 0) return 0;

 		var n = nr[0]; n.value = n.value.replace(/[^\d\.]/g,'');
		var c = n.value.replace(/\D/g,'').split(''), a = c.length, e = 0;
		if (a == 9) for (var i = 0; i < 9; i++) e += (9 - i) * c[i];
		if(a < 6 || a == 8 || e % 11 != 0 ||  a > 9) {
			setItemRed(accfield + "_label");
			return true;
		};

		unSetItemRed(accfield + "_label");
		return false;	
};

// Optellen van pakketprijzen

var prices = [];
	
function createGroupArray (size) {
		prices = new Array(size);
		for (i = 0; i<prices.length; i++) {
			prices[i] = 0;
		}
}
	
function updateGroupArray (loc, value) {
		prices[loc] = value;
}

function updateGroupArrayCheckbox (loc, value, field) {
		if (field.checked == true) {
			prices[loc] = value;
		}
		else prices[loc] = 0;
}

function reCalculate() {
		extraprice = 0;
		
		for (i = 0; i<prices.length; i++) {
			if (!prices[i]) prices[i] = 0;
			extraprice += prices[i];
		}	
		
		document.shopall.eprice.value = parseFloat(extraprice);
		document.shopall.totalprice.value = parseFloat(document.shopall.pprice.value) + parseFloat(document.shopall.eprice.value) + parseFloat(document.shopall.shipping.value);
}
