﻿var showDialogWindow = null;
var mask = new Mask("maskingContainer", 1);

function showDialog1(url)
{
    showDialog2(url, 600, 500);
}

function showDialog2(url, width, height)
{
   showDialog3(url, width, height, 1);
}

function showDialog3(url, width, height, scroll)
{
    var xleft = (window.screen.width/2)-(width/2);
	var xtop = (window.screen.height/2)-(height/2);
	if (showDialogWindow) showDialogWindow.close();
	showDialogWindow = window.open(url, 'dialogWindow','left='+xleft+',top='+xtop+',width='+width+',height='+height+',menubar=1,status=1,scrollbars='+scroll);
	showDialogWindow.focus();
}

function formatNumber2(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function formatNumber(number, X)
{
    if (X ==0)
    {
        return formatNumber2(number);
    }
    
    X = (!X ? 2 : X);
    
    return formatNumber2(number.toFixed(X));
}

function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + dblValue + '.' + strCents);
}

function encodeHex(str)
{
    var result = "";
    for (var i=0; i<str.length; i++)
    {
        result += pad(toHex(str.charCodeAt(i)&0xff),2,'0');
    }
    return result;
}

var digitArray = new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
    
function toHex(n)
{
    var result = '';
    var start = true;
    
    for (var i=32; i>0;)
    {
        i-=4;
        var digit = (n>>i) & 0xf;
        if (!start || digit != 0)
        {
            start = false;
            result += digitArray[digit];
        }
    }
    return (result == '' ? '0' : result);
}

function pad(str, len, pad)
{
    var result = str;
    for (var i=str.length; i<len; i++)
    {
        result = pad + result;
    }
    return result;
}

function decodeHex(str)
{
    str = str.replace(new RegExp("s/[^0-9a-zA-Z]//g"));
    var result = "";
    var nextchar = "";
    for (var i=0; i<str.length; i++)
    {
        nextchar += str.charAt(i);
        if (nextchar.length == 2)
        {
            result += ntos(eval('0x'+nextchar));
            nextchar = "";
        }
    }
    return result;    
}

function ntos(n)
{
    n=n.toString(16);
    if (n.length == 1) n="0"+n;
    n="%"+n;
    return unescape(n);
}







//**************** fade in/out effects *************///

var timeoutFadeIns = new Array();
var timeoutFadeOuts = new Array();

function setOpacity(id, opacity)
{
    var obj = document.getElementById(id);
    setElementOpacity(obj, opacity);
}

function setElementOpacity(obj, opacity)
{
    obj.style.display = '';
    
    // IE filter opacity:
    obj.style.filter = "alpha(opacity:" + opacity + ")";
    
    // Safari and Konqueror:
    obj.style.KHTMLOpacity = opacity / 100;
    
    // Old Mozilla and Firefox:
    obj.style.MozOpacity = opacity / 100;
    
    // CSS3 opacity for browsers that support it:
    obj.style.opacity = opacity / 100;
    
    obj.opacity = opacity;        
    
 }
 
 function fadeOut_Start(id)
 {
    if (timeoutFadeIns[id])
    {
        clearTimeout(timeoutFadeIns[id]);
    }
    
    fadeOut(id); 
 }
 
 function fadeIn_Start(id)
 {               
    if (timeoutFadeOuts[id])
    {
        clearTimeout(timeoutFadeOuts[id]);
    }
      
    fadeIn(id); 
 }
      
 function fadeIn(id)
 {     
    var obj = document.getElementById(id);
    var opacity = obj.opacity;
    if (opacity < 100)
    {
        opacity += 10;
        setElementOpacity(obj, opacity);
        timeoutFadeIns[id] = setTimeout("fadeIn('" + id + "')", 1);
    }
 }
 
 function fadeOut(id)
 {
    var obj = document.getElementById(id);        
    var opacity = obj.opacity;
    if (opacity > 0)
    {
       opacity -= 10;
       setElementOpacity(obj, opacity);
       timeoutFadeOuts[id] = setTimeout("fadeOut('" + id + "')", 1);
    }
 }    
 
 
 
 
 
 
                   /*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/

var offsetfrommouse=[15,15]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration=0; //duration in seconds image should remain visible. 0 for always.

if (document.getElementById || document.all)
{
	document.write('<div id="trailimageid" style="position:absolute;width:300px"></div>');
}

function gettrailobj()
{
    if (document.getElementById)
        return document.getElementById("trailimageid").style;
    else if (document.all)
        return document.all.trailimagid.style;
}

function gettrailobjnostyle()
{
    if (document.getElementById)
        return document.getElementById("trailimageid");
    else if (document.all)
        return document.all.trailimagid;
}


function truebody()
{
    return (!window.opera && document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body;
}

function showtrail(title,description)
{
    document.onmousemove = followmouse;

	newHTML = '<div style="padding:10px; background-color: #FFF; border: 1px solid #888;">';
	newHTML = newHTML + '<h4 class="color" style="margin:0px;font-size:14px;">' + title + '</h4>';
	newHTML = newHTML + '<p style="margin:0px;font-size:11px;">' + description + '</p>';
	newHTML = newHTML + '</div>';
	
	gettrailobjnostyle().innerHTML = newHTML;		
	followmouse();
	gettrailobj().display = "inline";	
	
}


function hidetrail()
{
	gettrailobj().innerHTML = " ";
	gettrailobj().display="none";
	document.onmousemove="";
	gettrailobj().left="-500px";
}

function followmouse2(e){
var xcoord=offsetfrommouse[0]
var ycoord=offsetfrommouse[1]
if (typeof e != "undefined"){
xcoord+=e.pageX
ycoord+=e.pageY
}
else if (typeof window.event !="undefined"){
xcoord+=truebody().scrollLeft+event.clientX
ycoord+=truebody().scrollTop+event.clientY
}
var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)
if (xcoord+trailimage[1]+3>docwidth || ycoord+trailimage[2]> docheight)
gettrailobj().display="none"
else 
gettrailobj().display=""
gettrailobj().left=xcoord+"px"
gettrailobj().top=ycoord+"px"
}

function followmouse(e)
{
	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]

	var docwidth = document.all ? truebody().scrollLeft+truebody().clientWidth : pageXOffset + window.innerWidth-15;
	var docheight = document.all ? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight);
               
    
    if (typeof e != "undefined")
    {
        xcoord += e.pageX;
        ycoord += e.pageY;
    }
    else if (typeof event != "undefined")
	{
		if (docwidth - event.clientX < 380)
		{
			xcoord = event.clientX + truebody().scrollLeft - xcoord - 300; // Move to the left side of the cursor
		} 
		else
		{
			xcoord += truebody().scrollLeft + event.clientX
		}
		
		ycoord += truebody().scrollTop + event.clientY;		
	}      

	if (ycoord < 0)
	{
	    ycoord = ycoord*-1; 
    }
    
	gettrailobj().left = xcoord + "px";
	gettrailobj().top = ycoord + "px";

}

function ListItem_SelectOptionByValue(element, value)
{
    element = $(element);
    
    for (var i=0; i<element.options.length; i++)
    {
        if (element.options[i].value == value)
        {
            element.selectedIndex = i;
            return;
        }
    }
}

function ListItem_SelectOptionByText(element, value)
{
    element = $(element);
    
    for (var i=0; i<element.options.length; i++)
    {
        if (element.options[i].text == value)
        {
            element.selectedIndex = i;
            return;
        }
    }
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = { pageWidth: pageWidth, pageHeight: pageHeight, winWidth: windowWidth, winHeight: windowHeight }; 
	
	return arrayPageSize;
}

// -----------------------------------------------------------------------------------

window.dimensions = getPageSize;

//------------------------------------------------------------------------------------
var winDebugger = null;

function trace(msg)
{    
    if (winDebugger == null)
    {  
        winDebugger = window.open('/debug.htm', 'winDebugger', 'width=400px,height=300px');
    }
    
    if (winDebugger.debug) winDebugger.debug(msg);
    else
    { 
        winDebugger.onload = function () { winDebugger.debug(msg); }
    }    
}


function setButtonEnabledState(id, enabled)
{
    var element = $(id);
    
    if (!enabled)
    {
        element.setStyle({color:'#CCCCCC'});        
        element.disable();
    }    
    else
    {
        element.setStyle({color:'#666666'});        
        element.enable();
    }
    
}

// -----------------------------------------------------------------------------------
function _ContentResize(LeftColumnName)
{
    var myHeight = 0;
    var header = $('header');            
    var contentBody = $("contentBody");
    var content = $("content");
    var contentHeader = $("contentHeader");
    var folders = $(LeftColumnName);
    
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myHeight = window.innerHeight;
    }
    else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
    }
    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
    }
    
    var specHeight = (myHeight - header.getHeight() - content.offsetTop);
    
    if(specHeight > 0)
        contentBody.style.height = specHeight + 'px';

    var foldersHeight = (content.offsetHeight - contentHeader.offsetHeight - 3);
    
    if (foldersHeight > 0)
        folders.style.height = foldersHeight + "px";
}

function validEmail(emailAddress) 
{
    var regEx = /^\w+([-+\.]\w+)*@\w+([-\.]\w+)*\.\w+([-\.]\w+)*$/;
    
    return regEx.test(emailAddress);
}

function getSelectedOption(dropdownlist)
{
    dropdownlist = $(dropdownlist);
    if (dropdownlist.selectedIndex == -1) return null;
    
    return dropdownlist.options[dropdownlist.selectedIndex];
}

function getSelectedOptionValue(dropdownlist)
{
    var option = getSelectedOption(dropdownlist);
    
    if (option) return option.value;
    
    return "";
}

function getSelectedOptionText(dropdownlist)
{
    var option = getSelectedOption(dropdownlist);
    
    if (option) return option.text;
    
    return "";
}

function findOptionByValue(dropdownlist, value)
{
    dropdownlist = $(dropdownlist);
    for( var i=0; i<dropdownlist.options.length; i++)
    {
        if (dropdownlist.options[i].value == value)
        {
            return i;
            break;
        }
    }
    
    return -1;
}

function selectOptionByValue(dropdownlist, value)
{
    dropdownlist = $(dropdownlist);
    
    for( var i=0; i<dropdownlist.options.length; i++)
    {
        if (dropdownlist.options[i].value == value)
        {
            dropdownlist.selectedIndex = i;
            break;
        }
    }
    
    return dropdownlist.selectedIndex;
}

function selectOptionByText(dropdownlist, value)
{
    dropdownlist = $(dropdownlist);
    for( var i=0; i<dropdownlist.options.length; i++)
    {
        if (dropdownlist.options[i].text.toUpperCase() == value.toUpperCase())
        {
            dropdownlist.selectedIndex = i;
            break;
        }
    }
    
    return dropdownlist.selectedIndex;
}

function clearOptions(dropdownlist)
{
    dropdownlist = $(dropdownlist);
    
    while (dropdownlist.options.length > 0)
    {
        dropdownlist.remove(0);
    }
}

function addOption(dropdownlist, text, value)
{
	dropdownlist = $(dropdownlist);
	
    var index = dropdownlist.length;
    dropdownlist.options[index] = new Option(text, value);
    
    return dropdownlist.options[index];
}



function $RF(el, radioGroup)
{
	if ($(el).type && $(el).type.toLowerCase() == 'radio')
	{
		var radioGroup = $(el).name;
		var el = $(el).form;
	} else if ($(el).tagName.toLowerCase() != 'form')
	{
		return false;
	}

	var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) { return re.checked; }
    );
	return (checked) ? $F(checked) : null;
}
