/*** Lam Dre registration functions ***/
function validateForm() {
	// mandatory registration fields
	checkFields = MM_validateForm('firstname','','R','surname','','R','address','','R','city','','R','country','','R','phone','','R','email','','RisEmail');
	if (checkFields) {
		// ticket type selected
		var qTicket = 0;
		for(i=0;i<document.forms[0].ticket_type.length;i++) {
			if (document.forms[0].ticket_type[i].checked) {
				qTicket++;
				break;
			}
		}
		if (qTicket == 0) {
			alert('Please select a ticket type.');
			return false;
		}
		// sakya centre if member discount selected & not ordering STL membership
		if (document.forms[0].sakyacentre.value == '' && !document.forms[0].membership.checked && (document.forms[0].ticket_type[2].checked || document.forms[0].ticket_type[3].checked)) {
			alert('For the Sakya Member discount, please specify the Sakya Centre to which you belong.');
			document.forms[0].sakyacentre.focus();
			return false;
		}
		// donation amount if donation selected
		if (document.forms[0].donate.checked == true && document.forms[0].donate_amount.value == '') {
			alert('Please enter the amount you wish to donate.');
			document.forms[0].donate_amount.focus();
			return false;
		}
		document.forms[0].benefactor_amount.value = CommaFormatted(document.forms[0].benefactor_amount.value);
		document.forms[0].donate_amount.value = CommaFormatted(document.forms[0].donate_amount.value);
		document.forms[0].ticket_amount.value = CommaFormatted(CurrencyFormatted(document.forms[0].ticket_amount.value));
		return true;
	} else {
		return false;
	}
}

function calc_ticket(ticketPrice) {
	(!document.forms[0].ticket_type[7].checked) ? document.forms[0].benefactor_amount.value = '' : document.forms[0].benefactor_amount.value = CurrencyFormatted(ticketPrice);
	document.forms[0].ticket_amount.value = ticketPrice;
	calc_total();
}

function set_benefactor() {
	document.forms[0].benefactor_amount.value = 12000;
	calc_ticket(12000);
	document.forms[0].benefactor_amount.focus();
}

function calc_benefactor() {
	ticketPrice = document.forms[0].benefactor_amount.value;
	document.forms[0].ticket_type[7].checked = true;
	if (ticketPrice < 12000 || !isNumeric(ticketPrice)){
		alert('Benefactor ticket cost is Aus$12,000 or more. Please enter numbers only.');
		document.forms[0].benefactor_amount.value = 12000;
		calc_total();
		document.forms[0].ticket_type[7].focus();
	} else {
		calc_ticket(ticketPrice);
		calc_total();
	}
}

function calc_membership() {
	(document.forms[0].membership.checked == true) ? document.forms[0].membership_amount.value = 100 : document.forms[0].membership_amount.value = 0;
	calc_total();
}

function do_donate() {
	(document.forms[0].donate.checked == true) ? document.forms[0].donate_amount.focus() : document.forms[0].donate_amount.value = '';
	calc_total();
}

function calc_donate() {
	donation = document.forms[0].donate_amount.value;
	if ((donation != '' && !isNumeric(donation)) || (donation != '' && donation < 0)) {
		alert('Donation amount - please enter numbers only.');
		document.forms[0].donate_amount.value = '';
		calc_total();
		document.forms[0].donate_amount.focus();
	} else if (donation != '' && donation > 0) {
		document.forms[0].donate.checked = true;
		document.forms[0].donate_amount.value = CurrencyFormatted(donation);
		calc_total();
	} else if (donation == '' || donation == 0) {
		document.forms[0].donate.checked = false;
		document.forms[0].donate_amount.value = '';
		calc_total();
	}
}

function calc_total() {
	ticketPrice = document.forms[0].ticket_amount.value;
	ticketPrice = parseFloat(ticketPrice.replace(/,/, ""));
	membershipPrice = parseFloat(document.forms[0].membership_amount.value);
	if (document.forms[0].donate_amount.value == '') {
		donation = 0 
	} else {
		donation = document.forms[0].donate_amount.value;
		donation = parseFloat(donation.replace(/,/, ""));
	}
	ticketTotal = ticketPrice + membershipPrice + donation;
	(ticketTotal > 0) ? document.forms[0].total_amount.value = CommaFormatted(CurrencyFormatted(ticketTotal)) : document.forms[0].total_amount.value = '';
}

/*** END Lam Dre registration functions ***/

/*** Lam Dre PayPal functions ***/
function setTicketPrice(ttype, tprice) {
	document.forms['buy_ticket'].item_name.value=ttype;
	document.forms['buy_ticket'].amount.value=tprice;
	(ttype == 'Benefactor Ticket') ? document.forms['select_ticket'].benefactor_amount.value=12000 : document.forms['select_ticket'].benefactor_amount.value='';
}

function checkTicketForm() {
	ticketType = document.forms['buy_ticket'].item_name.value;
	ticketAmount = document.forms['buy_ticket'].amount.value;
	if (ticketType == '' || ticketAmount == '') {
		alert("Please select your ticket type.");
		return false;
	} else if (ticketType == 'Benefactor Ticket' && ((ticketAmount) < 12000 || !isNumeric(ticketAmount))) {
		alert('Benefactor ticket cost is Aus$12,000 or more. Please enter numbers only.');
		document.forms['select_ticket'].benefactor_amount.value='';
		document.forms['select_ticket'].benefactor_amount.focus();
		return false;
	}
	return true;
}

function checkDonationForm() {
	donation = document.forms['add_donation'].amount.value;
	if (donation == '' || (donation != '' && !isNumeric(donation)) || (donation != '' && donation < 0)) {
		alert('Donation amount - please enter numbers only.');
		document.forms['set_donation'].donate_amount.value = '';
		document.forms['set_donation'].donate_amount.focus();
		return false;
	}
	return true;
}

function checkPartPayment() {
	payment = document.forms['part_payment'].amount.value;
	if (payment == '' || (payment != '' && !isNumeric(payment)) || (payment != '' && payment < 0)) {
		alert('Payment amount - please enter numbers only.');
		document.forms['set_payment'].payment_amount.value = '';
		document.forms['part_payment'].payment_amount.focus();
		return false;
	}
	return true;
}
/*** END Lam Dre PayPal functions ***/

/*** Countdown script ***/
function countdown() {
	var today=new Date();
	var countdown=new Date(2009, 3, 25);
	//countdown.setMonth(countdown.getMonth()-1) //change to 0-11 month format
	var beforedayText=" until Lam Dre 2009!";
	var ondaytext="Lam Dre 2009 begins today!";
	var monthtext=new Array("Jan","Feb","Mar","April","May","June","July","Aug","Sep","Oct","Nov","Dec");
	var showdate="("+monthtext[countdown.getMonth()]+" "+countdown.getDate()+")";
	var one_day=1000*60*60*24; //milliseconds
	var calculatediff="";

	calculatediff=Math.ceil((countdown.getTime()-today.getTime())/(one_day));
	/*if (calculatediff<0) { // past event, countdown to next year
		var nextyeartoday=new Date();
		nextyeartoday.setFullYear(today.getFullYear()+1);
		calculatediff=Math.ceil((nextyeartoday.getTime()-today.getTime())/(one_day)+calculatediff);
	}*/
	var pluraldayornot=(calculatediff==1)? "day" : "days";
	if (calculatediff>0) {
		document.write("<b>"+calculatediff+" "+pluraldayornot+" "+beforedayText+"</b>");
	} else if (calculatediff==0) {
		document.write("<b>"+ondaytext+"</b>");
	}
}
/*** END Countdown script ***/

/*** General functions ***/

//-------------------------------------------------------------------
// isNumeric(value) - Returns true if value contains a positive float value
//-------------------------------------------------------------------
function isNumeric(val){return(parseFloat(val,10)==(val*1));}

function CurrencyFormatted(amount) {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = s;
	return s;
}

function CommaFormatted(amount) {
	var delimiter = ","; 
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3) {
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } 
  if (errors) { 
  	alert('Please enter the following information:\n'+errors);
	return false;
  } else {
  	return true;
  }
}

/*** END General functions ***/

