/* $Id$ */

var ticketName = "IAMAGENTTICKET";
function getCookie(cookieName)
{
    if (typeof(cookieName) == "undefined")
    {
        cookieName = ticketName;
    }
    var cookieValue;
    var cookie = document.cookie;

    var cookieArr = cookie.split(';');

    for(var i=0;i<cookieArr.length;i++ )
    {
        var tmp = cookieArr[i];
        if(tmp.indexOf(cookieName) != -1)       
        {
            tmp=tmp.split('=');
            cookieValue = tmp[1];
            break;
        }
    }
    return cookieValue;
}

function authenticate()
{
    var cookie = getCookie();
    if(!cookie)
    {
        cookie = getCookie();//This is done because Mozilla requires login twice
        if (!cookie)
        {
            return false;
        }
        return false;
    }
    else
    {
        return true;
    }
}
function getPageWidth( object )
{
    var x;
    if( object == null ) object = document.body; 
    var test1 = object.scrollWidth;
    var test2 = object.offsetWidth
    if (test1 > test2 || browser_ie)
    {
        x = object.scrollWidth;
    }
    else 
    {
        x = object.offsetWidth;
    }
    return x;
}
function getPageHeight( object )
{
    var y;
    if( object == null ) object = document.body; 
    var test1 = object.scrollHeight;
    var test2 = object.offsetHeight
    if (test1 > test2 || browser_ie )
    {
        y = object.scrollHeight;
    }
    else
    {
        y = object.offsetHeight;
    }
    var sy = getScreenHeight();
    if( sy > y )
    {
        y = sy;
    }
    return y;
}
function getScreenWidth() {
    var x;
    if (self.innerHeight) 
    {
        x = self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
        x = document.documentElement.clientWidth;
    }
    else if (document.body)
    {
        x = document.body.clientWidth;
    }
    return x;
}
function getScreenHeight() {
    var y;
    if (self.innerHeight)
    {
        y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
        y = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
        y = document.body.clientHeight;
    }
    return y;
}

function loginDialog(targetURL,sid)
{
    var durl = document.URL;
    var serverName = "";
    if (targetURL=="") {
        targetURL = window.location;
    }
    if(authenticate()) {
        window.location = targetURL;
    }
    else
    {
        showLoginDialog(targetURL, sid);
    }
}
function showLoginDialog(targetURL,sid)
{
    var _url = "/platform/login.jsp?serviceurl=" + encodeURIComponent(targetURL);
    var sobj = document.getElementById(sid);
    var x = findPosX(sobj);
    var y = findPosY(sobj);
    var left = 0;
    var top = 0;
    var width = 290;
    if((x + width) > getPageWidth() ) {
        left = getPageWidth() - (x-width) + sobj.offsetWidth;
    } else {
        left = x ;
    }
    if ((y + 332) > getPageHeight()) {
        top = getPageHeight() - (y + 332) + sobj.offsetHeight + 30;
    } else {
        top = y + sobj.offsetHeight + 20;
    }
    showZCURLInDialog(_url, "modal=no, position=absolute, top=" + top + ", left=" + left + ", transitionInterval=80,closeOnEscKey=no, closeOnBodyClick=no, closeButton=no,width=" + width);
}

function getXmlHttpObj(formElem) {
	var xmlObject = null;
   	if(document.all) {
   	    
		var xmlObject = null;
		if (window.XMLHttpRequest){ // check for IE7
		   xmlObject = new XMLHttpRequest();
		}
		
		else 
		{
		    if(!formElem) {
				try { 
					xmlObject = new ActiveXObject('Microsoft.XMLDOM');
				} catch(e) {}
			}
			if(xmlObject == null) {
				try { 
					xmlObject = new ActiveXObject('Msxml2.XMLHTTP');
				} catch(e) {}
			}
			if(xmlObject == null) {
				try { 
					xmlObject = new ActiveXObject('Microsoft.XMLHTTP'); 
				} catch(e) {
					throw new Exception('Browser not supported');
				}
			}
	    }
		
		
	} else {
		xmlObject = new XMLHttpRequest();
	}
	return xmlObject;
}

function showZCURLInDialog(url, features)
{
	var xmlhttp = getXmlHttpObj(true);
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			showDialog(xmlhttp.responseText, features);
		}
	}	
	xmlhttp.send(null);
}
