function getObj(objID)
{
    if (document.getElementById) {return document.getElementById(objID);}
    else if (document.all) {return document.all[objID];}
    else if (document.layers) {return document.layers[objID];}
}

function isChild(s,d) {
	while(s) {
		if (s==d) 
			return true;
		s=s.parentNode;
	}
	return false;
}

function Left(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function Top(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


// Calendar script
var calendar_date = '';
var now = new Date;
var sccm=now.getMonth();
var sccy=now.getFullYear();
var ccm=now.getMonth();
var ccy=now.getFullYear();
var a_m=ccm;
var a_y=ccy;
var tdcolor='';
var yes = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

var updobj;

var seat_product_codes = new Array();

function evtTgt(e)
{
	var el;
	if(e.target)el=e.target;
	else if(e.srcElement)el=e.srcElement;
	if(el.nodeType==3)el=el.parentNode; // defeat Safari bug
	return el;
}
function EvtObj(e){if(!e)e=window.event;return e;}

function cs_click(e) { // click on date
	hide_available_div();
	for(var d=1;d<=42;d++) if (getObj('v'+parseInt(d)).className=='dayselected') getObj('v'+parseInt(d)).className='futuredayactive'; // clear cell bg
	evtTgt(EvtObj(e)).className='dayselected';
	i = parseInt(evtTgt(EvtObj(e)).innerHTML);
	
	calendar_date = mn[a_m-1] + ' ' + i + ', ' + a_y;
	
	getObj("form_d").value = i;
	getObj("form_m").value = a_m;
	getObj("form_y").value = a_y;
	
	if(i<10) i = '0' + i;
	if(a_m<10) url='date='+a_y+'-0'+a_m+'-'+i;
	else url='date='+a_y+'-'+a_m+'-'+i;
	new net.ContentLoader("ajax_products.php", product, "","",url);
}
function _product(http_request){
	if(!http_request)
	{
		http_request=this.req;
	}
	var elDocRoot=http_request.responseXML.getElementsByTagName("product");
	k = elDocRoot.length;
	
	mainObj = document.getElementById("seats_available");
	while (mainObj.childNodes.length > 0) {
    	oneChild = mainObj.lastChild;
    	mainObj.removeChild(oneChild);
	}

	var newItem = document.createElement("tr");
	var td1 = document.createElement("th");
	td1.appendChild(document.createTextNode(" "));
	newItem.appendChild(td1);
	var td1 = document.createElement("th");
	td1.appendChild(document.createTextNode("Product"));
	newItem.appendChild(td1);
	var td1 = document.createElement("th");
	td1.appendChild(document.createTextNode("Adult"));
	newItem.appendChild(td1);
	var td1 = document.createElement("th");
	td1.appendChild(document.createTextNode("Child"));
	newItem.appendChild(td1);
	mainObj.appendChild(newItem);
	
	a = document.getElementById('form_a').value;
	c = document.getElementById('form_c').value;
	ch = 1;
	for(var i=0; i<k; i++){
		attrs = elDocRoot[i].attributes;
		
		var newItem = document.createElement("tr");
		
		var td1 = document.createElement("td");
		var td2 = document.createElement("td");
		var td3 = document.createElement("td");
		td3.setAttribute('align', 'right');
		var td4 = document.createElement("td");
		td4.setAttribute('align', 'right');
		
		code = attrs.getNamedItem("seattypecode").value;
		productcode = attrs.getNamedItem("productcode").value;
		seat_product_codes[productcode] = code;
		
		try{
			if(default_r == productcode && ch) {
				m_input = document.createElement('<input type="radio" name="r" checked/>');
				ch = 0;
			}
			else m_input = document.createElement('<input type="radio" name="r" />');
		}catch(err){
			m_input = document.createElement('input');
			if(default_r == productcode && ch) {
				m_input.setAttribute('checked', 'true');
				ch = 0;
			}
		}
		
		m_input.setAttribute('type','radio');
		m_input.setAttribute('name','r');

		if(default_r == productcode) {
			seatcode = code;
			getObj("r2").value = productcode;
			alert_change('a');
			ii = document.getElementById('form_d').value;
			a_m = document.getElementById('form_m').value;
			a_y = document.getElementById('form_y').value;
			
			if(ii<10) ii = '0' + ii;
			if(a_m<10) d='sdate='+a_y+'-0'+a_m+'-'+ii;
			else d='sdate='+a_y+'-'+a_m+'-'+ii;

			
			url = d+"&a="+a+"&c="+c+"&seattype=" + code+"&r="+productcode;

			if((a+c)>0){
				show_chart(url);
			} else {
				hide_available_div();
			}
		}
		m_input.id = 'folder'+productcode;

		m_input.value = productcode;
		m_input.onclick = showseats;
		
      	td1.appendChild(m_input);
      	td2.appendChild(document.createTextNode(elDocRoot[i].firstChild.nodeValue));
      	td3.appendChild(document.createTextNode('$ '+attrs.getNamedItem("adultprice").value));
      	td4.appendChild(document.createTextNode('$ '+attrs.getNamedItem("childprice").value));
      	
      	newItem.appendChild(td1);
      	newItem.appendChild(td2);
      	newItem.appendChild(td3);
      	newItem.appendChild(td4);
      	
      	mainObj.appendChild(newItem);
	}
	if(default_r == 0 && k>0) alert_change('p');
	getObj("available_div").style.display = 'block';
	main_init_d = 0;

}

function product(http_request){
	if(!http_request)
	{
		http_request=this.req;
	}
	var elDocRoot=http_request.responseXML.getElementsByTagName("product");
	k = elDocRoot.length;
	
	mainObj = document.getElementById("seats_available");
	while (mainObj.childNodes.length > 0) {
    	oneChild = mainObj.lastChild;
    	mainObj.removeChild(oneChild);
	}
	m_input = document.createElement('option');
	m_input.value = 0;
	m_input.appendChild(document.createTextNode('Choose the product'));
	mainObj.appendChild(m_input);

	a = document.getElementById('form_a').value;
	c = document.getElementById('form_c').value;
	ch = 1;
	for(var i=0; i<k; i++){
		attrs = elDocRoot[i].attributes;
		
		code = attrs.getNamedItem("seattypecode").value;
		productcode = attrs.getNamedItem("productcode").value;
		seat_product_codes[productcode] = code;

		
		m_input = document.createElement('option');
		if(default_r == productcode && ch) {
			m_input.setAttribute('selected', 'true');
			ch = 0;
		}

		if(default_r == productcode) {
			seatcode = code;
			getObj("r2").value = productcode;
			alert_change('a');
			ii = document.getElementById('form_d').value;
			a_m = document.getElementById('form_m').value;
			a_y = document.getElementById('form_y').value;
			
			if(ii<10) ii = '0' + ii;
			if(a_m<10) d='sdate='+a_y+'-0'+a_m+'-'+ii;
			else d='sdate='+a_y+'-'+a_m+'-'+ii;

			
			url = d+"&a="+a+"&c="+c+"&seattype=" + code+"&r="+productcode

			mainObj2 = document.getElementById("div_chart");
            if (mainObj2) {
			oneChild2 = mainObj2.lastChild;
			mainObj2.removeChild(oneChild2);
			}
			if((a+c)>0){
				show_chart(url);
			} else {
				hide_available_div();
			}
			
			if (mainObj2) {
             document.getElementById("div_chart").appendChild(img);
            }
		}

		m_input.value = productcode;

      	m_input.appendChild(document.createTextNode(elDocRoot[i].firstChild.nodeValue));


      	mainObj.appendChild(m_input);
	}
	if(default_r == 0 && k>0) alert_change('p');
	getObj("available_div").style.display = 'block';
	main_init_d = 0;

}

var mn=new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var mnn=new Array('31','28','31','30','31','30','31','31','30','31','30','31');
var mnl=new Array('31','29','31','30','31','30','31','31','30','31','30','31');
var calvalarr=new Array(42);

function f_cpps(obj) { // previos
	obj.className = 'previosday';
}

function f_cps(obj) { // future
	obj.className = 'futureday';
}

function f_cps_a(obj) { // future
	obj.className = 'futuredayactive';
}

function f_hds(obj) { // now
	obj.className = 'daynow';
}

// day selected
function prepcalendar(hd,cm,cy) {
	a_m=cm+1;
	a_y=cy;
	now=new Date();
	sd=now.getDate();
	td=new Date();
	td.setDate(1);
	td.setFullYear(cy);
	td.setMonth(cm);
	cd=td.getDay();
	
	//if (cd==0)cd=6; else cd--;
	
	try{
		getObj('mns').innerHTML='<div style="padding-top:3px;font-family:Tahoma;"><b>'+mn[cm]+ ' ' + cy+'</b></div>';
	}
	catch(e){
	}
	marr=((cy%4)==0)?mnl:mnn;
	for(var d=1;d<=42;d++) {
		f_cps(getObj('v'+parseInt(d)));
		getObj('v'+parseInt(d)).onclick=null;
		if ((d >= (cd -(-1))) && (d<=cd-(-marr[cm]))) {
			dip=(((d-cd < sd)&&(cm==sccm)&&(cy==sccy))||((cm<sccm)&&(cy<=sccy))||(cy<sccy) );
			htd=(d-cd==hd);
			if (dip){
				if ((d-cd)==hd)		{
					if(yes[parseInt(d-cd)] == 1) {
						f_cps_a(getObj('v'+parseInt(d)));
						getObj('v'+parseInt(d)).onclick=cs_click;
					}
					else f_cps(getObj('v'+parseInt(d)));
				}
				else	f_cpps(getObj('v'+parseInt(d)));
			}
			else {
				if(yes[parseInt(d-cd)] == 1) {
					f_cps_a(getObj('v'+parseInt(d)));
					getObj('v'+parseInt(d)).onclick=cs_click;
				}
				else f_cps(getObj('v'+parseInt(d)));
			}
			getObj('v'+parseInt(d)).innerHTML=d-cd;	
		}
		else {
			getObj('v'+d).innerHTML='&nbsp;';
			getObj('v'+parseInt(d)).style.cursor='default';
		}
	}
}

function caddm() {
	marr=((ccy%4)==0)?mnl:mnn;
	
	ccm+=1;
	if (ccm>12) {
		ccm=1;
		ccy++;
	}
	cdayf();
	//prepcalendar('',ccm,ccy);
}

function csubm() {
	marr=((ccy%4)==0)?mnl:mnn;
	
	ccm -= 1;
	if (ccm<=0) {
		ccm=12;
		ccy--;
	}
	cdayf();
	//prepcalendar('',ccm,ccy);
}

function cdayf() {
	if ((ccy<sccy)||((ccy==sccy)&&(ccm<=sccm))){
		ccy=sccy;
		ccm=sccm;
		prepcalendar('',ccm,ccy);
		if(ccm<sccm) ccm++;
	}
	else {
		for(var d=1;d<=42;d++) getObj('v'+parseInt(d)).style.background='';
		
    	getObj("cal_head").style.visibility = 'hidden';
		getObj("fc").style.background='url("ajax-loader.gif") center center no-repeat';	
		
		hide_available_div();
		
		for(var d=1;d<=42;d++) {
			f_cpps(getObj('v'+parseInt(d)));
			getObj('v'+d).innerHTML='&nbsp;';
		}
		for (i=0; i < yes.length; i++) yes[i] = 0;
		if(ccm < 10) send = "date="+ccy+"-0"+ccm+"-01";
		else send = "date="+ccy+"-"+ccm+"-01";
		new net.ContentLoader("ajax_events.php", days, "","",send);
		return;
	}
}

function days(http_request){		
	if(!http_request)
	{
		http_request=this.req;
	}
	var elDocRoot=http_request.responseXML.getElementsByTagName("day");
	k = elDocRoot.length;
	for(var i=0; i<k; i++){
		yes[elDocRoot[i].firstChild.nodeValue] = 1;
	}
	if(main_init_d>0){
		prepcalendar(main_init_d,main_init_m-1,main_init_y);
	}
	else{
		prepcalendar('',ccm-1,ccy);
	}
	getObj("fc").style.background='';
	getObj("cal_head").style.visibility = 'visible';
	
	// for initialisation
	if(main_init_d>0){
		for(var i=1; i<=42; i++) {
			getObj('v'+parseInt(i)).style.background=''; // clear cell bg
			if(parseInt(getObj('v'+parseInt(i)).innerHTML) == main_init_d) act = getObj('v'+parseInt(i));
		}
		if (act.className=='futuredayactive') {
			act.className='dayselected';
			
			getObj("form_d").value = main_init_d;
			getObj("form_m").value = main_init_m;
			getObj("form_y").value = main_init_y;
			
			if(main_init_d<10) main_init_d = '0' + main_init_d;
			if(main_init_m<10) url='date='+main_init_y+'-0'+main_init_m+'-'+main_init_d;
			else url='date='+main_init_y+'-'+main_init_m+'-'+main_init_d;
			new net.ContentLoader("ajax_products.php", product, "","",url);
			main_init_d=0;
		} else {
			alert_change('d');
		}
	}
	else {
		alert_change('d');
	}
}

function subform(){
	var a = parseInt(getObj("form_a").value);
	var c = parseInt(getObj("form_c").value);
	var rr = parseInt(getObj("r2").value);
	
	if(rr>0 && (a>0 || c>0)) getObj("av_seats").submit();
}
var seatcode = 0;
var temp;
function showseats(s) {
	if(s>0){
		rr = seat_product_codes[s];
		getObj("r2").value = s;
	}
	else rr = seatcode;
	if(rr != seatcode || (this.name != 'r' && seatcode>0)){
		alert_change('a');
		i = document.getElementById('form_d').value;
		a_m = document.getElementById('form_m').value;
		a_y = document.getElementById('form_y').value;
		
		if(i<10) i = '0' + i;
		if(a_m<10) d='sdate='+a_y+'-0'+a_m+'-'+i;
		else d='sdate='+a_y+'-'+a_m+'-'+i;

		a = document.getElementById('form_a').value;
		c = document.getElementById('form_c').value;
		url = d+"&a="+a+"&c="+c+"&seattype=" + rr+"&r="+s;
		if((a+c)>0){
			show_chart(url);
		} else {
			hide_available_div();
		}
		seatcode = rr;
	}
}

function show_chart(url) {
	var mainObj = document.getElementById("chart");
	mainObj.href =  chart_url + url;
	mainObj.style.display = 'block';
	
//	var mainObj2 = document.getElementById("chart_static");
//	mainObj2.style.display = 'none';
}

function hide_available_div(){
	if(main_init_d==0) getObj("r2").value = 0;
	getObj("available_div").style.display = 'none';
	mainObj = document.getElementById("seats_available");
	while (mainObj.childNodes.length > 0) {
    	oneChild = mainObj.lastChild;
    	mainObj.removeChild(oneChild);
	}
	
	alert_change('l');
		
	mainObj = document.getElementById("chart");
	mainObj.href = 'chart.jpg';
	mainObj.style.display = 'none';
	
//	var mainObj2 = document.getElementById("chart_static");
//	mainObj2.style.display = 'block';

	
	seatcode = 0;
}

function alert_change(s){
	getObj("loading").style.display = 'none';
	getObj("chose_date").style.display = 'none';
	getObj("chose_product").style.display = 'none';
	getObj("seats_available_alert").style.display = 'none';
	switch(s){
		case 'l':
			getObj("loading").style.display = 'block';
			break;
		case 'd':
			getObj("chose_date").style.display = 'block';
			break;
		case 'p':
			getObj("chose_product").style.display = 'block';
			break;
		case 'a':
			getObj("seats_available_alert").style.display = 'block';
			break;
	}
}

var main_init_d = 0;
var main_init_m = 0;
var main_init_y = 0;
var default_r = 0;

function init(init_d, init_m, init_y){
	default_r = document.getElementById('r2').value;
	if(init_d>0){
		main_init_d = init_d;
		main_init_m = init_m;
		main_init_y = init_y;
	}
	ccm = init_m;
	ccy = init_y;
	
	calendar_date = mn[init_m-1] + ' ' + init_d + ', ' + init_y;
	
	
	var mainObj = document.getElementById("chart");
	mainObj.title = calendar_date;

	getObj("fc").style.background='url("images/ajax-loader.gif") center center no-repeat';
	if(init_m<10) new net.ContentLoader("ajax_events.php", days, "","","date="+init_y+"-0"+init_m+"-01");
	else new net.ContentLoader("ajax_events.php", days, "","","date="+init_y+"-"+init_m+"-01");
	
	//hide_available_div();
}

