function deleteVideoComment(CommentID){
	if(confirm("Are you sure wamt to delete this comment")){
		var start	=	getDivByID("start").value;
		var ajaxRequest = getAjaxRequest();
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				paginateVidoeComments(start,"","");
			}
		}
		var params = [
			{ name: "CommentID", value: CommentID}
		];
		var body = buildQueryString(params);
		ajaxRequest.open("POST", "ajax/delet_video_comment.php", true);
		ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajaxRequest.send(body); 
	}
}
function paginateVidoeComments(start,sortField,sortOrder){
	var videoID	=	getDivByID('videoid').value;
	//Section for ajax pagination --starts
	var ajaxRequest = getAjaxRequest();
	getDivByID("start").value	=	start;
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var divTable = getDivByID('paginationOptions');
			divTable.style.display = "block";
			divTable.innerHTML = ajaxRequest.responseText;
		}
	}
	
	var params = [
		{ name: "id", value: videoID },
		{ name: "start", value: start }
	];
	ajaxRequest.open("POST", "ajax/paginate_video_comments.php", true);
	ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	var body = buildQueryString(params);
	ajaxRequest.send(body); 
	//Section for ajax pagination --startsh
	//window.location	= "view_video.php?id="+videoID+"&start="+start;
}
function ajaxTableOperationVideo(page, sort, rows) 
{
    var fieldtxtrecordspage = getDivByID('txtrecordsonpage');
    var fieldtxtpagenumber = getDivByID('txtpagenumber');
    var fieldtxtsortexpression = getDivByID('txtsortexpression');
    var fieldtxtsortdirection = getDivByID('txtsortdirection');
    direct = fieldtxtsortdirection.value;
    if ((sort!='') && (fieldtxtsortexpression.value.indexOf(sort) != -1))
        if (direct == ' DESC')
            direct = ' ASC';
        else
            direct = ' DESC';
    if (page=='')
        page = fieldtxtpagenumber.value;
    if (sort=='')
        sort = fieldtxtsortexpression.value;
    if (rows=='')
        rows = fieldtxtrecordspage.value;
    if (rows == -1)
        rows = 0;
    var ajaxRequest = getAjaxRequest();
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var divTable = getDivByID('dataTable');
            divTable.style.display = "block";
            divTable.innerHTML = ajaxRequest.responseText;
        }
    }
        
    var params = [
        { name: "rows", value: rows },
        { name: "page", value: page },
        { name: "sort", value: sort },
        { name: "direct", value: direct }
    ];
    ajaxRequest.open("POST", "videos_list.php", true);
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var body = buildQueryString(params);
    ajaxRequest.send(body); 
}

function ajaxRemoveSelectedVideo(id, name) {
    var ajaxRequest = getAjaxRequest();
    var fieldtxtrecordspage = getDivByID('txtrecordsonpage');
    var fieldtxtpagenumber = getDivByID('txtpagenumber');
    var fieldtxtsortexpression = getDivByID('txtsortexpression');
    var fieldtxtsortdirection = getDivByID('txtsortdirection');
    var confirmation;
    confirmation = confirm("Are you sure you want to remove video " + name + "?");
    
    ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
            var divTable = getDivByID('dataTable');
            divTable.style.display = "block";
            divTable.innerHTML = ajaxRequest.responseText;
      }
    }
    var arrayaddress = getDivByID('addresses[]');
    
    if (confirmation)
    {
        var params = [
            { name: "rows", value: fieldtxtrecordspage.value },
            { name: "page", value: fieldtxtpagenumber.value },
            { name: "sort", value: fieldtxtsortexpression.value },
            { name: "direct", value: fieldtxtsortdirection.value },
            { name: "delete", value: id}
        ];
        ajaxRequest.open("POST", "videos_list.php", true);
        ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        var body = buildQueryString(params);
        ajaxRequest.send(body); 
    }
}

function ajaxSaveVideo(videoID) {
    var ajaxRequest = getAjaxRequest();

    ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
          var divMessage = getDivByID('errMessage');
          divMessage.style.display = "block";
          divMessage.innerHTML = ajaxRequest.responseText;
      }
    }

    var fieldTxtName = getDivByID('TxtName');
    var fieldTxtDescription = getDivByID('TxtDescription');
    var fieldTxtCaption = getDivByID('TxtCaption');
    var fieldDdlVisibility = getDivByID('DdlVisibility');
    
    var fieldSections = document.getElementsByName('DdlSections[]');
    var sel = "";
    for (i=0; i<fieldSections.length; i++) {
        if (fieldSections[i].checked) {
            if (sel != "") {
                sel += ",";
            }
            sel += "" + fieldSections[i].value;
        }
    }

    var fieldEvents = document.getElementsByName('DdlEvents[]');
    var selEvents = "";
    for (i=0; i<fieldEvents.length; i++) {
        if (fieldEvents[i].checked) {
            if (selEvents != "") {
                selEvents += ",";
            }
            selEvents += "" + fieldEvents[i].value;
        }
    }
    var network				=		getDivByID('network');
    var networkItem		=	0;
		if(network.checked)
		{
			networkItem		=	1;
		}
    var params = [
        { name: "VideoID", value: videoID },
        { name: "Name", value: fieldTxtName.value },
        { name: "Description", value: fieldTxtDescription.value },
        { name: "Caption", value: fieldTxtCaption.value },
        { name: "Visibility", value: fieldDdlVisibility.value },
        { name: "sections", value: sel},
        { name: "events", value: selEvents},
				{ name: "networkItem", value: networkItem}
        //{ name: "venues", value: selVenues}
    ];
    var body = buildQueryString(params);

    ajaxRequest.open("POST", "ajax/video_save.php", true);
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajaxRequest.send(body); 
}

function ajaxVoteVideo(videoID,rating,voted)
{
    if (voted=='true')
        return;
    var ajaxRequest = getAjaxRequest();
    ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
          if (ajaxRequest.responseText.indexOf('error') == -1)
          {
          }
      }
    }
    var params = [
        { name: "rating", value: rating},
        { name: "VideoID", value: videoID}
    ];
    var body = buildQueryString(params);

    ajaxRequest.open("POST", "ajax/video_rate.php", true);
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajaxRequest.send(body); 
}

function ajaxFlagVideo(videoID)
{
    var DdlReasons = getDivByID('DdlReasons');
    if (DdlReasons.value != 0)
    {
    var ajaxRequest = getAjaxRequest();
    ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
            var divMessage = getDivByID('divInfo');
            divMessage.style.display = "none";
      }
    }
    
    var params = [
        { name: "VideoID", value: videoID},
        { name: "ReasonID", value: DdlReasons.value}
    ];
    var body = buildQueryString(params);

    ajaxRequest.open("POST", "ajax/video_flag.php", true);
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajaxRequest.send(body); 
    }
}

function ajaxClickFlag()
{
    var divInfo = getDivByID('divInfo');
    if (divInfo.style.display == "block")
        divInfo.style.display = "none";
    else
        divInfo.style.display="block";
}

function ajaxClickBucket()
{
    var divInfo = getDivByID('divBucket');
    if (divInfo.style.display == "block")
        divInfo.style.display = "none";
    else
        divInfo.style.display="block";
}

function ajaxClickPost()
{
    var divInfo = getDivByID('divPost');
    if (divInfo.style.display == "block")
        divInfo.style.display = "none";
    else
        divInfo.style.display="block";
}

function ajaxAddVideoComment(videoID) {
	getDivByID("BtnSaveComment").value	=	"Saving....";
	getDivByID("BtnSaveComment").disabled	=	true;
	var start	=	getDivByID("start").value;
	var fieldTxtComment = getDivByID('TxtComment');
	var ajaxRequest = getAjaxRequest();
	/*var divMessage = getDivByID('divVideoCommentsSection');*/
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			paginateVidoeComments(start,"","");
			fieldTxtComment.value	=	"";
			getDivByID("BtnSaveComment").value	=	"Submit";
			getDivByID("BtnSaveComment").disabled	=	false;
		}
	}
	if (fieldTxtComment.value == "") {
		alert("Please add a comment!");
		getDivByID("BtnSaveComment").value	=	"Submit";
		getDivByID("BtnSaveComment").disabled	=	false;
		return;
	}
	
	var params = [
		{ name: "videoID", value: videoID},
		{ name: "Comment", value: fieldTxtComment.value}
	];
	var body = buildQueryString(params);
	
	ajaxRequest.open("POST", "ajax/add_video_comment.php", true);
	ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajaxRequest.send(body); 
}

function ajaxAddToPlaylist() {
    var fieldDdlPlaylist = getDivByID('DdlPlaylists');
    if (fieldDdlPlaylist.value == 0) return;
    var ajaxRequest = getAjaxRequest();
    ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
          var divMessage = getDivByID('errMessage');
          divMessage.style.display = "block";
          divMessage.innerHTML = ajaxRequest.responseText;
     
      }
    }
    var fieldHidminID = getDivByID('hidminID');
    var fieldHidmaxID = getDivByID('hidmaxID');
    
    var selVideos = "";
    for (i=fieldHidminID.value; i<=fieldHidmaxID.value; i++) {

        var fieldCheckbox = getDivByID('check'+i);
        if (fieldCheckbox != null && fieldCheckbox.checked) {
            if (selVideos != "") {
                selVideos += ",";
            }
            selVideos += i;
        }
    }
    if (selVideos == "")
    {
          var divMessage = getDivByID('errMessage');
          divMessage.style.display = "block";
          divMessage.innerHTML = "Check some videos first";
    }
    else
    {
        var params = [
            { name: "selVideos", value: selVideos},
            { name: "PlaylistID", value: fieldDdlPlaylist.value}
        ];
        var body = buildQueryString(params);

        ajaxRequest.open("POST", "ajax/add_video_playlist.php", true);
        ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajaxRequest.send(body); 
    }
}

function ajaxShowDescription(val)
{
    var divDescription = getDivByID('divDescription');
    var divDescriptionMore = getDivByID('divDescriptionMore');
    var fieldDescriptionLong = getDivByID('TxtDescriptionLong');
    
    var temp = fieldDescriptionLong.value;
    fieldDescriptionLong.value = divDescription.innerHTML;
    divDescription.innerHTML = temp;
    
    if (val == 1)
        divDescriptionMore.innerHTML = '&nbsp;<a href="javascript:" onclick="ajaxShowDescription(2)">(less)</a>';
    else
        divDescriptionMore.innerHTML = '&nbsp;<a href="javascript:" onclick="ajaxShowDescription(1)">(more)</a>';
}

function ajaxShowCaption(val)
{
    var divCaption = getDivByID('divCaption');
    var divCaptionMore = getDivByID('divCaptionMore');
    var fieldCaptionLong = getDivByID('TxtCaptionLong');
    
    var temp = fieldCaptionLong.value;
    fieldCaptionLong.value = divCaption.innerHTML;
    divCaption.innerHTML = temp;
    
    if (val == 1)
        divCaptionMore.innerHTML = '&nbsp;<a href="javascript:" onclick="ajaxShowCaption(2)">(less)</a>';
    else
        divCaptionMore.innerHTML = '&nbsp;<a href="javascript:" onclick="ajaxShowCaption(1)">(more)</a>';
}    

function ajaxAddToBucket(objectID,UserID,ObjectType)
{
    var DdlBuckets = getDivByID('DdlBuckets');
    if (DdlBuckets.value != 0)
    {
    var ajaxRequest = getAjaxRequest();
    ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
          var divMessage = getDivByID('errMessage');
          divMessage.style.display = "block";
          divMessage.innerHTML = ajaxRequest.responseText;
     
      }
    }
    var params = [
        { name: "BucketID", value: DdlBuckets.value},
        { name: "ObjectID", value: objectID},
        { name: "UserID", value: UserID},
        { name: "ObjectType", value: ObjectType}
    ];
    var body = buildQueryString(params);

    ajaxRequest.open("POST", "ajax/add_video_bucket.php", true);
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajaxRequest.send(body); 
    }
}



var slider;
function sliderInit() {
   slider = YAHOO.widget.Slider.getHorizSlider("sliderbg", "sliderthumb", 0, 200, 50);
   slider.setValue(50);
   slider.onChange = displayNewValue;
}

function displayNewValue(offsetFromStart) {
   var newVal = (offsetFromStart/50+1)*5;
   if (newVal > 20) 
   {
        updateDisplay('All');
        ajaxTableOperationVideo('','',-1);
   }
       else 
   {
        updateDisplay(newVal);
        ajaxTableOperationVideo('','',newVal);
   }
}

function updateDisplay(value) {
    var Display = getDivByID('recordsonpage');
    Display.innerHTML = 'Records on page: ' + value;
}


YAHOO.util.Event.addListener(window, "load", sliderInit);
