// JavaScript Document
function getCalendarHTML(){
	var oRequest = new XMLHttpRequest();
	var sURL  = "http://"+self.location.hostname+"/inc/calendarHtml.php?timestamp=" + new Date().getTime();
	var divObj=document.getElementById('scheduleCalendar2');
	
	if (divObj){
		oRequest.open("GET",sURL,false);
		oRequest.setRequestHeader("User-Agent",navigator.userAgent);
		oRequest.send(null)
		
		if (oRequest.status==200){
			divObj.style.display='block';
			divObj.innerHTML=oRequest.responseText;
		}
		else alert("Error executing XMLHttpRequest call!");
	}
}

function showPData(){
	var gid=document.getElementById('p_gid');
	var pid=document.getElementById('p_pid');
	var sName=document.getElementById('p_sname');
	var bDesc=document.getElementById('p_bdesc');
	
	
	
	if (!bDesc) bDesc='';

	if(gid && pid){	
		gid=gid.innerHTML;
		pid=pid.innerHTML;
		sName=sName.innerHTML;
		bDesc=bDesc.innerHTML;
		
		var sURL  = "http://"+self.location.hostname+"/inc/getprice.php?gid="+gid+"&pid="+pid+"&sname="+sName+"&bdesc="+bDesc+"&timestamp=" + new Date().getTime();
		var divObj=document.getElementById('pGrid');
		
		if (divObj){
			divObj.innerHTML=getHTTPData(sURL);
			setTimeout("hideObj('p_waiting')",2000);
			setTimeout("showObj('pGrid')",2001);
		}
	}
}

function getHTTPData(sURL){
	var oRequest = new XMLHttpRequest();
	oRequest.open("GET",sURL,false);
	oRequest.setRequestHeader("User-Agent",navigator.userAgent);
	oRequest.send(null)
	
	if (oRequest.status==200)
		return oRequest.responseText;
	else alert("Error executing XMLHttpRequest call!");
}

function showCalendar(){
	var objTemp;
	var strHTML, strHTML2;
	objTemp=document.getElementById('scheduleCalendar');
	if(objTemp){
		objTemp.style.display='block';
		
		objTemp=document.getElementById('testTD');
		strHTML='<table class="mainTable" cellspacing="1" cellpadding="0">' + 
			' <tr>' +
			'  <td class="monthYearText monthYearRow" colspan="7" title="Easy PHP Calendar 6">' +
			'    ' + aMonths[currMonth] + ' ' + currYear +
			'  </td>' +
			' </tr>' +
			' <tr class="dayNamesText">' +
			'  <td class="dayNamesRow" width="14%">Sun</td>' +
			'  <td class="dayNamesRow" width="14%">Mon</td>' +
			'  <td class="dayNamesRow" width="14%">Tue</td>' +
			'  <td class="dayNamesRow" width="14%">Wed</td>' +
			'  <td class="dayNamesRow" width="14%">Thu</td>' +
			'  <td class="dayNamesRow" width="14%">Fri</td>' +
			'  <td class="dayNamesRow" width="14%">Sat</td>' +
			' </tr>';
		strHTML2=displayCalendarGrid();
		objTemp.innerHTML=strHTML + strHTML2 + "</table>";
	
		highlightDate('today');
		highlightDate('inhouse engraving');
		highlightDate('express customization');
		highlightDate('standard delivery');
	}
}

function highlightDate(targetDateType){
	var strClassName, targetDate;
	var tableObj=document.getElementById('testTD');
	var tableCells=tableObj.getElementsByTagName("td");
	
	switch (targetDateType){
		case 'today':
			strClassName='calendarToday';
			targetDate=currDate;
			break;
		case 'inhouse engraving':
			strClassName='calendarInHouseEngraving';
			targetDate=currentMonthOffset(addDays(currDate,1));
			break;
		case 'express customization':
			strClassName='expressCustomization';
			targetDate=currentMonthOffset(addDays(currDate,3));
			break;
		case 'standard delivery':
			strClassName='standardDelivery';
			targetDate=currentMonthOffset(addDays(currDate,9));
			break;
	}
	
	
	for(var i=0; i<tableCells.length; i++){
		if(tableCells[i].innerHTML==targetDate && tableCells[i].className==currClassName){
			tableCells[i].className=strClassName;
			break;
		}
	}
}

function addDays(baseDate, intNumDays){
	var dateObj = new Date();
	var currDay, newTargetDate;
	dateObj.setDate(baseDate);
	currDay=dateObj.getDay();
	newTargetDate=baseDate;
	for(var i=0; i<intNumDays; i++){
		if (currDay<5){
			currDay++;
			newTargetDate++;
		}
		else{
			currDay=1;
			newTargetDate+=3;
		}
	}
	return newTargetDate;
}

function weekendOffset(intDate){
	var dateObj = new Date();
	dateObj.setDate(intDate);
	var dayOfWeek=dateObj.getDay();
	
	if (dayOfWeek==0 || dayOfWeek==6){
		intDate++;
		return weekendOffset(intDate);
	}
	else return intDate;
}

function currentMonthOffset(intNewDate){
	// this runs into next month
	if(intNewDate>currDays){
		currClassName='sNext';
		var dateObj=new Date();
		dateObj.setDate(currDays);
		var lastDayOfCurrMonth=dateObj.getDay();
		var intDaysOut=intNewDate-currDays;
		
		//uses modulus to calculate what day of the week we land on
		var intTemp=(lastDayOfCurrMonth+intDaysOut) % 7;
		if (intTemp==0)
			intDaysOut++;
		else if (intTemp==6)
			intDaysOut+=2;

		return intDaysOut;
	}
	else {
		currClassName='s2';
		return weekendOffset(intNewDate);
	}
}

function getDaysInMonth(intMonth, intYear){
	var intTemp;
	//if (intMonth && intYear){
		/*
		var intDate = new Date(intYear, intMonth, 32);
		intDate = intDate.getDate();
		intTemp=32-intDate;
		return intTemp;
		*/
		var intDate = new Date(intYear, intMonth+1, 0);
		return intDate.getDate();
	//}
}

function displayCalendarGrid(){
	var dateObj = new Date();
	dateObj.setDate(1);
	var day1=dateObj.getDay();
	var strTemp='<tr class="rows">';
	var currLastMonthDate=prevDays;
	var strTempClass;

	//first fill in previous month dates
	if (day1>0){			//if the 1st of the month is not sunday, fill in the previous days of the month
		currLastMonthDate-=day1-1;
		for(var i=0; currLastMonthDate<=prevDays && i<6; i++){
			strTemp+='<td class="sPrev">' + currLastMonthDate + '</td>';
			currLastMonthDate++;
		}
	}
	
	//fill in the rest of the days of the month
	for(i=1; i<=currDays; i++){
		dateObj.setDate(i);
		strTempClass='s2';
		if (dateObj.getDay()==0 || dateObj.getDay()==6 ){
			strTempClass='s200';
			if (dateObj.getDay()==6){
				strTemp+=' <td class="' + strTempClass + '">' + i + '</td>';
				strTemp+='</tr><tr class="rows">';
			}
			else
				strTemp+=' <td class="' + strTempClass + '">' + i + '</td>';
		}
		else
			strTemp+=' <td class="' + strTempClass + '">' + i + '</td>';
	}
	
	
	var totalDaysToDisplay=(6-dateObj.getDay())+14;
	var dayOfWeek;					//temporary day of the week counter for the for-loop below
	if (dateObj.getDay()==6){
		dayOfWeek=0;
	}
	else
		dayOfWeek=dateObj.getDay()+1;
	strTempClass='sNext';
	//fill in the next couple weeks of next month
	for(i=1; i<=totalDaysToDisplay; i++){
		if (dayOfWeek==0 || dayOfWeek==6 ){
			if (dayOfWeek==6){
				strTemp+=' <td class="' + strTempClass + '">' + i + '</td>';
				strTemp+='</tr><tr class="rows">';
			}
			else
				strTemp+=' <td class="' + strTempClass + '">' + i + '</td>';
		}
		else
			strTemp+=' <td class="' + strTempClass + '">' + i + '</td>';
		
		if (dayOfWeek==6)
			dayOfWeek=0;
		else
			dayOfWeek++;
	}

	return strTemp;
}

var aMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var dateObj = new Date();
var currDate = dateObj.getDate();
var currMonth = dateObj.getMonth();
var currYear = dateObj.getFullYear();
var currDays = getDaysInMonth(currMonth, currYear);
var prevDays,nextDays;
var currClassName='s2';
if (currMonth>0)
	prevDays = getDaysInMonth(currMonth-1, currYear);
else
	prevDays = getDaysInMonth(12, currYear-1);
if (currMonth<11)
	nextDays = getDaysInMonth(currMonth+1, currYear);
else
	nextDays = getDaysInMonth(1, currYear+1);
	
function showObj(strTemp){
	var elem;
	elem=document.getElementById(strTemp);
	elem.style.display='block';
}
function hideObj(strTemp){
	var elem;
	elem=document.getElementById(strTemp);
	elem.style.display='none';
}