
//*** DIV POP-UP ***

function showPopup( text, W, H, X, Y )
{

	if (!W)
	{
		W = 0
	}

	if (!H)
	{
		H = 0
	}

	if (!X)
	{
		X = 0
	}

	if (!Y)
	{
		Y = 0
	}
	
	var div = parent.document.createElement("div")
	
	div.style.zIndex = "10";
	div.style.position = "absolute";
	div.style.top = Y + "px";
	div.style.left = X + "px"
	div.style.width = W + "px"
	div.style.height = H + "px"
	div.style.padding = "10px"
	div.style.fontFamily = "tahoma"
	div.style.background = "lightyellow"
	div.style.border = "solid 1px black"
	div.style.overflow = "auto"
	div.style.fontSize = "8pt"
	div.id = "dPopup";	
	div.onclick = function(){hidePopup()}
	//div.setAttribute("onclick", 'hidePopup()',false);
	
	var parentDiv = parent.document.getElementById("dPopup");
	if (parentDiv)
	{
		parentDiv.parentNode.removeChild(parentDiv)
	}
	
	div.innerHTML = text
	parent.document.body.appendChild(div)
}

function hidePopup()
{
	var div = parent.document.getElementById("dPopup");
	if (div)
	{
		div.parentNode.removeChild(div)
	}
}

function doClick() { 
	parent.parent.location = this.href;
	return false;
}
 
function getInfo(text) {
	getInfo2(text, 450, 120);
}

function getInfo2(text, w, h){
	showPopup(text, w, h, event.screenX, event.screenY);	
}

function ipopup(key, W, H)
{
	var iframe = document.getElementById("ipopup");
	if (iframe)
	{
		var selectedPosX = 0;
		var selectedPosY = 0;
		if (!event)
		{
			return
		}
		else if (event.x > 0)
		{
			selectedPosX = event.x
			selectedPosY = event.y
						
		}
		else
		{
			selectedPosX = event.screenX
			selectedPosY = event.screenY
		}
		iframe.src = "/CortexDotNet/common/ipopup.aspx?key=" + key + "&W=" + W + "&H=" + H + "&X=" + selectedPosX + "&Y=" + selectedPosY;
	}
}

function ipopup2(key, W, H, evt)
{
	var iframe = document.getElementById("ipopup");
	if (iframe)
	{
		var selectedPosX = 0;
		var selectedPosY = 0;
		var theElement = evt.srcElement
		if (!theElement)
		{
			theElement = evt.target
		}
		if (theElement != null)
		{
			selectedPosX = 32

			while (theElement != null)
			{
				selectedPosX += theElement.offsetLeft;
				selectedPosY += theElement.offsetTop;
				theElement = theElement.offsetParent;
			}
		}
		else
		{
			selectedPosX = evt.pageX;
			selectedPosY = evt.pageY;
		}
		iframe.src = "/CortexDotNet/common/ipopup.aspx?key=" + key + "&W=" + W + "&H=" + H + "&X=" + selectedPosX + "&Y=" + selectedPosY;
	}
	
}

function servicePopup(key, W, H) {
	var iframe = document.getElementById("servicePopup");
    		
	if (iframe) {
		iframe.src = "/CortexDotNet/common/ServicePopup.aspx?key=" + key + "&W=" + W + "&H=" + H + "&X=" + event.screenX + "&Y=" + event.screenY;
	}
}	

// *** EXPAND - COLLAPSE

function dg(Item){
    return document.getElementById(Item);
}

function none(){}

function showhideF(link,image){
    showhide(document.getElementById(link),document.getElementById(image));
}

function showhide2(what,itemID){
	showhide(what,null,itemID);
} 

function showhide(what,what2){
	showhide(what,what2,0);
} 

function showhide(what,what2,itemID){	
	showhide(what,what2,itemID,null)
}

function showhide(what,what2,itemID,img){  
    if (itemID == null) itemID = -1;
	if (what.style.display == 'none'){
		what.style.display = '';
		if (what2 != null){
			if (img == null) {		
			    if (what2.src.indexOf('m.gif') != "-1" || what2.src.indexOf('p.gif') != "-1"){
			        what2.src = "/CortexDotNet/pics/m.gif";
			    } else {
			        what2.src = "/CortexDotNet/pics/coll.jpg";
			    }
			} else {
				what2.src = img;
			}	
		}

		if (itemID != -1) { 
			SetiFrame(itemID, 60);
			SetiFrame(itemID, 5);
		}
	} else {
		what.style.display = 'none'
		if (what2 != null){
			if (img == null) {
			    if (what2.src.indexOf('m.gif') != "-1" || what2.src.indexOf('p.gif') != "-1"){
			        what2.src = "/CortexDotNet/pics/p.gif";
			    } else {
			        what2.src = "/CortexDotNet/pics/exp.jpg";
			    }
			} else {
				what2.src = img.replace(".gif", "1.gif");
			}			
		}
	
		if (itemID != -1) {
			SetiFrame(itemID, 5);
			SetiFrame(itemID, 5);
		}
	}
}
 
//*** CHECKED LIST BOX ***

 function CheckedListBox_Init() {
    if ( typeof( document.getElementById ) == "undefined" ) return;
    if (typeof(Page_CheckedListBoxes) == "undefined") return;
    var i, checkedListBox;
    for (i = 0; i < Page_CheckedListBoxes.length; i++) {
        checkedListBox = Page_CheckedListBoxes[i];
        if (typeof(checkedListBox) == "string") {
            checkedListBox = document.getElementById( checkedListBox );
            if ( checkedListBox != null ) {
                CheckedListBox_Load( checkedListBox );
            }
        }
    }
}
function CheckedListBox_Load( checkedListBox ) {
	CheckedListBox_EnsureProperties( checkedListBox );
	checkedListBox.resizeWidth = CheckedListBox_resizeWidth;
	if ( checkedListBox.style.width == "" ) {
		checkedListBox.resizeWidth();
	}
}
function CheckedListBox_EnsureProperties( control ) {
    if ( typeof(control.style.borderColor) == "undefined" || control.style.borderColor == "" ) {
        control.style.borderColor = "ButtonFace";
    }
}
function CheckedListBox_resizeWidth() {
    var container = document.getElementById( this.id + "_Container" );
    var ScrollbarPadding = 20;
    var ContainerWidth;
    if( typeof( document.defaultView ) != "undefined" ) { // The w3c standard
        ContainerWidth = document.defaultView.getComputedStyle( container, "" ).getPropertyValue("width");
    } else if ( typeof( document.all ) != "undefined" ) { // ie
        ContainerWidth = container.offsetWidth;
    }
    this.style.width = parseInt( ContainerWidth ) + ScrollbarPadding + "px";
}

//*** General Functions

function ShowProgress(btnText, imgText){
    document.getElementById(btnText).style.display = 'none';
    document.getElementById(imgText).style.display = '';
    return true;   
}

function ShowProgressValid(btnText, imgText){
    if (Page_IsValid){
        ShowProgress(btnText, imgText);
    }
}

function StopProgress(btnText, imgText){
    document.getElementById(btnText).style.display = '';
    document.getElementById(imgText).style.display = 'none';
    return true;   
}

function refresh(){
	window.location.href = window.location.href;
}

function refreshParent(){
	var loc = top.location + "";
	top.location = loc.replace("#ManageAppTextSpan", "").replace("New=True&", "").replace("?New=True", "");
}

function parentLink(url){
    top.location = url;
}

function LoadAlphabet(URL, viewAllText){
	var lblAlphabet = document.getElementById("lblAlphabet");
	 
	if (lblAlphabet != null){
		var alph = "|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z";
		var letters = alph.split("|");
		alph = "";

		for (var i = 0; i < letters.length; i++){
			alph = alph + '<a href="javascript:AplhabetLink(\'' + URL + letters[i] + '\');">' + letters[i] + "</a> ";
		}
		
		alph = alph + '&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript:AplhabetLink(\'' + URL + 'all' + '\');">' + viewAllText.replace(" ","&nbsp;") + '</a>';
		lblAlphabet.innerHTML = alph;
	}
}

function AplhabetLink(URL){
	var ddlSearch = document.getElementById(pagePrefix + "ddlSearch");
	var ddlCustomer = document.getElementById(pagePrefix + "ddlCustomer");
	var searchOn = "&search="

	if (ddlSearch != null) searchOn = searchOn + ddlSearch.options[ddlSearch.selectedIndex].value;
	if (ddlCustomer != null) searchOn = searchOn + "&customer=" + ddlCustomer.options[ddlCustomer.selectedIndex].value;
	if (searchOn == "&search=") searchOn = "";
		
	window.location.href = URL + searchOn;	
}

//*** CHECKED LIST BOX - SELECT DEFAULTS OPTION FUNCTIONS ***
	function doDDL(obj){
		var ddl = getDefaultDDL();
		var lbl = getLabel(obj.id);
		var lblInfo = document.getElementById("selectLabel");

		for (var i = 0; i < ddl.options.length; i++){
			if (ddl.options[i].value == lbl){
				ddl.options[i] = null;
				break;
			}
		}

		if (obj.checked){
			ddl.options[ddl.options.length] = new Option(lbl,lbl);
		}

		if (ddl.options.length == 0){
			ddl.style.display = 'none';
			lblInfo.innerHTML = '';
			lblError.innerHTML = ErrorText;
		} else {
			ddl.style.display = '';
			if (lblError.innerHTML == ErrorText){
				lblError.innerHTML = "";
			}
			
			lblInfo.innerHTML = OKText;
		}
	}

	function getLabel(objID){
	    var elements = getAllElements("LABEL");
		for (var i = 0; i < elements.length; i++){
				if (elements[i].parentElement.childNodes[0].id == objID){
					return elements[i].parentElement.childNodes[1].innerHTML;
				}
		}
	}

	function getDefaultDDL(){
		for (var i = 0; i < document.forms[0].length; i++){
			if (document.forms[0].elements[i].nodeName == "SELECT" && document.forms[0].elements[i].id.search("ddlSelect") != -1){
				return document.forms[0].elements[i];
			}
		}
	}

	function checkAll(chkAll, objName){
	    var elements = getAllElements("INPUT");
		for (var i = 0; i < elements.length; i++){
			if (elements[i].id.indexOf(objName) != "-1" && elements[i].id.indexOf(objName + 'Container') == "-1"){
				if (!elements[i].disabled) elements[i].checked = chkAll.checked;	
			}
		}		
	}	 

// *** General Functions

function showDiv(item){
	var dtag = document.getElementById(item);
	var info = document.getElementById("info");

	if (dtag.style.visibility == "hidden"){
		dtag.style.visibility = "";
		info.style.visibility = "hidden";
	} else {
		dtag.style.visibility = "hidden";
		info.style.visibility = "";
	}
}

function showTag(item){
	var dtag = document.getElementById(item);
	var info = document.getElementById("info").style.display = dtag.style.display;
	if (dtag.style.display == "none"){
		dtag.style.display = "";
	} else {
		dtag.style.display = "none";
	}
}

function round(float,dec){
	return Math.round(float*Math.pow(10,dec))/Math.pow(10,dec)
}

// *** Create and Destroy Iframe Code 
// (call these function or else the back button will break) 

function createIFrame(TDframe, URL)
{
	removeIFrame(TDframe);
	var img = document.createElement("img");
	img.src = "/CortexDotNet/pics/loadingmedium.gif";
	TDframe.appendChild(img);
		
	var iFrame = document.createElement("iframe");
	iFrame.src = URL;
	iFrame.style.width = "100%";
	iFrame.style.height = "35px";
	TDframe.appendChild(iFrame);
}

function StartRefresh(vars, itemID)
{
	var iFrame = document.getElementById("sr" + itemID);
	if (!iFrame) iFrame = document.createElement("iframe");
	iFrame.src = "../../../../../CortexDotNet/Common/RefreshStatus.aspx" + vars;
	iFrame.style.width = "0px";
	iFrame.style.height = "0px";
	iFrame.style.display = "none";
	iFrame.id = "sr" + itemID;
	document.forms[0].appendChild(iFrame); 
}

function StartBulkRefresh(vars){
	StartiFrameRefresh("../../../../../CortexDotNet/Common/RefreshStatus.aspx" + vars, "brefresh")
}

function StartiFrameRefresh(url, tagID){
	var iFrame = document.getElementById(tagID);
	if (!iFrame) iFrame = document.createElement("iframe");
	iFrame.src = url;
	iFrame.style.width = "0px";
	iFrame.style.height = "0px";
	iFrame.style.display = "none";
	iFrame.id = tagID;
    document.forms[0].appendChild(iFrame); 
}

function StopRefresh(itemID){
	var ObjID = "sr" + itemID
	for (var i = 0; i < document.forms[0].childNodes.length -1; i++){
		if (document.forms[0].childNodes[i].id == ObjID){
			document.forms[0].removeChild(i);
			break;
		}
	}
}

function StopBulkRefresh(){
    StopiFrameRefresh("brefresh");
}

function StopiFrameRefresh(tagID){
    var ref = document.getElementById(tagID);    
    if (ref) document.body.removeChild(ref); 
}

function removeIFrame(TDframe)
{
	for (var i = TDframe.childNodes.length -1; i > -1; i--){
		TDframe.removeChild(TDframe.childNodes[i]);
	}
}

function removeHourGlass(TDframe){
	if (TDframe){
		for (var i = TDframe.childNodes.length -1; i > -1; i--){
			if (TDframe.childNodes[i].nodeName == "IMG"){
				TDframe.removeChild(TDframe.childNodes[i]);
				i = -1;
			}
		}	
	}
}

function iFrameHeight(TDframe, val)
{
//	for (var i = TDframe.getElementsByTagName("iframe").length - 1; i > -1; i--)
//	{
//		TDframe.getElementsByTagName("iframe")[i].style.height = val;
//		i = -1;
	//	}
	for (var i = TDframe.childNodes.length -1; i > -1; i--){
		if (TDframe.childNodes[i].nodeName == "IFRAME"){
			TDframe.childNodes[i].style.height = val + "px";
			i = -1;
		}
	}	
} 

// -- Search for entries in a drop-down-list
function ddlSort(dropdownlist,AutoPostBack){
	var undefined; 
	if (dropdownlist.keypressBuffer == undefined) { 
		dropdownlist.keypressBuffer = ''; 
	} 
	// user pressed Enter, submit
	if (window.event.keyCode == 13 && AutoPostBack) __doPostBack(dropdownlist.id,'');
	
	var key = String.fromCharCode(window.event.keyCode); 
	dropdownlist.keypressBuffer += key;
	dropdownlist.keypressBuffer = dropdownlist.keypressBuffer.toLowerCase();

	// find if it is the start of any of the options 
	var optionsLength = dropdownlist.options.length; 
	for (var n=0; n < optionsLength; n++) { 
		var optionText = dropdownlist.options[n].text; 
		optionText = optionText.toLowerCase();
		if (optionText.indexOf(dropdownlist.keypressBuffer,0) == 0) { 
			dropdownlist.selectedIndex = n; 
			return false; // cancel the default behavior since we have selected our own value 
		} 
	} 
	// reset initial key to be inline with default behavior 
	dropdownlist.keypressBuffer = key; 
	return true;
} 

function ddlCheck(ddl){
	if (ddlSelected > -1 && ddlSelected != ddl.selectedIndex){
		__doPostBack(ddl.id,'');
	}
}

function ddlInit(ObjID){
	if (document.getElementById(ObjID)){
		ddlSelected = document.getElementById(ObjID).selectedIndex;
	}	
}

function CustSearch(){
    var SearchInfo = document.getElementById("uc_CustSearch_trSearchInfo");
    var CustSearch = document.getElementById("uc_CustSearch_tdCustSearch");
    var CustName = document.getElementById("uc_CustSearch_tdCustName");
    var txtSearch = document.getElementById("uc_CustSearch_txtCustomer");
    
    if (SearchInfo.style.display == 'none'){
        SearchInfo.style.display = '';
        CustName.style.display = 'none';
        CustSearch.style.display = '';
        if (txtSearch) txtSearch.focus();
    } else {
        SearchInfo.style.display = 'none';
        CustName.style.display = '';
        CustSearch.style.display = 'none';            
    }
}

function LoadBulkUserData(key){
    createIFrame(document.getElementById("ifLoadData"), "LoadUserImportData.aspx?key=" + key);  
    document.getElementById("div" + key).className = "importloading";
}

function ToggleCustApp(){
    lblCust = document.getElementById("lblCustomer");
    txtCust = document.getElementById("txtCustomer");
    if (lblCust.style.display == ''){
        lblCust.style.display = 'none';
        txtCust.style.display = '';   
    } else {
        lblCust.style.display = '';
        txtCust.style.display = 'none';   
    }
}

function ToggleAccExp(){      
    if (document.getElementById("Uc_UserPrivileges1_rbAccExpire_0").checked){
        document.getElementById("AccExpDate").style.display = "none";
    } else {
        document.getElementById("AccExpDate").style.display = "";
    }
}

function ToggleBar(imgID, itemID){
    var obj = document.getElementById(itemID);
    var img = document.getElementById(imgID);
    if (obj && img){
        if (obj.style.display == ""){
            obj.style.display = "none";
            img.src = "/CortexDotNet/pics/exp1.gif";
            return 0;
        } else {
            obj.style.display = "";
            img.src = "/CortexDotNet/pics/coll1.gif";
            return 1;
        }
    }
}

function GenCustName(CustLongName){
    var txtShortName = document.getElementById("txtShortName");

    if (txtShortName.value == '' && CustLongName.replace(' ', '').length > 0){
        UpdateProgress("", "imgGenerate", true);   
        CustomerEditiFrame.ShortNameGenerate(CustLongName,callback_GenCustName);
        txtShortName.disabled = true;
        return false;
    }
}

function callback_GenCustName(res) {
    var txtShortName = document.getElementById("txtShortName");
    if (res.value.length < 15) {
        txtShortName.value = res.value;
        document.getElementById("txtContactName").focus();
        SetCustShortName(res.value);
    }
    txtShortName.disabled = false;
	UpdateProgress("", "imgGenerate", false);
}

function PropPageValidate(){
    if (Page_IsValid){
        for (var i = 0; i < document.body.all.length; i++){
            if (document.body.all[i].id.search("rptData_") == 0 && document.body.all[i].id.search("_pnl") > -1){
                document.body.all[i].disabled = false;
            }
        }
    }
    return Page_IsValid;
}		

function ADSearchFormat(oDiv, Count){
    if (oDiv){
	    if (Count == 0){
		    oDiv.style.display = "none";
	    } else {
		    oDiv.style.display = "";
		    if (Count > 6) oDiv.style.height = "187px";
	    }
    }
}

function getAllElements(name){
    if(document.getElementsByTagName) {
        return document.body.getElementsByTagName(name);
    } else if (document.body.all) {
        return document.body.all.tags(name);
    } else {
        return Array();
    }			
}		

function getObjElements(obj, name){
    if(document.getElementsByTagName) {
        return obj.getElementsByTagName(name);
    } else if (document.body.all) {
        return obj.all.tags(name);
    } else {
        return Array();
    }			
}		

function DisableCheckboxList(name){
    var tbl = document.getElementById(name + '_Container');
    if (tbl) {
        var elements = getObjElements(tbl, "TD");
        for (var i = 0; i < elements.length; i++) {
            if (elements[i].childNodes[0].getAttribute("disabled")) {
                elements[i].childNodes[0].childNodes[0].disabled = true;
            }
        }
    }
}

function confirmTextDelete(msg) {
    var userinput = prompt(msg, "");
    if (userinput == null) return false;
    return userinput.toLowerCase() == "delete";
}

//*** Edit Customer
function SetCustShortName(val){
    var label = document.getElementById("lblcaUIDSuffix");
    if (label) label.innerHTML = "_" + val;
}

function SetCustUPNDomain(val){
    var label = document.getElementById("lblCADomain");
    if (label) label.innerHTML = "@" + val;
}

function ManageContent(){
    var tdContent = document.getElementById("ctl00_tdManageContent");
    var frame = document.getElementById("frPageText");
    if (frame.style.display == 'none'){
        frame.style.display = '';
        frame.src = "/CortexDotNet/Administration/Language/PageTextHolder.aspx?Hide=false";
    } else {
        frame.style.display = 'none';
        frame.src = "/CortexDotNet/Administration/Language/PageTextHolder.aspx?Hide=true";
    }
    tdContent.style.display = frame.style.display;
}


//*** Shop
function UISet(ElementID)
{
    objImg = document.getElementById("imgBar" + ElementID);
    objBar = document.getElementById("Bar" + ElementID);
    objBarState = document.getElementById("BarState" + ElementID);

    if (objBar)
    {
        if (objBar.style.display == "")
        {
            objBar.style.display = "none";
            if (objImg)
            {
                objImg.src = "/CortexDotNet/pics/Shop/exp1.gif";
            }
            if (objBarState)
            {
                objBarState.value = "False";
            }
        }
        else
        {
            objBar.style.display = "";
            if (objImg)
            {
                objImg.src = "/CortexDotNet/pics/Shop/coll1.gif";
            }
            if (objBarState)
            {
                objBarState.value = "True";
            }
        }
    }
}

function MaximizeOrMinimizeElement(name)
{
    var element = document.getElementById(name);
    if (!element)
    {
        return;
    }
    if (element.style.display == 'none')
    {
        element.style.display = '';
    }
    else
    {
        element.style.display = 'none';
    }
}

function GeneratePassword() {
    var chars = "^[_]!#$%&()<=>{}|?@*+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~abcdefghijklmnopqrstuvwxyz";
    var pass = ""
    for (var i = 0; i < 14; i++) {
        rand = Math.random() * chars.length;
        genn = Math.round(rand);
        while (genn <= 0) genn++;
        pass += chars.charAt(genn);
    }
    return pass;
}