

var SendStaf = "/blog/wp-content/plugins/sendtoafriend/sendtoafriend.php?staf=true";

function initStaf() {
	// initiates the object for sending and receiving data
	httpStaf = staf_getHTTPObject();
	
	// Inserting html tags direct after "body"
	oRemovedNode1 = document.getElementById("myframe1");
	oRemovedNode1.parentNode.removeChild(oRemovedNode1);
	document.body.appendChild(oRemovedNode1);
		
	oRemovedNode2 = document.getElementById("myframe2");
	oRemovedNode2.parentNode.removeChild(oRemovedNode2);
	document.body.appendChild(oRemovedNode2);
	
	oRemovedNode = document.getElementById("stafBlock");
	oRemovedNode.parentNode.removeChild(oRemovedNode);
	document.body.appendChild(oRemovedNode);
	// ---
	
	stafLnk = document.getElementById('stafLink');
	staffrm = document.getElementById('stafForm').innerHTML;
	if(stafLnk) {
		stafLnk.onclick = function() {
			showBgItems( new Array('myframe1','myframe2','stafBlock') );
			changeLayoutSize();
			
			document.getElementById('stafUrl').value = document.URL;
			
			//document.getElementById('stafBlock').style.display = 'block';
			/*
			ws = staf_windowsize();
			w = parseInt(document.getElementById('stafBlock').offsetWidth);
			h = parseInt(document.getElementById('stafBlock').offsetHeight);
			stafTopOffset =0;
			if(document.body.scrollTop) {
				stafTopOffset = document.body.scrollTop;
			} else if(document.documentElement.scrollTop){ 
				stafTopOffset = document.documentElement.scrollTop;
			}
			document.getElementById('stafBlock').style.top =  (stafTopOffset + ((ws.y/2)-(h/2)))+"px" ;
			document.getElementById('stafBlock').style.left =  ((ws.x/2)-(w/2))+"px";
			*/
			return false;
		}
	}
}

function showBgItems(ids){
	scroll(0,0);

	for(key in ids) {
		var obj = document.getElementById(ids[key] );
		if(obj != null){
			obj.style.display = "block";
		}
	}
	//document.body.style.overflow = 'hidden';
}


function hideBgItems(ids){
	for(key in ids) {
		var obj = document.getElementById(ids[key] );
		if(obj != null){
			obj.style.display = "none";
		}
	}
		// commented line causes problem in IE with doubled scroll bar
		//document.body.style.overflow = 'auto'; 
	return false;
}

function changeLayoutSize() {
	var pageSize = getPageSize();
	document.getElementById("myframe1").style.height = pageSize[1]+'px';
}

function getPageSize() {
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {  
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} 
	else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} 
	else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	//  console.log(self.innerWidth);
	//  console.log(document.documentElement.clientWidth);
	
	if (self.innerHeight) { // all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} 
		else {
			windowWidth = self.innerWidth;
		}
		
		windowHeight = self.innerHeight;
	} 
	else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} 
	else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	} 
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} 
	else { 
		pageHeight = yScroll;
	}
	
	//  console.log("xScroll " + xScroll)
	//  console.log("windowWidth " + windowWidth)
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){  
		pageWidth = xScroll;    
	} 
	else {
		pageWidth = windowWidth;
	}
	//  console.log("pageWidth " + pageWidth)
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

	
function staf_Close() {
	document.getElementById('stafBlock').style.display = 'none';
	return false;
}
function staf_Reset() {
	hideBgItems( new Array('myframe1','myframe2','stafBlock') );
//	document.getElementById('stafBlock').style.display = 'none';
	document.getElementById('stafForm').innerHTML = staffrm;
}
function staf_Error(stafErrorArr) {
	document.getElementById('stafLblFromName').style.color = "inherit";
	document.getElementById('stafLblFromMail').style.color = "inherit";
	document.getElementById('stafLblToName').style.color = "inherit";
	document.getElementById('stafLblToMail').style.color = "inherit";
	
	for(i = 0; i < stafErrorArr.length; i++) {
		document.getElementById(stafErrorArr[i]).style.color = '#ff0000';
	}
	
}

function staf_Submit() {
	if (httpStaf.readyState == 4 || httpStaf.readyState == 0) {
		httpStaf.open("POST", SendStaf, true);
		httpStaf.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  		params = 'url='+document.getElementById('stafUrl').value;
		params+= '&fromname='+document.getElementById('stafFromName').value;
		params+= '&frommail='+document.getElementById('stafFromMail').value;
		params+= '&toname='+document.getElementById('stafToName').value;
		params+= '&tomail='+document.getElementById('stafToMail').value;
		httpStaf.send(params);
		httpStaf.onreadystatechange = function () {
			if (httpStaf.readyState == 4) {
				results = httpStaf.responseText;
				if(results.substring(0,5) == "Array") {
					results = eval(results);
					staf_Error(results);
				} else {
					document.getElementById("stafForm").innerHTML = staf_confirmtext;
					setTimeout('staf_Reset()', 3000);
				}
    		}
		}
  	} else {
  	    setTimeout('staf()', 400)
  	}
	
	return false;
}

//returns the window size 
function staf_windowsize() {
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	retval = new Object;
	retval.x = x;
	retval.y = y; 
	return retval;
}
//initiates and handles the XMLHttpRequest object
//as found here: http://www.webpasties.com/xmlHttpRequest

function staf_getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}


// brothercake's generic onload
// http://www.brothercake.com/site/resources/scripts/onload/

if(typeof window.addEventListener != 'undefined')
{
	//.. gecko, safari, konqueror and standard
	window.addEventListener('load', initStaf, false);
}
else if(typeof document.addEventListener != 'undefined')
{
	//.. opera 7
	document.addEventListener('load', initStaf, false);
}
else if(typeof window.attachEvent != 'undefined')
{
	//.. win/ie
	window.attachEvent('onload', initStaf);
}
