
// Core Library
// Determine browser.

var isMinNS4 = document.layers;
var ie = document.all && navigator.userAgent.indexOf("Opera") == - 1;
var dom = document.getElementById && ! ie && navigator.userAgent.indexOf("Opera") == - 1;
var isMinIE5 = (ie && navigator.appVersion.indexOf("5.")) >= 0 ? 1:0;
var isMinIE7 = (ie && navigator.appVersion.indexOf("7.")) >= 0 ? 1:0;
var fadetimerID = 0;
var selm;
var slvl;
var slvlstep;
var slvlend;
var sthisstep;
var sfinish;
var fxtimerID = 0;
 
//-----------------------------------------------------------------------------
// Layer visibility.
//-----------------------------------------------------------------------------

var progresstimerID = 0;
function stopWorking(){
    $("#working").hide();
    $('#workingLayer').css('visibility', 'hidden');
    clearTimeout(progresstimerID);
} 

function startWorking(displaytext,delay){
    if($('#errorDiv'))
        $('#errorDiv').hide();
    if (displaytext == undefined){
        displaytext = "Working";
    } 

	if (delay == undefined){
		delay = 3000;
    }

    if ($("#working").length == 0)
        return;
	
    $("#working").show()
    $("#statusText").html(displaytext);
    clearTimeout(progresstimerID);
    progresstimerID = 0;
    progresstimerID = setTimeout("showBusy()",delay);
} 

function showBusy(){
    var mX, my, docID;
    mX = (getWindowWidth() - getWidth(document.getElementById("workingLayer"))) / 2;
    mY = (getWindowHeight() - getHeight(document.getElementById("workingLayer"))) / 2;    
    moveLayerTo(document.getElementById("workingLayer"),mX,300);
    showLayer(document.getElementById("workingLayer"));
   } 

function showBusyText(displaytext){
	$("#statusText").html(displaytext);
	showBusy();
} 

function showLayer(layer){    
    $('#' + layer.id).css('visibility', 'visible');
}  

function getPageScrollX(){
    if (isMinNS4 || dom) 
        return (window.pageXOffset);
    if (ie) if (isMinIE7){
        return (document.documentElement.scrollLeft);
    } 
    else{
        return (document.body.scrollLeft);
    } 
    return ( - 1);
} 

function showHalt(){
       $("#teri").css("height", $(document).height());
       $("#teri").show();       
       //windowObj.style.display ='';	    	
}

function removeHalt(){   
//        var windowObj = document.getElementById("teri");
//       windowObj.style.display ='none';	
$("#teri").hide();
}


function getPageScrollY(){
    if (isMinNS4 || dom) 
        return (window.pageYOffset);
    if (ie){
        return (document.documentElement.scrollTop || document.body.scrollTop || 0);
    } 
    return (0);
} 

function changeRow(e,styletouse){
    e.className = styletouse;
}


function moveLayerTo(layer,x,y){
    if (isMinNS4) {
        layer.moveTo(x,y);
    }
    if (ie || dom){
        layer.style.left = x + "px";
        layer.style.top = y + "px";
       }
} 

function moveLayerBy(layer,dx,dy){
    if (isMinNS4) layer.moveBy(dx,dy);
    if (ie || dom){
        layer.style.left = parseInt(layer.style.left) + dx;
        layer.style.top = parseInt(layer.style.top) + dy;
       }
   } 

function getPageTop(layer){
    if (isMinNS4) 
        return (layer.pageY);
    if (ie || dom) return (layer.offsetTop);
    return ( - 1);
   } 

function Left(str,n)
   {if (n <= 0) return "";
    else if (n > String(str).length) return str;
    else return String(str).substring(0,n);
   } 

function getWidth(layer){
    if (isMinNS4){
        if (layer.document.width) 
            return (layer.document.width);
        else 
            return (layer.clip.right - layer.clip.left);
       } 
    if (ie || dom){
        if (layer.style.width) 
            return (layer.style.width);
        else 
            return (layer.offsetWidth);
    } 
    return ( - 1);
} 

function setWidth(layer,x){
    if (ie || dom){
		layer.style.width = x + 'px';
   }} 

function setHeight(layer,y){
    if (ie || dom){
        layer.style.height = y + 'px';
    }
} 

function getHeight(layer){
    if (isMinNS4){
        if (layer.document.height) 
            return (layer.document.height);
        else 
            return (layer.clip.bottom - layer.clip.top);
       } 
    if (ie || dom){
       if (false && layer.style.height) 
           return (layer.style.height);
       else 
            return (layer.offsetHeight);
       } 
    return ( - 1);
} 

function clipLayer(layer,clipleft,cliptop,clipright,clipbottom){
    if (isMinNS4){
        layer.clip.left = clipleft;
        layer.clip.top = cliptop;
        layer.clip.right = clipright;
        layer.clip.bottom = clipbottom;
    } 
    if (ie || dom) 
        layer.style.clip = 'rect(' + cliptop + ' ' + clipright + ' ' + clipbottom + ' ' + clipleft + ')';
   } 

function getClipLeft(layer){
    if (isMinNS4) 
        return (layer.clip.left);
    if (ie || dom){
        var str = layer.style.clip;
        if ( ! str) 
            return (0);
        var clip = getIEClipValues(layer.style.clip);
        return (clip[3]);
    } 
    return ( - 1);
} 

function getClipTop(layer){
    if (isMinNS4) 
        return (layer.clip.top);
    if (ie || dom){
        var str = layer.style.clip;
        if ( ! str) return (0);
        var clip = getIEClipValues(layer.style.clip);
        return (clip[0]);
    } 
    return ( - 1);
} 

function getClipRight(layer){
    if (isMinNS4) 
        return (layer.clip.right);
    if (ie || dom){
        var str = layer.style.clip;
        if ( ! str) 
            return (layer.style.width);
        var clip = getIEClipValues(layer.style.clip);
        return (clip[1]);
    } 
    return ( - 1);
} 

function getClipBottom(layer)
   {if (isMinNS4) return (layer.clip.bottom);
    if (ie || dom)
       {var str = layer.style.clip;
        if ( ! str) return (layer.style.height);
        var clip = getIEClipValues(layer.style.clip);
        return (clip[2]);
       } 
    return ( - 1);
   } 

function getIEClipValues(str){
    var clip = new Array();
    var i;    
    i = str.indexOf("(");
    clip[0] = parseInt(str.substring(i + 1,str.length),10);
    i = str.indexOf(" ",i + 1);
    clip[1] = parseInt(str.substring(i + 1,str.length),10);
    i = str.indexOf(" ",i + 1);
    clip[2] = parseInt(str.substring(i + 1,str.length),10);
    i = str.indexOf(" ",i + 1);
    clip[3] = parseInt(str.substring(i + 1,str.length),10);
    return (clip);
} 

function findLayer(name,doc){
    var i, layer;
    for (i = 0;i < doc.layers.length;i++){
        layer = doc.layers[i];
        if (layer.name == name) 
            return layer;
        if (layer.document.layers.length > 0){
            layer = findLayer(name,layer.document);
            if (layer != null) 
                return layer;
        }
    } 
    return null;
} 

function getWindowWidth(){    
    return(window.innerWidth || document.body.clientWidth || document.documentElement.clientWidth || 0);
} 

function getWindowHeight(){    
    return(window.innerHeight || document.body.clientHeight || document.documentElement.clientHeight || 0);
} 

function parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) 
    return Params;
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) 
   {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) 
        continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

function URLEncode(plaintext){    
// The Javascript escape and unescape functions do not correspond
// with what browsers actually do...
    var SAFECHARS = "0123456789" +        // Numeric
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +    // Alphabetic
    "abcdefghijklmnopqrstuvwxyz" + "-_.!~*'()";    // RFC2396 Mark characters
    var HEX = "0123456789ABCDEF";
    var encoded = "";
    for (var i = 0;i < plaintext.length;i++)
       {var ch = plaintext.charAt(i);
        if (ch == " ")
           {encoded += "+";    // x-www-urlencoded, rather than %20
           } 
        else if (SAFECHARS.indexOf(ch) != - 1)
           {encoded += ch;
           } 
        else
           {var charCode = ch.charCodeAt(0);
            if (charCode > 255)
               {alert("Unicode Character '" + ch + "' cannot be encoded using standard URL encoding.\n" + 
                "(URL encoding only supports 8-bit characters.)\n" + "A space (+) will be substituted.");
                encoded += "+";
               } 
            else
               {encoded += "%";
                encoded += HEX.charAt((charCode >> 4) & 0xF);
                encoded += HEX.charAt(charCode & 0xF);
               }
           }
       } // for

    return encoded;
   } 

function URLDecode(encoded){    
// Replace + with ' '
// Replace %xx with equivalent character
// Put [ERROR] in output if %xx is invalid.
    var HEXCHARS = "0123456789ABCDEFabcdef";
    var plaintext = "";
    var i = 0;
    while (i < encoded.length){
        var ch = encoded.charAt(i);
        if (ch == "+")
           {plaintext += " ";
            i++;
           } 
        else if (ch == "%")
           {if (i < (encoded.length - 2) && HEXCHARS.indexOf(encoded.charAt(i + 1)) != - 1 && HEXCHARS.indexOf(encoded.charAt(i + 
            2)) != - 1)
               {plaintext += unescape(encoded.substr(i,3));
                i += 3;
               } 
            else
               {alert('Bad escape combination near ...' + encoded.substr(i));
                plaintext += "%[ERROR]";
                i++;
               }
           } 
        else{
            plaintext += ch;
            i++;
           }
       }
    return plaintext;
} 

function Pause(duration,busy){
    this.duration = duration * 1000;
    this.busywork = null;    // function to call while waiting.
    this.runner = 0;
    if (arguments.length == 2){
        this.busywork = busy;
    } 
    this.pause(this.duration);
} 

Pause.prototype.pause = function (duration){
    if ((duration == null) || (duration < 0)){
        return ;
    } 
    var later = (new Date()).getTime() + duration;
    while (true){
        if ((new Date()).getTime() > later){
            break ;
        } 
        this.runner++;
        if (this.busywork != null){
            this.busywork(this.runner);
       }
    }
}; 

function handleEnter(field,event){
    var keyCode = event.keyCode ? event.keyCode:event.which ? event.which:event.charCode;
    if (keyCode == 13){
        var i;
        for (i = 0;i < field.form.elements.length;i++) if (field == field.form.elements[i]) break ;
        i = (i + 1) % field.form.elements.length;
        if ((field.form.elements[i].disabled == true) || (field.form.elements[i].type == "hidden") || (field.form.elements[i].type == "textarea")){ 
        } 
        else{
            field.form.elements[i].focus();
            return ;
        } 
        return false;
    } 
    else 
        return true;
} 

function addEnterhandlers(f){    
    for (var i = 0;i < f.elements.length;i++){
        var e = f.elements[i];
        if (e.type != "button"){
            e.onkeypress = new Function("return handleEnter(this, event)");
        }
    }
}  

function checkValidV2(textElement,buttonElement){ 
    if (textElement.value.length > 0){
        if (buttonElement.disabled == true){
            buttonElement.disabled = false;
        }
    } 
    else{
        if (buttonElement.disabled == false){
            buttonElement.disabled = true;
        }
    }
} 

function hex2num(hex) {
 if(hex.charAt(0) == "#") { 
  hex = hex.slice(1);
 }
 hex = hex.toUpperCase();
 var hex_alphabets = "0123456789ABCDEF";
 var value = new Array(3);
 var k = 0;
 var int1,int2;
 for(var i=0;i<6;i+=2) {
  int1 = hex_alphabets.indexOf(hex.charAt(i));
  int2 = hex_alphabets.indexOf(hex.charAt(i+1));
  value[k] = (int1 * 16) + int2;
  k++;
 }
 return(value);
}

function num2hex(triplet) {
 var hex_alphabets = "0123456789ABCDEF";
 var hex = "#";
 var int1,int2;
 for(var i=0;i<3;i++) {
  int1 = triplet[i] / 16;
  int2 = triplet[i] % 16;

  hex += hex_alphabets.charAt(int1) + hex_alphabets.charAt(int2);
 }
 return(hex);
}

function fadeColor(id,start_hex,stop_hex,difference,delay,color_background) {
 if(!difference) difference = 20;
 if(!delay) delay = 100;
 if(!start_hex) start_hex = "#FFFFFF";
 if(!stop_hex) stop_hex = "#000000";
 if(!color_background) color_background = "c";

 var ele = document.getElementById(id);
 if(!ele) return;
 var start= hex2num(start_hex);
 var stop = hex2num(stop_hex);
 
 //Make it numbers rather than strings.
 for(var i=0;i<3;i++) {
  start[i] = Number(start[i]);
  stop[i] = Number(stop[i]);
 }

 for(var i=0;i<3;i++) {
  if (start[i] < stop[i]) {
   start[i] += difference;
   if(start[i] > stop[i]) start[i] = stop[i];  }
  else if(start[i] > stop[i]) {
   start[i] -= difference;
   if(start[i] < stop[i]) start[i] = stop[i];
  }
 }

 var color = "rgb("+start[0]+","+start[1]+","+start[2]+")";
 if(color_background == "b") {
  ele.style.backgroundColor = color;
 } else {
  ele.style.color = color;
 }

 if((start[0] == stop[0]) && (start[1] == stop[1]) && (start[2] == stop[2])) return;
 start_hex = num2hex(start);
 //Keep calling this function
 window.setTimeout("fadeColor('"+id+"','"+start_hex+"','"+stop_hex+"',"+difference+","+delay+",'"+color_background+"')",delay);

}

var ie = document.all && navigator.userAgent.indexOf("Opera") == -1;
var ns6 = document.getElementById && !document.all;
var dragapproved = false; var z, x, y;
function lightup(imageobject, opacity) {
    try {
        if (ns6) {
            imageobject.style.MozOpacity = opacity / 100;
        }
        if (ie) {
            imageobject.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Style=0,Opacity=" + opacity + ")";
        }
    }
    catch (e) {
        try {
            imageobject.filters.item('alpha').opacity = opacity / 100;
        }
        catch (e) {
        }
    }
}

function move(e) {
    if (dragapproved) {
        z.style.left = (ns6 ? temp1 + e.clientX - x : temp1 + event.clientX - x) + "px";
        z.style.top = (ns6 ? temp2 + e.clientY - y : temp2 + event.clientY - y) + "px";
        return false;
    }
}

function dragEnd(e) {
    if (!ie && !ns6) return;
    var firedobj = ns6 ? e.target : event.srcElement;
    var topelement = ns6 ? "HTML" : "BODY";

    if (!firedobj.className)
        return;


    while (firedobj.tagName != topelement && firedobj.className.indexOf("popup_window") == -1) {
        firedobj = ns6 ? firedobj.parentNode : firedobj.parentElement;
        if (firedobj == null)
            return;
    }
    if (firedobj == null)
        return;
    if (firedobj.className.indexOf("popup_window") > -1) {
        dragapproved = false;
        z = firedobj;
        lightup(z, 100);
        return false;
    }
}

function drags(e) {
    if (!ie && !ns6)
        return;

    if (dragapproved) {
        dragEnd(e);
    }
    var firedobj = ns6 ? e.target : event.srcElement;
    var topelement = ns6 ? "HTML" : "BODY";

    if (!firedobj.className)
        return;

    if (firedobj.className.indexOf("titleBar") == -1)
        return;

    if (firedobj.nodeName == "BUTTON") return;
    if (firedobj.nodeName == "INPUT") return;
    if (firedobj.nodeName == "SELECT") return;
    if (firedobj.nodeName == "TEXTAREA") return;
    if (firedobj.id == "namelist") return;
    if (firedobj.id == "ignorethisitem") return;
    if (firedobj.id == "Expando") return;

    while (firedobj.tagName != topelement && firedobj.className.indexOf("popup_window") == -1) {
        firedobj = ns6 ? firedobj.parentNode : firedobj.parentElement;
        if (firedobj.id == "namelist") return;
        if (firedobj.id == "ignorethisitem") return;
        if (firedobj.className == "ignorethisitem") return;
        if (firedobj == null) return;
    }


    if (firedobj == null) return;
    if (firedobj.id == "namelist") return;
    if (firedobj.id == "ignorethisitem") return;
    if (firedobj.className == "ignorethisitem") return;
    if (firedobj.id == "Expando") return;

    if (firedobj.className.indexOf("popup_window") > -1) {
        dragapproved = true;
        z = firedobj;
        lightup(z, 70);
        temp1 = parseInt(z.style.left + 0);
        temp2 = parseInt(z.style.top + 0);
        x = ns6 ? e.clientX : event.clientX;
        y = ns6 ? e.clientY : event.clientY;
        document.onmousemove = move;
        return false;
    }
}

function showDialogWindow() {
    var mX, mY;
    var offsetY = document.body.scrollTop;
    windowObj = document.getElementById(windowName);
    mX = (getWindowWidth() - getWidth(document.getElementById("floaty"))) / 2;
    mY = (getWindowHeight() - getHeight(document.getElementById("floaty"))) / 2;
    moveLayerTo(document.getElementById("floaty"), mX, my + offsetY);
    $('#floaty').css('display', 'block');
    $('#floaty').css('visibility', 'visible');
}

function showFloatingDialog(windowName) {
    $('#' + windowName).show();
    //$('#' + windowName.id).css('visibility', 'visible');    
    var mX, mY;
    var windowObj;
    var offsetY = getPageScrollY();

    if (offsetY == undefined) {
        offsetY = 0;
    }

    windowObj = document.getElementById(windowName);
    mX = (getWindowWidth() - getWidth(windowObj)) / 2;
   // alert(mX);
    mY = ($(window).height() - getHeight(windowObj)) / 2;
    
    mY = mY + offsetY;
    //alert(mY);
    if (isMinIE7) {
        windowObj.style.position = "absolute";
    }
    
    moveLayerTo(windowObj, mX, mY);
    
    showLayer(windowObj);
    //alert("end fo the method");
}


function showFloatingDialogNew(windowName) {
    $('#' + windowName).show();
    //$('#' + windowName.id).css('visibility', 'visible');    
    var mX, mY;
    var windowObj;
    var offsetY = getPageScrollY();

    if (offsetY == undefined) {
        offsetY = 0;
    }

    windowObj = document.getElementById(windowName);
    mX = (getWindowWidth() - getWidth(windowObj)) / 2;
    //alert(mX);
    mY = ($(window).height() - getHeight(windowObj)) / 2;
    
    mY = mY + offsetY;
    //alert(mY);
    if (isMinIE7) {
        windowObj.style.position = "absolute";
    }
    
    moveLayerTo(windowObj, mX, mY);
    setWidth(windowObj,780);
    showLayer(windowObj);
   // alert("end fo the method");
}



function showFloatingDialogWithOffSet(windowName, xpos, ypos) {
    $('#' + windowName).show();
    var mX, mY;
    var windowObj;
    
     var offsetY = getPageScrollY();

    if (offsetY == undefined) {
        offsetY = 0;
    }

    windowObj = document.getElementById(windowName);
    mX = (getWindowWidth() - getWidth(windowObj)) / 2;
   // alert(mX);
    mY = ($(window).height() - getHeight(windowObj)) / 2;
    
    mY = mY + offsetY;
    
    windowObj = document.getElementById(windowName);
    if (isMinIE7) {
        windowObj.style.position = "absolute";
    }

    if (ypos < 0) {
        ypos = 0;
    }

    moveLayerTo(windowObj, mX+ xpos, mY);
    showLayer(windowObj);
}

function showFloatingDialogWithOffSet1(windowName, event) {
    //$('#' + windowName.id).css('display', 'block');
    $('#' + windowName).show();
  try
  {
  event.stopPropagation();
  }
  catch(er)
  {
 
  }
    var xpos = event.clientX;
    var ypos = event.clientY;
    xpos = xpos - getPageScrollX(); 
    ypos =  ypos + getPageScrollY();        
    var windowObj;
    windowObj = document.getElementById(windowName);
    var windowWidth=getWidth(windowObj);
    if (isMinIE7) {
        windowObj.style.position = "absolute";
    }

    if (ypos < 0) {
        ypos = 0;
    }    
    moveLayerTo(windowObj, parseInt(xpos)+15-windowWidth,parseInt(ypos)-15);
    showLayer(windowObj);
}

function hideDialogWindow() {
    //$('#floaty').css('visibility', 'hidden');
    $('#floaty').css('display', 'none');
    
}

function hideFloatingDialog(windowName) {
    var windowObj;
    windowObj = document.getElementById(windowName);
    moveLayerTo(windowObj, -2000, 0);
    $('#' + windowName).hide();
    //$('#' + windowName.id).css('visibility', 'hidden');    
    return false;
}

document.onmousedown = drags;
document.onmouseup = dragEnd;

