/**************** CODE ADDED BY Mohamed MELLOUL *************/

/* This function needs 3 parameters :
*  - The ID of the dropdownlist that contains alist of states
*  - The ID of the dropdownlist that contains a list of countries
*  - The ID of the DIV containing the state dropdownlist
*  
*  This function enables hides the state DropDownList if the current selected 
*  country is not "United States"
*/
function enableStates(StateDropDownName, CountryDropDownName, DivId)
{
    StateDropDown = GetElementsByName(StateDropDownName);
    CountryDropDown = GetElementsByName(CountryDropDownName);
    MyDiv = document.getElementById(DivId);

    if (MyDiv != null)
    {
	    if (CountryDropDown[0].value == 'US')
	    {		
		    MyDiv.style.visibility = 'visible';
		    MyDiv.style.display = 'block';
		    StateDropDown[0].disabled = false;
	    } 
	    else 
	    {
		    MyDiv.style.visibility = 'hidden';
		    MyDiv.style.display = 'none';
		    StateDropDown[0].disabled = true;
		    StateDropDown[0].selectedIndex = 0;
	    }
    }
}
	    
function GetElementsByName(SearchName)
{
    var MainForm = document.forms[0];
    var ResultElements = new Array();

    for(i=0; i < MainForm.elements.length; i++)
    {
	    if (MainForm.elements[i].id.indexOf(SearchName) != -1)
		    ResultElements.push(MainForm.elements[i]);
    }
    return ResultElements;
}


//This function checks if there is an input tag which id is equal to myId. Then, it puts the className class
function replaceCheckBox (myId, className)
{
    var inputs = document.getElementsByTagName ('input');
    for (i=0; i<inputs.length; i++)
    {
        if ((inputs[i].id).indexOf (myId) != -1)
        {
            document.getElementById (inputs[i].id).className = className;
        } 
    } 
}

function showLoginErrors (div0, div1, search1, div2, search2, div3)
{
    html = '<img src="/us/images/error-alert.gif" />';
    if (document.getElementById (div0) != null && document.getElementById (div0).innerHTML != "")
    {
        document.getElementById (div1) != null ? document.getElementById (div1).style.display = 'block' : '';
        getId (search1) != null ? document.getElementById (div2).innerHTML = html : '';
        getId (search2) != null ? document.getElementById (div3).innerHTML = html : '';
    }
}

function getId (div)
{
    var inputs = document.getElementsByTagName ('input');
    for (i=0; i<inputs.length; i++)
    {
        if ((inputs[i].id).indexOf (div) != -1)
        {
            return inputs[i].id;
        } 
    } 
    return null;
}


        
function getSelectedItem (id)
{
    var select = document.getElementById (id);
    if (select != null)
    {
        return document.getElementById(id)[document.getElementById(id).selectedIndex].value;
    }
    else
        return '#';
}



function SimulateClick (id)
{
    href = getSelectedItem (id);
    
    document.forms[0].setAttribute ('action', href);
    document.forms[0].submit ();
}


function applyParameters(id, params)
{
    key = params[0];
    value = params[1];
    
    if (key == "className"){document.getElementById (id).className = value; return;}
    if (key == "backgroundColor"){document.getElementById (id).style.backgroundColor = value; return;}
    if (key == "innerHTML"){document.getElementById (id).innerHTML = value; return;}
    if (key == "position"){document.getElementById (id).style.position = value; return;}
    if (key == "right"){document.getElementById (id).style.right = value; return;}
    if (key == "left"){document.getElementById (id).style.left = value; return;}
    if (key == "top"){document.getElementById (id).style.left = value; return;}
    if (key == "bottom"){document.getElementById (id).style.bottom = value; return;}
    if (key == "color"){document.getElementById (id).color = value; return;}
    if (key == "parentNode.innerHTML"){ document.getElementById (id).parentNode.innerHTML = value + findParameter (id, key) + params[2] ; return;}
    if (key == "onclick"){document.getElementById (id).onclick = value; return;}
    else
        return;
}

function getParentNodeContent (id)
{
    
}

function findParameter(id, key)
{
    if (key == "className"){return document.getElementById (id).className;}
    if (key == "backgroundColor"){return document.getElementById (id).style.backgroundColor;}
    if (key == "innerHTML"){return document.getElementById (id).innerHTML;}
    if (key == "position"){return document.getElementById (id).style.position;}
    if (key == "right"){return document.getElementById (id).style.right;}
    if (key == "left"){return document.getElementById (id).style.left;}
    if (key == "top"){return document.getElementById (id).style.left;}
    if (key == "bottom"){return document.getElementById (id).style.bottom;}
    if (key == "color"){return document.getElementById (id).color;}
    if (key == "parentNode.innerHTML"){return document.getElementById (id).parentNode.innerHTML;}
    else
        return;
}

function getValue(tagType, contained, key)
{
    var tab = document.getElementsByTagName (tagType);
    for (i=0; i<tab.length; i++)
    {
        if ((tab[i].id).indexOf (contained) != -1)
        {
            //alert (findParameter (tab[i].id, key));
            return findParameter (tab[i].id, key);
        }                
    }
}

function setParameters (tagType, contained, params)
{
    var tab = document.getElementsByTagName (tagType);
    for (i=0; i<tab.length; i++)
    {
        if ((tab[i].id).indexOf (contained) != -1)
        {
            for (j = 0; j< params.length; j++)
            {
                applyParameters (tab[i].id, params[j]);
            }
        }                
    }
}

function showElements(array)
{
    for (i=0; i<array.length;i++)
    {
        if(document.getElementById(array[i]) != null)
            document.getElementById(array[i]).style.display='block';
    }
}

function hideElements(array)
{
    for (i=0; i<array.length;i++)
    {
        if(document.getElementById(array[i]) != null)
            document.getElementById(array[i]).style.display='none';
    }
}
