/*
Title: 		
Author: 	
Update: 	
*/

/*******************************************
Working Storage
*******************************************/
var librarydomain;
var librarydirectory;
var clientdomain;
var supsubcatcount;
/******************************************
Load event 
******************************************/

//Subscribe Initialization functions to the window.onload event
//i.e. addLoadEvent(_setExpandableSectioin);
addLoadEvent(urchin);

/* ---------------------------------------
	doNothing
	This function does nothing.
	Use it when you need to call javascript but nothig should happen
--------------------------------------- */
function doNothing()
{
    return;
}
/* ---------------------------------------
	addLoadEvent
	This function shall be used to subscribe event handlers to the onload event
	of the window object.
--------------------------------------- */
function addLoadEvent(func)
{
	//get a pointer to the first handler
	var oldonload = window.onload;
	//if the event has no subscribers
	if (typeof window.onload != 'function')
		window.onload = func;	//subscribe the func handler
	//otherwise, queue it up
	else
	{
		window.onload = function(){
			oldonload();
			func();			
		}
	}
}


//private functions
/*--------------------------------------------------------
	_show(tag)
	Removes the hidden class from the tag's class list 
	and sets its expanded attribute to 'yes'
--------------------------------------------------------*/
function _show(tag)
{
    removeElementFromClass(tag, "hidden");
    //tag.className = '';
    tag.setAttribute("expanded", "yes");
}
/*--------------------------------------------------------
	_hide(tag)
	Adds the hidden class to the tag's class list 
	and sets its expanded attribute to 'no'
--------------------------------------------------------*/
function _hide(tag)
{
    addElementToClass(tag, "hidden");
    //tag.className = "hidden";
    tag.setAttribute("expanded", "no");
}
//--------------------------------------------------------//

//public methods
/*
function exploreTopic(topicDropdown)
{
    var path = "/ExploreTopics/";
      
    if (topicDropdown.value == 1){
        path = path + "StartingBusiness";
    }else if (topicDropdown.value == 2){
        path = path + "DealingLicenses";
    }else if (topicDropdown.value == 3){
        path = path + "EmployingWorkers";
    }else if (topicDropdown.value == 4){
        path = path + "RegisteringProperty";
    }else if (topicDropdown.value == 5){
        path = path + "GettingCredit";
    }else if (topicDropdown.value == 6){
        path = path + "ProtectingInvestors";
    }else if (topicDropdown.value == 7){
        path = path + "PayingTaxes";
    }else if (topicDropdown.value == 8){
        path = path + "TradingAcrossBorders";
    }else if (topicDropdown.value == 9){
        path = path + "EnforcingContracts";
    }else if (topicDropdown.value == 10){
        path = path + "ClosingBusiness";
    }else if (topicDropdown.value == 11){
        path = path + "Infrastructure";        
    }else if (topicDropdown.value == 12){
        path = path + "Transparency";        
    }
    
    window.location = path;
}
*/
//--------------------------------------------------------//
/*
function exploreEconomy(economyDropdown, path)
{
    if(path.match(/\./)){
        path = "/" + path;
    }else{
        path =  "/" + path + "/";
    }

    window.location = path + '?economyid=' + economyDropdown.value;
}
*/
/*--------------------------------------------------------
	showHide(id)
	This function hides or displays the contents of a given tag.
	Such tag must have the attribute 'expanded'.
--------------------------------------------------------*/
function showHide(id)
{
    var expandableSection = document.getElementById(id);
    if (root.getAttribute("expanded") == "no")
        _show(expandableSection);
    else
        _hide(expandableSection);
}
/*--------------------------------------------------------
	showHide(id, cmd)
	This function implements the show and hide commands
	to hide or display the contents of a given tag.
	Such tag must have the attribute 'expanded'.
--------------------------------------------------------*/
function showHide(id, cmd)
{
    var expandableSection = document.getElementById(id);
    if (cmd == "show")
        _show(expandableSection);
    else
        _hide(expandableSection);
}

//--------------------------------------------------------//
function ExploreLawType(lawTypeDropDown){
    var path = "/LawLibrary/";
    
    window.location = path + '?typeid=' + lawTypeDropDown.value;
}
//--------------------------------------------------------//
function EconomyRanking(RankingCtrl, RankingDropDown)
{
    //var path = "";
     
    if(RankingDropDown.value != 0)
    {
        if (RankingCtrl == 'Region'){
            window.location = '?regionid=' + RankingDropDown.value;
        }
        else if (RankingCtrl == 'Population'){
            window.location = '?tercile=' + RankingDropDown.value;
        }
        else{
            window.location = '?IncomeId=' + RankingDropDown.value;
        } 
    }   
    else
    {
     window.location = '/economyrankings/'
    }
}
//--------------------------------------------------------//
//////Email Alerts
//--------------------------------------------------------//

function submitAlert(txtEmail,lblMsg)
{
    var emailAddress = document.getElementById(txtEmail);
    var successDiv = document.getElementById(lblMsg);
    
    var url = '/Ajax/Alert.aspx';
    
    //Check email validity
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

    if (filter.test(emailAddress.value))
    {
        var pars = 'email=' + emailAddress.value;
  
        var myAjax = new Ajax.Request(
        url,
        {
            method:'get',
            parameters:pars,
            onSuccess:function()
            {
                successDiv.style.display='block';
                successDiv.innerHTML = 'Thank you for subscribing';
            }
        });
    }
    else
    {
        successDiv.style.display='block';
        successDiv.innerHTML = 'Please enter a valid email address';
    }
}

//--------------------------------------------------------//
//////Print
//--------------------------------------------------------//
function printpage()
{	    
    window.print();
}

//--------------------------------------------------------//
//////returns the id of the given element
//--------------------------------------------------------//
function getElementId(element)
{    
    if (element.id != null) 
    {
        var elementId = element.id;        
        var aElementId = elementId.split('_');
        var id = '';
        
        for(var i = 2; i < aElementId.length; ++i)
        {  
            if(id != '')  
                id = id + '_' + aElementId[i];
            else
                id = id + aElementId[i];
         }   
        return id;     
    }
}

//--------------------------------------------------------//
//////returns the element of the given id
//--------------------------------------------------------//
function getElementbyGivenId(id, element)
{    
    var e;
    var i = 0;
 
    if (id == getElementId(element))
    {
        e = element;        
    }
    //Depth firts search the element tree
    while((e == undefined) && (i < element.childNodes.length))
    {
        e = getElementbyGivenId(id, element.childNodes[i]);   
        ++i;
    }
    return e;        
}
