// JavaScript Document

d = document;

function shrink(target, size, speed) {
	t = d.getElementById(target);
	start = t.offsetHeight;
	speed = speed / 1000;
	timer = 0;
	
	for (i=start;i>size;i--) {
		
		setTimeout("t.style.height = '"+ i + "px'", (speed * timer));
		timer++;
	}
}

function expand(target, size, speed) {
	t = d.getElementById(target);
	start = t.offsetHeight;
	speed = speed / 1000;
	timer = 0;
	
	for (i=start;i<size;i++) {
		
		setTimeout("t.style.height = '"+ i + "px'", (speed * timer));
		timer++;
	}
}

function moveDiv() {
	div = d.getElementById('1');
	speed = 5000 / 1000;
	total = 100;
	timer = 0;
	
	for (i=0;i<total;i++) {
		
		setTimeout("div.style.marginTop = '" + i + "px'", (speed * timer));
		setTimeout("div.style.marginLeft = '" + i + "px'", (speed * timer));
		timer++;
		
	}
}

function loading(target, pic) {
	t = d.getElementById(target);
	t.innerHTML = '<table border="0" id="loading"><tr><td valign="top"><img src="' + pic.src + '" /></td><td valign="top"><span style="font-size: 150%;"> Loading...</span></td></tr></table>';
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
		
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)
		cents = "0" + cents;
		
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		
	num.substring(num.length-(4*i+3));
	
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function updateTotal(cRate, aRate/*, mRate*/) {
	f = d.forms['tickets'];
	adults = f.elements['adults'];
	children = f.elements['children'];
	td = d.getElementById('updatedTotal');
	var newTotal;
	
	cRate = parseFloat(cRate);
	aRate = parseFloat(aRate);
	numAdults = (adults.value == '') ? 0 : parseInt(adults.value);
	numChildren = (children.value == '') ? 0 :  parseInt(children.value);

	newTotal = (roundNumber((numChildren * cRate), 2) + roundNumber((numAdults * aRate), 2));

	f.elements['total'].value = newTotal;
	td.innerHTML = formatCurrency(newTotal);
}

function updateEasterTotal(cRate, aRate, cRate_member, aRate_member) {
	adults = $('easter_adults').value;
	children = $('easter_children').value;
	adults_member = $('easter_adult_member').value;
	children_member = $('easter_children_member').value;
	
	cRate = parseInt(cRate);
	aRate = parseInt(aRate);
	cRate_member = parseInt(cRate_member);
	aRate_member = parseInt(aRate_member);
	
	numAdults = (adults == '') ? 0 : parseInt(adults);
	numChildren = (children == '') ? 0 : parseInt(children);
	numAdults_member = (adults_member == '') ? 0 : parseInt(adults_member);
	numChildren_member = (children_member == '') ? 0 : parseInt(children_member);
	
	newTotal = ((numAdults * aRate) + (numChildren * cRate) + (numAdults_member * aRate_member) + (numChildren_member * cRate_member));
	
	$('easter_total').value = newTotal;
	$('updatedTotal').innerHTML = formatCurrency(newTotal);
}

function stepBack(i) {
	
	shrink('orderForm', '50', '1000');
	
	loading('content', pic1);
	
	if (i == '1') {
		showStepOne();
	}
	if (i == '2') {
		showStepTwo();
	}
}

function validateStepOne() {
	f = d.forms['personal'];
	e = f.elements;
	error = false;
	msg = "Please correct the following errors:\n\n";
	
	date = new Date();
	
	month = e['month'].value;
	month.toString();
	if (parseInt(month) == 0) {
		month = parseInt(month.substring(1));
	}
	else
		month = parseInt(month);
		
	day = e['day'].value;
	day.toString();
	if (parseInt(day) == 0) {
		day = parseInt(day.substring(1));
	}
	else 
		day = parseInt(day);
	
	if (e['lastName'].value == '') { error=true; msg+=" * Please enter your last name\n"; }
	if (e['firstName'].value == '') { error=true; msg+=" * Please enter your first name\n"; }
	if (e['address'].value == '') { error=true; msg+=" * Please enter your address\n"; }
	if (e['city'].value == '') { error=true; msg+=" * Please enter your city\n"; }
	if (e['country'].value == 'United States') {
		if (e['state'].value == '') { error=true; msg+=" * Please enter your state\n"; }
		if (e['zip'].value == '') { error=true; msg+=" * Please enter your zip code\n"; }
	}
	//else if (parseInt(e['zip'].value) != (e['zip'].value-0)) { error=true; msg+=" * Your zip code must be a number\n"; }
	//if (e['country'].value == '') { error=true; msg+=" * Please enter your country\n"; }
	if (e['email'].value == '') { error=true; msg+=" * Please enter your email\n"; }
	
	if (!parseInt(e['month'].value-0) || !parseInt(e['day'].value-0) || !parseInt(e['year'].value-0)) {
		error=true;
		msg+=" * You must enter a number for the day, month and year\n";
	}
	else {
		if (e['month'].value == '') { error=true; msg+=" * Please enter the month you will be visiting\n"; }
		if (e['day'].value == '') { error=true; msg+=" * Please enter the day you will be visiting\n"; }
		if (e['year'].value == '') { error=true; msg+=" * Please enter the year you will be visiting\n"; }
		else if (e['year'].value.length != 4) {error=true; msg+=" * Please enter a 4-digit year\n"; }
		else if (parseInt(e['year'].value) < date.getFullYear()) { 
			error=true; 
			msg+=" * The year you are visiting cannot be in the past\n"; 		
		}
			
		else if (month < (date.getMonth()+1) && parseInt(e['year'].value) == date.getFullYear()) { 
			error=true; 
			msg+=" * The month you are visiting cannot be in the past\n"; 
		}
		else if (month > 12 || month < 1) {
			error=true;
			msg+=" * The month must be between 1 and 12\n";
		}
			
		else if (day < (date.getDate()) && month == (date.getMonth()+1)  && parseInt(e['year'].value) == date.getFullYear()) { 
			error=true; 
			msg+=" * The day you are visiting cannot be in the past\n"; 
		}
		else if (day > 31 || day < 1) {
			error=true;
			msg+=" * The day must be between 1 and 31\n";
		}
	}
	
	if (error) {
		alert(msg);
		return false;
	}
	else {
		return true;
	}
}

function validateStepTwo() {
	f = d.forms['tickets'];
	e = f.elements;
	error = false;
	msg = "Please correct the following errors:\n\n";
	
	if (e['military']) {
		if (e['military'].value == '' && e['adults'].value == '' && e['children'].value == '') { 
			error=true; 
			msg+=" * You must have at least one ticket\n"; 
		}
		
		if (e['military'].value != '' && parseInt(e['military'].value) < 0) { 
			error=true;
			msg+=" * All ticket values must be positive.\n";
		}
		else if (e['military'].value != '' && e['military'].value != 0 && Math.floor(e['military'].value) == 0) {
		error=true;
		msg+=" * All ticket values must be whole numbers and greater than 0";
		}
		else {
			e['military'].value = Math.floor(e['military'].value);
		}
		
		if (parseInt(e['military'].value).toString() == 'NaN' && e['military'].value != '') {
			error=true;
			msg+=" * All ticket values must be numerical.\n";
		}
	}
	else if (e['adults'].value == '' && e['children'].value == '') { 
		error=true; 
		msg+=" * You must have at least one ticket\n"; 
	}
	
	if ((e['adults'].value != '' && parseInt(e['adults'].value) < 0) || (e['children'].value != '' && parseInt(e['children'].value) < 0)) {
		error=true;
		msg+=" * All ticket values must be positive.\n";
	}
	else if ((e['adults'].value != '' && e['adults'].value != 0 && Math.floor(e['adults'].value) == 0) || (e['children'].value != '' && e['children'].value != 0 && Math.floor(e['children'].value) == 0)) {
		error=true;
		msg+=" * All ticket values must be whole numbers and greater than 0";
	}
	else {
		e['adults'].value = Math.floor(e['adults'].value);
		e['children'].value = Math.floor(e['children'].value);
	}
	
	if ((parseInt(e['adults'].value).toString() == 'NaN' && e['adults'].value != '') || (parseInt(e['children'].value).toString() == 'NaN' && e['children'].value != '')) {
		error=true;
		msg+=" * All ticket value must be numerical.\n";
	}
	
	if (error) {
		alert(msg);
		return false;
	}
	else {
		return true;
	}
}

function validateCC() {
	f = d.forms['CCINFO'];
	e = f.elements;
	error = false;
	msg = "Please correct the following errors:\n\n";
	
	if (e['x_first_name'].value == '') {
		error=true;
		msg += " * You must enter your first name\n";
	}
	if (e['x_last_name'].value == '') {
		error=true;
		msg += " * You must enter your last name\n";
	}
	if (e['x_address'].value == '') {
		error=true;
		msg += " * You must enter your address\n";
	}
	if (e['x_city'].value == '') {
		error=true; 
		msg += " * You must enter your city\n";
	}
	if (e['x_state'].value == '') {
		error = true;
		msg += " * You must enter your state\n";
	}
	
	re = /^\d{5}$/;
	
	if (e['x_zip'].value == '') {
		error=true;
		msg += " * You must enter your zip\n";
	}
//	else if (!e['x_zip'].value.match(re)) {
//		error=true;
//		msg += " * Your zip must be a five digit string\n";
//	}
	if (e['x_country'].value == '') {
		error=true;
		msg += " * You must enter your country\n";
	}
	if (e['x_phone'].value == '') {
		error=true;
		msg += " * You must enter your phone number\n";
	}
	
	re = /^([a-z|A-Z|0-9|\_|\-|\.])*\@([a-z|A-Z|0-9|\_|\-|\.])*\.([a-z|A-Z|0-9|\_|\-|\.])*$/;
	
	if (e['x_email'].value == '') {
		error=true;
		msg += " * You must enter your email\n";
	}
	else if (!e['x_email'].value.match(re)) {
		error=true;
		msg += " * That is not a valid email address\n";
	}
	
	re = /^[0-9]{16}$/;
	
	if (e['x_card_num'].value == '') {
		error=true;
		msg += " * You must enter a credit card number\n";
	}
	else if (!e['x_card_num'].value.match(re)) {
		error=true;
		msg += " * You must enter your CC number in the format XXXXXXXXXXXXXXXX\n";
	}
	
	if (e['x_exp_date'].value == '') {
		error=true;
		msg += " * You must enter a credit card number\n";
	}
	
	if (e['x_card_code'].value == '') {
		error =true;
		msg += " * You must enter your CVC code\n";
	}
	
	if (error) {
		alert(msg);
		return false;
	}
	else {
		return true;
	}
}

function goToStepThree() {
	
	if (validateStepTwo()) {
		submitForm('orderForm', '50', '1000', '2', pic1);
		return true;
	}
	else 
		return false;
	
}

function createPDF(i, result) {
	if (result != '') {
		// result mode
		str = result.substring(0);
		d = document;
		c = d.getElementById('createTicket');
		c.innerHTML = '<a href="' + str + '" target="_blank">Click here to get your ticket!</a>';
		
		url = '/tickets/xml/sendEmail.php';
		parameters = '';
		loadXMLDoc(url, parameters);
	}
	else {
		// input mode
		url = '/tickets/public_html/html2ps.php?process_mode=single&URL=http%3A%2F%2Fwww.desertmuseum.org' + i +
			  '&batch%5B%5D=www.google.com&batch%5B%5D=www.altavista.com&batch' +
			  '%5B%5D=www.msn.com&pixels=800&scalepoints=1&renderimages=1&renderlinks=1&renderfields=1' +
			  '&media=Letter&cssmedia=screen&leftmargin=5&rightmargin=5&topmargin=5&bottommargin=5&' +
			  'encoding=&headerhtml=&footerhtml=&watermarkhtml=&pslevel=3&method=fpdf&pdfversion=1.3&' +
			  'output=2&convert=Convert+File';
		parameters = '';
		loadXMLDoc(url, parameters);
	}
}

function createPDF_easter(i, result, session) {
	if (result != '') {
		// result mode
		str = result.substring(0);
		d = document;
		c = d.getElementById('createTicket');
		c.innerHTML = '<a href="' + str + '" target="_blank">Click here to get your ticket!</a>';
		
		url = '/tickets/xml/sendEmail_easter.php?session=' + session;
		parameters = '';
		loadXMLDoc(url, parameters);
	}
	else {
		// input mode
		url = '/tickets/public_html/html2ps.php?process_mode=single&URL=http%3A%2F%2Fwww.desertmuseum.org' + i +
			  '&batch%5B%5D=www.google.com&batch%5B%5D=www.altavista.com&batch' +
			  '%5B%5D=www.msn.com&pixels=800&scalepoints=1&renderimages=1&renderlinks=1&renderfields=1' +
			  '&media=Letter&cssmedia=screen&leftmargin=5&rightmargin=5&topmargin=5&bottommargin=5&' +
			  'encoding=&headerhtml=&footerhtml=&watermarkhtml=&pslevel=3&method=fpdf&pdfversion=1.3&' +
			  'output=2&convert=Convert+File';
		parameters = '';
		loadXMLDoc(url, parameters);
	}
}

function popupCVC() {

	window.open("/tickets/cvc_help.php", 'cvc_help', "menubar=0,status=0,toolbar=0,location=0,directories=0,width=650,height=750");

}