if (typeof cca=='undefined')
	cca=1;
else
	cca++;

if (typeof ccs=='undefined')
	ccs='';

if (typeof propid=='undefined')
	propid=new Array(10);

propid[cca]=id;

if (typeof crid=='undefined')
	crid=new Array(10);
crid[cca]='null';

if (typeof button_img=='undefined')
	button_img='http://www.avail.co.za/images/availabilitycalendar.gif';

function trim(str) {
	if(!str || typeof str != 'string')
		return '';
	
	return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}

function lca() {
	links=document.getElementsByTagName('a');
	linkpresent=false;
	for(var i=0;i<links.length;i++)
		if (((links[i].href.indexOf('-direct.com') > 0) | ((links[i].href.indexOf('avail.co.za') > 0) & (links[i].innerHTML!='AVAIL'))) & (trim(links[i].innerHTML).length >= 5))
			linkpresent=true;

	if (!linkpresent)
		for(var i=1;i<=10;i++) {
			if (getObj('ii'+i))
				getObj('ii'+i).style.display='none';
			if (getObj('msga'+i))
				getObj('msga'+i).innerHTML='<div style="padding:10px;border:1px solid #ABABAB;background:#FFFFFF"><p><b>AVAIL / LINK REQUIRED</b></p><p>AVAIL is free for homeowners to use.  The only requirement is that a link is placed to one of the sites below on any page using the calendar.</p><li> www.capetown-direct.com</li><li> www.gardenroute-direct.com</li><li> www.durban-direct.com</li><li> www.krugerpark-direct.com</li><li> www.johannesburg-direct.com</li><li> www.victoriafalls-direct.com</li><li> www.kenyasafari-direct.com</li><p style="color:#FF0000><b>Place a link to one of the above sites somewhere on this page to remove this message</b></p></div>';
		}
				
}

function JSONscriptRequest(fullUrl) {
  // REST request path
  this.fullUrl = fullUrl; 
  // Keep IE from caching requests
  this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
  // Get the DOM location to put the script tag
  this.headLoc = document.getElementsByTagName("head").item(0);
  // Generate a unique script tag id
  this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
JSONscriptRequest.prototype.buildScriptTag = function () {
  // Create the script tag
  this.scriptObj = document.createElement("script");

  // Add script object attributes
  this.scriptObj.setAttribute("type", "text/javascript");
  this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
  this.scriptObj.setAttribute("id", this.scriptId);
}

// removeScriptTag method
JSONscriptRequest.prototype.removeScriptTag = function () {
  // Destroy the script tag
  this.headLoc.removeChild(this.scriptObj); 
}

// addScriptTag method
JSONscriptRequest.prototype.addScriptTag = function () {
  // Create the script tag
  this.headLoc.appendChild(this.scriptObj);
}

if (propid=='') {
	document.write('No property specified');
}
else {
	
	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 EvtObj(e){if(!e)e=window.event;return e;}
	
	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;
	}
	
	function checkClick(e) {
		e?evt=e:evt=event;
		CSE=evt.target?evt.target:evt.srcElement;
		if (getObj('ac'))
			if (!isChild(CSE,getObj('ac'))) {
				getObj('ac').style.display='none';
			}
	}
	
	function isChild(s,d) {
		while(s) {
			if (s==d) 
				return true;
			s=s.parentNode;
		}
		return false;
	}
	
	document.all?document.attachEvent('onclick',checkClick):document.addEventListener('click',checkClick,false);
	
	function lcal(tobjid,e,cn) {
		ccs=cn;
		EvtObj(e).cancelBubble=true;
		getObj('ac').style.left=Left(getObj(tobjid)) +'px';
		getObj('ac').style.top=Top(getObj(tobjid))- (-getObj(tobjid).offsetHeight)+'px';
		
		getObj('mns_v').innerHTML='<span style="color:#ABABAB">Loading...</span>';
		
		ac(cn);
		
		getObj('ac').style.display='';
	}
	
	function ac(c_n) {
		URL='http://www.avail.co.za/includes/calendarfeedjson.asp?calnum=' + c_n + '&id=' + propid[c_n] + '&roomid=' + crid[c_n] + '&y=' + ccy_a + '&m=' + (ccm_a-(-1));
		//alert(URL);
		req=new JSONscriptRequest(URL);
		req.buildScriptTag();
		req.addScriptTag();
	}
	
	function ch_r(v,c_n) {
		crid[c_n]=v;
		ac(c_n);
	}
	
	function ga(jd,c_n) {
		rlistname=jd.roomnamelist.split(',');
		rlistid=jd.roomidlist.split(',');
		if (rlistid.length > 1) {
			getObj('multiroom').style.background='#FFFFFF';
			ih='<select style="width:100%" onchange="ch_r(this.value,' + cca + ')">';
			selcode='';
			for (var fi=0;fi<rlistid.length;fi++) {
				if (rlistid[fi]==crid[c_n])
					selcode='selected';
				else
					selcode='';
				ih+='<option value="' + rlistid[fi] + '" ' + selcode + '>' + rlistname[fi] + '</option>';
				}
			ih+='</select>';
			getObj('multiroom').innerHTML=ih;
		}
		else {
			if (rlistname[0].length > 20)
				rlistname[0]=rlistname[0].substring(0,20) + '...';
			getObj('multiroom').innerHTML=rlistname[0];
			getObj('multiroom').style.background='#DEDEDE';
			getObj('multiroom').style.fontWeight='bold'; 
		}
		pcal(ccm_a,ccy_a,jd.availability);
	} 
	
	document.write('<span id="msga' + cca + '"></span><img id="ii' + cca + '" src="' + button_img + '" style="cursor:pointer;border:none" onclick="lcal(\'ii' + cca + '\',event,' + cca + ')">');
	
	if (!getObj('ac')) {
	document.write('<table id="ac" style="z-index:5;position:absolute;border-collapse:collapse;background:#FFFFFF;border:1px solid #78A1D0;display:none;width:140px" cellpadding=2>');
	document.write('<tr><td style="cursor:pointer" onclick="csubm_a()"><img src="http://www.avail.co.za/images/arrowleftmonth2.gif" style="border:none"></td><td colspan=5 id="mns_v" align="center" style="font:bold 13px Arial;color:#333333"></td><td align="right" style="cursor:pointer" onclick="caddm_a()"><img src="http://www.avail.co.za/images/arrowrightmonth2.gif" style="border:none"></td></tr>');
	document.write('<tr><td colspan=7 id="multiroom" align="center" style="font:11px Arial"></td></tr>');
	document.write('<tr><td align=center style="background:#FFFFFF;font:bold 12px Arial;" width=14%>S</td><td align=center style="background:#FFFFFF;font:bold 12px Arial;" width=14%>M</td><td align=center style="background:#FFFFFF;font:bold 12px Arial;" width=14%>T</td><td align=center style="background:#FFFFFF;font:bold 12px Arial;" width=14%>W</td><td align=center style="background:#FFFFFF;font:bold 12px Arial;" width=14%>T</td><td align=center style="background:#FFFFFF;font:bold 12px Arial;" width=14%>F</td><td align=center style="background:#FFFFFF;font:bold 12px Arial;" width=14%>S</td></tr>');
	for(var kk=1;kk<=6;kk++) {
		document.write('<tr>');
		for(var tt=1;tt<=7;tt++) {
			num=7 * (kk-1) - (-tt);
			document.write('<td id="v' + num + '" style="height:18px">&nbsp;</td>');
		}
		document.write('</tr>');
	}
	document.write('<tr><td colspan=8 style="font:10px Arial;background:#78A1D0;cursor:pointer" onclick="window.location=\'http://www.avail.co.za\'">free calendar from <a style="font:bold 10px Arial;color:#FFFFFF" href="http://www.avail.co.za">AVAIL</a></td></table>');
	 }
	 
	var mn=new Array('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC');
	var mnn_a=new Array('31','28','31','30','31','30','31','31','30','31','30','31');
	var mnl_a=new Array('31','29','31','30','31','30','31','31','30','31','30','31');
	var calvalarr_a=new Array(42);
	
	
	function f_a(obj) {
		obj.style.background='#FFFFFF';
		obj.style.font='10px Arial';
		obj.style.color='#333333';
		obj.style.textAlign='center';
		obj.style.textDecoration='none';
		obj.style.cursor='pointer';
	}
	
	function f_b(obj) {
		obj.style.background='#D1D2D0';
		obj.style.font='10px Arial';
		obj.style.color='#D1D2D0';
		obj.style.textAlign='center';
		obj.style.textDecoration='none';
		obj.style.textDecoration='line-through';
		obj.style.cursor='pointer';
	}
	
	function f_cpps(obj) {
		obj.style.background='#FFFFFF';
		obj.style.font='10px Arial';
		obj.style.color='#ABABAB';
		obj.style.textAlign='center';
		obj.style.textDecoration='line-through';
		obj.style.cursor='default';
	}
	
	function pcal(cm,cy,cdata) {
		cdarr=cdata.split(',');
		now=new Date();
		sd=now.getDate();
		td=new Date();
		td.setDate(1);
		td.setFullYear(cy);
		td.setMonth(cm);
		cd=td.getDay();
		getObj('mns_v').innerHTML=mn[cm]+ ' ' + cy;
		marr_a=((cy%4)==0)?mnl_a:mnn_a;
		for(var d=1;d<=42;d++) {
			f_a(getObj('v'+parseInt(d)));
			if ((d >= (cd -(-1))) && (d<=cd-(-marr_a[cm]))) {
				dip=((d-cd < sd)&&(cm==sccm_a)&&(cy==sccy_a));
				//htd=((hd!='')&&(d-cd==hd));
				if (dip)
					f_cpps(getObj('v'+parseInt(d)));
				else
					if (cdarr[d-cd-1]=='0')
						f_a(getObj('v'+parseInt(d)));
					else
						if (cdarr[d-cd-1]=='1')
							f_b(getObj('v'+parseInt(d)));
	
				getObj('v'+parseInt(d)).onmouseover=(dip)?null:null;
				getObj('v'+parseInt(d)).onmouseout=(dip)?null:null;
				getObj('v'+parseInt(d)).onclick=(dip)?null:null;
				
				getObj('v'+parseInt(d)).innerHTML=d-cd;	
				calvalarr_a[d]=''+(d-cd)+'/'+(cm-(-1))+'/'+cy;
			}
			else {
				getObj('v'+d).innerHTML='&nbsp;';
				getObj('v'+parseInt(d)).onmouseover=null;
				getObj('v'+parseInt(d)).onmouseout=null;
				getObj('v'+parseInt(d)).style.cursor='default';
				}
		}
	}
	
	function caddm_a(c_n) {
		marr_a=((ccy_a%4)==0)?mnl_a:mnn_a;
		
		ccm_a+=1;
		if (ccm_a>=12) {
			ccm_a=0;
			ccy_a++;
		}
		cdayf_a();
		ac(ccs);
	}
	
	function csubm_a(c_n) {
		marr_a=((ccy_a%4)==0)?mnl_a:mnn_a;
		
		ccm_a-=1;
		if (ccm_a<0) {
			ccm_a=11;
			ccy_a--;
		}
		cdayf_a();
		ac(ccs);
	}
	
	function cdayf_a() {
	if ((ccy_a>sccy_a)|((ccy_a==sccy_a)&&(ccm_a>=sccm_a))) {		
		getObj('mns_v').innerHTML='<span style="color:#ABABAB">Loading...</span>';
	}
	else {
		ccy_a=sccy_a;
		ccm_a=sccm_a;
		cfd_a=scfd_a;
		}
	}
	
	var now = new Date;
	var sccm_a=now.getMonth();
	var sccy_a=now.getFullYear();
	var ccm_a=now.getMonth();
	var ccy_a=now.getFullYear();	
}

if(window.addEventListener)
window.addEventListener('load', lca, false);
else if(window.attachEvent)
window.attachEvent('onload', lca);

button_img='http://www.avail.co.za/images/availabilitycalendar.gif';

id='';