function docLoaded(){
	
	if (document.forms){
		var frms=document.forms
		for (var i=0;i<frms.length;i++){
			resetForm(frms[i]); // Also called if the form is reset!
			
			if (frms[i].name="contactFrm"){
				frms[i].Submit.disabled=true;
				setTimeout("setFormAction()", 5000);
			}
		}
	}
	
	doMenuLinks(); // Sets the active link
	
	if (document.title=="Montanha Lodge - Schedule - View"){ //this is the Schedule page.
		setTimeout("doSchedule()",1700);
		
	}
	
	if (document.title=="Montanha Lodge - Schedule2 - View"){ //this is the Schedule page.
		setTimeout("doSchedule2()",25);
		
	}
	
	if (document.title=="Montanha Lodge - Units - Edit" || document.title=="Montanha Lodge - Units - New"){ //this is the Schedule page.
		doColourPicker();
	}
	
	
}

function setFormAction(){
	
	if (document.forms){
		var frm=document.contactFrm;
		frm.action=frm.act.value + "jsSub=True";
		frm.Submit.disabled=false;
	}
	
	
}

function doColourPicker(){
	var tbl=newTable("ColourTbl");
	tbl.style.cursor="hand";
	tbl.deleteRow(0);
	var theTD=getEl("ColourTD");
	var theTbl;
	var theCell;
	var tr=8;
	var tc=46;
	theTD.appendChild(tbl);
	for (var r=0;r<tr;r++){
		theRow=tbl.insertRow(-1);
		for (var c=0;c<tc;c++){
			theCell=theRow.insertCell(-1);
			theCell.style.backgroundColor=cRGB(r*36,Math.floor(c*5.6), Math.floor( (r * c)/1.235) );
			theCell.innerText="_";
			theCell.onclick=function(){selectColor(this);};
		}
	}
}

function selectColor(o){
	var theColour=o.style.backgroundColor;
	UnitsFrm.Colour.value=theColour;	
	UnitsFrm.style.backgroundColor=theColour;
	
	
}

function doSchedule2(){
	var theTable;
	var theUnits=getEl("UnitsDiv").innerText.split("|");
	var TotalUnits=theUnits.length-1;
	var theBookings=getBookings();
	var theDataDiv=getEl("DataDiv");
	var	theCell;
	var theDateID;
	var MonthName=["January","February","March","April","May","June","July","August","September","October","November","December"];
	var UnitColor=new Array();
	var Group=getEl("TopLeftDiv").innerText;
	Group=Group.substring(Group.indexOf("(")+1,Group.indexOf(")"));
	
	
	for (var cc=0;cc<TotalUnits;cc++){
		UnitColor[cc]=theUnits[cc].split(",")[2];
	}
	
	
	setIDs();
	
	



}

function setIDs(){
	var T=getEl("tblTheYear");
	var y=parseInt(getEl("CurrentYear").innerText);
	for (var r=0;r<T.rows.length-1;r++){
		for (var c=0;c<T.rows(r).cells.length;c++){
			var t=T.rows(r).cells(c).children[0];
			doMonth(t,y,r*T.rows(r).cells.length+c);
		}
	}
}

function doMonth(o,y,m){
	var days=getDaysInMonth(y,m+1);
	var firstDay = new Date(y, m, 1).getDay();
	var rs=o.rows.length;
	var cs=o.rows(0).cells.length;
	var r=1;
	var c=0;
	for (var i=1; i<43;i++){
		var s=o.rows(r).cells(c);
		s.style.backgroundColor="#ffffff";
		if ((i-firstDay)<=days && i>firstDay){
			s.innerText=i-(firstDay);
			s.id=y+""+(m+1)+""+(i-(firstDay));
			
			s.onclick=doBooking;
			
			
		}else{
			s.innerHTML="&nbsp;";
			s.id="";
			s.title="";
		}
		c++;
		if (c>=cs){
			r++;
			c=0;
		}
	}
}

function doBooking(){
	var c=this.style.backgroundColor;
	if (c=="#ff0000"){
		c="#ffffff";	
	}else{
		c="#ff0000";
	}
	
	this.style.backgroundColor=c;
	
}

function setTheYear(d){
	var e=getEl("CurrentYear");
	var y;
	switch(d){
		case -1:
			y=parseInt(e.innerText)-1;
			break;
		case 0:
			y=new Date().getYear();			
			break;
		case 1:
			y=parseInt(e.innerText)+1;
			break;
	}
	
	e.innerText=y;
	
}


function doSchedule(){
	var theTable;
	var theUnits=getEl("UnitsDiv").innerText.split("|");
	var TotalUnits=theUnits.length-1;
	var theBookings=getBookings();
	var theDataDiv=getEl("DataDiv");
	var	theCell;
	var theDateID;
	var MonthName=["January","February","March","April","May","June","July","August","September","October","November","December"];
	var UnitColor=new Array();
	for (var cc=0;cc<TotalUnits;cc++){
		UnitColor[cc]=theUnits[cc].split(",")[2];
	}
	
	for (var i=theBookings[0].inDate.getYear();i<=theBookings[theBookings.length-1].outDate.getYear();i++){
		var theDiv=newDiv(i);
		theDataDiv.appendChild(theDiv);
	}
	
	for (var i=0;i<theBookings.length;i++){
		var tmp=[theBookings[i].inDate,theBookings[i].outDate];
		var tblID=[tmp[0].getYear()+""+tmp[0].getMonth(),tmp[1].getYear()+""+tmp[1].getMonth()];
		
		for (var m=0;m<=1;m++){
			if (!getEl(tblID[m])){
				var theTable=newTable(tblID[m]);
				var tl=theTable.rows[0].cells[0];
				tl.innerText=MonthName[tmp[m].getMonth()];
				tl.style.textAlign="left";
				getEl(tmp[m].getYear()).appendChild(theTable);
				for (var u=0;u<TotalUnits;u++){
					var theRow=theTable.insertRow(-1);
					
					theRow.style.backgroundColor=UnitColor[u];
					
					var tmpCell=theRow.insertCell(-1);
					tmpCell.innerText=theUnits[u].split(",")[1];
					tmpCell.style.textAlign="left";
					var DaysInMonth=getDaysInMonth(tmp[m].getYear(),tmp[m].getMonth()+1);
					for (var d=1;d<=DaysInMonth;d++){
						var newCell=theRow.insertCell(-1);
						newCell.id=tmpCell.innerText+tmp[m].getYear()+""+(tmp[m].getMonth()+1)+d+"";
						newCell.style.backgroundColor="#FFFFFF";
						newCell.innerHTML="&nbsp;&nbsp;&nbsp;";
						newCell.title="Not Booked";
					}
				}
			}
		}
	}
	
	for (var i=0;i<theBookings.length;i++){
		var days=getDays(theBookings[i].inDate,theBookings[i].outDate);
		var tmpBooking=theBookings[i];
		var tmpDate=new Date();
		for (var d=0;d<=days;d++){
			tmpDate.setYear(tmpBooking.inDate.getYear());
			tmpDate.setMonth(tmpBooking.inDate.getMonth());
			tmpDate.setDate(tmpBooking.inDate.getDate()+d);
			var theID=tmpBooking.Unit+tmpDate.getYear()+""+(tmpDate.getMonth()+1)+""+tmpDate.getDate()+"";
			var theEl=getEl(theID);
			theEl.style.backgroundColor=theEl.parentNode.style.backgroundColor;
			theEl.title=theBookings[i].Unit + "\nFrom:"+ theBookings[i].inDate.toDateString() + "\nTo:"+theBookings[i].outDate.toDateString();
			if (d==0){
				
				if (theEl.innerText.indexOf("Out")!=-1) {
					theEl.innerText+="-In"	
				}else{
					theEl.innerText="In";	
				}
			}
			if (d==days){
				theEl.innerText="Out";
			}
		}
	}
	getEl("LoadingText").style.display="none";
}

function getDays(fromDate,toDate){
	var date1=new Date();
	var date2=new Date();
	
	date1.setYear(fromDate.getYear());
	date1.setMonth(fromDate.getMonth());
	date1.setDate(fromDate.getDate());
	
	date2.setYear(toDate.getYear());
	date2.setMonth(toDate.getMonth());
	date2.setDate(toDate.getDate());
	
	var diff=date2.getTime()-date1.getTime();
	diff=Math.floor(diff / (1000*60*60*24));
	return diff;
	
	
}




function getBookings(){
	var tmpArr=getEl("BookingsDiv").innerText.split("|")	
	
	var a=new Array();
	
	for (var i=0;i<tmpArr.length-1;i++){
		var tmp=tmpArr[i].split(",");
		var o=new Object();
		o.Unit=tmp[2];
		o.inDate=new Date(tmp[3],tmp[4]-1,tmp[5]);
		o.outDate=new Date(tmp[6],tmp[7]-1,tmp[8]);
		a[i]=o;
	}
	return a;
}


function newDiv(ID){
	var d=document.createElement("div");
	d.id=ID;
	d.innerHTML="<i>"+ID+"</i>";
	return d;
}


function newTable(ID){
	var t=document.createElement("TABLE")
	var th=t.createTHead();
	var r=th.insertRow(-1);
	r.style.backgroundColor="#CCFFCC";
	for (var i=0;i<32;i++){
		var c=r.insertCell(-1);
		if (i>0) c.innerText=i;
	}
	t.id=ID;
	return t;
	
	
}


function cRGB (r,g,b) {
  var r = r.toString(16); if (r.length == 1) r = '0'+r;
  var g = g.toString(16); if (g.length == 1) g = '0'+g;
  var b = b.toString(16); if (b.length == 1) b = '0'+b;

  return "#" + r + g + b
}


function doMenuLinks(){
	var l=document.links;
	var cl=document.location.toString();
	var hr,dna,dn;
	
	for (var i=0;i<l.length;i++){
		hr=l[i].href;
		dna=hr.split("/");
		dn=dna[dna.length-1];
		if (cl.indexOf(dn)!=-1){
			l[i].style.color="#FFFFFF";
		}
	}
}


function resetForm(frm){
	// Get all the text inside the fields for validation comparison later.
	// While we are looping the elements, also set the title and functions.
	for (var i=0;i<frm.length;i++){
		var e=frm.elements[i];
		if (e.value !="Submit" && e.value !="Reset"){
			//e.oldValue=e.value;
			//e.title=e.oldValue;
			e.onfocus= function(){elFocus(this);}; //Clear the default text!
			e.onfocusout= function(){elFocusOut(this);}; //If blank, back to default else colour & Validate!
			e.style.border="1px solid #000000";	
			e.style.color="#999999";
		}
	}
}

function elFocus(o){
	if(o.value==o.title) {o.value="";};
	o.style.border="1px solid #0000FF";	
	o.style.color="#000000";
}

function elFocusOut(o){
	if(o.value==''){
		o.value=o.title;
	}else{
		o.style.border="1px solid #00CC00";	
		o.style.color="#000000";

		if(o.name=="Email"){
			if(o.value.indexOf("@")==-1) {
				o.style.border="2px solid #FF0000";
			}
		} else if (o.name.indexOf("_Y")!=-1){
			var nowY= new Date().getFullYear();
			if (parseInt(o.value) <nowY){
				o.style.border="2px solid #FF0000";	
			}
		} else if (o.name.indexOf("_M")!=-1){
			var M= parseInt(o.value);
			if (M <1 || M>12 ){
				o.style.border="2px solid #FF0000";	
			}
		}
	}
}

//setOptions(this.form.In_D,getDaysInMonth(this.form.In_Y.value,this.value))

function getDaysInMonth(Y,M){
	var dd = new Date(Y, M, 0);
	return dd.getDate();
}

function setOptions(o,d){
	var s=false;
	var cd=new Date();
	o.length=0
	for (var i=0;i<parseInt(d);i++){
		s=(i+1==cd.getDate()?true:false);
		o.options[i]=new Option(i+1, i+1, s, s);
	}
}

function getEl(elID){
	var e=false;
	if (document.getElementById(elID)){
		e=document.getElementById(elID);
	}
	return e;
}

function cleareDefaults(frm){
		for (var i=0;i<frm.length;i++){
		var e=frm.elements[i];
		if (e.value !="Submit" && e.value !="Reset"){
			if(e.value==e.title) {e.value=" * NA * ";};
		}
	}
}
