//our js function

function openPopup(page, width, height, target, top, left, wName, resize) {
	if (top == null || left == null) {
		var str = "width="+width+",height="+height+",scrollbars=yes,status=no,toolbar=no,menubar=no";
	}
	else {
		var str = "width="+width+",height="+height+",left="+left+",top="+top+",scrollbars=yes,status=no,toolbar=no,menubar=no";
	}

	if( resize )
		str = str + ",resizable=yes";

	// Replacing any # in GET params
	var page_url_parsed = "";
	var questionMarkFound = "false";
	for (i = 0; i < page.length; i++) {
		if (questionMarkFound == "false") {
			if (page.charAt(i) == "?") {
				questionMarkFound = "true";
			}
			page_url_parsed += page.charAt(i);
		} else {
			if (page.charAt(i) == "#") {
				page_url_parsed += "%23";
			} else {
				page_url_parsed += page.charAt(i);
			}
		}
	}

	var wTmp = window.open(page_url_parsed, target, str);
	wTmp.focus();
	
	if (wName) {
		eval(wName + " = wTmp;");
	}
}

function openPlanetaryPopup(page, target, top, left, wName) {
	openPopup(page, "800", "600", target, top, left, wName);
}

// Opens an url in 'parent' window, or same window if no parent
function openInParent(url) {
	if (opener = window.top.opener) {
		opener.location = url;
		opener.focus();
	}
	else {
		window.top.location = url;
	}
}

function openHelp(path) {
	if (path.lastIndexOf('/')  + 1 != path.length) {
		path += "/";
	}
	helpWindow = window.open("/support/faq/list/" + path, "helpWindow", "width=650,height=600,scrollbars=no,status=no,toolbar=no,menubar=no");
	helpWindow.focus();
}

function openHelpSearch(query, section) {
	url = "/support/faq/result/?search=" + query;
	
	if (section) {
		url += "&range=" + section;
	}
	
	helpWindow = window.open(url, "helpWindow", "width=600,height=520,scrollbars=no,status=no,toolbar=no,menubar=no");
	helpWindow.focus();
}

function submitHelpSearch(param) {
	value = document.forms['searchForm'].search.value;
	
	openHelpSearch(value);
	return param;
}

function setcookie(cookieName,cookieValue, nSec) {
	var today = new Date();
	var expire = new Date();
	if (nSec==null || nSec==0) nSec=1;
	expire.setTime(today.getTime() + 1000*nSec);
	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

function changeInputType(oldElm,iType,iValue,blankValue,noFocus) {
  if(!oldElm || !oldElm.parentNode || (iType.length<4) || 
    !document.getElementById || !document.createElement) return;
  var isMSIE=/*@cc_on!@*/false; 
  if(!isMSIE){
    var newElm=document.createElement('input');
    newElm.type=iType;
  } else {
    var newElm=document.createElement('span');
    newElm.innerHTML='<input type="'+iType+'" name="'+oldElm.name+'">';
    newElm=newElm.firstChild;
  }
  var props=['name','id','className','size','tabIndex','accessKey'];
  for(var i=0,l=props.length;i<l;i++){
    if(oldElm[props[i]]) newElm[props[i]]=oldElm[props[i]];
  }
  newElm.onfocus=function(){return function(){
    if(this.hasFocus) return;
    var newElm=changeInputType(this,'password',iValue,
      (this.value.toLowerCase()==iValue.toLowerCase())?true:false);
    if(newElm) newElm.hasFocus=true;
  }}();
  newElm.onblur=function(){return function(){
    if(this.hasFocus)
    if(this.value=='' || (this.value.toLowerCase()==iValue.toLowerCase())) {
      changeInputType(this,'text',iValue,false,true);
    }
  }}();
  newElm.hasFocus=false;
  if(!blankValue) newElm.value=iValue;
  oldElm.parentNode.replaceChild(newElm,oldElm);
  if(!isMSIE && !blankValue) newElm.value=iValue;
  if(!noFocus || typeof(noFocus)=='undefined') {
    window.tempElm=newElm;
    setTimeout("tempElm.hasFocus=true;tempElm.focus();",1);
  }
  return newElm;
}

// Original code by Ismael Jurado
// http://www.ismaelj.com/articulos/addevent-recoding-contest/ 
function addEvent(obj,evType,fn){
 var r = false;
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    r = true;
  }
  else if (obj.attachEvent) {
    var id = obj.sourceIndex || -1;

    if (!fn[evType + id]) {
      var f = fn[evType + id] = function(e) {
        var o = document.all[id] || document;
        o._f = fn;
        var s = o._f(e);
        o._f = null;
        return s;
      };

      r = obj.attachEvent("on" + evType, f);
      obj = null;
    }
  }
  return r;
};

function removeEvent(obj, evType, fn){
  var r = false
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, false);
    r = true;
  } else if (obj.detachEvent) {
    r = obj.detachEvent("on" + evType, fn[evType + (obj.sourceIndex || -1)]);
  }
  return r;
};


