//Variable used to stop processData function if resizeIframe is called
var resizeIframeCalled = false;

//Functions for old PSF Grids using onload="resizeIframe('pricegrid')"
function resizeIframe(frameid)
{
    resizeIframeCalled = true;
    replaceContent('fromPrices',frameid);
    var currentfr=document.getElementById(frameid);
    var ddlLoc;
    if (currentfr)
    {
        currentfr.style.visibility = "visible";
        currentfr.style.display = "block";
        if (currentfr.Document && currentfr.Document.body.scrollHeight) //ie5+ syntax
        {
            currentfr.height = currentfr.Document.body.scrollHeight;
            ddlLoc = currentfr.Document.getElementById('ctl00_MainContent_ddlLocation');
            if (!ddlLoc)
            {
                ddlLoc = currentfr.Document.getElementById('ddlLocation');
            }
        }
        else if (currentfr.contentDocument && currentfr.contentDocument.body.scrollHeight) //ns6+ / opera syntax
        {
            currentfr.height = currentfr.contentDocument.body.scrollHeight + 16;
            ddlLoc = currentfr.contentDocument.getElementById('ctl00_MainContent_ddlLocation');
            if (!ddlLoc)
            {
                ddlLoc = currentfr.contentDocument.getElementById('ddlLocation');
            }
        }
        else if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) //standards compliant syntax - probably nothing will drop through to this
        {
            currentfr.height = currentfr.contentDocument.body.offsetHeight + 16;
            ddlLoc = currentfr.contentDocument.getElementById('ctl00_MainContent_ddlLocation');
            if (!ddlLoc)
            {
                ddlLoc = currentfr.contentDocument.getElementById('ddlLocation');
            }
        }

        //Fix problem in Opera browser
        if (currentfr.contentDocument) {
            currentfr.height = currentfr.contentDocument.body.offsetHeight + 16;
        } else {
            currentfr.height = currentfr.contentWindow.document.body.scrollHeight;
        }
        //End of Opera Fix

        if (ddlLoc && typeof(ddlLoc) != 'undefined' && typeof(onGridUpdate) == 'function')
        {
            onGridUpdate(getLocation(ddlLoc));
        }
    }
}

function replaceContent(elId, frameid)
{
    var replaceAry = [{
        objectId:'ctl00_MainContent_hdnPrice1',
        replaceString:'[PRICE1]'
    },{
        objectId:'ctl00_MainContent_hdnPrice2',
        replaceString:'[PRICE2]'
    }];
    var el = document.getElementById(elId);
    if (el == null)
    {
        return;
    }
    var currentfr=document.getElementById(frameid);
    var html = el.innerHTML;
    var input = null;
    for (var i=0;i<replaceAry.length;i++)
    {
        input = null;
        if (html.indexOf(replaceAry[i].replaceString) >= 0)
        {
            if (currentfr.contentDocument) //ns6 syntax
            {
                input = currentfr.contentDocument.getElementById(replaceAry[i].objectId);
            }
            else if (currentfr.Document) //ie5+ syntax
            {
                input = currentfr.Document.getElementById(replaceAry[i].objectId);
            }
            if (input != null && input.value != "0" && input.value != "99999")
            {
                html = html.replace(replaceAry[i].replaceString, input.value);
            }
        }
    }
    if (html != el.innerHTML)
    {
        el.innerHTML = html;
    }
    el.style.display = 'block';
}

function getLocation(ddl)
{
    if (ddl.selectedIndex < 1)
    {
        return "";
    }
    else
    {
        var txt = ddl.options[ddl.selectedIndex].text;
        if (txt.indexOf(' (') > 0)
        {
            txt = txt.substring(0, txt.indexOf('(') - 1);
        }
        if (txt.indexOf(', ') > 0)
        {
            txt = txt.substring(0, txt.indexOf(', '));
        }
        while (txt.charCodeAt(0) == 160)
        {
            txt = txt.substr(1);
        }
        return txt;
    }
}

function setLocation(f)
{
    var ddlLoc;
    var currentfr = document.getElementById(f);
    if (currentfr.contentDocument) //ns6+ / opera syntax
    {
        ddlLoc = currentfr.contentDocument.getElementById('ctl00_MainContent_ddlLocation');
        if (!ddlLoc)
        {
            ddlLoc = currentfr.contentDocument.getElementById('ddlLocation');
        }
    }
    else if (currentfr.Document) //ie5+ syntax
    {
        ddlLoc = currentfr.Document.getElementById('ctl00_MainContent_ddlLocation');
        if (!ddlLoc)
        {
            ddlLoc = currentfr.Document.getElementById('ddlLocation');
        }
    }
    if (ddlLoc && typeof(ddlLoc) != 'undefined' && typeof(onGridUpdate) == 'function')
    {
        onGridUpdate(getLocation(ddlLoc));
    }
}

//Functions for new PSF Grids called from xmlgrid_proxy.html located in daily_us\common
function processData(args)
{
	try
	{
		if (!resizeIframeCalled) //If resizeIframe is called onload in iFrame skip this process
		{
			var h = parseInt(args.height);
			var p1 = parseInt(args.price1);
			var p2 = parseInt(args.price2);
			var loc = args.location;
			var iframe = document.getElementById('pricegrid');
			iframe.style.visibility = 'visible';
			iframe.style.display = 'block';
			iframe.style.height = h + 'px';
			sendAdCallData(loc);
			sendFromPrices('fromPrices', p1, p2);
		}		
	}
	catch (e)
	{
		window.status = e.description;
	}
}

function sendAdCallData(data)
{
	try
	{
		if (data != '' && typeof(onGridUpdate) == 'function')
		{
			onGridUpdate(data);
		}		
	}
	catch (e)
	{
		window.status = e.description;
	}
}

function sendFromPrices(elId, p1, p2)
{
	try
	{
		var replaceAry = [{
			price:p1,
			replaceString:'[PRICE1]'
		},{
			price:p2,
			replaceString:'[PRICE2]'
		}];
		var el = document.getElementById(elId);
		if (el == null)
		{
			return;
		}
		var html = el.innerHTML;
		var input = null;
		for (var i=0;i<replaceAry.length;i++)
		{
			input = null;
			if (html.indexOf(replaceAry[i].replaceString) >= 0)
			{
				
				if (replaceAry[i].price != null && replaceAry[i].price != "0" && replaceAry[i].price != "99999")
				{
					html = html.replace(replaceAry[i].replaceString, replaceAry[i].price);
				}
			}
		}
		if (html != el.innerHTML)
		{
			el.innerHTML = html;
		}
		el.style.display = 'block';		
	}
	catch (e)
	{
		window.status = e.description;
	}
}
