//Contains functions for Global use

var currentBrowser = isBrowser();

/**
 *  $
 *  Receives id to get
 *  returns Handle on element
 */
function $(id){
  return document.getElementById(id);
}

function isBrowser(){
	if(navigator.appVersion.match("MSIE 7.0") == "MSIE 7.0"){
		return "IE7";
	}
	else if(navigator.appVersion.match("MSIE 6.0") == "MSIE 6.0"){
		return "IE6";
	}
	else if(navigator.userAgent.match("Firefox") == "Firefox"){
		return "Firefox";
	}
	else if(navigator.userAgent.match("Chrome") == "Chrome"){
		return "Chrome";
	}
	else{
		return "other";
	}
}

/**
 *  Make an XMLHTTP js object
 */
function MakeAJAX(){
  var xmlhttp = false;

  try{
    // JS higher than 5
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    //alert("Using IE");
  } catch(e){
    try{
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      //alert("Using IE");
    } catch(E){
      xmlhttp = false;
    }
  }
  //Non IE
  if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
    xmlhttp = new XMLHttpRequest();
    //alert("using non-IE, good for you");
  }
  return xmlhttp;
}

/**
 *  Fill Content
 *  Receives :
 *    serverPage - page name requested from DB
 *    objID - where to load content
 */

function FillContent(serverPage, objID){
  var obj = $(objID);
  var xmlhttp = MakeAJAX();

  xmlhttp.open("GET", serverPage);
  xmlhttp.onreadystatechange = function(){
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
      obj.innerHTML = xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
}

function GetContent(serverPage, objID){
  var obj = $(objID);
  var xmlhttp = MakeAJAX();

  xmlhttp.open("GET", serverPage);
  xmlhttp.onreadystatechange = function(){
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
      obj.value = xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
}

function addEvent(input, event, eventCall){
	if(isBrowser().match("IE")){
		input.attachEvent("on"+event,eventCall);
	}
	else{
		input.addEventListener(event,eventCall, false);
	}
}

function getForm(){
	var curForm = document.getElementsByTagName("form");
	return curForm[0];
}

function changePic(form, picture){
	var eeForm = document.getElementById(form);

	var pich = document.createElement("input");

	pich.name = "MAX_FILE_SIZE";
	pich.type = "hidden";
	pich.value = "100000";

	var pic = document.createElement("input");

	pic.name = "uploadedfile";
	pic.type = "file";

	eeForm.replaceChild(pic, document.getElementById(picture));
	eeForm.appendChild(pich);

}

//Fade code
// HAS TO HAVE HEIGHT WIDTH FOR IE
//end part to avoid flicker
//style="width: 100%; height: 100%; filter: alpha(opacity=0); -moz-opacity: 0; opacity: 0;"
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    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("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}
//********************************************

//Lightbox code
function OpenLightBox(light, fade){
	$(light).style.display='block';
	$(fade).style.display='block';
}

function CloseLightBox(light, fade){
	$(light).style.display='none';
	$(fade).style.display='none';
}
//********************************************