var PACKAGE = ".zip";
var PRODUCTS = new Array("webrack","deskrack","ddk");
var VERSIONS = new Array("1.1","1.0.a","1.0");
var DEVICE_NAMES = new Array("Display", "MP3 Player", "MP3 Search", "Ribbit", "RSS Reader", "Twitter", "XM Radio", "XPath Processor");
var details = " Device details page will be available soon."
var DEVICE_SHORT_DESC = new Array("Renders the data provided at its input.", "A basic MP3 player.", "Searches for MP3 files available on the Internet. Supports multiple search modules.", "A voiceware device built on the Ribbit platform."+details, "Basic RSS reader and aggregator.", "A simple Twitter client."+details, "Retrives what's currently on a Sirius XM sattelite channel.", "Extracts data from an XML document using XPath."+details);

function showTip(topic) {
	var tipElName = topic + "Tip";
	var tipEl = document.getElementById(tipElName);
	if("product" == topic) {
		tipEl.innerHTML = "";
	} else if("name" == topic) {
		tipEl.innerHTML = "Enter your name (optional).";
	} else if("email" == topic) {
		tipEl.innerHTML = "Don't worry, your email will not be shared.";
	} else if("username" == topic) {
		tipEl.innerHTML = "Minimum 4 characters, maximum 15 characters.";
	} else if("password" == topic) {
		tipEl.innerHTML = "Must be at least 6 characters long.";
	}
	tipEl.className = "tip";
}

function hideTip(topic) {
	var tipElName = topic + "Tip";
	var tipEl = document.getElementById(tipElName);
	tipEl.innerHTML = '';
	tipEl.className = "tip";
}


function hideAllTips() {
	hideTip('product');
	hideTip('name');
	hideTip('email');
	hideTip('downloadBtn');
	hideTip('username');
	hideTip('password');
}


function hiliteInput(name) {
	var inputEl = document.getElementById(name);
	inputEl.className = 'formInputSelected';
}

function unhiliteInput(name) {
	var inputEl = document.getElementById(name);
	inputEl.className = 'formInput';
}

function unhiliteAllInputs() {
	unhiliteInput('product');
	unhiliteInput('name');
	unhiliteInput('email');
	unhiliteInput('username');
	unhiliteInput('password');
}

function showError(topic) {
	var tipElName = topic + "Tip";
	var tipEl = document.getElementById(tipElName);
	if("product" == topic) {
		tipEl.innerHTML = "Select the product you wish to download.";
	} else if("email" == topic) {
		tipEl.innerHTML = "A valid email is required.";
	} else if("username" == topic) {
		tipEl.innerHTML = "A valid username is required.";
	} else if("password" == topic) {
		tipEl.innerHTML = "A valid password is required.";
	}
	tipEl.className = "error";
}

function toggleRegisterFields() {
	var registerCheckbox = document.getElementById("register");
	var usernameSection = document.getElementById("usernameSection");
	var passwordSection = document.getElementById("passwordSection");
	if(registerCheckbox.checked) {
		usernameSection.className = 'visibleFormField';
		passwordSection.className = 'visibleFormField';
	} else {
		usernameSection.className = 'invisibleFormField';
		passwordSection.className = 'invisibleFormField';
	}
}

function validateDownloadForm() {
	var ret = false;
	
	var product = document.getElementById('product').value;
	var email = document.getElementById('email').value;
	var registerFlag = document.getElementById('register').checked;
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;
	
	var isProductValid = product != '';
	var emailFilter = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
	var isEmailValid = emailFilter.test(email);
	var isUsernameValid = true;
	var isPasswordValid = true;
	if(registerFlag) {
		var upFilter = /[a-zA-Z0-9!@#$%^&*()_+-=]/i;
		isUsernameValid = upFilter.test(username) && (username.length>=4) && (username.length<=15);
		isPasswordValid = upFilter.test(password) && (password.length>=6) && (password.length<=15);
	}
	
	if(!isProductValid) showError('product');
	if(!isEmailValid) showError('email');
	if(!isUsernameValid) showError('username');
	if(!isPasswordValid) showError('password');
	
	if(isProductValid && isEmailValid && isUsernameValid && isPasswordValid) {
		ret = true;
		download();
		clearForm();
		if(product != 1) {
			document.getElementById('downloadBtnTip').innerHTML = "<span style='font-size:18px;color:#ffaa00;font-family:Arial;'>Thank you!</span>";
		} else {
			document.getElementById('downloadBtnTip').innerHTML = "Thank you! You were aded to the notification list.";
		}
	} else {
		document.getElementById('downloadBtnTip').innerHTML	= "";
	}
	
	return ret;
}

function download() {
	var downloadForm = document.forms[0];
	var pc = document.getElementById('product');
	var fr = document.getElementById('formRedirect');
	var es = document.getElementById('emailSubject');
	if(pc.value == 1) {
		fr.value = "http://virtual-devices.net/downloads.html";
	} else {
		fr.value = "http://virtual-devices.net/downloads/" + PRODUCTS[pc.value] + VERSIONS[pc.value] + PACKAGE;
		es.value = PRODUCTS[pc.value] + VERSIONS[pc.value] + " download request";
	}
	var emailBody = '';
	emailBody += 'product:'+PRODUCTS[pc.value];
	emailBody += ',version:'+VERSIONS[pc.value];
	if(downloadForm.elements['name'].value) {
		emailBody += ',name:'+downloadForm.elements['name'].value;
	}
	emailBody += ',email:'+downloadForm.elements['useremail'].value;
	if(downloadForm.elements['username'].value) {
		emailBody += ',username:'+downloadForm.elements['username'].value;
	}
	if(downloadForm.elements['password'].value) {
		emailBody += ',password:'+downloadForm.elements['password'].value;
	}
	downloadForm.elements['emailBody'].value = emailBody;
}

function clearForm() {
	document.getElementById('product').value = '';
	document.getElementById('name').value = '';
	document.getElementById('email').value = '';
	document.getElementById('register').checked = false;
	document.getElementById('username').value = '';
	document.getElementById('password').value = '';
}

function showDeviceShortDescription(devId) {
	var deviceShortDescription = document.getElementById('deviceShortDescription');
	deviceShortDescription.className = "shortDescriptionVisible";
	deviceShortDescription.innerHTML = "<h1>" + DEVICE_NAMES[devId] + "</h1>";
	deviceShortDescription.innerHTML += DEVICE_SHORT_DESC[devId];
}

function hideDeviceShortDescription() {
	var deviceShortDescription = document.getElementById('deviceShortDescription');
	deviceShortDescription.className = "shortDescriptionHidden";
	deviceShortDescription.innerHTML = "";
}
