/* Itris Viewpoint Javascript library; copyright (c) 2003 */

var hexVals = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
var unsafeString = "\"<>%\\^[]`";

function highlight(element1){element1.focus();element1.select();}

function URLDecode() {
    var returnstr=unescape(form1.string1.value);
    document.all.div1.innerHTML='RESULT:<br><textarea name="textarea2">'+returnstr+'</textarea>';
    highlight(form3.textarea2);
    // while coding i found that IE had problem writing '<form>' to innerhtml. </form> was ok. 'unknown runtime error' IE5.5.
}

function URLEncode(val) {
    var state   = 'urlenc';
    var len     = val.length;
    var backlen = len;
    var i       = 0;
    var newStr  = "";
    var frag    = "";
    var encval  = "";

    for (i=0;i<len;i++){
      tval1=val.substring(i,i+1);
      newStr = newStr + "%" + decToHex(tval1.charCodeAt(0),16);
    }
    return newStr;
}

function decToHex(num, radix) { // part of URL Encode
    var hexString = "";
    while (num >= radix) {
           temp = num % radix;
           num = Math.floor(num / radix);
           hexString += hexVals[temp];
    }
    hexString += hexVals[num];
    return reversal(hexString);
}

function reversal(s) { // part of URL Encode
    var len = s.length;
    var trans = "";
    for (i=0; i<len; i++)
     trans = trans + s.substring(len-i-1, len-i);
    s = trans;
    return s;
}

function isURLok(compareChar) { // part of URL Encode
    if (unsafeString.indexOf(compareChar) == -1 && compareChar.charCodeAt(0) > 32 && compareChar.charCodeAt(0) < 123)
     return true;
    else
     return false;
}

function getCookie(name)  {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function setCookie(name,value,expires,path,domain,secure)  {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

/**
 * Waits for the number of milliseconds
 * specified in ms to give back control to the calling routine.
 */
function pause(ms) {
  d = new Date() //today's date
  mill=new Date() // Date Now
  diff = mill-d //difference in milliseconds
  while (diff < ms) {
    mill=new Date() // Date Now
    diff = mill-d //difference in milliseconds
  }
}

function replaceAll(checkMe,toberep,repwith){
    var temp = checkMe;
    var i = temp.indexOf(toberep);
    while(i > -1){
    temp = temp.replace(toberep, repwith);
    i = temp.indexOf(toberep);
    }
    return temp;
}

function findPosX(obj){
var curleft = 0;
if (obj.offsetParent){
	
  while(1){
   curleft+=obj.offsetLeft;
   if (!obj.offsetParent){break}
    obj=obj.offsetParent;
  }
 }
 else if(obj.x){curleft+=obj.x}
 return curleft;
}
	
function findPosY(obj){
 var curtop = 0;
  if (obj.offsetParent){
  while(1){
    curtop+=obj.offsetTop;
    if (!obj.offsetParent){break}
      obj=obj.offsetParent;
  }
	}else if(obj.y){curtop+=obj.y}
 return curtop;
}

function getWindowHeight() {
    var myHeight = 0;
    if( typeof( window.innerHeight ) == 'number' ) {
      //Non-IE
      myHeight = window.innerHeight;
    } else if( document.documentElement && document.documentElement.clientHeight ) {
      //IE 6+ in 'standards compliant mode'
      myHeight = document.documentElement.clientHeight;
    } else if( document.body && document.body.clientHeight ) {
      //IE 4 compatible
      myHeight = document.body.clientHeight;
    }
    return myHeight;
}

function getWindowWidth() {
    var myWidth = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
      //Non-IE
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth ) {
      //IE 6+ in 'standards compliant mode'
      myWidth = document.documentElement.clientWidth;
    } else if( document.body && document.body.clientWidth ) {
      //IE 4 compatible
      myWidth = document.body.clientWidth;
    }
    return myWidth;
}

function toggleButton(buttonname, divname) {
      var list = document.getElementById(divname);
      var btn = document.getElementById(buttonname);
      list.style.display = '';
      var leftLocation = findPosX(btn) +20;// - list.offsetWidth + 16;
      var topLocation = findPosY(btn) + 15; //list.offsetHeight - 100;
      try {
        top = 100;
      } catch(e) { //IE
        leftLocation -= 10;
      }
      list.style.top = topLocation + "px";
      list.style.left = leftLocation + "px";
      list.style.zIndex = document.getElementById(divname).style.zIndex+1;
}

function isShiftPressed(evt) {
  if (!evt)
   evt = window.event;
  var isShiftKeyPressed = (evt.shiftKey)? evt.shiftKey : evt.SHIFT_MASK;
  return isShiftKeyPressed;
/*
	var shift=0;
	if (parseInt(navigator.appVersion)>3) {		 
	 if (navigator.appName=="Netscape") {
	   var mString =(e.modifiers+32).toString(2).substring(3,6);
	   shift=(mString.charAt(0)=="1");
		 }
	 else
	   shift=event.shiftKey;
	}		 
	return shift;
*/ 
}

function isCtrlPressed(e) {
	var ctrl=0;
	if (parseInt(navigator.appVersion)>3) {		 
	 if (navigator.appName=="Netscape") {
	   var mString =(e.modifiers+32).toString(2).substring(3,6);
	   ctrl=(mString.charAt(1)=="1");
		 }
	 else
	   ctrl=event.ctrlKey;
	}		 
	return ctrl;
}

function isAltPressed(e) {
	var alt=0;
	if (parseInt(navigator.appVersion)>3) {		 
	 if (navigator.appName=="Netscape") {
	   var mString =(e.modifiers+32).toString(2).substring(3,6);
	   alt=(mString.charAt(2)=="1");
		 }
	 else
	   alt=event.altKey;
	}		 
	return alt;
}

function getIFRAME_doc_width(IFrameDoc) {
	return (IFrameDoc.body && typeof IFrameDoc.body.scrollWidth != 'undefined') ? IFrameDoc.body.scrollWidth :
	(typeof IFrameDoc.width != 'undefined') ? IFrameDoc.width : null;
}


function getIFRAME_doc_height(IFrameDoc) {
	return (IFrameDoc.body && typeof IFrameDoc.body.scrollHeight != 'undefined') ? IFrameDoc.body.scrollHeight :
	(typeof IFrameDoc.height != 'undefined') ? IFrameDoc.height : null;
}


//Methode for resizing IFrames
// with max height max width parameters 
function setIFRAME_size_to_content(iframe_ref,maxheight ,maxwidth ){ 
		if(maxheight==0){
			maxheight = 2000;
		}
		if(maxwidth==0){
			maxwidth = 2000;
		}
		
	var IFrameDoc, oIframe = document.getElementById(iframe_ref);
	if (oIframe) {
		if (oIframe.contentDocument){ 
		// For NS6/moz
		IFrameDoc = oIframe.contentDocument; 
		}
		else if (oIframe.contentWindow){ 
		// For IE5.5+
		IFrameDoc = oIframe.contentWindow.document;
		}
		else if (oIframe.document){ 
		// For IE5
		IFrameDoc = oIframe.document;
		}
		
		var wid = getIFRAME_doc_width(IFrameDoc);
		var hgt = getIFRAME_doc_height(IFrameDoc);
		
		if (wid && hgt){
            if(hgt>maxheight){
                hgt=maxheight; 
            }
            if(wid>maxwidth){
                wid=maxwidth;
            }             
            oIframe.style.width = wid;
            oIframe.style.height = hgt;
            
        }
        
		oIframe.style.width = wid;
		oIframe.style.height = hgt;
		} 
}

//Methode for resizing IFrames
// with max height max width parameters 
function setIFRAME_size_to_content_min(iframe_ref,minheight ,minwidth ) {

		if(minheight==0){
			minheight = 2000;
		}
		if(minwidth==0){
			minwidth = 2000;
		}
		
	var IFrameDoc, oIframe =  document.getElementById(iframe_ref);

	if (oIframe) {
		if (oIframe.contentDocument){ 
		// For NS6/moz
		IFrameDoc = oIframe.contentDocument; 
		}
		else if (oIframe.contentWindow){ 
		// For IE5.5+
		IFrameDoc = oIframe.contentWindow.document;
		}
		else if (oIframe.document){ 
		// For IE5
		IFrameDoc = oIframe.document;
		}
		
		var wid = getIFRAME_doc_width(IFrameDoc);
		var hgt = getIFRAME_doc_height(IFrameDoc);
	
		if (wid && hgt){
            if(hgt<minheight){
                hgt=minheight; 
            }
            if(wid<minwidth){
                wid=minwidth;
            }             
            oIframe.style.width = wid;
            oIframe.style.height = hgt;
            
        }

		oIframe.style.width = wid;
		oIframe.style.height = hgt;
		} 
}

  function getParentRowForElement(element) {
    while(element.tagName != 'TR' && element.tagName != 'tr') {
        element = element.parentNode
    }
    return element    
  }

 function cancelMouseClick(e){
    try {
        if(e.stopPropagation) e.stopPropagation();
        else e.cancelBubble = true;
    }
    catch(e){}
 }
 
function getViewportHeight() {
    if (window.innerHeight!=window.undefined) return window.innerHeight;
    if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
    if (document.body) return document.body.clientHeight; 
    return window.undefined; 
}

function getViewportWidth() {
    if (window.innerWidth!=window.undefined) return window.innerWidth; 
    if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
    if (document.body) return document.body.clientWidth; 
    return window.undefined; 
}
 
