var agt = navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();
var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);
var iePos = appVer.indexOf('msie');
if (iePos != -1) {
    is_minor = parseFloat(appVer.substring(iePos + 5, appVer.indexOf(';', iePos)))
    is_major = parseInt(is_minor);
}
var is_getElementById = (document.getElementById) ? "true" : "false";
var is_getElementsByTagName = (document.getElementsByTagName) ? "true" : "false";
var is_documentElement = (document.documentElement) ? "true" : "false";
var is_ie = ((iePos != -1));
var is_ie6 = (is_ie && is_major == 6);

var isIE = is_ie;

function catchEvent(e){
	var targ;
	if (!e)
	{
	var e=window.event;
	}
	if (e.target)
	{
	targ=e.target;
	}
	else if (e.srcElement)
	{
	targ=e.srcElement;
	}
	if (targ.nodeType==3) // defeat Safari bug
	{
	targ = targ.parentNode;
	}
	return targ	
}

/*Sets menu highlights*/

function highlightMenu(e){
	
	var targ = catchEvent(e)	
		//	alert(targ.parentNode.tagName);

	if (targ.className == "nav"){
		var span = targ.getElementsByTagName('span')[0]
	}	
	
	if (targ.parentNode.tagName == "P"){
		p = targ.parentNode
		var div = p.parentNode
		var span = div.getElementsByTagName('span')[0]
	}
	
	if (span.className == "current_page")return
		else span.className = "hover"
	
	if(targ.addEventListener)targ.addEventListener("mouseout", function(){span.className = ""}, false); 
		else if (targ.attachEvent)targ.attachEvent('onmouseout', function(){span.className = ""});
					
	if (targ.removeEventListener)targ.removeEventListener('mouseout',function(){},false);
		else if (targ.detachEvent)targ.detachEvent('onmouseout',function(){});


}

/*Validate form fields*/

function validateMyForm(){
	var x = document.getElementById('myform').getElementsByTagName("input");
	var ob; //object
	var err = 0;
	
	//Checks weather necessary input fields were left unfilled
	if(x[0].value == '' || x[0].value.length < 2){
		x[0].style.backgroundColor="#ffffd7";
		err++
	}else x[0].style.backgroundColor="#ffffff";
	
	ob = x[1].value;
	var email = ob.match("@");
	if(email == null){
		x[1].style.backgroundColor="#ffffd7";	
		err++;
	}else x[1].style.backgroundColor="#ffffff";	
	
	var select= document.getElementById('myform').getElementsByTagName("select");
	
	if(select[1].value == ''){
		select[1].style.backgroundColor="#ffffd7";
		err++
	}else select[1].style.backgroundColor="#ffffff";
	
	if(err > 0){
		return false;
	}
	else{ return true; }
}

/* ------------------------------------------------------------------------------------*/
/* ------------------------ Set functions for Pop_up -------------------------------*/
/* ------------------------------------------------------------------------------------*/

function closeModal(id){
	//if (isIE){
	//	showSelectBox();
	//}
	document.getElementById("modalBackdrop").style.display="none";
	document.getElementById(id).style.display="none";
}

function showModal(id) {
    var modalBackdrop = document.getElementById("modalBackdrop");
    var modalBox = document.getElementById(id);
    if (!modalBackdrop) {
        modalBackdrop = createModalBackdrop();
    }
    modalBackdrop.style.display = "block";

    if (!modalBox.boxWidth && !modalBox.boxHeight) 
    {
            modalBox.boxWidth = modalBox.offsetWidth != 0 ? modalBox.offsetWidth : modalBox.clientWidth;
            modalBox.boxHeight = modalBox.offsetHeight != 0 ? modalBox.offsetHeight : modalBox.clientHeight;
    }
    //alert(xDocSize().h)
    modalBox.style.display = "none";
    modalBox.style.left = Math.round((xClientWidth() / 2 - modalBox.boxWidth / 2) - 100) + "px";
    modalBox.style.top = Math.round((xClientHeight() / 2) - modalBox.boxHeight / 2 + xScrollTop(document.body) - 50) + "px";


    modalBox.style.display = "block";

  // if (isIE) {
   //     hideSelectBox();
   //     modalBackdrop.style.height = document.documentElement.scrollHeight + "px";
 //  }
}

var isSessionExist = false;
function SessionCheckResponse(data)
{
        isSessionExist = data == "1";
}

function GetMediaItem(ContainerID,ItemURL, ItemType)
{

  //  $.get("../Handlers/SessionCheck.ashx?UniqParam=" + ItemURL + new Date(), SessionCheckResponse); // async request
    $.ajax({url: "../Handlers/SessionCheck.ashx?UniqParam=" + ItemURL + new Date(), success: SessionCheckResponse, async: false });
    
    if(isSessionExist)
    {
        if(ItemType == 'IMAGE'){ShowImage(ContainerID,ItemURL);}
        else document.location.href = ItemURL;
    }
    else
    {
        alert("Session time out");
        document.location = "../default.aspx?Err=2";
    }
}

function ShowImage(ContainerID, url)
{
     var Container = document.getElementById('divImage');
     var ArrItems = Container.getElementsByTagName('img');
     for(var i=0; i<ArrItems.length;i++)
       Container.removeChild(ArrItems[i]);

     var img = new Image();
     img.className = 'ImagePopup';
     img.onload = function () 
     {
        var imgWidth = img.width;
        var imgHeight = img.height;
        if (imgHeight > 0) {
            var Proportion = 1.0 * imgWidth / imgHeight;
            var PX = 1.0 * imgWidth / 640;
            var PY = 1.0 * imgHeight / 480;

            if (imgWidth > 640 || imgHeight > 480) 
            {
                if (PX > PY) 
                {
                    imgWidth = 640;
                    imgHeight = imgHeight / PX;
                } else 
                {
                    imgHeight = 480;
                    imgWidth = imgWidth / PY;
                }
                img.width = imgWidth;
                img.height = imgHeight;               
            }
       }
        RelocatePicture('picContainer',img.width,img.height); 
     }  
     
     img.setAttribute('src',url); 
     Container.appendChild(img);         
}



function RelocatePicture(id,imgWidth, imgHeight)
{
	var modalBackdrop= document.getElementById('modalBackdrop');
	var modalBox=document.getElementById(id);
	if (!modalBackdrop){
		modalBackdrop=createModalBackdrop();
	}
	modalBackdrop.style.display="block";
	
//	if (!modalBox.boxWidth && !modalBox.boxHeight)
//	{
//		modalBox.boxWidth=modalBox.offsetWidth;
//		modalBox.boxHeight=modalBox.offsetHeight;	
//	}
    modalBox.style.display="none";
	modalBox.style.left=Math.round((xClientWidth()/2-imgWidth/2))+"px";
	modalBox.style.top=Math.round((xClientHeight()/2)-imgHeight/2+xScrollTop(document.body))+"px";
	//if (isIE){
	//	hideSelectBox();		
	//	modalBackdrop.style.height=document.documentElement.scrollHeight+"px";
	//}
	modalBox.style.display="block";
}

function xClientWidth()
{
 var v=0,d=document,w=window;
 if(d.compatMode == 'CSS1Compat' && !w.opera && d.documentElement && d.documentElement.clientWidth)
 {v=d.documentElement.clientWidth;}
 else if(d.body && d.body.clientWidth)
 {v=d.body.clientWidth;}
 else if(xDef(w.innerWidth,w.innerHeight,d.height)) {
 v=w.innerWidth;
 if(d.height>w.innerHeight) v-=16;
 }
 return v;
}

function xClientHeight()
{
  var v=0,d=document,w=window;
  if((!d.compatMode || d.compatMode == 'CSS1Compat') && !w.opera && d.documentElement && d.documentElement.clientHeight)
	{v=d.documentElement.clientHeight;}
  else if(d.body && d.body.clientHeight)
	{v=d.body.clientHeight;}
  else if(xDef(w.innerWidth,w.innerHeight,d.width)) {
	v=w.innerHeight;
	if(d.width>w.innerWidth) v-=16;
  }
  return v;
}

function xScrollTop(e, bWin)
{
  var offset=0;
  if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
	var w = window;
	if (bWin && e) w = e;
	if(w.document.documentElement && w.document.documentElement.scrollTop) offset=w.document.documentElement.scrollTop;
	else if(w.document.body && xDef(w.document.body.scrollTop)) offset=w.document.body.scrollTop;
  }
  else {
	e = xGetElementById(e);
	if (e && xNum(e.scrollTop)) offset = e.scrollTop;
  }
  return offset;
}

function xDef()
{
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
  return true;
}

function hideSelectBox(){
    //hide selects to fix annoying select bug in ie
	if(isIE){
		var allSelects=document.getElementsByTagName("select");
		for(var x=0;x<allSelects.length;x++){		
			//if not in a modal box
			if(allSelects[x].className.indexOf('modal')==-1){
			    allSelects[x].style.visibility="hidden";	
			    }
		}
	}
}

function showSelectBox() {
    //show selects to fix annoying select bug in ie
    if (isIE) {
        var allSelects = document.getElementsByTagName("select");

        for (var x = 0; x < allSelects.length; x++) {
            allSelects[x].style.visibility = "visible";
        }
    }
}

function SetObjectOnCenter(objID)
{
    var obj = document.getElementById(objID);
    obj.style.left=Math.round((xClientWidth()/2-obj.offsetWidth/2))+"px";
	obj.style.top=Math.round((xClientHeight()/2)-obj.offsetHeight/2+xScrollTop(document.body))+"px";
}

function GetPopupTop(height)
{
	return Math.round((xClientHeight()/2)- height / 2 + xScrollTop(document.body));
}

function GetPopupLeft(width)
{
    return Math.round((xClientWidth() / 2 - width /2));
}




