/* $Id: loginDialog.js,v 1.5 2009/08/04 13:40:42 raffic Exp $ */

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 loginZohoDialog(targetURL,sid)
{
    var durl = document.URL;
    var serverName = "";
    if (targetURL=="") {
        targetURL = window.location;
    }
    if(authenticate()) {
        window.location = "http://creator.zoho.com"+targetURL;
    }
    else
    {
	
        showZohoLoginDialog(targetURL, sid);
    }
}
function showZohoLoginDialog(targetURL,sid)
{
	
    var _url = "/creator/login/login.html?serviceurl=" + encodeURIComponent(targetURL);
    var sobj = document.getElementById(sid);
    var x = findPosX1(sobj);
    var y = findPosY1(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);
}
/**
 * Retruns the x position of the given object in the window / screen.
 */
function findPosX1(obj) {
	var curleft = 0;
	if (document.getElementById || document.all) {
		curleft += document.body.offsetLeft;
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (document.layers) {
		curleft += obj.x;
	}
	return curleft;
}

/**
 * Retruns the x position of the given object in the window / screen.
 */
function findPosY1(obj) {
	var curtop = 0;
	if (document.getElementById || document.all) {
		curtop += document.body.offsetTop;
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (document.layers) {
		curtop += obj.y;
	}
	return curtop;
}

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);
}


function fnShowMenu(id,divid)
{
    var sobj = document.getElementById(id);
    var x = findPosX(sobj);
    var y = findPosY(sobj);
    var mobj = document.getElementById("zc-homedropdown"+divid);
    mobj.style.display="";
    mobj.style.left = x-11;
    mobj.style.top = y+20;
}
function fnRemMenu(divid)
{
   document.getElementById("zc-homedropdown"+divid).style.display="none";
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function fnHide(fieldid)
			{
				var idArray = new Array("forms","views","bizrules","customization","collaboration","extras");

				for(i=0;i<idArray.length;i++)
				{
					document.getElementById(idArray[i]).style.display = 'none';
					document.getElementById(idArray[i]+"-anchor").className = 'body-sub-nav-inactive';
					document.getElementById(idArray[i]+"-anchor").style.color = '#0066FE';
					document.getElementById(idArray[i]+"-anchor").onmouseover = new Function("evt","this.className='body-sub-nav-inactive-h'");
					document.getElementById(idArray[i]+"-anchor").onmouseout = new Function("evt","this.className='body-sub-nav-inactive'");
				}
				
				document.getElementById(fieldid).style.display = 'block';
				document.getElementById(fieldid+"-anchor").className = 'body-sub-nav-active';
				document.getElementById(fieldid+"-anchor").style.color = '#FFF';
				document.getElementById(fieldid+"-anchor").onmouseover = new Function("evt","");
				document.getElementById(fieldid+"-anchor").onmouseout = new Function("evt","");

			}