
function NewWindow(sUrl, sTitle, sWidth, sHeight, sScrollable) 
{
	var iLeft = (screen.width - sWidth) / 2;
	var iTop = (screen.height - sHeight) / 2;

	var oNewWindow = window.open(sUrl, sTitle, 'height='+sHeight+',width='+sWidth+',top='+iTop+',left='+iLeft+',scrollbars='+sScrollable+',resizable=yes');

	if (oNewWindow && oNewWindow.open && !oNewWindow.closed)
	{
		oNewWindow.window.focus();
	}
	return oNewWindow;
}

var oPoppedWindow = null;

function ClosePoppedWindows()
{
	if (oPoppedWindow && oPoppedWindow.open && !oPoppedWindow.closed)
	{
		oPoppedWindow.close();
	}
}

function ResourceManager(return_function)
{	
	var height = 520;
	var width = 725;
	var scroll = 'no';
	
	oPoppedWindow = NewWindow('ResourceManager.aspx?select_function='+return_function,'dep_search',width,height,scroll);
}

function change_page(number)
{
	new_search('start', number);
}

function change_search(text)
{
	new_search('q', text);
}

function change_group(text)
{
	new_search('search', text);
}

function new_search(name, value)
{
	var search = document.location.search;

	search = remove_keys(search, 'page_number');
	search = remove_keys(search, 'start');
	search = remove_keys(search, name);
	
	search = add_key(search, name, value);

	document.location.search = search;
}

function add_key(string, name, value)
{
	value = new String(value);
	
	if (value.length > 0)
	{
		if (string.indexOf('?') < (string.length - 1))
		{
			string = string + '&';
		}

		string = string + name + '=' + value;
	}
	
	return (string);
}

function remove_keys(string, name)
{
	if (name.length > 0)
	{
		name = name + '=';
		
		string = new String(string);
		
		var start = string.indexOf(name);

		while(start > -1)
		{
			var temp = string.substring(0, start);

			var end = string.indexOf('&', start);

			if (end > -1)
			{
				temp = temp + string.substr(end + 1);
			}
			else
			{
				if (temp.charAt(temp.length - 1) == '&')
				{
					temp = temp.substring(0, temp.length - 1);
				}
			}

			string = temp;

			start = string.indexOf(name);
		}
	}
	
	return (string);
}

function FlipFilterDiv(sId, sIdLabel)
{
	if(document.getElementById(sId).style.display=='none') 
	{
		document.getElementById(sId).style.display='block';
		document.getElementById(sIdLabel).innerText='Close Filter';
	}
	else
	{
		document.getElementById(sId).style.display='none';
		document.getElementById(sIdLabel).innerText='Open Filter';
	}
}

function ContentEditor(sIdContent, sPage, sCode, iSortOrder)
{
	if (isNaN(iSortOrder))
		iSortOrder = 100;
		
	sQuery = '?';
	if (sIdContent != null)
		sQuery += 'id_content=' + sIdContent + '&';
	sQuery += 'page=' + sPage + '&code=' + sCode + '&sort_order=' + iSortOrder;
	
	var sUrlNoProt = document.location.href.substring(document.location.protocol.length + 2);
	var aUrl = sUrlNoProt.split('/');
	
	var sBase = document.location.protocol + "//" + document.location.host;
	if (sBase.indexOf('localhost') > -1)
	    sBase += '/' + aUrl[1];
	    
	oPoppedWindow = NewWindow(sBase + "/Utility/ContentEditor.aspx" + sQuery,'content_editor','725','600','yes');
}

function ChangeABC(abc_value)
{
	new_search('abc', abc_value);
}

function KeywordSearch(value)
{
	var search = document.location.search;

	search = remove_keys(search, 'page_number');
	search = remove_keys(search, 'start');
	search = remove_keys(search, 'keyword');
	search = remove_keys(search, 'id_destination');
	search = remove_keys(search, 'id_supplier');
	
	search = add_key(search, 'keyword', value);

	document.location.search = search;
}

function SupplierSearch(value)
{
	var search = document.location.search;

	search = remove_keys(search, 'page_number');
	search = remove_keys(search, 'start');
	search = remove_keys(search, 'keyword');
	search = remove_keys(search, 'id_destination');
	search = remove_keys(search, 'id_supplier');
	
	search = add_key(search, 'id_supplier', value);

	document.location.search = search;
}

function DestinationSearch(value)
{	
	var search = document.location.search;

	search = remove_keys(search, 'page_number');
	search = remove_keys(search, 'start');
	search = remove_keys(search, 'keyword');
	search = remove_keys(search, 'id_destination');
	search = remove_keys(search, 'id_supplier');
	
	search = add_key(search, 'id_destination', value);

	document.location.search = search;
}

function checkHttp(oEl)
{
	if ((oEl.value.substring(0,7) != 'http://') && (oEl.value.substring(0,8) != 'https://')) 
		oEl.value = 'http://' + oEl.value;
}

function ForgotPassword()
{
	oPoppedWindow = NewWindow("ForgotPassword.aspx",'_X_X_X_X_record_dep',450,300,"no");
}

function AmIRegistered()
{
	oPoppedWindow = NewWindow("AmISubscribed.aspx",'_X_X_X_X_record_dep',450,300,"no");
}

function ImageSizer(oImage, iMaxWidth, iMaxHeight, bForceWidth, bForceHeight)
{
    var iHeight = oImage.height;
    var iWidth = oImage.width;

    var fixRatio = iWidth / iHeight;

    if (bForceWidth=="1")
    {
        iWidth = iMaxWidth;
        iHeight = iMaxWidth / fixRatio;
    }
    else if (bForceHeight=="1")
    {
        iHeight = iMaxHeight;
        iWidth = iMaxHeight * fixRatio;
    }
    else
    {
        if ((iMaxWidth > 0) && (iWidth > iMaxWidth))
        {
            iWidth = iMaxWidth;
            iHeight = iMaxWidth / fixRatio;
        }
        if ((iMaxHeight > 0) && (iHeight > iMaxHeight))
        {
            iHeight = iMaxHeight;
            iWidth = iMaxHeight * fixRatio;
        }
    }
    
    oImage.width = iWidth;
    oImage.height = iHeight;
}








