// JavaScript Document

function returnEvents(){
	// get list of events
	xmlhttpPost("action=1",1);
	calcTotal();
}

function eventInfo(){
	var eventNum = form1.eventNum.options[form1.eventNum.options.selectedIndex].value;
	var eventName = form1.eventNum.options[form1.eventNum.options.selectedIndex].text;
	//var eventNum = form1.eventNum.selectedIndex.value;
	form1.event.value = eventName;
	// alert(form1.event.value);
	// get number of tickets available
	// get cost of ticket
	xmlhttpPost("action=2&id="+eventNum,2);
	document.form1.numtickets.value = 1;
	document.getElementById("button2").disabled = false;

}

function decCount(){
	var eventNum = form1.eventNum.options[form1.eventNum.options.selectedIndex].value;
	var numTix = form1.numtickets.value;
//	alert(eventNum);
//	alert(numTix);
	xmlhttpPost("action=3&id="+eventNum+"&num="+numTix,3);
	// do ajax perl submit.   eventnum and tixreq
}

function checkAvail(){
	var numChosen = 0;
	var availTickets = 0;
	numChosen = parseInt(form1.numtickets.value);
	availTickets = parseInt(form1.numavail.value);
	
	var status = 0;

	if(numChosen > availTickets){
		alert("There aren't enough tickets available.  Please contact Creative Spark directly or choose fewer tickets.");
	    document.getElementById("button2").disabled = true;
		calcTotal();
		status = 0;
	} else {
	    document.getElementById("button2").disabled = false;
		calcTotal();
		status = 1;
	}
	numChosen = 0;
	availTickets = 0;
	
	return status;

}

function calcTotal(){
	var numTix = parseInt(document.form1.numtickets.value);
	var cost = document.form1.cost.value;
	var sslAmount = numTix * cost;
//	document.form1.ssl_amount.value = sslAmount;
// forcing two decimal places
	document.form1.ssl_amount.value = sslAmount.toFixed(2).toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'');	
	
}

function xmlhttpPost(str,action) {
	var strUrl = "cgi-bin/xmlEditor.cgi?"+str;
	var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strUrl, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText,action);
        }
    }
    self.xmlHttpReq.send(str,action);
}

function updatepage(str,action){
	if(action == 1){
		// returns select list
		var info = eval('(' + str + ')');
		removeOptionLast();
		for(var i = 0; i < info.events.length; i++){
			appendOptionLast(info.events[i].optionValue, info.events[i].optionDisplay);
		}
	}
	if(action == 2){
		// returns number of available tickets and cost of tickets
		var info = eval('(' + str + ')');
		document.form1.event.value = form1.eventNum.options[form1.eventNum.options.selectedIndex].text;
		var numAvailable = info.numavail;
		if( parseInt(numAvailable) == 0){
			alert("Sorry, but there are no tickets left for this event.  Please contact Creative Spark directly for options");
	    	document.getElementById("button2").disabled = true;

		} else {
			document.form1.numavail.value = numAvailable;
			document.form1.cost.value = info.cost;
			calcTotal();			
		}
	}
	if(action == 3){
  	  form1.submit();
	}
	
}

function appendOptionLast(num, display)
{
  var elOptNew = document.createElement('option');
  elOptNew.value = num;
  elOptNew.text = display;
  var elSel = document.getElementById('eventNum');

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}

function removeOptionLast()
{
  var elSel = document.getElementById('eventNum');
  elSel.remove(0);
}

function formSubmit()
{ 
  var amt = document.form1.ssl_amount.value;
  
  var status = parseInt(checkAvail());
  
  if(amt < 1){
	  alert("You need to choose a valid ticket");
	  return;
  } else if ( status == 0) {
	  alert("You need to choose fewer tickets.");
	  return;
  } else {
	  decCount();
  }
}
