﻿// JScript File

var val_expmonth="";
var val_expyear="";
var val_smonth="";
var val_syear="";

var val_email="";

var OrderFormTotal=0;
var OrderFormDelivery=0;

function GotoErrorsCheck(source, clientside_arguments){
	setTimeout("GotoErrors();",5);
	clientside_arguments.IsValid=true;
}

function GotoErrors(){
	//d=document.getElementById("focussetter");
	//d.style.display='';
	//d.focus();
	//d.enabled=false;
	//d.style.display:none;
	//d.style.visibility="hidden"
	window.location.hash="errorlist";

}
function GotoPaymentFail(){

	
	setTimeout("GotoPaymentFail2()",5);


}
function GotoPaymentFail2(){
	//alert("paymentfailed");
	window.location.hash="paymentfailed";
}

function setOrderFormDelivery(val){
	OrderFormDelivery=val;
	setOrderFormTotal();
}

function setOrderFormTotal(){
	var t=(OrderFormTotal*1)+(OrderFormDelivery*1)
	if(document.getElementById("OrderFormCalculatedValue"))
		document.getElementById("OrderFormCalculatedValue").innerHTML=t.toFixed(2);
}

function check_month(source, clientside_arguments)
{
	val_expmonth=clientside_arguments.Value;
}
function check_year(source, clientside_arguments)
{ 
	val_expyear=clientside_arguments.Value;
	clientside_arguments.IsValid= check_expirydate();
}

function check_expirydate(){
	if (val_expmonth!=""&&val_expyear!="")
	{
		if(val_expmonth>12)
			val_expmonth=0;
		
		d=new Date();
		d.setFullYear(val_expyear,val_expmonth,1)
		
		now=new Date()
		d.setHours(0);
		d.setMinutes(0);
		d.setSeconds(0);

		if(d<new Date()){
			return false;
		}
	}
	
	return true;
}

function check_smonth(source, clientside_arguments)
{
	val_smonth=clientside_arguments.Value;
}
function check_syear(source, clientside_arguments)
{ 
	val_syear=clientside_arguments.Value;
	clientside_arguments.IsValid= check_startdate();
}

function check_startdate(){
	if (val_smonth!=""&&val_syear!=""&&val_smonth!="-"&&val_syear!="-")
	{
		val_smonth--;
		
		d=new Date();
		d.setFullYear(val_syear,val_smonth,1)
		d.setHours(0);
		d.setMinutes(0);
		d.setSeconds(0);
		
		if(d>new Date()){
			return false;
		}
	}
	
	return true;
}

function check_email(source, clientside_arguments)
{
	val_email=clientside_arguments.Value;
}
function check_cemail(source, clientside_arguments)
{ 
	
	if(val_email!=clientside_arguments.Value){
		
		clientside_arguments.IsValid= false;
	}else{
		clientside_arguments.IsValid= true;
	}
}

var qtyboxes = "", prices="", cashprices="", tickettypes={}, totaltickets=0, maxtickets=-1;

function trackMaxValues(qtybox, max, tickettype, typename){
	if(qtybox!=null){
		if(!tickettypes.hasOwnProperty(tickettype)){
			tickettypes[tickettype]={}
			tickettypes[tickettype]["max"]=(max*1)
			tickettypes[tickettype][qtybox.id]=typename
		}else{
			tickettypes[tickettype][qtybox.id]=typename
		}
	}
}

function checkMaxValues(source, clientside_arguments){
	var max, count
	n=clientside_arguments.Value
	
	try{max=tickettypes[n]["max"];
	}catch(e){max=999}
	count=0;
	for (var x in tickettypes[n]) {
			if(x!="max"&&document.getElementById(x))
				if(!isNaN(document.getElementById(x).value))
					count+=(document.getElementById(x).value*1)
	}
	if(count>max){
		clientside_arguments.IsValid= false;
	}else{
		clientside_arguments.IsValid= true;
	}
		
}

function calculatetotals(qtybox, price, cashprice, tickettype){
	total=0; totalprice=0; totalcash=0
	newbox=true;
	if(qtyboxes==""){
		qtyboxes = new Array();
		prices=new Array(); cashprices=new Array()
	}
	for(i=0;i<qtyboxes.length;i++){
		//alert("i="+i);
		if(qtyboxes[i]!=null){
			if(!isNaN((qtyboxes[i].value*1))){
				total+=(qtyboxes[i].value*1);
			}
			if(!isNaN(((qtyboxes[i].value*1)*(prices[i]*1)))){
				totalprice+=((qtyboxes[i].value*1)*(prices[i]*1));
			}
			if(!isNaN(((qtyboxes[i].value*1)*(cashprices[i]*1)))){
				totalcash+=((qtyboxes[i].value*1)*(cashprices[i]*1));
			}
			if(qtyboxes[i]==qtybox)
			    newbox=false
		}
	}
	if(newbox){
		qtyboxes[qtyboxes.length]=qtybox;
		prices[prices.length]=price;
		cashprices[cashprices.length]=cashprice

		if(qtybox!=null){
			if(!isNaN((qtybox.value*1))){
				total+=(qtybox.value*1);
			}
			totalprice+=((qtybox.value*1)*(price*1));
			totalcash+=((qtybox.value*1)*(cashprice*1));
		}
	}
			
	if(document.getElementById("EventOptionsTicketTotal"))
		document.getElementById("EventOptionsTicketTotal").innerHTML=total;
	if(document.getElementById("EventOptionsCalculatedTotal"))
		document.getElementById("EventOptionsCalculatedTotal").innerHTML="£"+totalprice.toFixed(2);
    if(document.getElementById("EventOptionsTicketOnlyTotal"))
    	document.getElementById("EventOptionsTicketOnlyTotal").innerHTML="£"+totalcash.toFixed(2);
	
	totaltickets=total;
}
function check_totaltickets(source, clientside_arguments)
{ 
	
	if(totaltickets<=0){
		document.location="#errorlist";
		clientside_arguments.IsValid= false;
	}else{
		clientside_arguments.IsValid= true;
	}
}

function check_maxtickets(source, clientside_arguments)
{ 
	if(totaltickets>maxtickets&&maxtickets>=0){
		document.location="#errorlist";
		clientside_arguments.IsValid= false;
	}else{
		clientside_arguments.IsValid= true;
	}
	//source.innerHTML="Sorry, a maximum of " + maxtickets + " tickets can be bought per person/card/household for this event.";
}
