var xmlHttp
var propnum = 1;
var isIE = document.all ? true : false;
var showDiv = false;
var MouseX = "no";
var MouseY = "no";
var positions = null;
var showDiv = false;
var idcount = 0;
var totalitems = 0;
var goToURL = '/cottage-details/';
var textlength = 0;
var customdiv = false;
var _divname = "pageitem";


//---------------------------------------------------------
//
// Generic Ajax
//
//---------------------------------------------------------

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		 // Firefox, Opera 8.0+, Safari
		 xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		//Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	return xmlHttp;
}


//--------------------------------------------------
//
// Calendar
//
//--------------------------------------------------

function getCalendar(event, day, month, year, target)
{
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	
	evt = (window.event) ? window.event : event;
	positions = getMousePosition(evt);
	
	var url = "/includes/calendar.php?day=";
	url = url + day;
	url = url + "&month=" + month
	url = url + "&year=" + year
	url = url + "&target=" + target
	url = url + "&rand=" + Math.floor(Math.random()*10000000)
	xmlHttp.onreadystatechange=calSent;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function calSent()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var change = false;
		if(!customdiv)
		{
			customdiv = true;
			change = true;
		}
		
		customToolTip(xmlHttp.responseText, 30, 0);
		
		if(change)
		{
			customdiv = false;
		}
	}
}

function setDate(date, target)
{
	document.getElementById(target).value = date;
}



function getPage(pagename, divname)
{ 
	xmlHttp=GetXmlHttpObject()
	_divname = divname;
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
		
	var url="/includes/getPage.php";
	url = url + "?pagename=" + pagename;
	url = url + "&rand=" + Math.floor(Math.random()*10000000);
	xmlHttp.onreadystatechange=statePage
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	return true;
}

function statePage() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById(_divname).innerHTML = xmlHttp.responseText;
	}
}

//--------------------------------------------------
//
// Functions
//
//--------------------------------------------------

function eCheck(str) 
{	
	 var at="@";
	 var dot=".";
	 var lat=str.indexOf(at);
	 var lstr=str.length;
	 var ldot=str.indexOf(dot);
	 
	 if (str.indexOf(at)==-1) {
	    return false;
	 }
	 if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
	    return false;
	 }
	 if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) {
	     return false;
	 }
	 if (str.indexOf(at,(lat+1))!=-1) {
	    return false;
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
	    return false;
	 }
	 if (str.indexOf(dot,(lat+2))==-1) {
	    return false;
	 }
	
	 if (str.indexOf(" ")!=-1) {
	    return false;
	 }
	 
	 return true;					
}

function customToolTip(msg, left, top)
{
	try
	{
		if(positions == null)
		{
			positions = getCenter(170, 200);
		}
	}
	catch(e)
	{
	
	}
	
	try
	{
		hideToolTip();
	}
	catch(e)
	{
	
	}
	
	
	if(!left)
	{
		left = 0;
	}
	
	if(!top)
	{
		top = 0;
	}

	if(customdiv)
	{
		var footer = "<a onClick=\"hideToolTip()\" style=\"cursor: pointer; float: right; line-height: 10px;\">x</a>";
			
		var myTip = document.createElement('div');
		myTip.style.backgroundColor = '#FFFFFF';
		myTip.style.borderWidth = '2px';
		myTip.style.borderStyle = 'solid';
		myTip.style.borderColor = '#CCCCCC';
		myTip.style.color = 'black';
		myTip.style.padding = '6px';
		myTip.style.position='absolute';
		myTip.style.fontSize='12px';
		myTip.style.width='170px';
		myTip.style.textAlign='left';
		myTip.style.lineHeight='20px';
		myTip.style.zIndex='5';
		myTip.style.top = ((parseInt(top)) + (positions[1]))+'px';
		myTip.style.left = (parseInt(positions[0]) + parseInt(left))+'px';
		myTip.setAttribute('id', 'myDiv');
		myTip.innerHTML = footer + msg;
		//myTip.innerHTML = msg;
		
		if(!showDiv)
		{
			document.body.appendChild(myTip);
			showDiv = true;
		}
	}
	else
	{
		msg = replaceAll(msg, "<br />", "\n");
		alert(msg);
	}
}

function centerToolTip(msg, left, top)
{
	try
	{
		positions = getCenter(170, 400);
	}
	catch(e)
	{
	
	}
	
	try
	{
		hideToolTip();
	}
	catch(e)
	{
	
	}
	
	
	if(!left)
	{
		left = 0;
	}
	
	if(!top)
	{
		top = 0;
	}
	
	if(customdiv)
	{
		msg = replaceAll(msg, "\n", "<br />");
		var footer = "<br /><br /><center><a onClick=\"hideToolTip()\" style=\"cursor: pointer; font-weight: bold; line-height: 10px;\">Ok</a></center>";
			
		var myTip = document.createElement('div');
		myTip.className = "centertooltip";
		myTip.setAttribute('id', 'myDiv');
		myTip.style.top = ((parseInt(top)) + (positions[1]))+'px';
		myTip.style.left = (parseInt(positions[0]) + parseInt(left))+'px';
		myTip.innerHTML = msg + footer;
		
		
		var blackBG = document.createElement('div');
		blackBG.className = "black_overlay";
		blackBG.setAttribute('id', 'blackBG');
		
		var dims = getScrollDimensions();
		
		blackBG.style.height = (dims.height) + 'px';
		
		if(!showDiv)
		{
			document.body.appendChild(myTip);
			document.body.appendChild(blackBG);
			showDiv = true;
		}
	}
	else
	{
		msg = replaceAll(msg, "<br />", "\n");
		alert(msg);
	}
	
}


function getMousePosition(ev) {
	var _x = 0;
	var _y = 0;
	
	if (!isIE) {
		_x = ev.pageX;
		_y = ev.pageY;
	}
	if (isIE) {
		_x = (ev.clientX + document.body.scrollLeft + document.documentElement.scrollLeft);
		_y = ev.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	posX = _x;
	posY = _y;
	
	if(isNaN(MouseX) && isNaN(MouseY))
	{
		MouseX = posX;
		MouseY = posY;
	}
	
	
	var pos = Array(MouseX, MouseY);
	return pos;
}

function getCenter(divWidth,divheight)
{
	// First, determine how much the visitor has scrolled
	var scrolledX, scrolledY;
	
	if( self.pageYoffset )
	{
		scrolledX = self.pageXoffset;
		scrolledY = self.pageYoffset;
	}
	else if( document.documentElement && document.documentElement.scrollTop )
	{
		scrolledX = document.documentElement.scrollLeft;
		scrolledY = document.documentElement.scrollTop;
	}
	else if( document.body )
	{
		scrolledX = document.body.scrollLeft;
		scrolledY = document.body.scrollTop;
	}

	// Next, determine the coordinates of the center of browser's window
	var centerX, centerY;
	if( self.innerHeight )
	{
		centerX = self.innerWidth;
		centerY = self.innerHeight;
	}
	else if( document.documentElement && document.documentElement.clientHeight )
	{
		centerX = document.documentElement.clientWidth;
		centerY = document.documentElement.clientHeight;
	}
	else if( document.body )
	{
		centerX = document.body.clientWidth;
		centerY = document.body.clientHeight;
	}

	// Xwidth is the width of the div, Yheight is the height of the
	// div passed as arguments to the function:
	var leftoffset = scrolledX + (centerX - divWidth) / 2;
	var topoffset = scrolledY + (centerY - divheight) / 2;
	// The initial width and height of the div can be set in the
	// style sheet with display:none; divid is passed as an argument to // the function
	
	var pos = Array(leftoffset, topoffset);
	return pos;
} 

function hideToolTip()
{
	try
	{
		var myTool = document.getElementById('myDiv');
		document.body.removeChild(myTool);
		showDiv = false;
		var myTool2 = document.getElementById('blackBG');
		document.body.removeChild(myTool2);
	}
	catch(err){}
}


//-----------------------------------------------
//
// Misc functions
//
//-----------------------------------------------


function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue)
{
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
	var oCurrent;
	var oAttribute;
	
	for(var i=0; i<arrElements.length; i++)
	{
		oCurrent = arrElements[i];
		oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
		
		if(typeof oAttribute == "string" && oAttribute.length > 0)
		{
			if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute)))
			{
				arrReturnElements.push(oCurrent);
			}
		}
	}
	
	return arrReturnElements;
}




/*--------------------

String replace all

----------------------*/

function replaceAll(Source,stringToFind,stringToReplace)
{
	var temp = Source;
	var index = temp.indexOf(stringToFind);
	while(index != -1)
	{
		temp = temp.replace(stringToFind,stringToReplace);
		index = temp.indexOf(stringToFind);
	}
	
	return temp;
}


/*---------------------------------------

Get page dimensions, returns an object

----------------------------------------*/

function getPageDimensions()
{
	var dims = new Object();
	var width,height;
	if (window.innerHeight) // all except Explorer
	{
		width = window.innerWidth;
		height = window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		width = document.body.clientWidth;
		height = document.body.clientHeight;
	}
	
	dims.width = width;
	dims.height = height;
	
	return dims;
}

function getScrollDimensions()
{
	var pageDims = getPageDimensions()
	var dims = new Object();
	var width,height;
	
	if (window.innerHeight && window.scrollMaxY)
	{// Firefox
		height = window.innerHeight + window.scrollMaxY;
		width = window.innerWidth + window.scrollMaxX;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ // all but Explorer Mac
		height = document.body.scrollHeight;
		width = document.body.scrollWidth;
	}
	else
	{ // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		height = document.body.offsetHeight;
		width = document.body.offsetWidth;
  	}

	
	//dims.width = pageDims.width + width;
	//dims.height = pageDims.height + height;
	dims.width = width;
	dims.height = height;
	
	if(width < pageDims.width)
	{
		dims.width = pageDims.width;
	}
	
	if(height < pageDims.height)
	{
		dims.height = pageDims.height;
	}
	
	return dims;
}