NS4 = (document.layers) ? 1 : 0;
IE4 = (document.all) ? 1 : 0;
DOM = (document.getElementById) ? 1 : 0;
ver4 = (NS4 || IE4) ? 1 : 0;

function showLayer(idlayer) {
	//makes (in)vizible the "invoice" div
	if (NS4) {
		if (document.layers[idlayer].style.display=='none')
			document.layers[idlayer].style.display = "inline";
		else
			document.layers[idlayer].style.display = "none";
	} 
	
	else if (DOM) {
		if (document.getElementById(idlayer).style.display=='none')
			document.getElementById(idlayer).style.display = "inline";
		else
			document.getElementById(idlayer).style.display='none';
	}
	
	else {
		if (document.all[idlayer].style.display=='none')
			document.all[idlayer].style.display = "inline";
		else
			document.all[idlayer].style.display = "none";
	}
}

//general javascript functions
function isEmpty(vstr) {
	//first replace all spaces, tabs, enters with nothing	
	vstr=vstr.replace(/ /gi,"");	vstr=vstr.replace(/\t/gi,"");	vstr=vstr.replace(/\n/gi,"");	vstr=vstr.replace(/\r/gi,"");		
	if (vstr.length==0) return true;		//the string is empty (or filled with spaces, tabs etc)
	else return false;
}

function isNumPos(vstr) {
	if (isNaN(vstr) || vstr <= 0) return false;		//vstr is not a number, or is zero, or negative
	else return true;	
}

function digitsOnly(vstr) {
	//check if vstr has digits only
	var reg=/^\d+$/;
	if (reg.exec(vstr) == null) return false;
	else return true;
}

function myIsInt(vstr) {
	//check if vstr is integer
	var reg=/^-{0,1}\d+$/;
	if (reg.exec(vstr) == null) return false;
	else return true;
}

//form validation functions
function check_categ() {
	//check that category name is not empty 
	
	var v_field=document.frm.cat_name;
	var str=v_field.value;
	
	//category name required
	if (isEmpty(str)) {alert(LANG_CAT_ADD_CAT_NAME_REQUIRED);v_field.focus();return false;}
	
}


function check_product() {
	//check that product info are ok 
	
	//product name required
	var v_field=document.frm.prod_name;
	var str=v_field.value;
	if (isEmpty(str)) {alert(LANG_PROD_ADD_PROD_NAME_REQUIRED);v_field.focus();return false;}
	
	//category is required
	var v_field=document.frm.prod_cat_id;
	if (v_field[v_field.selectedIndex].value==0)
		{alert(LANG_PROD_ADD_CAT_REQUIRED);v_field.focus();return false;}
	
	//product code required
	var v_field=document.frm.prod_code;
	var str=v_field.value;
	if (isEmpty(str)) {alert(LANG_PROD_ADD_PROD_CODE_REQUIRED);v_field.focus();return false;}
	
	//product price1 required and numeric positive
	var v_field=document.frm.prod_standard_price;
	var price1=v_field.value;
	price1=price1.replace(/,/gi,".");
	if (isEmpty(price1)) {alert(LANG_PROD_ADD_PROD_PRICE1_REQUIRED);v_field.focus();return false;}
	if (!isNumPos(price1)) {alert(LANG_PROD_ADD_PROD_PRICE1_NUMERIC);v_field.focus();return false;}
	
	//product price2 required and numeric positive
	var v_field=document.frm.prod_reduced_price;
	var price2=v_field.value;
	price2=price2.replace(/,/gi,".");
	if (isEmpty(price2)) {alert(LANG_PROD_ADD_PROD_PRICE2_REQUIRED);v_field.focus();return false;}
	if (!isNumPos(price2)) {alert(LANG_PROD_ADD_PROD_PRICE2_NUMERIC);v_field.focus();return false;}
		
	//price2 (reduced) <= price1 (standard)
	if (Number(price2)>Number(price1)) {alert(LANG_PROD_ADD_PROD_PRICES_NOT_OK);v_field.focus();return false;}
	
	//stock, if provided must be integer
	var v_field=document.frm.prod_quantity;
	var str=v_field.value;
	if (!isEmpty(str)) 
		if (!myIsInt(str)) {alert(LANG_PROD_ADD_PROD_STOCK_NUMERIC);v_field.focus();return false;}

	//threshold, if provided must be integer
	var v_field=document.frm.prod_threshold;
	var str=v_field.value;
	if (!isEmpty(str)) 
		if (!myIsInt(str)) {alert(LANG_PROD_ADD_PROD_THRESHOLD_NUMERIC);v_field.focus();return false;}
	
}

function check_add_to_cart(prod_id) {
	var v_stock=eval('document.frm'+ prod_id +'.prod_quantity');	
	var old_stock=eval('document.frm'+ prod_id +'.old_quantity.value');
	var new_stock=old_stock+v_stock.value;
	if (isEmpty(v_stock.value) || !digitsOnly(v_stock.value))
		{alert(LANG_ADD_CART_STOCK_NUMERIC);v_stock.focus();return false;}
	if (new_stock<10)
		{alert(LANG_ADD_CART_STOCK_NOT_OK);v_stock.focus();return false;}
	
	eval('document.frm'+ prod_id +'.submit()');
}

function check_customer() {
	//check that customer info are ok 
	
	//company name required
	var v_field=document.frm.cust_company_name;
	var str=v_field.value;
	if (isEmpty(str)) {alert(LANG_CUST_COMPANY_REQUIRED);v_field.focus();return false;}

	//surname required
	var v_field=document.frm.cust_surname;
	var str=v_field.value;
	if (isEmpty(str)) {alert(LANG_CUST_SURNAME_REQUIRED);v_field.focus();return false;}

	//first name required
	var v_field=document.frm.cust_first_name;
	var str=v_field.value;
	if (isEmpty(str)) {alert(LANG_CUST_FNAME_REQUIRED);v_field.focus();return false;}

	//street required
	var v_field=document.frm.cust_street;
	var str=v_field.value;
	if (isEmpty(str)) {alert(LANG_CUST_STREET_REQUIRED);v_field.focus();return false;}

	//street number required
	var v_field=document.frm.cust_streetno;
	var str=v_field.value;
	if (isEmpty(str)) {alert(LANG_CUST_STREET_NO_REQUIRED);v_field.focus();return false;}

	//zipcode required
	var v_field=document.frm.cust_zipcode1;
	var str=v_field.value;
	if (isEmpty(str)) {alert(LANG_CUST_ZIPCODE_REQUIRED);v_field.focus();return false;}
	var v_field=document.frm.cust_zipcode2;
	var str=v_field.value;
	if (isEmpty(str)) {alert(LANG_CUST_ZIPCODE_REQUIRED);v_field.focus();return false;}

	//city required
	var v_field=document.frm.cust_city;
	var str=v_field.value;
	if (isEmpty(str)) {alert(LANG_CUST_CITY_REQUIRED);v_field.focus();return false;}

	//phone required
	var v_field=document.frm.cust_phone;
	var str=v_field.value;
	if (isEmpty(str)) {alert(LANG_CUST_PHONE_REQUIRED);v_field.focus();return false;}
	
	//email required
	var v_field=document.frm.cust_email;
	var str=v_field.value;
	if (isEmpty(str)) {alert(LANG_CUST_EMAIL_REQUIRED);v_field.focus();return false;}

	//if he completes the invoice fields, check them
	if (document.frm.show_invoice.checked) {
		//invoice street required
		var v_field=document.frm.invaddr_street;
		var str=v_field.value;
		if (isEmpty(str)) {alert(LANG_INV_STREET_REQUIRED);v_field.focus();return false;}
	
		//invoice street number required
		var v_field=document.frm.invaddr_streetno;
		var str=v_field.value;
		if (isEmpty(str)) {alert(LANG_INV_STREET_NO_REQUIRED);v_field.focus();return false;}
	
		//invoice zipcode required
		var v_field=document.frm.invaddr_zipcode1;
		var str=v_field.value;
		if (isEmpty(str)) {alert(LANG_INV_ZIPCODE_REQUIRED);v_field.focus();return false;}
		var v_field=document.frm.invaddr_zipcode2;
		var str=v_field.value;
		if (isEmpty(str)) {alert(LANG_INV_ZIPCODE_REQUIRED);v_field.focus();return false;}
	
		//invoice city required
		var v_field=document.frm.invaddr_city;
		var str=v_field.value;
		if (isEmpty(str)) {alert(LANG_INV_CITY_REQUIRED);v_field.focus();return false;}
	}
	
	document.frm.submit();
}

function check_read_terms() {
	//verify if the user has checked the 'Read terms of delivery' checkbox
	if (document.frm.read_terms.checked)
		document.frm.submit();
	else
		{alert(LANG_READ_TERMS_REQUIRED);return false;}
}

function calc_prices(shipping_cost, vat_tax) {
	var price_excl=document.frm.products_price_excl.value;		//default pick up (no shipping cost)
	var price_incl=document.frm.products_price_incl.value;
	
	for(i=0;i<document.frm.ord_shipping_type.length;i++) {		
		if (document.frm.ord_shipping_type[i].checked == true) {
			if (document.frm.ord_shipping_type[i].value=='M') {
				//shipping cost added 
				price_excl=document.frm.with_shipping_products_price_excl.value;
				price_incl=document.frm.with_shipping_products_price_incl.value;
			}	
		}		
	}
	
	document.getElementById('price_excl').innerHTML = price_excl;
	document.getElementById('price_incl').innerHTML = price_incl;	
}




