var SendFriendEmailUrl  = "actions/sendFriendEmail.php";
var votePhpScript	= "actions/ajaxVote.php";
var ClearChatUrl 	= "actions/clearChat.php";
var viewlm10Url 	= "actions/viewlm10.php";
var viewlive10Url 	= "actions/viewlive10.php";
var viewbot5Url		= "actions/viewbot5.php";

var httpSendFriendEmail = getHTTPObject();
var httpClearChat 	= getHTTPObject();
var httpViewLast10	= getHTTPObject();
var httpViewMost10	= getHTTPObject();
var httpViewLive10	= getHTTPObject();
var httpViewBot5	= getHTTPObject();
var request_vote;

//TELL A FRIEND
function tellfriendEmail(emailfrom,emailto,url,folder) {
        if (emailfrom != '' & emailto != '' &(httpSendFriendEmail.readyState == 4 || httpSendFriendEmail.readyState == 0)) {
                param = 'emailto='+ emailto+'&url='+escape(url)+'&emailfrom='+emailfrom+'&folder='+folder;
                httpSendFriendEmail.open("POST", SendFriendEmailUrl, true);
                httpSendFriendEmail.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                httpSendFriendEmail.onreadystatechange = handlehHttpSendFriendEmail;
                httpSendFriendEmail.send(param);
                document.forms['tellafriend'].elements['friendemail'].value = '';
                overlayclose('subcontent');
        }
}
function handlehHttpSendFriendEmail() {
    if (httpSendFriendEmail.readyState == 4) {
        var resp = httpSendFriendEmail.responseText;
        if(resp==1)
            alert('Email couldn\'t be sent, Please specify valid email address.');
        else
            alert('Email Sent!');
    }
}

//CLEAR CHAT
function clearChat(login,password,videoid,camtype) {
	if (login != '' & password!= '' & (httpClearChat.readyState == 4 || httpClearChat.readyState == 0)) {
		param = 'login='+ login + '&password=' + password +'&videoid='+videoid+'&camtype='+camtype;	
		httpClearChat.open("POST", ClearChatUrl, true);
		httpClearChat.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  		httpClearChat.onreadystatechange = handlehHttpClearChat;
  		httpClearChat.send(param);
		document.forms['clearchat'].elements['clearlogin'].value = '';
		document.forms['clearchat'].elements['clearpassword'].value = '';
		overlayclose('subcontent2');
	} 
}
function handlehHttpClearChat() {
    if (httpClearChat.readyState == 4) {
	var resp = httpClearChat.responseText;	
	if(resp==1)
	    alert('Invalid login name or password!');
	else{
	    main = frames['chatframe'].document.getElementById("outputList");
	    for (i=main.childNodes.length-2; i>=0; i--){
		if(main.childNodes[i].id!='lastnode')
		    main.removeChild(main.childNodes[i]);
	    }
		
	    alert('Chat has been cleaned!');
	}
    }
}



// VOTE
function handleReadyStateChangeVote() {
   if (!request_vote) return;
   if (request_vote.readyState == 4) {
      if (request_vote.status == 200) {
         var text = request_vote.responseText;
         request_vote = false;
         processResponseVote(text);
      }
    }
}

function processResponseVote(text)
{
        var m = text.split(',');
        if ( m[0] == 0 ) {
                alert("Your vote has been accepted");
                renderRateStars(m[1], m[2]);
        }
        else if ( m[0] == 2 ) alert("You have already voted for this video");
        else alert("Error accoured while voting please try again");
        var button = document.getElementById('send_vote_button');
        button.disabled = false;
}

function sendVote(videoid,camtype)
{
        var button = document.getElementById('send_vote_button');
        button.disabled = true;
        var rate = document.getElementById('vote_rate');
        rate = rate.options[rate.selectedIndex].value;
        request_vote = getHTTPObject();
        request_vote.onreadystatechange = handleReadyStateChangeVote;
        request_vote.open('get',votePhpScript+'?videoid='+videoid+'&rate='+rate+'&camtype='+camtype);
        request_vote.send(null);
}

function renderRateStars(votes, rate)
{
        var i, tmp = "";
        tmp += "Average("+votes+" Votes):";
        for(i = 1; i <= Math.floor(rate); i++)       tmp +="<img src=\"images/icons/star_on.gif\" width=\"19\" height=\"20\" alt=\"\" align=\"absmiddle\">";
        for(i = 1; i <= (5 - Math.floor(rate)); i++) tmp +="<img src=\"images/icons/star_off.gif\" width=\"19\" height=\"20\" alt=\"\" align=\"absmiddle\">";
         document.getElementById("div_rate_stars").innerHTML = tmp;
}


//Last Most 10
function viewlm10(lm,index) {
    if(lm=='last10_box'){
        if (httpViewLast10!=null) {
                param = 'lm='+lm+'&last10index='+index;
                document.getElementById('last10_box').innerHTML='<table  cellpadding="0" width="100%" cellspacing="0" align="center" border="0"><tr><td height="143" align="center" valign="center"><img src="images/icons/loading.gif" align="center"> Loading ...</td></tr></table>';
                httpViewLast10.open("POST", viewlm10Url, true);
                httpViewLast10.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                httpViewLast10.onreadystatechange = handleViewLast10;
                httpViewLast10.send(param);
        }
    }
    if(lm=='most10_box'){
        if (httpViewMost10!=null) {
                param = 'lm='+lm+'&most10index='+index;
                document.getElementById('most10_box').innerHTML='<table  cellpadding="0" width="100%" cellspacing="0" align="center" border="0"><tr><td height="143" align="center" valign="center"><img src="images/icons/loading.gif" align="center"> Loading ...</td></tr></table>';
                httpViewMost10.open("POST", viewlm10Url, true);
                httpViewMost10.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                httpViewMost10.onreadystatechange = handleViewMost10;
                httpViewMost10.send(param);
        }
    }
}
function handleViewLast10() {
    if (httpViewLast10.readyState == 4 && httpViewLast10.status == 200) {
        var resp = httpViewLast10.responseText;
        var resparr = resp.split('|spl|');
	document.getElementById(resparr[0]).innerHTML = resparr[1];
    }
}
function handleViewMost10() {
    if (httpViewMost10.readyState == 4 && httpViewMost10.status == 200) {
        var resp = httpViewMost10.responseText;
        var resparr = resp.split('|spl|');
	document.getElementById(resparr[0]).innerHTML = resparr[1];
    }
}

//Live 10
function viewlive10(lm,index) {
    if (httpViewLive10!=null) {
                param = 'lm='+lm+'&live10index='+index;
                document.getElementById('live10_box').innerHTML='<table  cellpadding="0" width="100%" cellspacing="0" align="center" border="0"><tr><td height="143" align="center" valign="center"><img src="images/icons/loading.gif" align="center"> Loading ...</td></tr></table>';
                httpViewLive10.open("POST", viewlive10Url, true);
                httpViewLive10.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                httpViewLive10.onreadystatechange = handleViewLive10;
                httpViewLive10.send(param);
    }
}
function handleViewLive10() {
    if (httpViewLive10.readyState == 4 && httpViewLive10.status == 200) {
        var resp = httpViewLive10.responseText;
	document.getElementById('live10_box').innerHTML = resp;
    }
}


// View Bot 5
function viewbot5(index,camuser,curvideo,camuserid,camuserhome,camuserurl) {
    if (httpViewBot5!=null) {
                param = 'bot5index='+index+'&camuser='+camuser+'&uservideocur='+curvideo+'&camuserid='+camuserid+'&camuserhome='+camuserhome+'&camuserurl='+camuserurl;
                document.getElementById('bot5_box').innerHTML='<table  cellpadding="0" width="100%" cellspacing="0" align="center" border="0"><tr><td height="229" align="center" valign="center"><img src="images/icons/loading.gif" align="center"> Loading ...</td></tr></table>';
                httpViewBot5.open("POST", viewbot5Url, true);
                httpViewBot5.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                httpViewBot5.onreadystatechange = handleViewBot5;
                httpViewBot5.send(param);
    }
}
function handleViewBot5() {
    if (httpViewBot5.readyState == 4 && httpViewBot5.status == 200) {
        var resp = httpViewBot5.responseText;
	document.getElementById('bot5_box').innerHTML = resp;
    }
}


//-----------------------------------------------------------------------------------------------//

function getHTTPObject() {
    var httpRequest;

    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
    //        httpRequest.overrideMimeType('text/xml');
            httpRequest.overrideMimeType('text/html');
            // See note below about this line
        }
    } 
    else if (window.ActiveXObject) { // IE
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
    	    try {
               httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	    } 
    	    catch (e) {
    		httpRequest = false;
    	    }
        }
    }
    
    return httpRequest;
}

