var ie=document.all;
var nn6=document.getElementById&&!document.all;

var myIsDragBoolean=false;
var x,y;
var myDragObject;

function movemouse(e)
{
  if (myIsDragBoolean && myDragObject != null)
  {
    var tbl = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
    var tbt = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
    
    myDragObject.style.top  = tbt + 'px';
    myDragObject.style.left = tbl + 'px';
    
    return false;
  }
}
function cancelDrag()
{
    myIsDragBoolean=false;
}
function myDragThis(e)
{
    var fobj       = document.getElementById('myDragTableId');
    myIsDragBoolean = true;
    myDragObject = fobj;
    tx = parseInt(myDragObject.style.left+0);
    ty = parseInt(myDragObject.style.top+0);
    x = nn6 ? e.clientX : event.clientX;
    y = nn6 ? e.clientY : event.clientY;
    document.onmousemove=movemouse;
    return false;
}
var myOpenedWindowSender =null;
var myDragObjectContainer = 'myDragObjectContainerId';
var firstTime = null;
var myDragObjectContainerObject = null;
function myCreateDragContainer(dragTitle, dragWidth, dragHeight,iframeSrc,sender)
{
//    debugger;
    if ( firstTime == false )
        return true;

//    if ( document.getElementById(myDragObjectContainer) != null )
//        document.forms[0].removeChild(myDragObject);

    myDragObjectContainerObject = document.getElementById(myDragObjectContainer);

    if ( myDragObjectContainerObject == null || myDragObjectContainerObject == undefined )
    {
        myDragObjectContainerObject = document.createElement("div");
        myDragObjectContainerObject.id = myDragObjectContainer;
        document.forms[0].appendChild(myDragObjectContainerObject);
    }

   var tableString = "<div id=\"myBackGroundDiv\" style=\"background-color: #DCDCDC; filter: alpha(opacity=50); opacity: 0.50;-moz-opacity: 0.50; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;z-index: 99;\"></div><table id=\"myDragTableId\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"width:"+dragWidth+";height:"+dragHeight+";position:absolute;border:solid 1px gray;z-index:100;\"><tr><td id=\"myDragTableTitle\" class=\"CaptionColor\" align=\"left\" style=\"width: 100%;cursor:move; font-family: Verdana;font-weight: bold;\" onmousedown=\"myDragThis(event);\">"+dragTitle+"<input type=\"hidden\" id=\"myHResult\" value=\"\" /></td><td align=\"right\" class=\"CaptionColor\" style=\"width: 30px;cursor:pointer; font-family: Verdana; padding-right:5px; font-weight: bold;\" onclick=\"myCloseDiv(false);\">close</td></tr><tr><td colspan=\"2\" style=\"height: 100%;background-color:#FFFFFF;\" id=\"myDragContainer\"><iframe frameborder=\"0\" src=\""+iframeSrc+"\" style=\"width:100%;height:100%;background-color:#FFFFFF;\"  ></iframe></td></tr></table>";
      
//    document.forms[0].innerHTML += tableString;
    
    myDragObjectContainerObject.innerHTML = tableString;
    
    w2cs22(document.getElementById('myDragTableId'));
    
    x2ws(document.getElementById('myBackGroundDiv'));
    
    myDragObject = document.getElementById('myDragTableId');

    myOpenedWindowSender = sender;

    firstTime = true;
    
    return false;
}

function x2ws(x2wsControl){
    //ctrl.style.width  = Math.max(doc.scrollWidth, doc.clientWidth) + "px";
    //ctrl.style.height = Math.max(doc.scrollHeight, doc.clientHeight) + "px";
    if(typeof(window.innerWidth)=='number'){
        x2wsControl.style.width = Math.max(document.body.scrollWidth, window.innerWidth) + "px";
    }
    else{
        x2wsControl.style.width = Math.max(document.body.scrollWidth, document.body.clientWidth) + "px";
    }
    if(typeof(window.innerHeight)=='number'){
        x2wsControl.style.height = Math.max(document.body.scrollHeight, window.innerHeight) + "px";
    }
    else{
        x2wsControl.style.height = Math.max(document.body.scrollHeight, document.body.clientHeight) + "px";
    }
}

function CloseMeOk(url){ 
     var divElem=document.getElementById('myBackGroundMessageTable');
     var divElem2=document.getElementById('myBackGroundDiv');
     if ( divElem.parentNode != null && divElem2.parentNode != null)
     {
        divElem.parentNode.removeChild(divElem);
        divElem2.parentNode.removeChild(divElem2);
        if (typeof(url)!='undefined')
        {
            window.location=url;
        }
     }
}

function ShowMessage(mess, url, title, buttonText){  
    var close='';
    if (typeof(url)=='undefined' || url=='')
        close="CloseMeOk()";
    else
        close="CloseMeOk('"+url+"')";
   
    var title1='Processing Request...';
    if (typeof(title)!='undefined')
        title1=title;   
   
    var text = 'Close';
    if(typeof(buttonText)!='undefined'&&buttonText!='')
        text = buttonText;
        
    var htmlString = "<div id=\"myBackGroundDiv\" style=\"filter: alpha(opacity=50);opacity: 0.50;-moz-opacity: 0.50; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;z-index: 999;background-color:gray;display:block\"></div>"+
                            "<table id=\"myBackGroundMessageTable\" border=\"0\" style=\"width:400px;height:70px;position:absolute;top:50px;left:50px;z-index:1000;border:solid 1px #cccccc;border-collapse:collapse;\"   cellpadding=\"0\" cellspacing=\"0\" class=\"Portlet\">"+
                            "<tr><td class=\"CaptionColor\" align=\"left\">"+title1+"</td></tr>"+
                            "<tr><td align=\"center\" valign=\"top\" style=\"height:70px;\">"+
                                "<table style=\"width:100%;height:30px;\" border=\"0\" cellpadding=\"10\" cellspacing=\"0\" class=\"PortletContent\">"+
                                "<tr><td  style=\"height:30px;\" align=\"center\" valign=\"top\" class=\"divTextCabriBlack\">" + mess +
                                "</td></tr>" +
                                "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"right\" style=\"padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px;\"><tr><td align=\"right\" valign=\"top\" style=\"width:30px;height:30px;\"><a onclick=\""+close+"\" class='button'><span>"+ text +"</span></a></td</tr></table>"+
                            "</table>"+                            
                            "</td></tr></table>";
        
        var myBackGroundDivContainer = document.createElement("div");
        
        myBackGroundDivContainer.innerHTML = htmlString;
         var p=document.getElementById('divWaitCenter');  
            if (p==null) {
            document.body.appendChild(myBackGroundDivContainer);    
            }
           else {
            p.appendChild(myBackGroundDivContainer);
        }
         x2ws(document.getElementById('myBackGroundDiv'));  
         //CenterDivControl(document.getElementById('myBackGroundMessageTable'));
         w2cs22(document.getElementById('myBackGroundMessageTable'));    	
	    return false;
    }
    
function showWaitingPage(mes,elemL){ 
    var elemL=document.getElementById(elemL);   
    if (!elemL.disabled){
        document.body.scrollTop = 0; 
        var htmlString = "<div id=\"myBackGroundDiv\" style=\"opacity: 0.50;filter: alpha(opacity=50); -moz-opacity: 0.5; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;z-index: 999;display:block; background-color:gray;\"></div>"+
                                "<table id=\"myBackGroundMessageTable\" border=\"0\" style=\"width:400px;height:120px;position:absolute;top:50px;left:50px;z-index:1000;border:solid 1px #cccccc;border-collapse:collapse;\"   cellpadding=\"0\" cellspacing=\"0\" class=\"Portlet\">"+
                                "<tr><td class=\"showWaitingPageTableTitle\" align=\"left\">Processing Request...</td></tr>"+
                                "<tr><td align=\"center\" valign=\"top\" style=\"height:100px;\">"+
                                    "<table style=\"width:100%;height:100px;\" border=\"0\" cellpadding=\"10\" cellspacing=\"0\" class=\"PortletContent\">"+
                                    "<tr><td  style=\"height:100px;\" align=\"center\" valign=\"top\" class=\"showWaitingPageText\">" + mes +
                                    "<table cellpadding=\"0\" cellspacing=\"0\"><tr><td style=\"height:8px;\">&nbsp;</td></tr><tr><td style=\"width:120px;height:40px;\" class=\"divLogoWait\">&nbsp;</td</tr></table></td></tr>" +
                                "</table>"+                            
                                "</td></tr></table>";
        var myBackGroundDivContainer = document.createElement("div");
        myBackGroundDivContainer.innerHTML = htmlString;        
        //StretchControl(myBackGroundDivContainer);
        var p=document.getElementById('divWaitCenter');  
        if (p==null){
            document.body.appendChild(myBackGroundDivContainer);    
        } else {
            p.appendChild(myBackGroundDivContainer);
        }
        x2ws(document.getElementById('myBackGroundDiv'));  
        //w2cs22(document.getElementById('myBackGroundMessageTable'));
        //CenterControl(document.getElementById('myBackGroundDiv'));
        CenterControl(document.getElementById('myBackGroundMessageTable'));
    }
    return true;
}


function showModalPopupMessage(mes,elemL){ 
    var elemL=document.getElementById(elemL);   
    if (!elemL.disabled){
        document.body.scrollTop = 0; 
        var htmlString = "<div id=\"myBackGroundDiv\" style=\"opacity: 0.50;filter: alpha(opacity=50); -moz-opacity: 0.5; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;z-index: 999;background-color:gray;\"></div>" +
                                "<table id=\"myBackGroundMessageTable\" border=\"0\" style=\"width:400px;height:120px;position:absolute;z-index:1000;border:solid 1px #cccccc;border-collapse:collapse;\"   cellpadding=\"0\" cellspacing=\"0\" class=\"Portlet\">"+
                                "<tr><td class=\"CaptionColor\" align=\"left\">Processing Request...</td></tr>"+
                                "<tr><td align=\"center\" valign=\"top\" style=\"height:100px;\">"+
                                    "<table style=\"width:100%;height:100px;\" border=\"0\" cellpadding=\"10\" cellspacing=\"0\" class=\"PortletContent\">"+
                                    "<tr><td  style=\"height:100px;\" align=\"center\" valign=\"top\" class=\"divTextCabri\">" + mes +
                                    "<table cellpadding=\"0\" cellspacing=\"0\"><tr><td style=\"height:8px;\">&nbsp;</td></tr><tr><td style=\"width:120px;height:40px;\" class=\"divLogoWait\">&nbsp;</td</tr></table></td></tr>" +
                                "</table>"+                            
                                "</td></tr></table>";
        var myBackGroundDivContainer = document.createElement("div");
        myBackGroundDivContainer.innerHTML = htmlString;        
        var p=document.getElementById('divWaitCenter');  
        if (p==null) {
            document.body.appendChild(myBackGroundDivContainer);    
        }
        else {
            p.appendChild(myBackGroundDivContainer);
        }
         x2ws(document.getElementById('myBackGroundDiv'));  
         w2cs22(document.getElementById('myBackGroundMessageTable'));
    }
    return true;
}


function myCloseDiv(result,refreshPage){
//    debugger;
    if ( myDragObject != null )
    {
        try
        {
            document.getElementById(myDragObjectContainer).removeChild(myDragObject);
            document.getElementById(myDragObjectContainer).removeChild(document.getElementById('myBackGroundDiv'));
//            document.forms[0].removeChild(myDragObject);
            myDragObject = null;
        }
        catch(e)
        {
            alert(e.message);
        }
    }
    if ( myOpenedWindowSender != null && result )
    {
        firstTime = false;
        document.getElementById(myOpenedWindowSender.id).click();
    }
    if ( !result )
    {
        firstTime = null;
        checkCreditAndOpenCurrencyPaymentFirstTime = null;
    }
    if ( refreshPage == true )
    {
        window.location.reload(true);
    }
}
function myRedirect(url)
{
//    alert(url);
    
    if ( window.parent != null ) window.parent.location.href = url;
//    debugger;
}
function w2cs22(wnd) {
    var pageW = null;
    var pageH = null;

    var wndH = wnd.offsetHeight;
    var wndW = wnd.offsetWidth;

    wnd.style.position = "absolute";

    if (window.innerHeight != null) {
        pageH = window.innerHeight;
        pageW = window.innerWidth;
    }
    else if (document.documentElement.clientHeight != null) {
        pageH = document.documentElement.clientHeight;
        pageW = document.documentElement.clientWidth;
    }
    else if (document.body.clientHeight != null) {
        pageH = document.body.clientHeight;
        pageW = document.body.clientWidth;
    }

    if (pageH == 0 && pageW == 0) {
        pageH = document.body.clientHeight;
        pageW = document.body.clientWidth;
    }
    if (wndH > pageH) {
        wnd.style.top = document.body.scrollTop + "px";
    }
    else {
        wnd.style.top = ((pageH - wndH) / 2) +document.body.scrollTop + "px";
    }
    wnd.style.left = (pageW - wndW) / 2 + "px";
}
document.onmouseup=new Function("myIsDragBoolean=false");

var DIALOG = null, OPACITY = null, LASTPT = null, UIBLOCKER = null, UIBLOCKERMSG = null, AUTH_OPACITY = null, AUTH_DIALOG = null, CLOSING = false;

function IsMSIE7()
{ return navigator.appVersion.toLowerCase().indexOf("msie 7.0") != -1; }

function Point(x, y)
{
    this.X = x;
    this.Y = y;
}

function ShowDialog(title, message, width, buttonText) {
    CloseDialog();
    
    OPACITY = document.createElement("div");
    OPACITY.id = "dvAlertOp";
    OPACITY.style.position = "absolute";
    OPACITY.style.left = "0px";
    OPACITY.style.top = "0px";
    OPACITY.style.backgroundColor = "#cccccc";
    OPACITY.style.zIndex = "1000";
    OPACITY.style.filter = "alpha(OPACITY=50)";
    OPACITY.style.opacity = "0.5";

    AddControl(OPACITY);
    StretchControl(OPACITY);

    DIALOG = document.createElement("div");
    DIALOG.id = "dvAlertDIALOG";
    DIALOG.style.position = "absolute";
    DIALOG.style.backgroundColor = "#ffffff";
    DIALOG.style.border = "outset 1px";
    DIALOG.style.zIndex = "1001";
    DIALOG.style.left = "0px";
    DIALOG.style.top = "0px";
    DIALOG.style.width = width+"px";

    var e_title = document.createElement("div");
    e_title.id = "dvAlert3";
    e_title.className="errorTitle";
    e_title.innerHTML = title;
    DIALOG.appendChild(e_title);

    var e_message = document.createElement("div");
    e_message.style.padding = "20px";
    e_message.style.fontFamily = "Arial";
    e_message.style.fontSize = "11px";
    e_message.innerHTML = message;
    DIALOG.appendChild(e_message);

    var text = "Ok";
    if(typeof(buttonText)!='undefined'&&buttonText!='')
        text = buttonText;

    var closeBtn = document.createElement("a");
    closeBtn.id = "dvAlertDIALOGCloseBtn"
    closeBtn.className = "button";
    closeBtn.innerHTML = "<span>" + text + "</span>";
    closeBtn.style.cursor = "pointer";
    closeBtn.useHandCursor = true;
    closeBtn.onclick = CloseDialog;

    var buttons = document.createElement("div");
    buttons.style.padding = "5px";
    //buttons.style.textAlign = "right";
    buttons.style.cssFloat = "right";
    buttons.style.styleFloat = "right";
    buttons.appendChild(closeBtn);
    DIALOG.appendChild(buttons);

    //DIALOG.innerHTML = WriteHTMLforDivPopup(e_title2, message);
    AddControl(DIALOG);
    //CenterControl(DIALOG);
    
    CenterDivControl(DIALOG.id);
    
    DragControl(DIALOG, e_title);
}

function CloseDialog() {
    if(OPACITY != null)
    { 
        RemoveControl(OPACITY);
        OPACITY = null;
    }
    if(DIALOG != null)
    {
        RemoveControl(DIALOG);
        DIALOG = null;
    }
}


function RemoveControl(ctrl)
{
    if(document.body.getElementsByTagName("div").length > 0)
        document.body.getElementsByTagName("div")[0].removeChild(ctrl);
    else
        document.body.removeChild(ctrl);
}

function AddControl(ctrl)
{
    if(document.body.getElementsByTagName("div").length > 0)
        document.body.getElementsByTagName("div")[0].insertBefore(ctrl, null);
    else
        document.body.appendChild(ctrl);
}

function DragControl(ctrl, hotspot)
{
    hotspot.onmousedown = function(event)
    {
        this.style.cursor = "move";
        LASTPT = GetCursor(IsMissing(event) ? window.event : event);
        
        document.documentElement.onmousemove = function(event)
        {
            var pt = GetCursor(IsMissing(event) ? window.event : event);
            ctrl.style.left = parseInt(ctrl.style.left) + (pt.X - LASTPT.X) + "px";
            ctrl.style.top = parseInt(ctrl.style.top) + (pt.Y - LASTPT.Y) + "px";
            
            LASTPT = pt;
        };
        document.documentElement.onmouseup = function()
        {
            hotspot.style.cursor = "default";
            document.documentElement.onmousemove = null;
            document.documentElement.onmouseup = null;
        };
    };
}

function StretchControl(ctrl)
{
//    var doc           = document.documentElement;
//    ctrl.style.width  = Math.max(doc.scrollWidth, doc.clientWidth) + "px";
//    ctrl.style.height = Math.max(doc.scrollHeight, doc.clientHeight) + "px";
    if(typeof(window.innerWidth)=='number'){
        ctrl.style.width = Math.max(document.body.scrollWidth, window.innerWidth) + "px";
    }
    else{
        ctrl.style.width = Math.max(document.body.scrollWidth, document.body.clientWidth) + "px";
    }
    if(typeof(window.innerHeight)=='number'){
        ctrl.style.height = Math.max(document.body.scrollHeight, window.innerHeight) + "px";
    }
    else{
        ctrl.style.height = Math.max(document.body.scrollHeight, document.body.clientHeight) + "px";
    }
}

function GetCursor(e)
{
    var pt = new Point(0, 0);
    var doc = document.documentElement;
    var body = document.body;

    if(IsMissing(e.clientX))
    {
        pt.X = e.pageX + Math.max(doc.scrollLeft, body.scrollLeft);
        pt.Y = e.pageY + Math.max(doc.scrollTop, body.scrollTop);
    }
    else
    {
        pt.X = e.clientX + Math.max(doc.scrollLeft, body.scrollLeft);
        pt.Y = e.clientY + Math.max(doc.scrollTop, body.scrollTop);
    }
    return pt;
}

function CenterControlAfterResizing() {
    var ctrl = document.getElementById("dvAlertDIALOG");
    if (ctrl != null) {
        CenterControl(ctrl);	
    }
}

function getScreenSizeXY() {
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) 
	  {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	  } 
	  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	  } 
	  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	  }
	  return [myWidth, myHeight];
}	
function getScrollXY() {
      var scrOfX = 0, scrOfY = 0;
      if( typeof( window.pageYOffset ) == 'number' ) 
      {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;	  
      } 
      else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
      {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
      } 
      else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
      {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
      }
      return [ scrOfX, scrOfY ];
}

function CenterControl(wnd)
{
	var pageW = null;
    var pageH = null;

    var wndH = wnd.offsetHeight;
    var wndW = wnd.offsetWidth;

    wnd.style.position = "absolute";

    if (window.innerHeight != null) {
        pageH = window.innerHeight;
        pageW = window.innerWidth;
    }
    else if (document.documentElement.clientHeight != null) {
        pageH = document.documentElement.clientHeight;
        pageW = document.documentElement.clientWidth;
    }
    else if (document.body.clientHeight != null) {
        pageH = document.body.clientHeight;
        pageW = document.body.clientWidth;
    }

    if (pageH == 0 && pageW == 0) {
        pageH = document.body.clientHeight;
        pageW = document.body.clientWidth;
    }

    if (wndH > pageH) {
        wnd.style.top = document.documentElement.scrollTop + "px";
    }
    else {
        wnd.style.top = ((pageH - wndH) / 2) + document.documentElement.scrollTop + "px";
    }
    wnd.style.left = (pageW - wndW) / 2 + "px";
}

function CenterDivControl(divId){    
    var x = $('#' + divId);
    var w = $(window);
    x.css({ left : (w.width() - x.width()) / 2 + "px", top : (w.height() - x.height()) / 2 + w.scrollTop() + "px" });
}

