var SiteURL = "http://www.freemovies365.com/";
function watchit(id) {

    var c = window.open(SiteURL + 'watch-movies.aspx?id=' + id, '', 'height=600,width=950,left = 30,top = 50,resizable=yes,scrollbars=1');

}
var UT_RATING_IMG = new Image();
UT_RATING_IMG.src =
SiteURL + 'images/ico/star1.gif';
var UT_RATING_IMG_BG = new Image();
UT_RATING_IMG_BG.src = SiteURL + 'images/ico/star2.gif';


var starTimer = null;

function checkReviewForm(signin) {
    if (signin == 0) {
        alert("You need to sign in to post a comment."); return false;
    }
    else {
        var code = $("input[id*=Code]");
        if (code.val() == "" || code.val().length != 4) {
            alert("Code required."); 
            return false;
        }
        return true;
    }
}

function clearReviewForm() {
    $(".inputText").val("");
}

function showStars(starNum, skipMessageUpdate) {

    clearStarTimer();
    greyStars();
    colorStars(starNum);
    if (!skipMessageUpdate)
        setMessage(starNum);

}

function setMessage(starNum) {
    messages = new Array("Rate this movie ", "Poor", "Nothing special", "Worth watching", "Pretty cool", "Awesome!");
    document.getElementById('ratingtext').innerHTML = messages[starNum];
}

function colorStars(starNum) {
    for (var i = 0; i < starNum; i++)
        document.getElementById('star' + (i + 1)).src = UT_RATING_IMG.src;
}

function greyStars() {
    for (var i = 0; i < 5; i++)
        if (i <= currentRating)
        document.getElementById('star' + (i + 1)).src = UT_RATING_IMG_BG.src;
    else
        document.getElementById('star' + (i + 1)).src = UT_RATING_IMG_BG.src;
}

function setStars(starNum) {
    currentRating = starNum;
    drawStars(starNum);
    document.forms[formName]['rating'].value = currentRating;
    var ratingElementId = ratingElementId;
    postForm(formName, true, function(req) { replaceDivContents(req, ratingElementId); });
}

function drawStars(starNum, skipMessageUpdate) {
    currentRating = starNum;
    showStars(starNum, skipMessageUpdate);
}

function clearStars() {
    starTimer = setTimeout("resetStars()", currentRating);
}

function resetStars() {
    clearStarTimer();
    if (currentRating)
        drawStars(currentRating);
    else
        greyStars();
    setMessage(0);
}

function clearStarTimer() {

    if (starTimer) {
        clearTimeout(starTimer);
        starTimer = null;
    }
}


function submitRating(r) {

    var movieid = document.frmrating.movieid.value;
    var url = SiteURL + 'rating-movies.aspx?movieid=' + movieid + '&rating=' + r +'&rnd=' + Math.random();

    sendRequest(url, '', 'GET');
}
function submitLinkRating(r, id) {

    var movieid = document.frmrating.movieid.value;
    var url = SiteURL + 'rating-movies.aspx?movieid=' + movieid + '&linkrating=' + r + '&id=' + id + '&rnd=' + Math.random();

    sendRequest(url, '', 'GET');

    return false;
}


function getXMLHTTPRequest() {
    var xRequest = null;
    if (window.XMLHttpRequest) {
        xRequest = new XMLHttpRequest();
    } else if (typeof ActiveXObject != "undefined") {
        xRequest = new ActiveXObject
("Microsoft.XMLHTTP");
    }
    return xRequest;
}

var READY_STATE_UNINITIALIZED = 0;
var READY_STATE_LOADING = 1;
var READY_STATE_LOADED = 2;
var READY_STATE_INTERACTIVE = 3;
var READY_STATE_COMPLETE = 4;
var req;
function sendRequest(url, params, HttpMethod) {
    if (!HttpMethod) {
        HttpMethod = "GET";
    }
    req = getXMLHTTPRequest();
    if (req) {
        req.onreadystatechange = onReadyStateChange;
        req.open(HttpMethod, url, true);
        req.setRequestHeader
("Content-Type", "application/x-www-form-urlencoded");
        req.send(params);
    }
}

function onReadyStateChange() {
    var ready = req.readyState;
    var data = null;
    if (ready == READY_STATE_COMPLETE) {
        ParseRating(req.responseText);
    } else {

    }

}

var ratingTextId = 'ratingTextId';
var votesId = 'votesId';
var vlinkId = 'vlink';

function ParseRating(txt) {

    var arr = txt.split(";");

    if (arr.length == 3 && arr[0] == 'rating') {

        currentRating = parseInt(arr[1]);


        if (currentRating < 0 || currentRating > 5) {
            currentRating = 0;
        }


        if (document.all) {

            document.getElementById(votesId).innerText = arr[2];
            document.getElementById(ratingTextId).innerText = currentRating;
        }
        else {

            document.getElementById(votesId).textContent = arr[2];
            document.getElementById(ratingTextId).textContent = currentRating;
        }
        resetStars();
    }
    if (arr.length == 3 && arr[0] == 'link') {

        percent = parseInt(arr[1]);

        var color = "";

        var html = "";
        if (percent > 50) {
            color = "red";
            html = percent + "% say not working";
        }
        else {
            percent = 100 - percent;
            html = percent + "% say working";
            color = "green";
        }

        if (document.all) {

            document.getElementById(vlinkId + arr[2]).innerText = html;
            document.getElementById(vlinkId + arr[2]).style.color = color;
        }
        else {

            document.getElementById(vlinkId + arr[2]).textContent = html;
            document.getElementById(vlinkId + arr[2]).style.color = color;
        }
        resetStars();
    }


}

function CommentTitleClick(item) { 
   if($(item).val() == "Title")
      $(item).val("");
}
function CommentClick(item) { 
   if($(item).val() == "Write your comment here" || $(item).val() =="Type in code" )
      $(item).val("");
}