//Ajax functions
//Returns the node text value

var g_euga_locale;
var g_loading_label;
var g_ajaxServer;
var g_euga_searchId;

function SetLoadingLabel(s) {g_loading_label = s;}
function SetSearchButtonId(id) {g_euga_searchId  = id;}
function SetEugaLocale(locale) { g_euga_locale = locale; }
function SetAjaxServer(s) { g_ajaxServer = s;}

function eugaSearch()
{
	document.getElementById(g_euga_searchId).click();
}

function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

function ToggleDropDown(i)
{
    var list = document.getElementById('dropDown_List' + i);
    var main = document.getElementById('dropDown_Main' + i);

    if (list != null && main != null)
    {
        list.style.top = parseInt(main.offsetTop, 10);

        if (list.style.visibility == 'visible')
            list.style.visibility = 'hidden';
        else
        {
			main.style.visibility = 'hidden';
           list.style.visibility = 'visible';
           list.focus();
        }
    }
}

function HideDropDown(o, i)
{
//alert("O:"+o.type+"</BR>");
//alert("O:"+o.id+"</BR>");
//alert("O:"+i+"</BR>");
    var main = document.getElementById('dropDown_Main'+i);
//alert ("main=dropDown_Main"+i);
    main.style.visibility = 'visible';

    if (o != null)
    {
//alert ("o != null");
        if (o.style != null)
        {
//alert ("o style != null");
            o.style.visibility = 'hidden';
        }
        else
        {
//alert ("o  style = null");
        }
    }
    else 
    {
//alert ("o = null");
    }
}

function GetTextValue(object)
{
	if(document.all)
	{
		return object.innerText;
	}
	else
	{
		return object.textContent;
	}
}

function ItemSelected(o, i)
{
    var txt = document.getElementById('dropDown_Main_Text'+i);
    var val = document.getElementById('dropDown_Main_Value'+i);

    var list = o.parentNode;

    for(var l=0;l<list.childNodes.length;l++)
    {
		list.childNodes[l].style.fontWeight='normal';
    }

    txt.innerHTML = GetTextValue(o);


    o.style.fontWeight='bold';
    val.value = o.getAttribute('val');

    HideDropDown(o.parentNode.parentNode, i);
}

function rollover(o)
{
    o.className="selected";
}

function rollout(o)
{

   o.className="";
}


function getSelectedLocation()
{
		var value='';
		for(l=1;l<10;l++)
		{
			var v = document.getElementById('dropDown_Main_Value' + l)
			if(v!=null)
			{
				if(v.value.length>0)  value = v.value;
			}
		}
		return value;
}

function AjaxGetSummary(o)
{

	var value = o.getAttribute('val');
	var text = o.innerHTML;
	var countryId = document.getElementById('dropDown_Main_ValueddlCountry').value;
	var businessTypeId = document.getElementById('dropDown_Main_ValueddlBusinessType').value;
	var osstitle = document.getElementById('schemeSummaryTitle');

	if(value==0)
	{
		value = getSelectedLocation();

		if(value.length==0)
		{
			AjaxGetSummaryByCountry(countryId);
			return;
		}
	}

	if(value.length > 0)
	{
		//if(osstitle!=null) osstitle.innerHTML = osstitle.innerHTML + " &gt; <span style='color:#ff6736;'> " + text + " </span>";

		if(document.getElementById("summary")!=null) {
			loadHtml2(g_ajaxServer + '?m=GetSummary&locale=' + g_euga_locale + '&businessTypeId='+ businessTypeId + '&countryId=' + countryId + '&locationId=' + value + '&uq' + new Date().getTime(), document.getElementById("summary"));
		}
	}

}

function AjaxGetSummaryByBusinessType(o)
{
	var locationId = getSelectedLocation();
	var countryId = document.getElementById('dropDown_Main_ValueddlCountry').value;

	if(document.getElementById("summary")!=null) {
		loadHtml2(g_ajaxServer + '?m=GetSummary&locale=' + g_euga_locale + '&countryId=' + countryId + '&businessTypeId='+ o.getAttribute('val') + '&locationId=' + locationId + '&uq' + new Date().getTime(), document.getElementById("summary"));
	}
}

function AjaxGetSummaryByCountry(cntid)
{
	var businessTypeId = document.getElementById('dropDown_Main_ValueddlBusinessType').value;

	if(document.getElementById("summary")!=null) {
		loadHtml2(g_ajaxServer + '?m=GetSummary&locale=' + g_euga_locale + '&businessTypeId='+ businessTypeId + '&countryId=' + cntid + '&uq' + new Date().getTime(), document.getElementById("summary"));
	}
}


function AjaxGetLocationChildren(o, targetId)
{

	if(document.getElementById('spanLocationLevel' + targetId)!=null)
	{
	    if(o.getAttribute('val').length>0)
	    {
	        var targetVal = document.getElementById('dropDown_Main_Value' + targetId);
	        var targetText = document.getElementById('dropDown_Main_Text' + targetId);

	        //targetText.className='populating';


	        targetText.innerText=g_loading_label;
	        loadHtml(g_ajaxServer + '?targetId=' + targetId + '&m=GetLocationChildren&locale=' + g_euga_locale +  '&locationId=' + o.getAttribute('val') + '&uq' + new Date().getTime(), document.getElementById('spanLocationLevel' + targetId));
	    }
	}

}

function AjaxBuildLocationPanel(o, targetPanelId)
{
	var panel = document.getElementById(targetPanelId);

	loadHtml(g_ajaxServer + '?m=GetLocationPanel&locale=' + g_euga_locale + '&countryId=' + o.getAttribute('val') + '&uq' + new Date().getTime(), panel);
}

function ResetSubLocations(i)
{
    for(var start = i+1; start<=6; start++)
    {
        var subText = document.getElementById('dropDown_Main_Text' + start)

        var o = document.getElementById('dropDown_Main' + start)

        if(subText!=null)
        {
            var subValue = document.getElementById('dropDown_Main_Value' + start)
            o.onclick='';
            subText.style.background='';
            subText.innerHTML=subText.getAttribute('ev');
            subValue.innerText = '';
        }
        else
        {
            break;
        }
    }
}

function ev(o, v)
{
    if(o.value==v) o.value='';
    o.className = 'textbox';
}


function validateVertical(oSrc, args)
{
	var emptyValue = document.getElementById('dropDown_ListddlVerticalIndustry').childNodes(0).childNodes(0).getAttribute('val');
	var currentValue = document.getElementById('dropDown_Main_ValueddlVerticalIndustry').value;

	if(currentValue==emptyValue || currentValue.length==0)
		args.IsValid = false;
	else
		args.IsValid = true;
}



function validateFundingType(oSrc, args)
{
	var currentValue = document.getElementById('dropDown_Main_ValueddlFunding').value;

	if(currentValue.length==0)
		args.IsValid = false;
	else
		args.IsValid = true;
}


function validateProjectType(oSrc, args)
{
	var currentValue = document.getElementById('dropDown_Main_ValueddlProjectType').value;

	if(currentValue.length==0)
		args.IsValid = false;
	else
		args.IsValid = true;
}

function validateYearsInBusiness(oSrc, args)
{
	var currentValue = document.getElementById('dropDown_Main_ValueddlYearsInBusiness').value;

	if(currentValue.length==0)
		args.IsValid = false;
	else
		args.IsValid = true;
}

function toggleSearch()
{
	toggleItem('SaveSearchPanel');
}

/* A more generic tool in case we want to re-use it later*/
function toggleItem(item)
{
	var o = document.getElementById(item);

	if (o==null)
		return

	if (o.style.visibility=='hidden')
	{
		o.style.position='relative';
		o.style.visibility='visible';
	}
	else
	{
		o.style.position='absolute';
		o.style.visibility='hidden';
	}
}
