﻿// JScript File

function btnLocSearchClick() 
{
    if (document.getElementById("location").selectedIndex > 0)
    {
        Home.FilterLocation(document.getElementById("location").options[document.getElementById("location").selectedIndex].value, btnLocSearchClick_CallBack);
    }
    else
    {
        alert('Please select your city from the list.', 'SayIDoDiamond.com');
    }
}

function btnLocSearchClick_CallBack(response) 
{
    //if the server side code threw an exception
	if (response.error != null)
	{    
		alert("Error occurred in btnLocSearchClick\n" + response.error.Message);
		return;
	}

	for (var i = 0; i < response.value.Tables.length; ++i)
	{
	    var drop = document.getElementById(response.value.Tables[i].Columns[0].Name);
	    if (response.value.Tables[i].Columns[0].Name !="carat")
        {
            for (var j = 0; j < response.value.Tables[i].Rows.length; ++j)
	        {
	            drop.options[drop.options.length] = new Option(response.value.Tables[i].Rows[j].value, response.value.Tables[i].Rows[j].value);
	        }   
        }
        else
        {
            var low = 0.00;
            var high = 0.25;
            var x = 0;
            for (var j = 0; j < response.value.Tables[i].Rows.length; ++j)
            {
                while (response.value.Tables[i].Rows[j].value > high)
                {
                    low = high + 0.01;
                    high = high + 0.25;
                    x++;
                }
                if (response.value.Tables[i].Rows[j].value >= low && response.value.Tables[i].Rows[j].value<= high)
                {
                    var item = new Option(low.toFixed(2) + ' - ' + high.toFixed(2), x);

                    if(contains(drop, item) == false)
                    {
                        drop.options[drop.options.length] = item;
                    }
                }
            }
        }
	}
    document.getElementById("lblLocation").innerHTML = document.getElementById("location").options[document.getElementById("location").selectedIndex].text;
    document.getElementById("location").style.display="none";
    document.getElementById("btnLocSearch").style.display="none";
    document.getElementById("searchBar").style.display="block";
    document.getElementById("locationBar").style.zindex=-5;
    document.getElementById("locationInfo").style.display="block";
}
function contains (drop, item)
{
    var contain = false;
    for(var x = 0; x< drop.options.length; ++x)
    {
        if(drop.options[x].text == item.text)
        {
            contain = true;
        }
    }
    return contain;
}

function btnSearchClick()
{
    var drops = new Array("cut", "color", "clarity", "carat", "shape");
    var queryString = "Search.aspx?";
    var items = 0;
    for (var i = 0; i < drops.length; ++i)
    {
        var drop = document.getElementById(drops[i]);
        if (drop.options[drop.selectedIndex].value != drops[i])
        {
                queryString = queryString + "&";
                queryString = queryString + drops[i];
                queryString = queryString + "=";
                queryString = queryString + drop.options[drop.selectedIndex].value;
                ++items;
        }
    }
    queryString = queryString + "&city=";
    queryString = queryString + document.getElementById("location").options[document.getElementById("location").selectedIndex].text.replace(", ", ","); //.substring(0, document.getElementById("location").options[document.getElementById("location").selectedIndex].text.indexOf(','));
    location.href = queryString;
}

function txtSignup_Click()
{
    var txtSignup = document.getElementById("txtSignup");
    
    // If this is first click, clear text box
    if(txtSignup.value == "enter email address") txtSignup.value = "";
    
}

