﻿// JScript File

var popup;

function OpenPopup(videoId)
{
    var url = 'http://www.facebook.com/sharer.php?u=' +
        encodeURIComponent("http://www.irannegah.com/Video.aspx?id=" + videoId);
        //encodeURIComponent("http://revver.com/watch/" + videoId + "/");
        //encodeURIComponent(currentUrl) +
        //'&t=' + encodeURIComponent(currentTitle);
    
    if(popup != null)
    {
        popup.close();
    }
    
    popup = window.open(url, 'facebook','toolbar=0,status=0,width=686,height=436');

}



function CheckCommentForm()
{

    with(document.form1)
    {
        if(!CheckMandatory(textName, "Please input your name"))
        {
            KillEvent(event);
            return false;
        }

        if(!CheckMandatory(textEmail, "Please input your email"))
        {
            KillEvent(event);
            return false;
        }

        if(!CheckEmail(textEmail, "Your email has incorrect format"))
        {
            KillEvent(event);
            return false;
        }

        if(!CheckMandatory(textComment, "Please input your comment"))
        {
            KillEvent(event);
            return false;
        }
    }

    return true;
}


function CheckSendMailForm()
{
    with(document.form1)
    {
        if(!CheckMandatory(textFriendsEmail, "Please input your friend's email"))
        {
            KillEvent(event);
            return false;
        }

        var emails = textFriendsEmail.value.replace(/ /g, "").split(",");
        for(i = 0; i < emails.length; i++)
        {
            if(!ValidEmail(emails[i]))
	        {
		        alert("Your friend's email has incorrect format");
		        textFriendsEmail.focus();
		        KillEvent(event);
		        return false;
	        }
        }


        if(!CheckMandatory(textYourEmail, "Please input your email"))
        {
            KillEvent(event);
            return false;
        }

        if(!CheckEmail(textYourEmail, "Your email has incorrect format"))
        {
            KillEvent(event);
            return false;
        }
    }

    return true;
}

function OpenEmailVideo()
{
    CloseAll();
    document.getElementById('video_email').style.display = "block";
}

function OpenShareVideo()
{
    CloseAll();
    document.getElementById('video_embed').style.display = "block";
}

function OpenVideoLink()
{
    CloseAll();
    document.getElementById('video_link').style.display = "block";
}

function CloseAll()
{

    document.getElementById('video_email').style.display = "none";
    document.getElementById('video_embed').style.display = "none";
    document.getElementById('video_link').style.display = "none";
}

function OpenComment()
{
    document.getElementById('video_comment').style.display = "block";
}

function CloseComment()
{
    document.getElementById('video_comment').style.display = "none";
}

function handleStateChange()
{

    if(xmlHttp.readyState == 4)
    {
		  //alert(xmlHttp.status);
        if(xmlHttp.status == 200)
        {
            OutputMailResponse(xmlHttp.responseText);
				return false;
        }
    }
}

function handleStateChangeComment()
{

    if(xmlHttp.readyState == 4)
    {
		  //alert(xmlHttp.status);
        if(xmlHttp.status == 200)
        {

            CloseComment();
				return false;
        }
    }
}

function OutputMailResponse(text)
{
    alert(text);
}

function SendEmail()
{

    if(CheckSendMailForm())
    {
        CloseAll();

        var data = "textFriendsEmail=" + encodeURI(document.form1.textFriendsEmail.value) +
            "&textYourEmail=" + encodeURI(document.form1.textYourEmail.value) +
            "&textMessage=" + encodeURI(document.form1.textMessage.value);

        StartRequestPost("AJAX/SendEmail.aspx", data,  handleStateChange);
    }
}

function handleComments()
{
    if(xmlHttp.readyState == 4)
    {
        if(xmlHttp.status == 200)
        {

                var firstItem = GetXmlElementValue("FirstResult");
                var lastItem = GetXmlElementValue("LastResult");
                var totalItem = GetXmlElementValue("TotalResults");

                SetSpanValue("firstComment", firstItem);
                SetSpanValue("lastComment", lastItem);
                SetSpanValue("totalComments", totalItem);

                document.getElementById("videoComments").innerHTML = GetXmlElementValue("HTML");
                CloseComment();
                window.location.hash = "topcomments";
				return false;
        }
    }
}

function handleVideos()
{
    if(xmlHttp.readyState == 4)
    {
        if(xmlHttp.status == 200)
        {
                var firstItem = GetXmlElementValue("FirstResult");
                var lastItem = GetXmlElementValue("LastResult");
                var totalItem = GetXmlElementValue("TotalResults");

                SetSpanValue("firstRelated", firstItem);
                SetSpanValue("lastRelated", lastItem);
                SetSpanValue("totalRelated", totalItem);

                document.getElementById("videosRelated").innerHTML = GetXmlElementValue("HTML");
                CloseComment();
				return false;
        }
    }
}

function GetXmlElementValue(tagName)
{
    var element = xmlHttp.responseXML.getElementsByTagName(tagName).item(0);
    
    if(element.textContent != null)
    {
        return element.textContent;
    }
    else
    {
        return element.text;
    }
}

function GetCommentsPage(pageToRetrieve)
{
    StartRequest("AJAX/GetComments.aspx", "?page=" + pageToRetrieve + "&videoId=" + document.form1.videoIdHidden.value, handleComments);
}

function GetRelatedPage(pageToRetrieve)
{
    StartRequest("AJAX/GetRelatedVideos.aspx", "?page=" + pageToRetrieve + "&videoId=" + document.form1.videoIdHidden.value, handleVideos);
}

function SetSpanValue(id, value)
{
    var element = document.getElementById(id);
    if(element.textContent != null)
    {
        element.textContent = value;
    }
    else
    {
        element.innerText = value;
    }
    
}
