// add a class to an element
function AddClass(myObj, newClass)
{
    var elem = myObj;
    
    if(newClass != null)
    {
        elem.className = elem.className + " " + newClass;
    }
}

function ReplaceClass(myObj, newClass)
{
    var elem = myObj;
    
    if(newClass != null)
    {
        elem.className = "" + newClass;
    }
}

// delete a class to an element
function DeleteClass(myObj, oldClass)
{
    var elem = myObj;
    var myClass = elem.className;
    xt = myClass.replace(/selectedHover/," ");
    elem.className = xt;
}

// Tooltip
$(document).ready(function() {
	// Tooltips
	InitToolTips();
	
	// Character limiters
	InitCharacterLimiters();
	
	 // Overlay Popup Message Skip
	$(function() {
		$('#activator').click(function(){
			$('#overlayBox').fadeIn('fast',function(){
				$('#boxSkip').animate({'top':'260px'},500);
			});
		});
		$('#boxcloseSkip').click(function(){
			$('#boxSkip').animate({'top':'-200px'},500,function(){
				$('#overlayBox').fadeOut('fast');
			});
		});
	});
	
	// Overlay Popup Message Skip
	$(function() {
		$('#blockPeriod').click(function(){
			$('#overlayBox').fadeIn('fast',function(){
				$('#boxSkip').animate({'top':'260px'},500);
			});
		});
		$('#boxcloseSkip').click(function(){
			$('#boxSkip').animate({'top':'-200px'},500,function(){
				$('#overlayBox').fadeOut('fast');
			});
		});
	});
});

function InitToolTips()
{
	//select all desired input fields and attach tooltips to them  
    $(".inputTooltip").tooltip({      
        events: {
            input: 'click, blur',         
            widget:'focus mouseover,blur mouseout'
        },
        // place tooltip on the right edge     
        position: "center right",          
        offset: [-205, 0],
        tip: '.tooltip',
        predelay: 1000
    });
}

function InitCharacterLimiters()
{
	$('.character_limit').each(function(){  
		LimitCharacters($(this));

		// bind on key up event  
		$(this).keyup(function(){
			LimitCharacters($(this));
		});
		
		$(this).keypress(function(){
			LimitCharacters($(this));
		});
		
		$(this).keydown(function(){
			LimitCharacters($(this));
		});				
	});
}

function LimitCharacters(element) {
	// get max number of characters
	var max = element.attr('maxchars');
	
	if (max == undefined) {
		max = 0;
	}
	
	// get current number of characters  
	var length = element.val().length;  
	
	if (length > max) {
		element.val(element.val().substring(0, max));
	}
	
	// get current number of characters  
	length = element.val().length;

	// update characters  left
	element.siblings('div:first').html(max - length);
}
 
function TryParseInt(str,defaultValue){
    var retValue = defaultValue;
    if(str!=null){
        if(str.length>0){
            if (!isNaN(str)){
                retValue = parseInt(str);
            }
        }
    }
    return retValue;
}

//Show hide content

function HideContent(d)
{
	if(d.length < 1) 
	{ 
		return; 
	}
	
	//Get all div element
	//oDom = document.getElementsByTagName('div');

    clearTimeout(popupTimerId);
	document.getElementById(d).style.display = "none";
	
}

var cX = 0; var cY = 0;
function UpdateCursorPosition(e){ 
    cX = e.pageX+5; cY = e.pageY+5;
}

function UpdateCursorPositionDocAll(e){ 
    if(document.body != null)
    {
        cX = event.clientX; cY = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
}

if(document.all) { 
    document.onmousemove = UpdateCursorPositionDocAll; 
}
else { 
    document.onmousemove = UpdateCursorPosition; 
}
	
function AssignPosition(obj, d, optionAssignPosition) 
{
    iViewportWidth = getViewportWidth();
    iViewportHeight = getViewportHeight();

    aPosition = findPos(obj);
    iPosX = aPosition[0];
    iPosY = aPosition[1];
    sX = (((getViewportWidth()-990)/2)-5);
    sY = "215";

    // xpos
    if((cX + document.getElementById(d).offsetWidth) > iViewportWidth)
    {
        document.getElementById(d).style.left = (cX - sX - document.getElementById(d).offsetWidth - 0) + "px";
    }
    else
    {
        document.getElementById(d).style.left = (cX - sX) + "px";
    }
    
    // ypos
    if((cY + document.getElementById(d).offsetHeight) > iViewportHeight)
    
    {
        document.getElementById(d).style.top = (cY - sY - document.getElementById(d).offsetHeight) + "px";
    }
    else
    {
        document.getElementById(d).style.top = (cY - sY) + "px";
    }
}

function DisplayPopup(obj, d, optionAssignPosition)
{
    document.getElementById(d).style.display = "block";
	
	// if this parameter excists then pass it through to the final function AssignPosition
	if(optionAssignPosition)
	{
	    AssignPosition(obj, d, optionAssignPosition);
	}
	else
	{
	    AssignPosition(obj, d);
	}
}

// showContent, optional parameters are Message and optionAssignPosition
// optionAssignPosition is for positioning purposus if the popup is not in the root of the website. ( is quickfix )
function ShowContent(obj, d, Message, optionAssignPosition)
{		
    if(d.length < 1) { return; }
    		
	if(document.all)
	{
		document.onmousemove = UpdateCursorPositionDocAll;
	}
	else
	{
		document.onmousemove = UpdateCursorPosition;
		//document.getElementById(firstObj).innerHTML = firstObjContain;
	}
	
	if(Message)
	{
	    document.getElementById(d).innerHTML = Message;
	}
	
	// if this parameter, pass it through to further function
	if(optionAssignPosition)
	{
	    popupTimerId = setTimeout(function() { DisplayPopup(obj, d, optionAssignPosition); }, 100);
	}
	else
	{
	    popupTimerId = setTimeout(function() { DisplayPopup(obj, d); }, 100);
	}
	
	
}

function getViewportWidth()
{
	return self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
}
function getViewportHeight()
{
	return self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

// Use for texareas to set maxlength (use onkeyup="return Ismaxlength(this, 500);")
function Ismaxlength(obj, maxLength){
if (obj.getAttribute && obj.value.length>maxLength)
obj.value=obj.value.substring(0,maxLength)
}

function HtmlEncode(value){  
  return $('<div/>').text(value).html();  
}  
 
function HtmlDecode(value){  
  return $('<div/>').html(value).text();  
} 

