﻿// JScript File

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;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

function isNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function getElementFromEvent(event){
    var obj = new Object()
    if (browser.isIE){
      obj = window.event.srcElement;
    }
    if (browser.isNS){
      obj = event.target;
    }
    return obj
}


function windowPos(){
    //cross-browser compatible window dimensions
    var scrollx = 0;
    var scrolly = 0;
    var w = 0;
    var h = 0;
    if (browser.isIE) {
        scrollx = document.body.scrollLeft;
        scrolly = document.body.scrollTop;
        if(document.documentElement){
        w = document.documentElement.clientWidth;
        h = document.documentElement.clientHeight;
        }else{
        
        }
    }
    if (browser.isNS) {
        scrollx = window.scrollX;
        scrolly = window.scrollY;
        w = window.innerWidth;
        h = window.innerHeight;
    }
      
    return {scrollx: scrollx, scrolly: scrolly, w: w, h: h};
}

function mousePos(event){
    //cross-broswer compatible mouse X & Y position
    var x = 0;
    var y = 0;
    if (browser.isIE) {
        x = window.event.clientX
        y = window.event.clientY
    }
    if (browser.isNS) {
        x = event.clientX
        y = event.clientY
    }
    return {x: x, y: y}; 
}


function elementPos(myelem) {
    //cross-browser compatible element x & y position & width & height
		var x = 0;
		var y = 0;
	    var w = 0;
	    var h = 0;
	    
	    var elem = myelem;
	    	
		while (elem.offsetParent) {	
			x += elem.offsetLeft + (elem.clientLeft || 0);
			y += elem.offsetTop + (elem.clientTop || 0);
			h += elem.offsetLeft + (elem.clientLeft || 0);
			w += elem.offsetWidth + (elem.clientTop || 0);
			elem = elem.offsetParent;
		}
		
		if(myelem.clientWidth){
		    w = myelem.clientWidth;
		}
		if(myelem.clientHeight){
		    h = myelem.clientHeight;
		}
		
		return {x: x, y: y, w: w, h: h};	
}

function centerElementOnScreen(elementId){
    var myElem = document.getElementById(elementId)
    var winPos = windowPos()
    var ePos = elementPos(myElem)
    myElem.style.left = ((winPos.w / 2) - (ePos.w / 2) + winPos.scrollx) + "px";
    myElem.style.top = ((winPos.h / 2) - (ePos.h / 2) + winPos.scrolly) + "px";
}

function writeInnerHtml(txt, divid){

	if (browser.isNS == true) {
		//Mozilla version of code
		document.getElementById(divid).innerHTML = txt
		
	} else {
	//IE version of code
		document.getElementById(divid).innerHTML = "";
		if(document.getElementById(divid + "Body")){
			document.getElementById(divid).removeChild(document.getElementById(divid + "Body"));
		}
		var newcontent = document.createElement("div");
		newcontent.id = divid + "Body"
		newcontent.innerHTML = txt
		document.getElementById(divid).appendChild(newcontent)
		
		
	}
}

function HideElement(id){
    document.getElementById(id).style.display = "none";
}

function ShowElement(id){
    document.getElementById(id).style.display = "block";
}

////////////////////////////////////////////////////////////////////////////////////
//FADE IN & OUT FUNCTIONS
function Opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    if(document.getElementById(id)){
        var speed = Math.round(millisec / 100);
        var timer = 0;

        //determine the direction for the blending, if start and end are the same nothing happens
        if(opacStart > opacEnd) {
            for(i = opacStart; i >= opacEnd; i--) {
                setTimeout("OpacityChangeById(" + i + ",'" + id + "')",(timer * speed));
                timer++;
            }
        } else if(opacStart < opacEnd) {
            for(i = opacStart; i <= opacEnd; i++)
                {
                setTimeout("OpacityChangeById(" + i + ",'" + id + "')",(timer * speed));
                timer++;
            }
        }
    }
}

//change the opacity for different browsers
function OpacityChange(opacity, object) {
    object.style.opacity = (opacity / 100);
    object.style.MozOpacity = (opacity / 100);
    object.style.KhtmlOpacity = (opacity / 100);
    object.style.filter = "alpha(opacity=" + opacity + ")";
} 

function OpacityChangeById(opacity, id) {
    if(document.getElementById(id)){
        var object = document.getElementById(id).style
        object.opacity = (opacity / 100);
        object.MozOpacity = (opacity / 100);
        object.KhtmlOpacity = (opacity / 100);
        object.filter = "alpha(opacity=" + opacity + ")";
    }
} 


//////////////////////////////////////////////////////////////////////////////////////////////
//DRAG ELEMENT FUNCTIONALITY

var dragElementObj = new Object();
var dragElementZindex = 1000;


function dragElementStart(event) {
      var el;
      var x, y;

        if (browser.isIE)
          dragElementObj.elNode = window.event.srcElement;
        if (browser.isNS)
          dragElementObj.elNode = event.target;

        // Find the parent, which is the DIV container of the evolvercomponent
        i = 0
        do{
            dragElementObj.elNode = dragElementObj.elNode.parentNode;
            i = i + 1
            if(i > 1000){
                break;
            }
        }while(dragElementObj.elNode.getAttribute("drag") != "1")
        //alert(dragElementObj.elNode.id)

        var mPos = mousePos(event);
        var winPos = windowPos();
      
        x = mPos.x + winPos.scrollx
        y = mPos.y + winPos.scrolly

      // Save starting positions of cursor and element.

      dragElementObj.cursorStartX = x;
      dragElementObj.cursorStartY = y;
      dragPos = elementPos(dragElementObj.elNode)
      dragElementObj.elStartLeft  = parseInt(dragPos.x, 10);
      dragElementObj.elStartTop   = parseInt(dragPos.y,  10);

      if (isNaN(dragElementObj.elStartLeft)) dragElementObj.elStartLeft = 0;
      if (isNaN(dragElementObj.elStartTop))  dragElementObj.elStartTop  = 0;


      
      
      
      
      // Update element's z-index.
      dragElementZindex = dragElementZindex + 1
      dragElementObj.elNode.style.zIndex = dragElementZindex;

      // Capture mousemove and mouseup events on the page.

      if (browser.isIE) {
        document.attachEvent("onmousemove", dragElementGo);
        document.attachEvent("onmouseup",   dragElementStop);
        window.event.cancelBubble = true;
        window.event.returnValue = false;
      }
      if (browser.isNS) {
        document.addEventListener("mousemove", dragElementGo,   true);
        document.addEventListener("mouseup",   dragElementStop, true);
        event.preventDefault();
      }
      
}

function dragElementGo(event) {

  var x, y;
  var mPos = mousePos(event);
  var winPos = windowPos();
  
  x = mPos.x + winPos.scrollx
  y = mPos.y + winPos.scrolly

  // Move drag element by the same amount the cursor has moved.

  newleft = (dragElementObj.elStartLeft + x - dragElementObj.cursorStartX);
  newtop  = (dragElementObj.elStartTop  + y - dragElementObj.cursorStartY);


//set up BOUNDRYS in the Content Area
    ebound = elementPos(document.body)
    cbound = elementPos(dragElementObj.elNode)
    
        
    if(newleft + cbound.w < (ebound.w + ebound.x) && (parseInt(newleft) - ebound.x) >= 0){
        dragElementObj.elNode.style.left = newleft - ebound.x + "px";
    }else if(newleft + cbound.w > ebound.w + ebound.x){
        dragElementObj.elNode.style.left =(ebound.w - cbound.w) + "px"
    }else{
        dragElementObj.elNode.style.left = 0 + "px";
    }
    
    if(newtop >= ebound.y){
        dragElementObj.elNode.style.top = newtop - ebound.y + "px";
    }else{
        dragElementObj.elNode.style.top = 0 + "px";
    }
        
  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();

}

function dragElementStop(event) {

  // Stop capturing mousemove and mouseup events.
  if (browser.isIE) {
    document.detachEvent("onmousemove", dragElementGo);
    document.detachEvent("onmouseup",   dragElementStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragElementGo,   true);
    document.removeEventListener("mouseup",   dragElementStop, true);
  }
  

}





/////////////////////////////////////////////////////////////////////
//COOKIE HANDLING

function setCookie(c_name,value,expiredays){
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString())
}

function getCookie(c_name){
    if (document.cookie.length > 0){
      c_start=document.cookie.indexOf(c_name + "=")
      if (c_start!=-1){ 
        c_start=c_start + c_name.length+1 ;
        c_end=document.cookie.indexOf(";",c_start)
        if (c_end==-1){
            c_end=document.cookie.length
            return unescape(document.cookie.substring(c_start,c_end));
        } 
      }
    }
    return ""
}