var isDOM=document.getElementById?1:0;
var isIE=document.all?1:0;
var isNS4=navigator.appName=='Netscape'&&!isDOM?1:0;

function getRef(i, p) {
	p = !p ? document : p.navigator ? p.document:p;
	return isIE ? p.all[i]:isDOM ? (p.getElementById ? p : p.ownerDocument ).getElementById(i) : isNS4 ? p.layers[i] : null;
}

function goBack() {
		window.history.back();
}

var passminlen = 6;
var datesep = '/';
var timesep = ':';

function validate(field, type, name) {
	switch (type) {
		case 'string':
			if (field.value == '') {
				if(name != '')
					alert('Please enter a valid '+name);
				field.focus();
				return false;
			}
			break;
			
		case 'email':
			if ((field.value.indexOf('@') == -1 
					|| field.value.indexOf('.') == -1 
					|| field.value.indexOf('.', field.value.indexOf('@')) <= field.value.indexOf('@')+1
					|| field.value.length < 6)
					&& field.value != 'admin' ) {
				
				alert('Please enter a valid '+name);
				field.focus();
				return false;
			}
			break;
			
		case 'password':
			if (field.value == ''
					|| (field.value.length < passminlen && field.value != 'admin')
					|| (field.value.substr(0,3) == "$1$")) {
				alert('Please enter a valid '+name);
				field.focus();
				return false;
			}
			break;
			
		case 'number':
			for (var x=0; x<field.value.length; x++) {
				var ch = field.value.charAt(x);
				
				if (( ch < '0' || ch > '9' ) && ch != '.') {
					alert('Please enter a valid '+name);
					field.focus();
					return false; 
				}
			}
			break;
			
		case 'non0number':
			if (field.value == '' || Number(field.value) == 0) {
				alert('Please enter a valid '+name);
				field.focus();
				return false;
			}
			
			for (var x=0; x<field.value.length; x++) {
				var ch = field.value.charAt(x);
				
				if (( ch < '0' || ch > '9' ) && ch != '.') {
					alert('Please enter a valid '+name);
					field.focus();
					return false; 
				}
			}
			break;
			
		case 'date_mdy':
			var d = new Date(field.value.substr(6, 4)
												, Number(field.value.substr(0, 2))-1
												, field.value.substr(3, 2));
			
			var d2 = ((d.getMonth() < 9)?'0':'')+Number(d.getMonth()+1).toString()+datesep
								+((d.getDate() < 10)?'0':'')+Number(d.getDate()).toString()+datesep
								+Number(d.getFullYear()).toString();
			
			if ((field.value != d2 
					|| field.value.charAt(2) != datesep
					|| field.value.charAt(5) != datesep
					|| field.value.length != 10) && field.value != '') {
				alert('Please enter a valid '+name);
				field.focus();
				return false; 
			}
			break;
			
		case 'date_dmy':
			var d = new Date(field.value.substr(6, 4)
												, Number(field.value.substr(3, 2))-1
												, field.value.substr(0, 2));
			
			var d2 = ((d.getDate() < 10)?'0':'')+Number(d.getDate()).toString()+datesep
								+((d.getMonth() < 9)?'0':'')+Number(d.getMonth()+1).toString()+datesep
								+Number(d.getFullYear()).toString();
			
			if ((field.value != d2 
					|| field.value.charAt(2) != datesep
					|| field.value.charAt(5) != datesep
					|| field.value.length != 10) && field.value != '') {
				alert('Please enter a valid '+name);
				field.focus();
				return false; 
			}
			break;
			
		case 'time24':
			if (field.value.charAt(2) != timesep
					|| field.value.length != 5
					|| Number(field.value.substr(0, 2)) > 23
					|| Number(field.value.substr(3, 2)) > 59) {
				alert('Please enter a valid '+name);
				field.focus();
				return false; 
			}
			break;
			
		case 'radio':
			if (field.value == null) {
				alert('Please select a valid '+name);
				return false; 
			}
			break;
			
		case 'url':
			if (field.value.substr(0, 7) != 'http://'
					|| field.value.substr(0, 8) != 'https://' 
					|| field.value.indexOf('.') == -1 
					|| field.value.indexOf(' ') != -1 
					|| field.value.length < 11) {
				
				alert('Please enter a valid '+name);
				field.focus();
				return false;
			}
			break;

	}
	
	return true;
}

function valPassword(pass, confirm) {
		if (pass.value != confirm.value) {
			alert('The password confirmation does not match');
			pass.focus();
			return false;
		}
			
		return true;
}

function ConfirmDel(url, name) {
		if (confirm('Are you sure you want to delete this '+name+'?'))
			window.location.href = url;
}

function valLogin() {
	var f = window.document.login;

	ret = validate(f.email, 'email', 'email') && validate(f.password, 'password', 'password');
				
	return ret;
}

function valSearch() {
	var f = window.document.editform;
	ret = validate(f.lx_name, 'string', 'name')
				&& validate(f.lx_description, 'string', 'description')
				&& validate(f.lx_cost, 'float', 'cost')
				;
				
	return ret;
}

function valContact() {
	var f = window.document.contactform;

	ret = validate(f.lx_firstname, 'string', 'first name')
			&& validate(f.lx_lastname, 'string', 'last name')
			&& validate(f.lx_email, 'email', 'email')
			&& validate(f.lx_country, 'string', 'country')
			&& validate(f.lx_state, 'string', 'state')
			&& validate(f.lx_city, 'string', 'city')
			&& validate(f.lx_postalcode, 'string', 'zip/postal code')
			&& validate(f.lx_comments, 'string', 'comments')
			;

	return ret;
}

function valRequest() {
	var f = window.document.requestform;

	ret = validate(f.lx_firstname, 'string', 'first name')
			&& validate(f.lx_lastname, 'string', 'last name')
			&& validate(f.lx_email, 'email', 'email')
			&& validate(f.lx_country, 'string', 'country')
			&& validate(f.lx_state, 'string', 'state')
			&& validate(f.lx_city, 'string', 'city')
			&& validate(f.lx_address1, 'string', 'address')
			&& validate(f.lx_postalcode, 'string', 'zip/postal code')
			;

	return ret;
}

function valRegister() {
	var f = window.document.register;

	ret = validate(f.lx_email, 'email', 'email') 
		&& validate(f.lx_password, 'password', 'password')
		&& valPassword(f.lx_password, f.confirm)
		&& validate(f.lx_firstname, 'string', 'first name')
		&& validate(f.lx_lastname, 'string', 'last name');
		
	if(ret && f.lx_phone.value == '' && f.lx_fax.value == ''){
		alert("Please, enter a valid phone or fax number");
		return false;
	}

	if(ret){
		ret = validate(f.lx_address1, 'string', 'address')
			&& validate(f.lx_country, 'string', 'country')
			&& validate(f.lx_state, 'string', 'state')
			&& validate(f.lx_city, 'string', 'city')
			&& validate(f.lx_postalcode, 'string', 'zip/postal code')
			;
	}
	
	if(ret && f.lx_billing_different.checked == true){
		ret = validate(f.lx_b_attention_to, 'string', 'billing attention to')
					&& validate(f.lx_b_address1, 'string', 'billing address')
					&& validate(f.lx_b_country, 'string', 'billing country')
					&& validate(f.lx_b_state, 'string', 'billing state')
					&& validate(f.lx_b_city, 'string', 'billing city')
					&& validate(f.lx_b_postalcode, 'string', 'billing zip/postal code')
					;
	}

	return ret;
}

function valCheckout() {
	var f = window.document.register;
//	var phone = validate(f.lx_phone, 'string', '');
//	var fax = validate(f.lx_fax, 'string', '');

	ret = validate(f.lx_firstname, 'string', 'first name')
		&& validate(f.lx_lastname, 'string', 'last name');
		
	if(ret && f.lx_phone.value == '' && f.lx_fax.value == ''){
		alert("Please, enter a valid phone or fax number");
		return false;
	}

	if(ret){
		ret = validate(f.lx_address1, 'string', 'address')
			&& validate(f.lx_country, 'string', 'country')
			&& validate(f.lx_state, 'string', 'state')
			&& validate(f.lx_city, 'string', 'city')
			&& validate(f.lx_postalcode, 'string', 'zip/postal code')
			;
	}
	
	if(ret && f.lx_billing_different.checked == true){
		ret = validate(f.lx_b_attention_to, 'string', 'billing attention to')
					&& validate(f.lx_b_address1, 'string', 'billing address')
					&& validate(f.lx_b_country, 'string', 'billing country')
					&& validate(f.lx_b_state, 'string', 'billing state')
					&& validate(f.lx_b_city, 'string', 'billing city')
					&& validate(f.lx_b_postalcode, 'string', 'billing zip/postal code')
					;
	}
	
	return ret;
}

function changeSubgroup(sid) {
	var f = window.document.browse;
	var selectedValue = f.subgroup.options[f.subgroup.selectedIndex].value;

	if (selectedValue > 0)
		location.href = "product-group.php?sid="+sid+"&id=" + selectedValue;
}

function changeSource(sid,group_id) {
	var f = window.document.browse;
	var selectedValue = f.source.options[f.source.selectedIndex].value;

	location.href = "product-group.php?sid="+sid+"&id="+group_id+"&source=" + selectedValue;
}

function placeOrder(sid) {
	f = window.document.cart;

	f.action = "place-order.php?sid=" + sid;
	f.submit();
}

function updateCart() {
	f = window.document.cart;

	f.action = f.action + "&update=1";
	f.submit();
}

function updateCart1(sid) {
	f = window.document.cart;

	f.action = "confirm-checkout.php?sid="+sid+"&update=1";
	f.submit();
}

function AddtoCart() {
	f = window.document.products;

	empty = true;
	for(i=0;i<f.elements.length;i++) {
		if (f.elements[i].checked == true) {
			empty = false;
		}
	}

	if (empty) alert("Please, select at least one item");
	else f.submit();
}

function lxSelectMove(source, destine) {

	var src = getRef(source);
	var dst = getRef(destine);
	var temp = new Array();
	
	for (var x=0; x<src.length; x++)
		if (src.options[x].selected) {
			dst.options[dst.length] = new Option(src.options[x].text, src.options[x].value, false, false);
			temp.push(src.options[x].value);
		}
		
	for (x=0; x<temp.length; x++)
		for (y=0; y<src.length; y++)
			if (Number(src.options[y].value) == temp[x]) {
				src.remove(y);
				break;
			}
}

function lxSelectList(listname) {

	var lname = getRef(listname);
	
	for (var x=0; x<lname.length; x++)
		lname.options[x].selected = true;
}
