
// Gets an element collection by class name
function getElementsByClassName(className, tag, elm)
{
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++)
	{
		current = elements[i];
		if(testClass.test(current.className))
		{
			returnElements.push(current);
		}
	}
	return returnElements;
}
// Used to collapse all the collapsable panels in the main body
function collapseAll()
{
    obj = getElementsByClassName('collapse');
    for(i=0; i<obj.length; i++)
    {
        //obj[i].style.display = "none";
        obj[i].className = "collapse hidden";
    }
    
    objCommentBody = document.getElementById("CommentFunctionBody");
    if (objCommentBody)
    {
        objCommentBody.style.display = "none";
    }
    
    objTipBody = document.getElementById("TipFunctionBody");
    if (objTipBody)
    {
        objTipBody.style.display = "none";
    }
    
    objTipBody = document.getElementById("OrderConditionsFunctionBody");
    if (objTipBody)
    {
        objTipBody.style.display = "none";
    }
    
    objTipBody = document.getElementById("TechnicalConditionsFunctionBody");
    if (objTipBody)
    {
        objTipBody.style.display = "none";
    }
    
}

// Collapses/Uncollapses one collapsable panel. The panel is the 
// closest sibling to the link that triggers this function
// Used in the collapsable divs in the main body (Level of content)
function collapseUncollapse(obj, linkTextMax, linkTextMin)
{
    container = obj.nextSibling;
    if (container.className == "collapse hidden")
    {
        container.className = "collapse unhidden";
        container.style.display = "block";
        obj.innerHTML = linkTextMin;
        obj.className = "collapsable expanded"
    }
    else
    {
        container.className = "collapse hidden";
        container.style.display = "none";
        obj.innerHTML = linkTextMax;
        obj.className = "collapsable collapsed"
    }
}

// Used in comment and tip page functions
function showHide(elementid, parentclass, link)
{
    divObj = document.getElementById(elementid);
    
    if (divObj.style.display == 'none')
    {
        divObj.style.display = 'block';
        link.className = "functionCollapsed";
        divObj.parentNode.parentNode.className = parentclass + " listFunctionUnCollapsed";
    } 
    else 
    {
        divObj.style.display = 'none';
        link.className = "functionUnCollapsed";
        divObj.parentNode.parentNode.className = parentclass;
    }
}
// COLAPSABLE DIVs
//Configuration section
//********************************************************
var speed = 15; //how often the div refreshes to the new height
var incriment = 10; //each time the div refreshes height will be increased or deceased by this amount
//********************************************************

//Code
//********************************************************
var iTimer;
var calcHeight;
function toggleDiv(divToShow, divHeader){
	var help = document.getElementById(divToShow);
	if(help.style.display != "block") {
		showDiv(divToShow);
		Expand(divToShow);
		divHeader.className = 'relatedInfoHeaderExpanded';
	}
	else {
		collapse(divToShow);
		divHeader.className = 'relatedInfoHeaderCollapsed';
	}
}
function Expand(divName) {
	var help = document.getElementById(divName);		
	var height = help.offsetHeight;
	if(height < calcHeight){
		help.style.height = height + incriment + "px";
		iTimer = setTimeout( "Expand('" + divName +"')" , speed);
	}
	else{
		clearTimeout(iTimer);
	}	
}		
function collapse(divName) {
	var help = document.getElementById(divName);		
	var height = help.offsetHeight;
	if(height > incriment){
		help.style.height = height - incriment + "px";
		iTimer = setTimeout( "collapse('" + divName +"')" , speed);
	}
	else{
		clearTimeout(iTimer);
		help.style.height ="100%";
		help.style.display = "none";
	}	
}		
function showDiv(divName){
	var div = document.getElementById(divName);
	div.style.display = "block";
	calcHeight = div.offsetHeight;
	div.style.height = "0px";
}
//*******************************//
// THE CODE ABOVE SHOULD BE      //
// DELETED AND THE BELOW ONE     //
// SHOULD BE USED INSTEAD, WHERE //
// YOU SET THE EXPANDED AND      //
// COLLAPSED CSS CLASES AS       //
// PARAMETERS SET TO THE METHOD  //
//*******************************//
// RELATED NEWS
var refreshSpeed = 15;
var incrementAmount = 10;
var refreshTimer;
var calculatedHeight;
function ToggleDiv(divContentId, divHeaderControl, expandedClass, collapsedClass)
{
	var contentControl = document.getElementById(divContentId);
	if (contentControl.style.display != "block")
	{
		ShowDiv(divContentId);
		ExpandDiv(divContentId);
		divHeaderControl.className = expandedClass;
	}
	else
	{
		CollapseDiv(divContentId);
		divHeaderControl.className = collapsedClass;
	}
}
function ExpandDiv(controlId)
{
	var expandControl = document.getElementById(controlId);		
	var height = expandControl.offsetHeight;
	if (height < calculatedHeight)
	{
		expandControl.style.height = height + incrementAmount + "px";
		refreshTimer = setTimeout("ExpandDiv('" + controlId +"')", refreshSpeed);
	}
	else
	{
		clearTimeout(refreshTimer);
	}	
}		
function CollapseDiv(controlId)
{
	var collapseControl = document.getElementById(controlId);		
	var height = collapseControl.offsetHeight;
	if(height > incriment)
	{
		collapseControl.style.height = height - incrementAmount + "px";
		refreshTimer = setTimeout("CollapseDiv('" + controlId +"')", refreshSpeed);
	}
	else
	{
		clearTimeout(refreshTimer);
		collapseControl.style.height = "100%";
		collapseControl.style.display = "none";
	}	
}		
function ShowDiv(controlId)
{
	var divControl = document.getElementById(controlId);
	divControl.style.display = "block";
	calculatedHeight = divControl.offsetHeight;
	divControl.style.height = "0px";
}

var TipBoxID = "ExplanationTextBox";
var tip_box_id;

function findPosX(obj)
{
   var curleft = 0;
   if(obj.offsetParent)
   while(1) 
   {
      curleft += obj.offsetLeft;
      if(!obj.offsetParent)
         break;
      obj = obj.offsetParent;
   }
   else if(obj.x)
      curleft += obj.x;
   return curleft;
}

function findPosY(obj)
{
   var curtop = 0;
   if(obj.offsetParent)
   while(1)
   {
      curtop += obj.offsetTop;
      if(!obj.offsetParent)
         break;
      obj = obj.offsetParent;
   }
   else if(obj.y)
      curtop += obj.y;
   return curtop;
}

function DisplayTip(me,offX,offY,content)
{

   var tipO = me;
   tip_box_id = document.getElementById(TipBoxID);
   var x = findPosX(me);
   var y = findPosY(me);
   tip_box_id.style.left = String(parseInt(x + offX) + 'px');
   tip_box_id.style.top = String(parseInt(y + offY) + 'px');
   tip_box_id.innerHTML = content;
   tip_box_id.style.display = "block";
   tipO.onmouseout = HideTip;
}

function HideTip() { tip_box_id.style.display = "none"; }

// used on community my page profile
function ShowHide(row1class, row2class, row3class) {
    if (row1class != null) {
        var row1 = getElementsByClassName(row1class);
        var bExpand = row1[0].style.display == '';
        row1[0].style.display = (bExpand ? 'none' : '');
    }

    if (row2class != null) {
        var row2 = getElementsByClassName(row2class);
        var bExpand = row2[0].style.display == '';
        row2[0].style.display = (bExpand ? 'none' : ''); 
    }

    if (row3class != null) {
        var row3 = getElementsByClassName(row3class);
        var bExpand = row3[0].style.display == '';
        row3[0].style.display = (bExpand ? 'none' : '');
    }   
}

// used on community my page profile
function ChangePlusMinus(obj) {
    if (obj.className == "plus") obj.className = "minus";
    else obj.className = "plus";
}

function ToggleUseActivity(control, activityId, hiddenFieldId) {
    var activities = document.getElementById(hiddenFieldId).value;
    var activitiesArray = new Array();
    if (activities.length > 0) {
        activitiesArray = activities.split(';');
    }
    var existIndex = ExistInArray(activityId, activitiesArray);
    if (existIndex == -1) {
        activitiesArray.push(activityId);
    }
    else if (control.checked) {
        activitiesArray.push(activityId);
    }
    else {
        activitiesArray.splice(existIndex, 1);
    }
    activities = "";
    for (var i = 0; i < activitiesArray.length; i++) {
        if (activitiesArray[i] != '') {
            activities = activities + activitiesArray[i] + ";";
        }
    }
    document.getElementById(hiddenFieldId).value = activities;
}
function ExistInArray(item, array) {
    for (var i = 0; i < array.length; i++) {
        if (array[i] == item) {
            return i;
        }
    }
    return -1;
}