var newDay = 0;
var newMonth = 0;
var newYear = 0;

function makeObject(){
	var x;
	if(window.XMLHttpRequest){
		x = new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
		x = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return x;
}
var request = makeObject();

var compareCal = 0;

function createCalendar(parentID, theID, idname, type) {
	var theBody = document.getElementById(parentID);
	var theCal = document.createElement('div');
	var theInput = document.createElement('input');
	var compareCal = 0;

	theInput.id = theID;
	theCal.id = idname;
	theCal.name = idname;
	theInput.value = idname;
	theInput.type = 'hidden';
	theBody.appendChild(theInput);
	theBody.appendChild(theCal);

	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();

	ajax_regenerateCal(month, year, day, theID, type);
}
function createCalendarCompare(parentID, theID, idname, theID1, idname1, theID2, idname2) {
	var theBody = document.getElementById(parentID);
	var theCal = document.createElement('div');
	var theInput = document.createElement('input');
	var theInput1 = document.createElement('input');
	var theInput2 = document.createElement('input');

	theCal.id = idname;
	theInput.id = theID;
	theInput.value = idname;
	theInput.type = 'hidden';
	theInput1.id = theID1;
	theInput1.value = idname1;
	theInput1.type = 'hidden';
	theInput2.id = theID2;
	theInput2.value = idname2;
	theInput2.type = 'hidden';

	theBody.appendChild(theCal);
	theBody.appendChild(theInput);
	theBody.appendChild(theInput1);
	theBody.appendChild(theInput2);

	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();

	ajax_regenerateCalCompare(month, year, day, theID1, idname1, month, year, day, theID2, idname2, theID, 3);
}



function ajax_regenerateCal(nmonth, nyear, nday, xnid, ntype){
	var param = '?nday=' + nday + '&nmonth=' + nmonth + '&nyear=' + nyear + '&nid=' + xnid + '&ntype=' + ntype;
	newDay = nday;
	newMonth = nmonth;
	newYear = nyear;
	
	
	
	
	
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	

	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajaxDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}


	ajaxRequest.open("GET", "/cal.php" + param, true);
	ajaxRequest.send(null);
	
	
	
	// alert(param);
	request.open('GET', '/calendar.php' + param, true);
	request.send('');
	request.onreadystatechange = parse_regenerateCal;
}
function ajax_regenerateCalCompare(nmonth1, nyear1, nday1, xnid1, xname1, nmonth2, nyear2, nday2, xnid2, xname2, xnid, ntype){
	var param = '?nday1=' + nday1 + '&nmonth1=' + nmonth1 + '&nyear1=' + nyear1 + '&nid1=' + xnid1 + '&nname1=' + xname1 + '&nday2=' + nday2 + '&nmonth2=' + nmonth2 + '&nyear2=' + nyear2 + '&nid2=' + xnid2 + '&nname2=' + xname2 + '&nid=' + xnid + '&ntype=' + ntype;
	
	request.open('GET', '/calendar.php' + param, true);
	request.send('');
	request.onreadystatechange = parse_regenerateCal;
}
function parse_regenerateCal(){
	if(request.readyState == 4){
		var answer = trimme(request.responseText);
		var nx= answer.split("|");
		var ans = nx[1];
		for(i = 2; i < nx.length; i++) 
			ans = ans + "|" + nx[i]; 
		//alert(nx[0]);
		eval("document.getElementById('" + document.getElementById(nx[0]).value + "').innerHTML = ans");
	}
}



function trimme(sInString) {
  	sInString = sInString.replace ( /^s+/g, "" );// strip leading
	return sInString.replace( /s+$/g, "" );// strip trailing
}



function regenerateCal(nmonth, nyear, nday, nid, ntype) {
	//alert(document.getElementById(nid + '_day').value);
	if(compareCal == 1) {
		var val_cal1 = (nid == cal1 ? nyear : document.getElementById(cal1 + '_year').value) + '' + (nid == 'cal1' ? (nmonth < 10 ? '0'+nmonth : nmonth) : (document.getElementById(cal1 + '_month').value < 10 ? '0'+document.getElementById(cal1 + '_month').value : document.getElementById(cal1 + '_month').value)) + '' + (nid == cal1 ? (nday < 10 ? '0'+nday : nday) : (document.getElementById(cal1 + '_day').value < 10 ? '0'+document.getElementById(cal1 + '_day').value : document.getElementById(cal1 + '_day').value)); 
		var val_cal2 = (nid == cal2 ? nyear : document.getElementById(cal2 + '_year').value) + '' + (nid == cal2 ? (nmonth < 10 ? '0'+nmonth : nmonth) : (document.getElementById(cal1 + '_month').value < 10 ? '0'+document.getElementById(cal1 + '_month').value : document.getElementById(cal1 + '_month').value)) + '' + (nid == cal2 ? (nday < 10 ? '0'+nday : nday) : (document.getElementById(cal1 + '_day').value < 10 ? '0'+document.getElementById(cal1 + '_day').value : document.getElementById(cal1 + '_day').value));

		if(val_cal2 < val_cal1) {
			document.getElementById(alertCal).innerHTML = 'Invalid time selection';
		}
		else if(val_cal2 >= val_cal1) {
			document.getElementById(alertCal).innerHTML = '';
		}
	}
	ajax_regenerateCal(nmonth, nyear, nday, nid, ntype);
}