//This is used in LoginLanguages.ascx, UserPreferences.ascx.
// To Show or Hide Telerik ComboBox.
function HideRadComboBox(){
    changeClass();
}

// clear transparent image
function clearpreloadpage(a){
    var el = $('#prepage');
    if (el.length != 0)
    	el.css("display", "none");    
}

/*
* Open URL in new window
* Added by Vitaly Richok on 20 nov, 2007 
*/
function OpenWindow(strURL){	
	var strOptions = "resizable=1, toolbar=0, location=0, menubar=0";
	var objWindow = CohortWindowOpen(strURL, "Help", strOptions);
	return false;
}

/* 
*  Some helpful functions
*  Created on November 22, 2007 by Vitaly Richok
*/

// Get reference on HTML element with specified ID
// Parameters:
// 1. strID - id attrubute value of some HTML element
// Returns: reference to HTML element or null
function getReference(strID){
	if (document.getElementById) {
		return document.getElementById(strID);
	}
	else if (document.all) {
		return document.all[strID];
	}
	else if (document.layers) {
		return document.layers[strID];
	}
	return null;
}

// Set style for HTML element.
// Parameters:
// 1. objElement - HTML element reference.
// 2. strAttribute - CSS attribute name (use "fontSize" inplace of "font-size" and so on).
// 3. strValue - CSS attribute value ("bold", "2px", "relative" and so on).
// Returns: true if success (HTML element is not null). Otherwise false.
function setStyle(objElement, strAttribute, strValue)
{
	if (objElement == null) { return false; }
	objElement.style[strAttribute] = strValue;
	return true;
}

// Get style attribute value of HTML element
// Parameters:
// 1. objElement - HTML element reference.
// 2. strAttribute - CSS attribute name
// Returns: CSS attribute value.
function getStyle(objElement, strAttribute)
{
	if (objElement == null) { return null; }
	var strValue = objElement.style[strAttribute];
	if (!strValue)
	{
		if (document.defaultView) {
			strValue = document.defaultView.getComputedStyle(objElement, "").getPropertyValue(strAttribute);
		}
		else if (objElement.currentStyle) {
			strValue = objElement.currentStyle[strAttribute];
		}
	}
	return strValue;
}


/*
*  Make control below Spacer always-on-top
*  Use with onscroll and onresize events of document:
*  onresize = FloatPosition;
*  onscroll = FloatPosition;
*  Created on November 22, 2007 by Valeriy Nikitin
*/
function FloatPosition()
{
	// TODO: Need other way to pass 150 and "Spacer" into function
	var strSpacerID = "Spacer";
	var intHeaderHeight = 160;

	var pos = document.all ? document.all[strSpacerID] : document.getElementById(strSpacerID);
	var pad = document.documentElement.scrollTop;              

	if (pad < 1) pad = 0;
	
	if(pos != null)
	{
		if (pad - intHeaderHeight < 0) {
			pos.style.height = 0 + 'px';
		}
		else
		{ pos.style.height = (pad - intHeaderHeight) + 'px'; }
	}
}
/*
*  Check up chars count in textarea (value of maxlength attribute is used)
*  Created on May 30, 2008 by Vitaly Richok
*/
//function CheckMaxLength(objTextArea, boolIsPaste)
function CheckMaxLength(ev, sender, isPaste)
{
    
	var selected = GetSelectedText(sender);

	var maxLength = parseInt(sender.getAttribute("maxlength"));
	var selectedLength = selected.length;
	var currentLength = sender.value.length - selectedLength;

	if (isPaste && window.clipboardData)
	{
		var strText = window.clipboardData.getData("Text");
		if (currentLength < maxLength && maxLength - currentLength <= strText.length)
		{
			strText = strText.substring(0, maxLength - currentLength);
			window.clipboardData.setData("Text", strText)
		}
		currentLength += strText.length;
	}
	var isCancel=!(isPaste ? (maxLength >= currentLength) : (maxLength > currentLength));
	if(isCancel){
    	//ev.returnValue = false;
    	if (null != ev.cancelBubble) {
    		ev.cancelBubble = true;
    	}
//	    if (ev.stopPropagation)
//		    ev.stopPropagation();
//	    if (ev.preventDefault)
//		    ev.preventDefault();

	}
	return !isCancel;
}

/*
*  Get selected text
*  Created on June 03, 2008 by Vitaly Richok
*/
function GetSelectedText(objTextArea)
{
	var strText = "";
	// Mozilla FireFox
	if (window.getSelection) {
		strText = objTextArea.value.substring(objTextArea.selectionStart, objTextArea.selectionEnd);
	}
	// Microsoft IE
	if (document.selection) {
		strText = document.selection.createRange().text;
	}

	return strText;
}

/* for open popup from menu */
function menu_open_popup(url) {
	var manager = typeof (GetRadWindowManager) != "undefined" ? GetRadWindowManager() : null;
	var wnd = manager != null ? manager.GetWindowByName(/*'Cohort'*/'OneHalf') : null;

	if (wnd != null) {
		wnd.SetUrl(url);
		wnd.OnClientClose = function() {
			window.setTimeout(function() {
				wnd.SetUrl('about:blank');
				if (wnd.RestoreRect != null && null != wnd.RestoreRect.height) {
					wnd.RestoreRect.height = 100;
				}
				wnd.SetTitle('&nbsp;');
				wnd.OnClientClose = null;
				wnd = null;
			}, 400);
		};
		wnd.Show();
	}
};

/*open calendar above or left    */
function CalPopupOffset(datepickerId, isleft )
{
	var datePicker = window[datepickerId];
	if (datePicker.IsPopupVisible())
	{
		datePicker.HidePopup();
	}
	else
	{
		var textBox = datePicker.GetTextBox();
		var popupElement = datePicker.GetPopupContainer();
		var dimensions = datePicker.GetElementDimensions(popupElement);
		var position = datePicker.GetElementPosition(textBox);

		if (isleft = 1)
		{
			datePicker.ShowPopup(position.x + textBox.clientWidth + 18 - dimensions.width, position.y + textBox.clientHeight);
		}
		else
		{
			datePicker.ShowPopup(position.x, position.y - dimensions.height);
		}
	}
};

function redirect(url){
	window.location.replace(url || "");
};
