// WinSaaS (http://www.winsaas.com) and HotelASP (http://www.hotelasp.com). Licensing apply to this source code. Please read and comply with source license available at site.
// Copyright (C) 2009 by Miguel Angelo Canaria Ribeiro (miguel.ribeiro@netcabo.pt). All rights reserved.

function ConfirmAction(msg)
{
  if (confirm(msg)==true)
    return true;
  else
    return false;
}

function UpdateLocalization(targetServerURL, applicationGuid, moduleID, workAreaID, key, culture, textControlID)
{
    var controlID = document.getElementById(textControlID);
    var value = controlID.value;
    
    var callBackData = textControlID;
	var callBack =
	{
		success: UpdateLocalizationResponse,
		failure: UpdateLocalizationResponseException,
		argument: callBackData,
		scope: this
	}
	
    var formData = "applicationGuid=" + applicationGuid + "&moduleID=" + moduleID + "&workAreaID=" + workAreaID + "&key=" + key + "&culture=" + culture;
    formData = formData + "&value=" + value;
    var callBackObject = YAHOO.util.Connect.asyncRequest('POST', targetServerURL, callBack, formData);
}

UpdateLocalizationResponse = function (response)
{
	var callBackData = response.argument.split(";");
	var textControlID = callBackData[0];	
	var controlID = document.getElementById(textControlID);
    controlID.style.backgroundColor = "white";
}

UpdateLocalizationResponseException = function (response)
{
	alert(response.responseText);	
}

function OpenCase(basePath, data, submitForm, sourceControlID)
{
    basePath = basePath + "&ModuleName=SYS_ACC&WorkAreaName=MANAGE_ACCOUNT_HELPDESK_TICKETS&ViewName=MANAGE_ACCOUNT_HELPDESK_TICKETS&RegionID=Form1";
    basePath = basePath + "&__CASES_FORM1_FORM1_COMMENTS=" + data;

    if (!submitForm)
    {        
        var popUp = OpenWindow(basePath, this, 'dialogHeight: 800; dialogWidth: 740; edge: Raised; center: Yes; help: No; resizable: Yes; status: No; scrollbars: No;');
        popUp.focus();	
    }
    else
    {	
        var caseForm = document.forms["caseForm"]
	    if ((caseForm!=null) && (sourceControlID!=""))
	    {
		    var elem = document.getElementsByName('$$_CASES_FORM1_FORM1_BODY')[0];
		    if (elem!=null)
		    {
			    var sourceControl = document.getElementById(sourceControlID);
			    if (sourceControl!=null)
			    {
				    elem.value = sourceControl.innerHTML;
			    }
		    }
		    caseForm.action = basePath;
		    caseForm.submit();
	    }
	    else
	    {
	        alert('No case form found. cannot load case.');
	    }    
	}
}

OpenWindow_dFeatures = 'dialogHeight: 500; dialogWidth: 700; edge: Raised; center: Yes; help: No; resizable: Yes; status: Yes; scrollbars: Yes;';
OpenWindow_modalWin = "";
function OpenWindow(sURL, vArguments, sFeatures)
{	
	if (sURL==null||sURL=='')
	{
		alert ("Invalid URL input.");
		return false;
	}
	if (vArguments==null||vArguments=='')
	{
		vArguments='';
	}
	if (sFeatures==null||sFeatures=='')
	{
		sFeatures=OpenWindow_dFeatures;
	}
	sFeatures = sFeatures.replace(/ /gi,'');
	aFeatures = sFeatures.split(";");
	sWinFeat = "directories=0,menubar=0,titlebar=0,toolbar=0,";
	for ( x in aFeatures )
	{
		aTmp = aFeatures[x].split(":");
		sKey = aTmp[0].toLowerCase();
		sVal = aTmp[1];
		switch (sKey)
		{
			case "dialogheight":
				sWinFeat += "height="+sVal+",";
				pHeight = sVal;
				break;
			case "dialogwidth":
				sWinFeat += "width="+sVal+",";
				pWidth = sVal;
				break;
			case "dialogtop":
				if (window.navigator.appVersion.indexOf("MSIE")!=-1)
				{
				    sWinFeat += "top="+sVal+",";									}
				else
				{
				    sWinFeat += "screenY="+sVal+",";					
				}
				break;
			case "dialogleft":
				if (window.navigator.appVersion.indexOf("MSIE")!=-1)
				{
				    sWinFeat += "left="+sVal+",";					
				}
				else
				{
				    sWinFeat += "screenX="+sVal+",";					
				}
				break;
			case "resizable":
				sWinFeat += "resizable="+sVal+",";
				break;
			case "status":
				sWinFeat += "status="+sVal+",";
				break;
			case "scrollbars":
				sWinFeat += "scrollbars="+sVal+",";
				break;
			case "center":
				if (sVal.toLowerCase() == "yes")
				{
					if (window.navigator.appVersion.indexOf("MSIE")!=-1)
					{
						sWinFeat += "top="+((screen.availHeight-pHeight)/2)+",";
						sWinFeat += "left="+((screen.availWidth-pWidth)/2)+",";
						
					}
					else
					{
						sWinFeat += "screenY="+((screen.availHeight-pHeight)/2)+",";
						sWinFeat += "screenX="+((screen.availWidth-pWidth)/2)+",";
					}
				}
				break;
		}
	}
	OpenWindow_modalWin=window.open(String(sURL),"",sWinFeat);
	if (vArguments!=null&&vArguments!='')
	{
		OpenWindow_modalWin.dialogArguments=vArguments;
	}
	
	return OpenWindow_modalWin;
}

function GetArrayIndex(array, key)
{
	var length = array.length;
	for(var i=0; i<length;i++)
	{
		if(array[i]==key)
		{
			return i;
		}
	}
	
	return -1;
}

// ************************************************************************************************************
//												START ** GRID **
// ************************************************************************************************************

function gridCheckAll(me)
{
    var index = me.name.indexOf('_');  
    var prefix = me.name.substr(0,index); 
    for(var i=0; i<document.forms[0].length; i++) 
    { 
        var o = document.forms[0][i]; 
        if (o.type == 'checkbox') 
        { 
            if (me.name != o.name) 
            {
                if (o.name.substring(0, prefix.length) == prefix) 
                {
                    // Must be this way
                    o.checked = !me.checked; 
                    o.click(); 
                }
            }
        } 
    } 
}

function ctxMenuShow(menuId, rowID, evt)
{
	evt = (evt) ? evt : ((window.event) ? window.event : null);
	
    var menuOffset = 2
	var menu = document.getElementById(menuId);
	if (menu == null) return;
	if (document.all)
	{
	    // ie
        menu.style.left = evt.clientX - menuOffset;
        menu.style.top = evt.clientY - menuOffset;
    }
    else
    {
        // ff
        menu.style.left = evt.clientX - menuOffset;
        menu.style.top = evt.clientY - menuOffset;
    }
    
    menu.style.display = 'block';
    menu.style.visibility = 'visible';
 
    var _rowID = document.getElementById('__ROWID');
    if (_rowID != null)
       _rowID.value = rowID;
	
    evt.cancelBubble = true;
    
    return false;
}

function ctxMenuTrapESC(menuId, evt)
{
    evt = (evt) ? evt : ((window.event) ? window.event : null);
    var target = (evt.target) ? evt.target : evt.srcElement;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
	
	//var key = window.event.keyCode;
	if (charCode == 27)
	{
	    var obj = document.getElementById(menuId);
		obj.style.display = 'none';
		obj.style.visibility = 'hidden';
	}
}

function ctxMenuHide(menuId, evt)
{
    evt = (evt) ? evt : ((window.event) ? window.event : null);
    var obj = document.getElementById(menuId);
    if (obj!=null)
    {
        obj.style.display = 'none';
        obj.style.visibility = 'hidden';
    }
}


function gridApplyStyle(me, selectedForeColor, selectedBackColor, foreColor, backColor, bold, checkBoxHeaderId) 
{ 
    var td = me.parentNode; 
    if (td == null) 
        return; 
        
    var tr = td.parentNode;
    if (me.checked)
    { 
       tr.style.fontWeight = 700; // bold
       tr.style.color = selectedForeColor; 
       tr.style.backgroundColor = selectedBackColor; 
    } 
    else 
    { 
        var elem = document.getElementById(checkBoxHeaderId);
        if (elem!=null)
        {
            elem.checked = false;
        }
        tr.style.fontWeight = bold; 
        tr.style.color = foreColor; 
        tr.style.backgroundColor = backColor; 
    } 
}

var mouseOutColor;
function gridMouseOverRow(source, mouseOverColor)
{
	mouseOutColor = source.style.backgroundColor;
	source.style.backgroundColor = mouseOverColor;
}

function gridMouseOutRow(source)
{
	source.style.backgroundColor = mouseOutColor;
}

// ************************************************************************************************************
//												END ** GRID **
// ************************************************************************************************************
