// function for validating and setting up worldpay

var frmvalidator  = new Validator("WorldPay");

function setupForm() {

 frmvalidator.addValidation("name","req","Please enter your Name");
 frmvalidator.addValidation("email","req","Please enter your E-Mail address");
 frmvalidator.addValidation("email","email");
 frmvalidator.addValidation("street","req","Please enter your Street");
 frmvalidator.addValidation("town","req","Please enter your Town/City");
 frmvalidator.addValidation("county","req","Please enter your County/State/Region");
 frmvalidator.addValidation("postcode","req","Please enter your Postcode");
 frmvalidator.addValidation("tel","req","Please enter your Telephone");
 frmvalidator.addValidation("tel","num","Telephone: Only numbers allowed with no spaces");
 frmvalidator.addValidation("tel","minlen=10","Telephone: This is too short!");

}

function copyDetails() {

	wp = document.WorldPay;

// make a copy of important stuff incase billing details are different
	wp.MC_name.value = wp.name.value;
	wp.MC_email.value = wp.email.value;
	wp.address.value = wp.street.value + ",\n" + wp.town.value + ",\n" + wp.county.value;
	wp.MC_address.value = wp.street.value + ",\n" + wp.town.value + ",\n" + wp.county.value;
	wp.MC_postcode.value = wp.postcode.value;
	wp.MC_tel.value = wp.tel.value;
	wp.MC_country.value = wp.country[wp.country.selectedIndex].value;
	wp.MC_countryString.value = wp.country[wp.country.selectedIndex].text;

}

function setupHosting() {

	setupForm();

	frmvalidator.addValidation("MC_domain1","req","Please enter a Domain");
	frmvalidator.addValidation("MC_domain1","domain");

	frmvalidator.setAddnlValidationFunction(postProcessHosting);

}


function postProcessHosting() {

	wp = document.WorldPay;

// check second domain
	if (wp.MC_domain2.value != "") {
		if (!validateDomain(wp.MC_domain2.value)) {
			alert('Secondary Domain Name invalid, please amend');
			return false;
		}
	}

	wp.desc.value = wp.MC_plan[wp.MC_plan.selectedIndex].value
		+ " hosting + domain(s) "
		+ wp.MC_domain1.value
		+ (wp.MC_domainStatus1[0].checked?" (trn)":" (reg)")
		+ (wp.MC_domain2.value!=""?
			", "+wp.MC_domain2.value
			+ (wp.MC_domainStatus2[0].checked?" (trn)":" (reg)")
			:'');

// set the type,amount and cartId
	wp.MC_type.value = 'hosting';
	wp.amount.value = plan[wp.MC_plan[wp.MC_plan.selectedIndex].value];
	wp.cartId.value = "hosting "
		+ wp.MC_plan[wp.MC_plan.selectedIndex].value;
	if (wp.MC_domain2.value != "" && wp.MC_domainStatus2[1].checked) {
		wp.amount.value = String(Number(wp.amount.value)+10);
		wp.cartId.value += " + secondary domain";
	} else if (wp.MC_domain2.value == "") {
		// change the second domain name status
		wp.MC_domainStatus2[1].checked = false;
		wp.MC_domainStatus2[0].value = "none";
	}

// save important stuff
	copyDetails();

	return true;

}

function setupRenew() {

	setupForm();
	frmvalidator.setAddnlValidationFunction(postProcessRenew);
}


function postProcessRenew() {

	wp = document.WorldPay;

// check second domain
	if (wp.MC_domain2.value != "") {
		if (!validateDomain(wp.MC_domain2.value)) {
			alert('Secondary Domain Name invalid, please amend');
			return false;
		}
	}

	wp.desc.value = wp.MC_plan[wp.MC_plan.selectedIndex].value
		+ " hosting + domain(s) "
		+ wp.MC_domain1.value
		+ (wp.MC_domain2.value!=""?", "+wp.MC_domain2.value:"");

// set the type,amount and cartId
	wp.MC_type.value = 'renew.hosting';
	wp.amount.value = plan[wp.MC_plan[wp.MC_plan.selectedIndex].value];
	wp.cartId.value = "renew hosting "
		+ wp.MC_plan[wp.MC_plan.selectedIndex].value;
	if (wp.MC_domain2.value != "") {
		wp.amount.value = String(Number(wp.amount.value)+10);
		wp.cartId.value += " + secondary domain";
	}

// save important stuff
	copyDetails();
	return true;

}

function setupDomains() {

	setupForm();

	frmvalidator.addValidation("MC_ns1","req","Please enter a the first Name Server");
	frmvalidator.addValidation("MC_ns1","domain");
	frmvalidator.addValidation("MC_ns2","req","Please enter a the second Name Server");
	frmvalidator.addValidation("MC_ns2","domain");

	frmvalidator.setAddnlValidationFunction(postProcessDomains)

}


function postProcessDomains() {

	wp = document.WorldPay;

// set the cartId
	wp.MC_type.value = 'domain';
	wp.cartId.value = "domains "

// save important stuff
	copyDetails();

	return true;

}

