/*
 * CVS version control block - do not edit manually
 * $RCSfile: form_widget.js,v $ - $Name: RELEASE_201006111730 $
 * $Revision: 1.10 $
 * $Date: 2010/05/25 19:09:20 $
 * $Source: /usr/local/repository/helpdesk/public/javascripts/form_widget.js,v $
 * Last modifications by $Author: ivaylo $
 *
 * Mojo Helpdesk Form Widget javascript functions.
 * Creates a tab, after clicking on which a popup window is opened with the submission form in it.
 * The tab is placed on the left side of the page, and doesn't move as the page scrolls or resize 
 * (stays at the same position on the screen)
 */
 
var ns4 = document.layers;
var op5 = (navigator.userAgent.indexOf("Opera 5")!=-1) ||(navigator.userAgent.indexOf("Opera/5")!=-1);
var op6 = (navigator.userAgent.indexOf("Opera 6")!=-1) ||(navigator.userAgent.indexOf("Opera/6")!=-1);
var agt=navigator.userAgent.toLowerCase();
var mac = (agt.indexOf("mac")!=-1);
var ie = (agt.indexOf("msie") != -1); 
var mac_ie = mac && ie;

/* Register a function, to be executed on page resizing */
function RegisterOnresizeHandler(func){
	var oldonresize=window.onresize;
	if (typeof window.onresize != 'function') {
		window.onresize = func;
	} else {
		window.onresize = function() {
			oldonresize();
			func();
		}
	}
}

/* Register a function, to be executed on page scrolling */
function RegisterOnscrollHandler(func){
	var oldonscroll=window.onscroll;
	if (typeof window.onscroll != 'function') {
		window.onscroll = func;
	} else {
		window.onscroll = function() {
			oldonscroll();
			func();
		}
	}
}
/* Move myObject to x, y coords */
function FWmoveXY(myObject, x, y) {
	obj = FWgetStyleObject(myObject)
	if (ns4) {
		obj.top = y;
 		obj.left = x;
	} else {
		if (op5) {
			obj.pixelTop = y;
 			obj.pixelLeft = x;
		} else {
			obj.top = y + 'px';
 			obj.left = x + 'px';
		}	
	}
}
/* get the style attribute of an object */
function FWgetStyleObject(objectId) {
    if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
		return FWgetObjNN4(document,objectId);
    } else {
	return false;
    } 
} 
function FWgetObjNN4(obj,name) {
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++){
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = FWgetObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}
function FWgetWindowWidth() {
	var myWidth=0;
	if (typeof(window.innerWidth)=='number') {
		// Non-IE
		myWidth = window.innerWidth;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		// IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		// IE 4 compatible
		myWidth = document.body.clientWidth;
	}
	return myWidth;
}
function FWgetWindowHeight() {
	var myHeight=0;
	if (typeof(window.innerHeight)=='number') {
		// Non-IE
		myHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		// IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		// IE 4 compatible
		myHeight = document.body.clientHeight;
	}
	return myHeight;
}
function FWf_scrollLeft() {
	return FWf_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function FWf_scrollTop() {
	return FWf_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function FWf_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
function FWgetElementHeight(Elem) {
	if (ns4) {
		var elem = FWgetObjNN4(document, Elem);
		return elem.clip.height;
	} else {
		var elem;
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) {
			xPos = elem.style.pixelHeight;
		} else {
			xPos = elem.offsetHeight;
		}
		return xPos;
	} 
}
function FWgetElementWidth(Elem) {
	if (ns4) {
		var elem = FWgetObjNN4(document, Elem);
		return elem.clip.width;
	} else {
		var elem;
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) {
			xPos = elem.style.pixelWidth;
		} else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}
function FWgetElementLeft(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.pageX;
	} else {
		var elem;
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		xPos = elem.offsetLeft;
		tempEl = elem.offsetParent;
  		while (tempEl != null) {
  			xPos += tempEl.offsetLeft;
	  		tempEl = tempEl.offsetParent;
  		}
		return xPos;
	}
}
function FWgetElementTop(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.pageY;
	} else {
		if(document.getElementById) {	
			var elem = document.getElementById(Elem);
		} else if (document.all) {
			var elem = document.all[Elem];
		}
		yPos = elem.offsetTop;
		tempEl = elem.offsetParent;
		while (tempEl != null) {
  			yPos += tempEl.offsetTop;
	  		tempEl = tempEl.offsetParent;
  		}
		return yPos;
	}
}


var mojoFormNeedReload = false;
var mojoFormBaseUrl = 'http://' + mojo_form_widget_params.site_url;
//var mojoFormIFrameUrl = mojoFormBaseUrl + "/widgets/widget_to_ticket?token=" + mojo_form_widget_params.token +
//  "&description=" + escape(mojo_form_widget_params.description);
var mojoFormIFrameUrl = "contactForm.php";

function showMojoFormWidget() {
  if (mojoFormNeedReload) {
    document.getElementById('mojo_form_widget_iframe').style.display = 'none';
    document.getElementById('mojo_form_widget_temp_message').style.display = '';
    document.getElementById('mojo_form_widget_temp_message_bottom').style.display = '';
    document.getElementById('mojo_form_widget_iframe').src = document.getElementById('mojo_form_widget_iframe').src ;// + "&reload="+(new Date()).getTime(); 
  } else {
    mojoFormNeedReload=true;
  }
    
  document.getElementById('mojo_form_widget_container').style.display= "";
	windowWidth = parseInt(FWgetWindowWidth());
	scrollLeft = parseInt(FWf_scrollLeft());
	windowHeight = parseInt(FWgetWindowHeight());
	scrollTop = parseInt(FWf_scrollTop());
	
  if (document.getElementById("mojo_form_widget_blurrer"))
    document.body.removeChild(document.getElementById("mojo_form_widget_blurrer"));
	blurrer=document.createElement('div');
	blurrer.setAttribute('id', 'mojo_form_widget_blurrer');
	blurrer.style.position = "absolute";
	blurrer.style.top = 0;
	blurrer.style.left = 0;
	blurrer.style.width = (windowWidth + scrollLeft) + "px";
	blurrer.style.height = (windowHeight + scrollTop) + "px";
	blurrer.style.zIndex = 1000;
	blurrer.className = "blurrer";
	blurrer.innerHTML = "&nbsp;";
	document.body.appendChild(blurrer);
	
	form_width = parseInt(FWgetElementWidth('mojo_form_widget_container'));
	form_height = parseInt(FWgetElementHeight('mojo_form_widget_container'));

  FWmoveXY("mojo_form_widget_container", 
    parseInt((windowWidth - form_width)/2 + scrollLeft),
    parseInt((windowHeight - form_height)/2 + scrollTop));;
	RegisterOnresizeHandler(fixFormAndBlurrer);
	RegisterOnscrollHandler(fixFormAndBlurrer);
	
}

function fixFormAndBlurrer() {
	if (document.getElementById('mojo_form_widget_blurrer')) {
		document.getElementById('mojo_form_widget_blurrer').style.width = (parseInt(FWgetWindowWidth()) + parseInt(FWf_scrollLeft())) + "px";
		document.getElementById('mojo_form_widget_blurrer').style.height = (parseInt(FWgetWindowHeight()) + parseInt(FWf_scrollTop())) + "px";

	  document.getElementById('mojo_form_widget_container').style.display= "";
		windowWidth = parseInt(FWgetWindowWidth());
		scrollLeft = parseInt(FWf_scrollLeft());
		windowHeight = parseInt(FWgetWindowHeight());
		scrollTop = parseInt(FWf_scrollTop());
		form_width = parseInt(FWgetElementWidth('mojo_form_widget_container'));
		form_height = parseInt(FWgetElementHeight('mojo_form_widget_container'));
		
	  FWmoveXY("mojo_form_widget_container", 
	    parseInt((windowWidth - form_width)/2 + scrollLeft),
	    parseInt((windowHeight - form_height)/2 + scrollTop));
	}
}

function fixTabPosition() {
	FWmoveXY("mojo_tab_container", 
	  parseInt(FWf_scrollLeft()), 
	  parseInt(mojo_form_widget_params.tab_top_offset ? mojo_form_widget_params.tab_top_offset : 200) + parseInt(FWf_scrollTop()));
}

function hideMojoFormWidget() {
  document.getElementById('mojo_form_widget_container').style.display= "none";
  if (document.getElementById("mojo_form_widget_blurrer"))
    document.body.removeChild(document.getElementById("mojo_form_widget_blurrer"));
}

function iframeLoaded() {
  document.getElementById('mojo_form_widget_temp_message').style.display = 'none';
  document.getElementById('mojo_form_widget_temp_message_bottom').style.display = 'none';
  document.getElementById('mojo_form_widget_iframe').style.display = '';
}

function place_the_tab() {
  /*document.write("<style type='text/css'>#mojo_tab_container img {background-color:" + mojo_form_widget_params.tab_bg_color + 
    ";border-color:" + mojo_form_widget_params.tab_bg_color + ";} " +
    "#mojo_tab_container a {border-top: 1px solid " + mojo_form_widget_params.tab_bg_color + ";" +
    "border-right: 1px solid " + mojo_form_widget_params.tab_bg_color + ";" +
    "border-bottom: 1px solid " + mojo_form_widget_params.tab_bg_color + ";} </style>");  */
/*  document.write('<div id="mojo_tab_container"><a href="" onclick="showMojoFormWidget();return false;"><!--img id="mojo_tab_container_image" src="' + mojoFormBaseUrl + '/images/form_widget/' +
    mojo_form_widget_params.tab_name + '_tab.png" border=0-->Contacto</a></div>');*/
  //FWmoveXY("mojo_tab_container", 0, mojo_form_widget_params.tab_top_offset ? mojo_form_widget_params.tab_top_offset : 200);
  
  document.write("<div id='mojo_form_widget_container' style='display:none;'>");
  document.write("<div id='mojo_form_widget_title_container' style='background-color:#3233FF;'	>");
  document.write("<div id='mojo_form_widget_title_close'><img id='mojo_form_widget_close_button' src='http://www.albentia.com/Imagenes/helpdesk/x_button.png' onclick='hideMojoFormWidget();'></div>");
  document.write("<div id='mojo_form_widget_title_text'>" + mojo_form_widget_params.title.substring(0,50) + "</div>");
  document.write("</div>");
  document.write("<div id='mojo_form_widget_title_bottom'>&nbsp;</div>");
  document.write("<div id='mojo_form_widget_temp_message' style='display:none;'>Loading...</div>");
  document.write("<div id='mojo_form_widget_temp_message_bottom' style='display:none;'>&nbsp;</div>");
  document.write("<iframe id='mojo_form_widget_iframe' src='" + mojoFormIFrameUrl + "'" +
     " frameborder='0' scrolling='no' allowTransparency='true' style='border:0;' onload='iframeLoaded();'></iframe>");
  document.write("");
  document.write("");
  document.write("");
  document.write("</div>");
 
	//RegisterOnresizeHandler(fixTabPosition);
	//RegisterOnscrollHandler(fixTabPosition);
  
}

place_the_tab();