<!-- Begin

function validate_form(form)
{
	var agreement_msg = "";
	var message = "Please, fix the following errors to continue:\n\n";
	var prefix;
	var result;
	var status = true;
	var type;

	if (form['contact_type']) {
		prefix = '';
		if (form['contact_type'].value == "affiliate") {
			prefix = 'affiliate_';
		}
		type = form['contact_type'].value.toUpperCase();
	} else {
		prefix = 'owner_';
		type = 'OWNER';
	}

	result = verify_contact(form, prefix);
	if (result[0] == false) {
		message += "\nPlease correct the " + type + " contact:\n";
	}
	status = status && result[0];
	message += result[1];

	if (form['flag_use_detailed'] && form['flag_use_detailed'].value > 0) {
		result = verify_contact(form, 'billing_');
		if (result[0] == false) {
			message += "\nPlease correct the BILLING contact:\n";
		}
		status = status && result[0];
		message += result[1];
	}

	if (form['termsandconditions'] && ! form['termsandconditions'].checked) {
		agreement_msg += "- the Registration Agreement is not checked.\n";
		status = false;
	}

	if (form['ciraverify'] && ! form['ciraverify'].checked) {
		agreement_msg += "- the CIRA Canadian Presence Agreement is not checked.\n";
		status = false;
	}
	
	if (form['transferverify'] && ! form['transferverify'].checked) {
		agreement_msg += "- the domain Owner Confirmation is not checked.\n";
		status = false;
	}
	
	if (agreement_msg != "") {
		message += "\nPlease correct the Agreement section:\n";
		message += agreement_msg;
	}

	if ( status == false ) {
		alert(message);
	}

	return status;
}

function verify_contact(form, type)
{
	var status = true;
	var message = "";
	var result = new Array(2);

	var country     = form[type + "country"];
	var province    = form[type + "province"];
	var state       = form[type + "state"];
	
	var first_name  = form[type + "first_name"];
	var last_name   = form[type + "last_name"];
	var org_name    = form[type + "org_name"];

	var address     = form[type + "address1"];
	var city        = form[type + "city"];
	var postal_code = form[type + "postal_code"];

	var phone       = form[type + "phone"];
	var phone_cc    = form[type + "phone_cc"];
	var phone_num   = form[type + "phone_num"];
	var phone_ext   = form[type + "phone_ext"];

	var fax         = form[type + "fax"];
	var fax_cc      = form[type + "fax_cc"];
	var fax_num     = form[type + "fax_num"];
	var fax_ext     = form[type + "fax_ext"];

	var email       = form[type + "email"];

    var field_names = new Array(
            "first_name",
            "last_name",
            "org_name",
            "address1",
            "city",
            "postal_code",
            "email"
    );

	var phone_regex = /^\+(\d){1,3}\.(\d){1,12}( *x(\d){1,4})?$/;
	var email_regex = /^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9\w\.-]+\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;

	if (first_name && first_name.value == "" ) {
        first_name = null;
		message += "- the first name is missing.\n";
		status = false;
	}

	if (last_name && last_name.value == "" ) {
        last_name = null;
		message += "- the last name is missing.\n";
		status = false;
	}

	if (org_name && org_name.value == "" ) {
		org_name.value = 'N/A';
	}

	if (address && address.value == "" ) {
        address = null;
		message += "- the street address is missing.\n";
		status = false;
	}

	if (city && city.value == "" ) {
        city = null;
		message += "- the city name is missing.\n";
		status = false;
	}

	if ( country && province && state &&
	 	 ! (country.selectedIndex || province.selectedIndex || state.selectedIndex) )
	{
		message += "- please, select ONE of: state, province or country outside US & Canada.\n";
		status = false;
	}

	if ( province && state && postal_code &&
	 	 (province.selectedIndex || state.selectedIndex) && postal_code.value == "" )
	{
		message += "- please, enter a valid postal/zip code\n";
		status = false;
	}

    if (phone_cc.options) {
        phone_cc = phone_cc.options[phone_cc.selectedIndex]
    }
    phone_cc.value  = phone_cc.value.replace(/\D/g, "");
    phone_num.value = phone_num.value.replace(/\D/g, "");
    phone_ext.value = phone_ext.value.replace(/\D/g, "");

    str = phone_cc.value + phone_num.value + phone_ext.value;

    if (str == "") {
        phone = null;
        message += "- the phone number is missing.\n";
        status = false;
    } else {
        //alert("phone number is: " + str);
        phone.value = "+" + phone_cc.value + "." + phone_num.value;
        if (phone_ext.value != "") {
            phone.value += "x" + phone_ext.value;
        }
    }

    if (fax_cc.options) {
        fax_cc = fax_cc.options[fax_cc.selectedIndex]
    }
    fax_cc.value  = fax_cc.value.replace(/\D/g, "");
    fax_num.value = fax_num.value.replace(/\D/g, "");
    fax_ext.value = fax_ext.value.replace(/\D/g, "");

    str = fax_cc.value + fax_num.value + fax_ext.value;

    if (str == "") {
        fax.value = "";
    } else {
        //alert("fax number is: " + str);
        fax.value = "+" + fax_cc.value + "." + fax_num.value;
        if (fax_ext.value != "") {
            fax.value += "x" + fax_ext.value;
        }
    }


	if (email) {
		if (email.value == "" ) {
            email = null;
			message += "- the email address is missing.\n";
            status = false;
		} else if (!email.value.match(email_regex)) {
			message += "- invalid email address.\n";
            status = false;
		}
	}

	if (form['biz'] && form['biz'].value == "1") {
		n_chars  = first_name ? first_name.value.length : 0;
		n_chars += last_name ? last_name.value.length : 0;
		if (n_chars > 30) {
			message += "- number of characters for first and last name combined needs to be 30 or less.\n";
			status = false;
		}
		n_chars = org_name ? org_name.value.length : 0;
		if (n_chars > 30) {
			message += "- number of characters for company name needs to be 30 or less.\n";
			status = false;
		}
		n_chars = address ? address.value.length : 0;
		if (n_chars > 30) {
			message += "- number of characters for address needs to be 30 or less.\n";
			status = false;
		}
	}
		
    if ( phone && !phone.value.match(phone_regex) ) {
        message += "- invalid phone number (country code & number required).  For the US & Canada, the country code is 1.\n";
        status = false;
    }

    if ( fax && fax.value != "" && !fax.value.match(phone_regex) ) {
        message += "- invalid fax number (country code & number required).\n- For the US & Canada, the country code is 1.\n";
        status = false;
    }

	result[0] = status;
	result[1] = message;

	return result;
}

// End -->
