//generic function to xml http request
function GetXmlHttpObject() {
   try { return new XMLHttpRequest(); } catch(e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   return null;
 }

 //generic function to send post ajax
 function sendPostAjax(xmlHttp, url, params){
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

//function to logout the admin page
function adminLogout(){
    xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
      alert ("Your browser does not support AJAX!");
      return;
	}
	var url = document.getElementById('website_url').value + "session.val";
	var params = "option=adminlogout";
	xmlHttp.onreadystatechange = responseAdminLogout;
	sendPostAjax(xmlHttp, url, params);
	return false;
}

function responseAdminLogout() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			var result = xmlHttp.responseText;
			document.location.href = document.getElementById('website_url').value + "admin/";
		} else {
			alert("Error: "+ xmlHttp.statusText +" "+ xmlHttp.status);
		}
	}
}

//generic function to go to the given url in a popup
function popupUrl(url, w, h) {
	if (!w) w = 470;
	if (!h) h = 435;
	iMyWidth = (window.screen.width - w) / 2;
	iMyHeight = (window.screen.height - h) / 2;

    var tmp = timestamp();
    newwin = window.open(url, tmp, 'scrollbars=1,width='+w+',height='+h+'') ;
    newwin.moveTo(iMyWidth, iMyHeight);
    newwin.focus();
}

//generic function to go get the timestamp
function timestamp() {
    var date = new Date();
    return date.getTime();
}

function getWindowHeight() {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	return blanket_height;
}

function getWindowWidth() {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	return window_width;
}

// Determine browser and version.

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    return;
  }

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    return;
  }
}
var browser = new Browser();

//function to logout the tesserra admin page
function tesserraAdminLogout(){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
      alert ("Your browser does not support AJAX!");
      return;
	}
	var url = document.getElementById('website_url').value + "tesserrasession.val";
	var params = "option=terrasseadminlogout";
	xmlHttp.onreadystatechange = responseTerrasseAdminLogout;
	sendPostAjax(xmlHttp, url, params);
	return false;
}

function responseTerrasseAdminLogout() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			var result = xmlHttp.responseText;
			if(result == 1) {
				document.location.href = document.getElementById('website_url').value + "terrasse/admin.html";
			}
		} else {
			alert("Error: "+ xmlHttp.statusText +" "+ xmlHttp.status);
		}
	}
}


//function to get view per page
function changePageNos(obj, returl, pagename) {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	}
	var url = document.getElementById('website_url').value + "session.val";
	var params = "option=navigation" + "&pagenos=" + obj.value + "&returl=" + returl + "&pagename=" + pagename;
	xmlHttp.onreadystatechange = changePageNosResponse;
	sendPostAjax(xmlHttp, url, params);
	return false;
}

//function to response from the server
function changePageNosResponse() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			var result = xmlHttp.responseText;
			document.location.href = result;
		} else {
			alert("Error: "+ xmlHttp.statusText +" "+ xmlHttp.status);
		}
	}
}