﻿
function validate_clientnumber() {
	var valid = true;
	if(util.byId('extra_clientnumber').style.display == 'block') {
		valid = /^[ ]*[0-9]{8,11}[ ]*$/.test(util.byId('M_client_number').value);
	}
	return valid;
}

function validate_username() {
	var valid = true;
	if(util.byId('extra_username').style.display == 'block') {
		valid = /^[ ]*[a-zA-Z0-9]{9,12}[ ]*$/.test(util.byId('M_client_username').value);
	}
	return valid;
}

function validate_activation() {
	var valid = true;
	if(util.byId('extra_activation').style.display == 'block') {
		valid = /^[ ]*[a-zA-Z0-9]{6}[ ]*(\-){0,1}[ ]*[0-9]{8}[ ]*$/.test(util.byId('M_client_activation_code').value);
	}
	return valid;
}

function validate_address() {
	var valid = true;
	if(util.byId('extra_otherdetails').style.display == 'block') {
		valid = util.byId('M_client_address').value != '';
	}
	return valid;
}

function validate_postcode() {
	var valid = true;
	if(util.byId('extra_otherdetails').style.display == 'block') {
		valid = util.byId('M_client_postcode').value.trim() != '';
	}
	return valid;
}

function validate_phone() {
	var valid = true;
	if(util.byId('extra_otherdetails').style.display == 'block') {
		valid = util.byId('M_client_phone').value != '';
	}
	return valid;
}

var vld = null;
function renewalLoad() {
	vld = new validator();
	vld.errorClass = 'error';
	vld.messageContainer = util.byId('errorDiv');
	vld.errorMessage = 'Please make sure you enter valid<br/>information into the fields below';
	
	vld.addField('Name', true, fieldTypes.text);
	vld.addField('Email', true, fieldTypes.email);
	if(document.getElementById('M_client_number') != null) {
		vld.addField('M_client_number', false, fieldTypes.text, validate_clientnumber);
		vld.addField('M_client_username', false, fieldTypes.text, validate_username);
		vld.addField('M_client_activation_code', false, fieldTypes.text, validate_activation);
		vld.addField('M_client_address', false, fieldTypes.text, validate_address);
		vld.addField('M_client_postcode', false, fieldTypes.postcode, validate_postcode);
		vld.addField('M_client_phone', false, fieldTypes.text, validate_phone);
	}
	vld.enableAutoValidation();
	
	setupProductOptions();
	
	if(initialItem == 'avf') sel(1);
	else if(initialItem == 'av') sel(2);
}

function extra_username() {
	util.byId('M_client_number').value = '';
	util.byId('extra_clientnumber').style.display = 'none';
	util.byId('extra_username').style.display = 'block';
}

function extra_activation() {
	util.byId('M_client_username').value = '';
	util.byId('extra_username').style.display = 'none';
	util.byId('extra_activation').style.display = 'block';
}

function extra_otherdetails() {
	util.byId('M_client_activation_code').value = '';
	util.byId('extra_activation').style.display = 'none';
	util.byId('extra_otherdetails').style.display = 'block';
}

function startagain() {
	util.byId('desc').options[0].selected = true;
	util.byId('Name').value = '';
	util.byId('Email').value = '';
	util.byId('M_client_number').value = '';
	util.byId('M_client_username').value = '';
	util.byId('M_client_activation_code').value = '';
	util.byId('M_client_address').value = '';
	util.byId('M_client_postcode').value = '';
	util.byId('M_client_phone').value = '';
	util.byId('extra_clientnumber').style.display = 'block';
	util.byId('extra_username').style.display = 'none';
	util.byId('extra_activation').style.display = 'none';
	util.byId('extra_otherdetails').style.display = 'none';
	util.byId('errorDiv').style.display = 'none';
	vld.clearErrors();
}

function product(name, price, rrp, licenseYears, download) {
	this.name = name;
	this.price = price;
	this.rrp = rrp;
	this.licenseYears = licenseYears;
	this.download = download;
}

var products = new Array();

function setupProductOptions() {
	var v = document.getElementsByName('product_type');
	for(var i=0; i<v.length; i++) v[i].onclick = productOptionChange;
	var v = document.getElementById('license_period');
	v.onclick = v.onchange = productOptionChange;
	var dd = document.getElementById('desc');
	dd.innerHTML = '';
	for(var i=0; i<products.length; i++) {
		if(products[i].download && products[i].licenseYears == 1) {
			util.createOption(dd, products[i].name, products[i].name, false);
		}
	}
	for(var i=0; i<dd.options.length; i++) { dd.options[i].originalValue = dd.options[i].text; }
	productOptionChange();
}

function productOptionChange() {
	var dd = document.getElementById('desc');
	var download = document.getElementById('productTypeDownload').checked;

	if(!download) document.getElementById('license_period').selectedIndex = 0;
	document.getElementById('license_period').disabled = !download;

	var years = document.getElementById('license_period').value.match(/\d/gi)[0] * 1;
	for(var i=0; i<products.length; i++) {
		if(products[i].licenseYears == years && products[i].download == download) {
			for(var j=0; j<dd.options.length; j++) {
				if(dd.options[j].originalValue == products[i].name) {
					dd.options[j].text = products[i].name + ' - £' + products[i].price;
					
					dd.options[j].rrp = 0;
					if(products[i].price != products[i].rrp) {
						dd.options[j].text += '*';
						dd.options[j].rrp = products[i].rrp;
						document.getElementById('offerrrp').style.display = 'block';
					}
				}
			}
		}
	}
	dd.onchange = ddlChange;
	ddlChange(null, dd);
}

function ddlChange(e, ob) {
	if(!ob) ob = this;
	if(ob.options[ob.selectedIndex].rrp != 0) {
		var rrp = document.getElementById('offerrrp');
		rrp.innerHTML = '*SRP: &pound;' + ob.options[ob.selectedIndex].rrp;
	}
}
