function externalLinks() { 
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
}
window.onload = externalLinks;

function validateEmail() {
	fields = new Array;
	if (document.email.from_name.value == "") {
		fields[fields.length] = "  Your Name\n";
	}
	if (document.email.from_address.value == "" || document.email.from_address.value.indexOf('@') == -1) {
		fields[fields.length] = "  Your E-mail Address\n";
	}
	if (fields.length > 0) {
		field_list = "";
		for (i = 0; i < fields.length; i++) {
			field_list = field_list + fields[i];
		}
		alert("Please fill in the following field(s):\n" + field_list);
		return false;
	}
	else {
		return true;
	}
}

function total() {
	var total = 0;
	for (var i = 0; i < document.sign_up.account_type.length; i++) {
		if (document.sign_up.account_type[i].checked) {
			if (document.sign_up.account_type[i].value == '1_user') {
				total += 3.95;
				if (document.sign_up.tasks_SSL.checked) {
					total += 2.5;
				}
				total += parseInt(document.sign_up.tasks_storage.options[document.sign_up.tasks_storage.selectedIndex].value) / 4;
			}
			else if (document.sign_up.account_type[i].value == '5_user') {
				total += 8.95;
				if (document.sign_up.tasks_pro_SSL.checked) {
					total += 5;
				}
				if (document.sign_up.tasks_pro_storage.value != '' && isNaN(document.sign_up.tasks_pro_storage.value)) {
					alert('The value in the extra storage field has to be a number.');
					document.sign_up.tasks_pro_storage.value = '';
					document.sign_up.tasks_pro_storage.focus();
				}
				else {
					total += 2.5 * parseInt(document.sign_up.tasks_pro_storage.value);
				}
			}
			else if (document.sign_up.account_type[i].value == '10_user') {
				total += 12.95;
				if (document.sign_up.tasks_pro_SSL.checked) {
					total += 5;
				}
				if (document.sign_up.tasks_pro_storage.value != '' && isNaN(document.sign_up.tasks_pro_storage.value)) {
					alert('The value in the extra storage field has to be a number.');
					document.sign_up.tasks_pro_storage.value = '';
					document.sign_up.tasks_pro_storage.focus();
				}
				else {
					total += 2.5 * parseInt(document.sign_up.tasks_pro_storage.value);
				}
			}
			else if (document.sign_up.account_type[i].value == '25_user') {
				total += 17.95;
				if (document.sign_up.tasks_pro_SSL.checked) {
					total += 5;
				}
				if (document.sign_up.tasks_pro_storage.value != '' && isNaN(document.sign_up.tasks_pro_storage.value)) {
					alert('The value in the extra storage field has to be a number.');
					document.sign_up.tasks_pro_storage.value = '';
					document.sign_up.tasks_pro_storage.focus();
				}
				else {
					total += 2.5 * parseInt(document.sign_up.tasks_pro_storage.value);
				}
			}
			else if (document.sign_up.account_type[i].value == 'unlimited') {
				total += 26.95;
				if (document.sign_up.tasks_pro_SSL.checked) {
					total += 5;
				}
				if (document.sign_up.tasks_pro_storage.value != '' && isNaN(document.sign_up.tasks_pro_storage.value)) {
					alert('The value in the extra storage field has to be a number.');
					document.sign_up.tasks_pro_storage.value = '';
					document.sign_up.tasks_pro_storage.focus();
				}
				else {
					total += 2.5 * parseInt(document.sign_up.tasks_pro_storage.value);
				}
			}
			else if (document.sign_up.account_type[i].value == 'special_unlimited') {
				total += 0;
				if (document.sign_up.tasks_pro_SSL.checked) {
					total += 5;
				}
				if (document.sign_up.tasks_pro_storage.value != '' && isNaN(document.sign_up.tasks_pro_storage.value)) {
					alert('The value in the extra storage field has to be a number.');
					document.sign_up.tasks_pro_storage.value = '';
					document.sign_up.tasks_pro_storage.focus();
				}
				else {
					total += 2.5 * parseInt(document.sign_up.tasks_pro_storage.value);
				}
				total += ' ($350 one time charge)';
			}
			else if (document.sign_up.account_type[i].value == 'special_1_user') {
				total += 0;
				if (document.sign_up.tasks_SSL.checked) {
					total += 2.5;
				}
				total += parseInt(document.sign_up.tasks_storage.options[document.sign_up.tasks_storage.selectedIndex].value) / 4;
				total += ' ($50 one time charge)';
			}
		}
	}
	document.getElementById('price').innerHTML = '$' + total;
}

function showAddOns() {
	for (var i = 0; i < document.sign_up.account_type.length; i++) {
		if (document.sign_up.account_type[i].checked) {
			if (document.sign_up.account_type[i].value.indexOf('1_user') != -1) {
				document.getElementById('add_on_instructions').style.display = 'none';
				document.getElementById('tasks_pro_add_ons').style.display = 'none';
				document.getElementById('tasks_add_ons').style.display = 'block';
			}
			else {
				document.getElementById('add_on_instructions').style.display = 'none';
				document.getElementById('tasks_add_ons').style.display = 'none';
				document.getElementById('tasks_pro_add_ons').style.display = 'block';
			}
		}
	}
	total();
}

function showPaymentForm(value) {
	if (value == 'credit_card') {
		document.getElementById('cc_form').style.display = 'block';
	}
	else {
		document.getElementById('cc_form').style.display = 'none';
	}
}

function validateSignUpData() {
	var errors = new Array();
	var check = 0;
	// check for account_type
	check = 0;
	for (i = 0; i < document.sign_up.account_type.length; i++) {
		if (document.sign_up.account_type[i].checked) {
			check++;
			if (document.sign_up.account_type[i].value != '1_user') {
				// check for numeric value for tasks pro storage
				if (document.sign_up.tasks_pro_storage.value < 0 || 
				    (isNaN(document.sign_up.tasks_pro_storage.value) && document.sign_up.tasks_pro_storage.value != '')) {
				    errors[errors.length] = 'The value for additional storage must be a number greater than zero.';
				}
			}
		}
	}
	if (check == 0) {
		errors[errors.length] = 'Please select a account type.'; 
	}
	var fields = new Array('account'
						  ,'username'
						  ,'password'
						  ,'password_confirm'
						  ,'first_name'
						  ,'last_name'
						  ,'email'
						  ,'address_1'
						  ,'city'
						  ,'state'
						  ,'zip'
						  ,'phone'
						  );
	var paymentMethod = document.sign_up.payment_method.options[document.sign_up.payment_method.selectedIndex].value;
	if (paymentMethod == '') {
		check++;
	}
	else if (paymentMethod == 'credit_card') {
		fields[fields.length] = 'cc_name';
		fields[fields.length] = 'cc_number';
	}
	// check for required text fields
	check = 0;
	for (i = 0; i < fields.length; i++) {
		eval('field = document.sign_up.' + fields[i] + '.value;');
		if (field == '') {
			check++;
		}
	}
	if (check > 0) {
		errors[errors.length] = 'Please fill in all required fields.'; 
	}
	if (paymentMethod == 'credit_card') {
		// check for cc_type
		check = 0;
		for (i = 0; i < document.sign_up.cc_type.length; i++) {
			if (document.sign_up.cc_type[i].checked) {
				check++;
			}
		}
		if (check == 0) {
			errors[errors.length] = 'Please select a credit card type.'; 
		}
		// check for expired cc
		var now = new Date();
		var month = now.getMonth() + 1;
		if (month < 10) {
			month = '0' + month;
		}
		now = parseInt('' + now.getFullYear() + month);
		var expDate = parseInt('' + document.sign_up.cc_year.options[document.sign_up.cc_year.selectedIndex].value
							  + document.sign_up.cc_month.options[document.sign_up.cc_month.selectedIndex].value
							  );
		if (expDate < now) {
			errors[errors.length] = 'It appears your credit card has expired, please check the expiration date.';
		}
	}
	// compare passwords
	if (document.sign_up.password.value != document.sign_up.password_confirm.value) {
		errors[errors.length] = 'The values in the two password fields do not match.';
	}
	if (errors.length > 0) {
		var errString = "The following need to be corrected:\n";
		for (i = 0; i < errors.length; i++) {
			errString += (i + 1) + '. ' + errors[i] + "\n";
		}
		alert(errString);
		return false;
	}
	else {
		return true;
	}
}
