/*
$Id: check_zipcode.js,v 1.5 2009/04/14 13:35:32 max Exp $
vim: set ts=2 sw=2 sts=2 et:
*/

// check_zip_code_rules is defined in check_zipcode_js.tpl

function check_zip_code_field(cnt, zip) {
	var zip_error = false;
	var rules = {};

	if (!zip || zip.value == "")
		return true;

  zip.value = zip.value.replace(/^\s+/g, '').replace(/\s+$/g, '');

	var c_code = config_default_country;
	if (cnt && cnt.options) {
		if ((cnt.options.length > 0) && (cnt.selectedIndex < cnt.options.length)) {
			c_code = cnt.options[cnt.selectedIndex].value;
		}
	}

	if (c_code && typeof(window.check_zip_code_rules) != 'undefined' && typeof(check_zip_code_rules[c_code]) != 'undefined') {
		var rule = check_zip_code_rules[c_code];
		if (rule && rule.rules && rule.rules.constructor == Array && rule.rules.length > 0) {
			zip_error = true;
			for (var i = 0; i < rule.rules.length && zip_error; i++) {
				if (zip.value.search(rule.rules[i]) != -1)
					zip_error = false;
			}
		}
	}

	if (zip_error) {
		if (rule && rule.error && rule.error.length > 0)
			alert(rule.error);

		if (zip.focus)
			zip.focus();
	}

	return !zip_error;
}

function check_zip_code() {
	return check_zip_code_field(document.forms["registerform"].b_country, document.forms["registerform"].b_zipcode) &&
		check_zip_code_field(document.forms["registerform"].s_country, document.forms["registerform"].s_zipcode); 
}


