
var current_x = 0;
var http = GetXMLHTTPRequest();
var target_x = 0;

function Changepassword() {
	var password = document.getElementById('password').value;
	document.getElementById('password_label').style.color = password == '' ? 'red' : 'black';	
	var password2 = document.getElementById('password2').value;
	document.getElementById('password2_label').style.color = password2 == '' ? 'red' : 'black';		
	if (password == '' || password2 == '')
		return;
	if (password != password2) {
		alert('Passwords do not match.');
		return;
	}
	var url = 'changepassword' +
						'?password=' + md5(password);						
	http.open('GET', url, true); 
	http.onreadystatechange = HandleChangepassword;
	http.send(null);
}

function CheckItem(i) {
	var row = document.getElementById('row' + i);
	if (row.className == '')
		row.className = 'highlight';
	else
		row.className = '';
}

function Contact() {
	var name = document.getElementById('name').value;
	document.getElementById('name_label').style.color = name == '' ? 'red' : 'black';
	var email = document.getElementById('email').value;	
	var message = document.getElementById('message').value;
	document.getElementById('message_label').style.color = message == '' ? 'red' : 'black';
	if (name == '' || message == '')
		return;
	var url = 'contact' +
						'?name=' + name +
						'&email=' + email +
						'&message=' + message;						
	http.open('GET', url, true); 
	http.onreadystatechange = HandleContact;
	http.send(null);
}

function DisableUpdate() {
	document.getElementById('button0').disabled = true;
	document.getElementById('button1').disabled = true;
}

function Forgotpassword() {
	var mobile = document.getElementById('mobile').value;
	if (mobile == '') {
		document.getElementById('mobile_label').style.color = 'red';
		return;
	}
	else
		document.getElementById('mobile_label').style.color = 'black';
	var url = 'forgotpassword' +
						'?mobile=' + mobile;						
	http.open('GET', url, true); 
	http.onreadystatechange = HandleForgotpassword;
	http.send(null);
}

function GetXMLHTTPRequest() {
	var req = null;
	if (window.XMLHttpRequest)
		req = new XMLHttpRequest();
	else
		if (window.ActiveXObject)
			req = new ActiveXObject('Microsoft.XMLHTTP');
	return req;
}

function HandleChangepassword() {
	if (http.readyState != 4)
		return;
	if (http.responseText == 'ok.') {
		alert('Your password has been changed.');
		location.href = 'list';
	} else
		alert(http.responseText);
}

function HandleContact() {
	if (http.readyState != 4)
		return;
	alert(http.responseText);
	location.href = 'http://www.mobsaver.com';	
}

function HandleForgotPassword() {
	if (http.readyState != 4)
		return;
	alert(http.responseText);
	location.href = 'signin';	
}

function HandleMaillist() {
	if (http.readyState != 4)
		return;
	alert(http.responseText);
	location.href = 'http://www.mobsaver.com';	
}

function HandleSignin() {
	if (http.readyState != 4)
		return;
	if (http.responseText == 'ok.')
		location.href = 'list';
	else
		alert(http.responseText);
}

function HandleTellafriend() {
	if (http.readyState != 4)
		return;
	alert(http.responseText);
	location.href = 'http://www.mobsaver.com';	
}

function htmlspecialchars(html) {
	html = html.replace(/&/g, '&amp;');
	html = html.replace(/"/g, '&quot;');
	html = html.replace(/'/g, '&#039;');
	html = html.replace(/</g, '&lt;');
	html = html.replace(/>/g, '&gt;');
	html = html.replace(/\n/g, '<br />');
	return html;
}

function Init() {
	setInterval(Update, 1);
}

function InitChangepassword() {
	document.getElementById('password').focus();
}

function InitContact() {
	document.getElementById('name').focus();
}

function InitForgotpassword() {
	document.getElementById('mobile').focus();
}

function InitList() {
	if (!items.length)
		DisableUpdate();
}

function InitMaillist() {
	document.getElementById('name').focus();
}

function InitSignin() {
	document.getElementById('mobile').focus();
}

function InitTellafriend() {
	document.getElementById('name').focus();
}

function Interpolate(current, target) {
	var delta = target - current;
	if (Math.abs(delta) < 1)
			return target;
	return current + delta * 0.25;
}

function Intro(n) {
	if (n == 4)
		location.href = 'demo';
	else
		target_x = -760 * (n - 1);
}

function Maillist() {
	var name = document.getElementById('name').value;
	document.getElementById('name_label').style.color = name == '' ? 'red' : 'black';
	var email = document.getElementById('email').value;	
	if (!ValidateEmail(email))
		email = '';	
	document.getElementById('email_label').style.color = email == '' ? 'red' : 'black';
	var name2 = document.getElementById('name2').value;
	document.getElementById('name2_label').style.color = name2 == '' ? 'red' : 'black';
	var email2 = document.getElementById('email2').value;	
	if (!ValidateEmail(email2))
		email2 = '';	
	document.getElementById('email2_label').style.color = email2 == '' ? 'red' : 'black';	
	if (name == '' || email == '' || name2 == '' || email2 == '')
		return;	
	var url = 'maillist' +
						'?name=' + name +
						'&email=' + email +
						'&name2=' + name2 +
						'&email2=' + email2;						
	http.open('GET', url, true); 
	http.onreadystatechange = HandleMaillist;
	http.send(null);
}

function Nickname() {
	var msg = '';
	msg += 'If you have created a text message nickname with a wireless carrier such as Verizon,\n';
	msg += 'you will need to enter that nickname instead of your mobile number.';
	alert(msg);
}

function OpenWindow(w, h) {
	var x = (screen.width - w) / 2;
	var y = (screen.height - h) / 2;
	return open('', 'Preview',
							'directories=0,menubar=0,resizeable=1,scrollbars=1,status=0,toolbar=0,' +
							'left=' + x + ',top=' + y + ',' +
							'width=' + w + ',height=' + h + ',' +
							'screenX=' + x + ',screenY=' + y);
}

function OutputInvite() {
	var form = document.getElementById('mail_form');
	var html = '';
	html += '<html><head><title>Preview of mail</title></head><body>';
	html += '<br>';
	html += 'Hi ' + (form.name2.value != '' ? form.name2.value : "[Friend's name]") + ',<br><br>';
	html += (form.name.value != '' ? form.name.value : '[Your name]') + ' ';
	html += '&lt;' + (form.email.value != '' ? form.email.value : '[Your email]') + '&gt; ';
	html += 'would like to invite you to<br>';
	html += 'a brand new site that will help you save both time and money!<br><br>';
	html += '--------------------------------------------------------------------------------<br><br>';
	html += 'mobsaver - SMS price comparison tool<br>';
	html += "Compare prices for FREE on Amazon and eBay while you're out shopping via text messaging.<br>";
	html += '<a href="http://www.mobsaver.com" target="_blank">http://www.mobsaver.com</a><br><br>';	
	if (form.message.value != '') {
		html += '--------------------------------------------------------------------------------<br><br>';
		html += (form.name.value != '' ? form.name.value : '[Your name]') + "'s message:<br><br>";
		html += htmlspecialchars(form.message.value);
	}	
	html += '</body></html>';
	return html;
}

function OutputList() {
	var form = document.getElementById('mail_form');
	var html = '';
	html += '<html><head><title>Preview of mail</title></head><body>';
	html += '<br>';
	html += 'Hi ' + (form.name2.value != '' ? form.name2.value : "[Friend's name]") + ',<br><br>';
	html += (form.name.value != '' ? form.name.value : '[Your name]') + ' ';
	html += '&lt;' + (form.email.value != '' ? form.email.value : '[Your email]') + '&gt; ';
	html += 'would like to invite you to<br>';
	html += 'a brand new site that will help you save both time and money!<br><br>';
	html += '--------------------------------------------------------------------------------<br><br>';
	html += 'mobsaver - SMS price comparison tool<br>';
	html += "Compare prices for FREE on Amazon and eBay while you're out shopping via text messaging.<br>";
	html += '<a href="http://www.mobsaver.com" target="_blank">http://www.mobsaver.com</a><br><br>';	
	if (form.message.value != '') {
		html += '--------------------------------------------------------------------------------<br><br>';
		html += (form.name.value != '' ? form.name.value : '[Your name]') + "'s message:<br><br>";
		html += htmlspecialchars(form.message.value);
	}	
	html += '</body></html>';
	return html;
}

function PreviewInvite() {
	var preview = OpenWindow(760, 400); 
	if (preview) {
		preview.document.open();
		preview.document.write(OutputInvite());
		preview.document.close();
		preview.focus();
	}
}

function PreviewList() {
	var preview = OpenWindow(760, 400); 
	if (preview) {
		preview.document.open();
		preview.document.write(OutputList());
		preview.document.close();
		preview.focus();
	}
}

function Signin() {
	var mobile = document.getElementById('mobile').value;
	document.getElementById('mobile_label').style.color = mobile == '' ? 'red' : 'black';
	var password = document.getElementById('password').value;
	document.getElementById('password_label').style.color = password == '' ? 'red' : 'black';
	if (mobile == '' || password == '')
		return;	
	var url = 'signin' +
						'?mobile=' + mobile +
						'&password=' + md5(password);						
	http.open('GET', url, true); 
	http.onreadystatechange = HandleSignin;
	http.send(null);
}

function Tellafriend() {
	var name = document.getElementById('name').value;
	document.getElementById('name_label').style.color = name == '' ? 'red' : 'black';
	var email = document.getElementById('email').value;	
	if (!ValidateEmail(email))
		email = '';	
	document.getElementById('email_label').style.color = email == '' ? 'red' : 'black';
	var name2 = document.getElementById('name2').value;
	document.getElementById('name2_label').style.color = name2 == '' ? 'red' : 'black';
	var email2 = document.getElementById('email2').value;	
	if (!ValidateEmail(email2))
		email2 = '';	
	document.getElementById('email2_label').style.color = email2 == '' ? 'red' : 'black';	
	var message = document.getElementById('message').value;
	if (name == '' || email == '' || name2 == '' || email2 == '')
		return;	
	var url = 'tellafriend' +
						'?name=' + name +
						'&email=' + email +
						'&name2=' + name2 +
						'&email2=' + email2 +
						'&message=' + message;						
	http.open('GET', url, true); 
	http.onreadystatechange = HandleTellafriend;
	http.send(null);
}

function Update() {
	current_x = Interpolate(current_x, target_x);
	document.getElementById('intro').style.left = current_x;
}		

function UpdateCount(n) {
	var html = n + (n == 1 ? ' item' : ' items');
	document.getElementById('count0').innerHTML = html;
	document.getElementById('count1').innerHTML = html;
}

function UpdateList() {
	var list = '';
	var count = 0;
	for (var i = 0; i < items.length; i++) {
		var row = document.getElementById('row' + i);
		if (row.style.display != 'none')
			if (row.className == 'highlight') {
				if (list != '')
					list += ',';
				list += items[i];
				row.style.display = 'none';
				var line = document.getElementById('line' + i);
				if (line)
					line.style.display = 'none';
			}
			else
				count++;
	}	
	if (list == '') {
		alert('There are no items selected.');
		return;
	}
	UpdateCount(count);
	if (!count) {
		DisableUpdate();
		document.getElementById('heading').style.display = 'none';
		document.getElementById('no_items').style.display = '';
	}
	var url = 'list' +
						'?delete=' + list;						
	http.open('GET', url, true);
	http.send(null);
}

function ValidateEmail(email) {
	return email.match(/^[a-zA-Z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([a-zA-Z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([a-zA-Z]{2,6})$/);
}
