var difference = 0;
var top = "0px";
var logged_in = false;
var action;

function ratingBox()
{
	action = "rating"
	sndReqLogin();
}

function doAction()
{
	if(action == "rating")
	{
		if(logged_in)
		{
			showRating();
		}
		else
		{
			showLogIn();
		}
	}
}

function showLogIn()
{
	var box = document.getElementById("dvLogIn")
	box.style.display = "inline"
	//var left = Number(findPosition())+
	//box.style.left = findPosition()+"px"
	//box.style.top = top;
}


function showRating()
{
	var box = document.getElementById("dvRate")
	box.style.display = "inline"
	//box.style.left = findPosition()+"px"
	//box.style.top = top;
}

function DisplayRating(star)
{
	for(i=1;i<=star;i++)
	{
		var img=document.getElementById("rate"+i);
		img.src = "/tillys/images/bluestar.gif";
	}
}

function ClearRating()
{
	for(i=1;i<6;i++)
	{
		var img=document.getElementById("rate"+i);
		img.src = "/tillys/images/graystar.gif";
	}
}

function closeLogIn()
{
	var box = document.getElementById("dvLogIn")
	box.style.display = "none"
}

function closeRating()
{
	var box = document.getElementById("dvRate")
	box.style.display = "none"
}

function Rate(rating)
{
	var action = "?prod="+PrdName+"&rating="+rating;
	sndReqRate(action);
}

function updateRating(votes, rating)
{
	dvVotes = document.getElementById("lblVotes");
	if(votes == 1)
	{
	    dvVotes.innerHTML = "(1 vote)";
	}
	else
	{
	    dvVotes.innerHTML = "(" + votes + " votes)";
    }
	for(i=1;i<=5;i++)
	{
		var img=document.getElementById("star"+i);
		if(rating >= 0.75)
		{
			img.src = "/tillys/images/bluestar.gif";
		}
		else if(rating >= 0.25)
		{
			img.src = "/tillys/images/halfstar.gif";
		}
		else
		{
			img.src = "/tillys/images/graystar.gif";
		}
		rating--;
    }
    try {
        lblTest = document.getElementById("lblRate");
        lblTest.innerHTML = "rate this item";
    }
    catch (e) {
    }
}

function sndReqLogin() {
    http.open('get', '/tillys/checklogin.aspx');
    http.onreadystatechange = handleResponseLogin;
    http.send(null);
}

function handleResponseLogin() 
{
    if(http.readyState == 4)
    {
        var response = http.responseText
		logged_in = response == "1";
		doAction()
	}
}

function login() {
    var err = document.getElementById("errMsg")
    if (isValidEmail()) {
        var myDate = new Date()
        myDate.setYear(myDate.getFullYear() + 1);
        document.cookie =
            'email=' + document.Form1.txtUserName.value + '; expires=' + myDate.toLocaleString() + '; path=/'
        err.innerHTML = "";
        closeLogIn();
        showRating();
    }
    else {
        var err = document.getElementById("errMsg")
        err.innerHTML = "Invalid email address";
    }
}

function isValidEmail() {
    var str = document.Form1.txtUserName.value;

    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0) && (str.length > 6);

}


function sndReqRate(action) {
    http.open('get', '/tillys/rateitem.aspx'+action);
    http.onreadystatechange = handleResponseRate;
    http.send(null);
}

function handleResponseRate() 
{
    if(http.readyState == 4)
    {
        var response = http.responseText.split("|")
		var dv = document.getElementById("dvRateText");
		switch (response[0])
		{
			case "0":
				dv.innerHTML = "<div style=\"color: red:\">There was an error when attempting to process this request</div>"+dv.InnerHtml;
				break;
			case "1":
				dv.innerHTML = "Thank you!";
				updateRating(response[1],response[2])
				setTimeout("closeRating()",2000);
				break;
			case "2":
				closeRating();
				showLogIn();
				break;		
		}
	}
}