///////////////////////////////////////////////////////////////////////////////////////
// global constants

// 'tab' controls naming convention
var tabTag = "Tab";
var tabContentTag = "Content";
var tabSeparator = "_";

// undefined value
var undefVal = -1;

// end global constants
///////////////////////////////////////////////////////////////////////////////////////

String.prototype.trim = function() 
{
    return this.replace(/^\s*|\s*$/g,"");
}

function StringToDate(dateStr)
{
	if(dateStr != null && /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/.test(dateStr))
	{
		// string is in correct format
		var dateParts = dateStr.split('/');

		var date = new Date();
		date.setFullYear
		(
			parseInt(dateParts[2]),
			parseInt(dateParts[1]),
			parseInt(dateParts[0])
		);
		return date;
	}
	else
	{
		// convert failed
		return null;
	}
}

function OpenWindow(query,w,h,scroll)
{
	if(w == 0 || h == 0)
	{
		w = screen.width;
		h = screen.height;
	}
	var l = (screen.width - w) / 2;
	var t = (screen.height - h) / 2;
	
	winprops = 'resizable=1, height='+h+',width='+w+',top='+t+',left='+l+'w';
	if (scroll)
	{
		winprops+=',scrollbars=1';
	}
	
	var f = window.open(query, "_blank", winprops);
}

function ValidateEmail(email)
{
	return /^([A-Za-z0-9\-_]*[\.A-Za-z0-9\-_]+@[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+){1,3})*$/.test(email);
}

function GetKeyCode(e)
{	
	var keycode;
	if(window.event) // IE
	{
		keycode = e.keyCode
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keycode = e.which
	}
	return keycode;
}

function GetStyle(elementId,styleProp)
{
	var element = document.getElementById(elementId);
	var val = "";
	if (element.currentStyle)
	{
		// IE
		val = element.currentStyle[styleProp];
	}
	else if (window.getComputedStyle)
	{
		// Mozilla
		val = document.defaultView.getComputedStyle(element,null).getPropertyValue(styleProp);
	}
	return val;
}


function ShowElement(elementId, displayStyle, innerHtml)
{
	var element = document.getElementById(elementId);
	element.style.display = displayStyle;
	element.innerHTML = innerHtml;
}

function DisplayElement(elementId, displayStyle)
{
	var element = document.getElementById(elementId);
	element.style.display = displayStyle;
}

function HideElement(elementId)
{
	DisplayElement(elementId, "none");
}

function HideAndClearElement(elementId)
{
	ShowElement(elementId, "none", "");
}

function HideAndResetElement(elementId)
{
	HideElement(elementId);
	ResetSelectionsAt(elementId);
}

function DisableUserInput(containerId)
{
	var container = document.getElementById(containerId);
	
	// apply blocker style
	if(container.className.indexOf("activate-blocker") < 0)
	{
		container.className += " activate-blocker";
	}
		
	// handle "select" controls
	var selectControls = container.getElementsByTagName("SELECT");
	for(i=0; i<selectControls.length; i++)
	{
		selectControls[i].disabled = true;
	}
}

function EnableUserInput(containerId)
{
	var container = document.getElementById(containerId);
	
	// adjust blocker height
	var blocker = YAHOO.util.Dom.getElementsByClassName("blocker", "div", container)[0];
	blocker.style.width = container.offsetWidth + "px";
	blocker.style.height = container.offsetHeight + "px";
	
	// remove blocker style
	container.className = container.className.replace(" activate-blocker","");
	
	// handle "select" controls
	var selectControls = container.getElementsByTagName("SELECT");
	for(i=0; i<selectControls.length; i++)
	{
		selectControls[i].disabled = false;
	}
}

function SelectTab(tab)
{
	var tabId = tab.id;
	var tabName = tab.name;
	var tabGroup = document.getElementsByName(tabName);
	for (i=0; i<tabGroup.length; i++)
	{
		// unselect all tab buttons within current group
		tabGroup[i].parentNode.className = "";
		
		// hide all content elements controlled by tab
		var contentId = tabGroup[i].id.replace(tabTag, tabContentTag);
		document.getElementById(contentId).style.display = "none";
	}
	// select 'this' tab
	tab.parentNode.className = "chosen";
	
	// show 'this' tab controlled element
	var selectedContentId = tabId.replace(tabTag, tabContentTag);
	document.getElementById(selectedContentId).style.display = "block";
}

function SetSearchUrl(searchLinkId, searchParam, searchVal)
{
	var searchLink = document.getElementById(searchLinkId);
	
	if(searchLink == null || searchVal == null)
	{
		return;
	}
	
	var currUrl = searchLink.href;
		
	// get path and querystring
	var pathAndQuery = currUrl.split("?");
	if(pathAndQuery.length > 1)
	{
		var path = pathAndQuery[0];
		var query = pathAndQuery[1];
			
		// split querystring to tokens
		var tokens = query.split("&");
		query = "";
		
		// search for currently changed parameter
		var found = false;
		for(i=0; i<tokens.length; i++)
		{
			if(tokens[i].indexOf(searchParam + "=") >= 0)
			{
				// found
				found = true;

				if(parseInt(searchVal) != undefVal)
				{
					// replace value if proper
					tokens[i] = searchParam + "=" + searchVal;
				}
				else
				{
					// remove whole token
					tokens[i] = "";
				}
				break;
			}
		}
			
		// rebuild querystring
		if(!found && parseInt(searchVal) != undefVal)
		{
			// add new search token
			query = "?" + searchParam + "=" + searchVal;
		}
		for(i=0; i<tokens.length; i++)
		{
			if(tokens[i] != "")
			{
				if(query == "")
				{
					query = "?" + tokens[i];
				}
				else
				{
					query = query + "&" + tokens[i];
				}
			}
		}
					
		currUrl = path + query;
	}
	else
	{
		if(parseInt(searchVal) != undefVal)
		{
			// add new search token as first querystring parameter
			currUrl = currUrl + "?" + searchParam + "=" + searchVal;
		}
	}
	
	// set the new href
	searchLink.href = currUrl;
}

function SetSearchUrlAndSelectOption(searchLinkId, searchParam, searchVal, selectId)
{
	var selectCtrl = document.getElementById(selectId);
	try
	{
		if(searchVal != null && parseInt(searchVal) > 0)
		{
			SetSearchUrl(searchLinkId, searchParam, searchVal);
			SelectOptionByVal(selectCtrl, searchVal);
		}
		else
		{
			ResetSelection(selectCtrl);
		}
	}
	catch(e)
	{
		ResetSelection(selectCtrl);
	}
}

function SetHref(linkId, href)
{
	var link = document.getElementById(linkId);
	if(link != null)
	{
		link.href = href;
	}
}

function RedirectOnSelect(urlTemplate, findVal, selectVal)
{
	try
	{
		if(parseInt(selectVal) > 0)
		{
			document.location.href = urlTemplate.replace(findVal, selectVal);
		}
	}
	catch(e)
	{
	}
}

///////////////////////////////////////////////////////////////////////////////////////
// select element utils

function HasOptions(obj)
{
	if (obj!=null && obj.options!=null) { return true; }
	return false;
}

function CopyOptions(from, to, selectedIndex)
{
	if (!HasOptions(from)) { return; }
	
	RemoveOptions(to);
	
	for (var i=0; i<from.options.length; i++)
	{
		var o = from.options[i];
		AddOption(to, o.text, o.value, false);
	}
	to.selectedIndex = -1;
}

function RemoveOptions(from)
{ 
	if (!HasOptions(from)) { return; }
	for (var i=(from.options.length-1); i>=0; i--)
	{ 
		from.options[i] = null; 
	} 
	from.selectedIndex = -1; 
} 

function AddOption(obj,text,value,selected)
{
	if (obj!=null && obj.options!=null)
	{
		obj.options[obj.options.length] = new Option(text, value, false, selected);
	}
}

function SelectOptionByVal(obj,value)
{
	if (!HasOptions(obj)) { return; }
	obj.value = value;
}

function SelectOptionByIndex(obj,index)
{
	if (!HasOptions(obj)) { return; }
	obj.selectedIndex = index;
}

function ResetSelection(obj)
{
	SelectOptionByIndex(obj,0)
}

function ResetSelectionsAt(containerId)
{
	var container = document.getElementById(containerId);
	if(container == null) { return; }
	var selectControls = container.getElementsByTagName("SELECT");
	for(i=0; i<selectControls.length; i++)
	{
		ResetSelection(selectControls[i]);
	}
}
// end select element utils
///////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////
// cookie utils
function writeCookie(name, value, days)
{
    var expDate = new Date();
    expDate.setTime(expDate.getTime() + (days*24*60*60*1000));

    document.cookie = name + "=" + value + ";expires=" + expDate.toGMTString();
}
 

function readCookie(name)
{
	var cookieArr = document.cookie.split(";");

    for (i = 0; i < cookieArr.length; i++)
    {
		var currentCookieArr = cookieArr[i].split("=");
        var cookieName = currentCookieArr[0];
        var cookieValue = currentCookieArr[1];
        
        if (cookieName.trim() == name)
        {
            if (cookieValue != null)
            {
                return cookieValue;
            }
        }

    }
    return "";
}
// end cookie utils
///////////////////////////////////////////////////////////////////////////////////////