///to show login box
function commentPicture(event)
{
	var xcoord = mouseX(event);
	var ycoord = mouseY(event);
	
	$('login_box').style.top = ycoord+'px';
	$('login_box').style.left = 650 +'px'; 
	show_login_box_picture_cmnt();
	document.getElementById('page_name').value = 'view_picture';
	document.getElementById('action').value = 'comment_picture';
	document.getElementById('login_id').focus();
	location.href="#short_login";
}
function show_login_box_picture_cmnt()
{
	document.getElementById('login_box').style.display='block';
	document.getElementById('login_ajxLoader').style.display='none';
	document.getElementById('login_divErr').innerHTML='';
	document.getElementById('login_id').value='';
	document.getElementById('login_pwd').value='';		
	//document.getElementById('login_id').focus();
}
//For top Pictures
function topPicturePager(strURL,ch_id) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatePagerResult(self.xmlHttpReq.responseText,ch_id);
        }
    }
    self.xmlHttpReq.send(strURL);
}

function get_top_picture_no(a,url2,chnl,did) {	
	if(a.keyCode==13) {		
		page = document.getElementById(did).value;		
		url2 = url2+'&page2='+page;		
		topPicturePager(url2,chnl);
	}
}

function updatePagerResult(str,ch_id){
    document.getElementById("div_"+ch_id).innerHTML = str;
}

//For Channel Picture Contest
function showContestForm(arg)
{
	if(is_login == 0) {
		show_login_text('showContestForm|'+arg,'popup');
	}
	else {
		document.getElementById("frmContest").reset();
		document.getElementById("contErr").innerHTML = "";
		document.getElementById("divContest").style.display='block';
		document.getElementById("hdn_CID").value = arg;
		if(document.getElementById("hdn_CID").value == "referral"){
			document.getElementById("contest_captcha").style.display = "none";
			document.getElementById("contest_btns").style.display = "none";
		}
		else {
			document.getElementById("contest_captcha").style.display = "block";
			document.getElementById("contest_btns").style.display = "block";
		}
		$('cont_ajaxLoader').show();
		formObj = document.getElementById('frmContest');
		postUrl = "action_view_picture.php";
		$(formObj).request({	
			action:postUrl,
		  method: 'post',
		  parameters: { actionType:'VIEW_CONTEST'}, 
		  onSuccess:handleShowContestResponse, 
		  onException: handleShowContestError
		});
	}
}
function handleShowContestResponse(transport)
{
		data=transport.responseText.evalJSON();
		if(data.MESSAGE == "PAGE_RELOAD")
				location.reload();
		else
		{
				if(data.STATUS=="SUCCESS")
				{
						document.getElementById("contest_Content").innerHTML = data.MESSAGE;
						if(document.getElementById("contest_description").scrollHeight == 440) {
							document.getElementById("contest_description").style.overflow = "hidden";
						}
						document.getElementById("CONTEST_WELCOME").innerHTML = data.UPDATEDATA;
						reloadPicContestCaptcha();
						$('cont_ajaxLoader').hide();
				}
				else
				{
						document.getElementById("contErr").innerHTML = data.MESSAGE;
						reloadPicContestCaptcha();
						$('cont_ajaxLoader').hide();
						closeContestForm()
				}
		}
}

function handleShowContestError(transport)
{
	document.getElementById("contErr").innerHTML = "Error processing: Please try later.";
	reloadPicContestCaptcha();
	$('cont_ajaxLoader').hide();
}
function closeContestForm()
{
	document.getElementById("contErr").innerHTML = "";
	document.getElementById("contest_Content").innerHTML = "";
	document.getElementById("CONTEST_WELCOME").innerHTML = "";
	document.getElementById("hdn_CID").value = "";
	document.getElementById("frmContest").reset();
	document.getElementById("contest_captcha").style.display = "block";
	document.getElementById("contest_btns").style.display = "block";
	thisMovie('contestslider').sendTextToFlash('play');
	reloadPicContestCaptcha();
	document.getElementById("divContest").style.display='';
}
function reloadPicContestCaptcha()
{
	document.getElementById("picContestCaptcha").src="captcha.php?forsec=vpcon&"+Math.random();
}
function postVote(formObj,postUrl)
{
		$('cont_ajaxLoader').show();
		$(formObj).request({	
		action:postUrl,
	  method: 'post',
	  parameters: { actionType:'POST_VOTE'}, 
	  onSuccess:handleVotingResponse, 
	  onException: handleVotingError
	});
}	
function handleVotingResponse(transport)
{
		data=transport.responseText.evalJSON();
		if(data.MESSAGE == "PAGE_RELOAD")
				location.reload();
		else
		{
				if(data.STATUS=="SUCCESS")
				{
						//document.getElementById("divResultset").innerHTML = data.UPDATEDATA;
						document.getElementById("votingStatus").innerHTML = data.MESSAGE;
						reloadPicContestCaptcha();
						$('cont_ajaxLoader').hide();
						closeContestForm();
				}
				else
				{
						document.getElementById("contErr").innerHTML = data.MESSAGE;
						reloadPicContestCaptcha();
						$('cont_ajaxLoader').hide();
				}
		}
}

function handleVotingError(transport)
{
	document.getElementById("contErr").innerHTML = "Error processing: Please try later.";
	reloadPicContestCaptcha();
	$('cont_ajaxLoader').hide();
}

//For related pictures in ind picture page

function callPicFunction(vkey, uid) {
	if(document.getElementById('related_dd').value == "RPICS") {
		getPics('RPICS',vkey,uid);
	}
	else if(document.getElementById('related_dd').value == "UPICS") {
		getPics('UPICS',vkey,uid);
	}
	else if(document.getElementById('related_dd').value == "UPHOTOSETS") {
		getPics('UPHOTOSETS',vkey,uid);
	}
}

function getPics(pictype,vkey,uid)
{
		var pars;
		var myAjax = new Ajax.Request("view_picture.php?action=GetPICS&cat="+pictype+"&viewkey="+vkey+"&UID="+uid, {method: 'post', parameters: pars,
		onSuccess: populatePicList
		});	
}

function populatePicList(transport)
{
	data=transport.responseText;
	document.getElementById("Related_Vdos").innerHTML = data;
}

//show report box
function reportComment(cid,touid)
{
		//document.getElementById("over2").style.height = document.body.scrollHeight + "px";
		//document.getElementById("over2").style.width = document.body.scrollWidth + "px";
		document.getElementById("divErr2").innerHTML = "";
		document.getElementById("hdnTOUID").value = cid;
		$('ajaxLoader2').hide();
		
		if(document.getElementById("divComment"))
		{
			document.getElementById("divComment").style.display='none';
		}
		document.getElementById("divReport").style.display='block';
		document.getElementById("frmReport").reset();
		document.getElementById("hdnCID").value=cid;
		document.getElementById("hdnTOUID").value=touid;				
		location.href="#pic_report";
}

// hide report box
function closeDivReport()
{
	document.getElementById('divReport').style.display='none';
}

// show add comment box
function addComment()
{
		//document.getElementById("over").style.height = document.body.scrollHeight + "px";
		//document.getElementById("over").style.width = document.body.scrollWidth + "px";
		document.getElementById("divErr").innerHTML = "";
		//clearRating();
		$('ajaxLoader_viewpicture').hide();
		
		if(document.getElementById("divReport"))
		{
			document.getElementById("divReport").style.display='none';
		}
		document.getElementById("divComment").style.display='block';
		document.getElementById('hdnRate').value="";
		document.getElementById('comment').value="";
		document.getElementById('picCmtCaptcha').value="";
		//document.getElementById("frmComment").reset();
		location.href="#pic_comment";
}

// hide add comment box
function closeDivComment()
{
	rate_comment_view_pic(0);
	document.getElementById('divComment').style.display='none';
}

// 
function manipulateRating(rate,imgurl)
{
	document.getElementById('hdnRate').value = rate;
	for(i=2; i<=rate; i=i+2)
	{
		document.getElementById("img"+i).src = imgurl+"/tpl_icon_star_full.gif";
	}
	for(i=parseInt(rate)+2; i<=10; i=i+2)
	{
		document.getElementById("img"+i).src = imgurl+"/tpl_icon_star_empty.gif";
	}	
}

function manipulateRating2(rate,imgurl)
{
	document.getElementById('hdnRate').value = rate;
	for(i=2; i<=rate; i=i+2)
	{
		document.getElementById("image"+i).src = imgurl+"/tpl_icon_star_full.gif";
	}
	for(i=parseInt(rate)+2; i<=10; i=i+2)
	{
		document.getElementById("image"+i).src = imgurl+"/tpl_icon_star_empty.gif";
	}	
}

function clearRating()
{
	for(i=2; i<=10; i=i+2)
	{
		document.getElementById("img"+i).src = imgurl+"/tpl_icon_star_empty.gif";
	}	
}
function clearRating_comment(id)
{
	document.getElementById(id).src = imgurl+"/tpl_icon_star_empty.gif";
}

// delete comment
function delPicComment(cid,vid)
{
		var pars = '';
		path = "action_view_picture.php?actionType=DELETE_COMMENT&cid="+cid+"&vid="+vid;
			
		var myAjax = new Ajax.Request(path, {method: 'post', parameters: pars,
		onSuccess: handleDelPicCommentResponse
		});	
}

function handleDelPicCommentResponse(transport)
{
		data=transport.responseText.evalJSON();
		if(data.MESSAGE == "PAGE_RELOAD")
				location.reload();
		else
		{
				if(data.STATUS=="SUCCESS")
				{
						document.getElementById("divStatus").innerHTML = data.MESSAGE;
						document.getElementById("divResultset").innerHTML = data.UPDATEDATA;
				}
				else
				{
						document.getElementById("divStatus").innerHTML = "Error in deleting Comment.";
				}
		}
}

// post the comment form
function postComment(formObj,postUrl)
{
		$('ajaxLoader_viewpicture').show();
		$(formObj).request({	
		action:postUrl,
	  method: 'post',
	  parameters: { actionType:'POST_COMMENT'}, 
	  onSuccess:handleCommentResponse, 
	  onException: handleCommentError
	});
}	
function handleCommentResponse(transport)
{
		data=transport.responseText.evalJSON();
		if(data.MESSAGE == "PAGE_RELOAD")
				location.reload();
		else
		{
				var var1 = data.UPDATEDATA.split("|");
				if(data.STATUS=="SUCCESS")
				{
						document.getElementById("comment_result").innerHTML = var1[1];
						document.getElementById("divResultset").innerHTML = var1[0];
						document.getElementById("divStatus").innerHTML = data.MESSAGE;
						rate_comment_view_pic(0);
						reloadPicCommentCaptcha();
						closeDivComment();
				}
				else
				{
						document.getElementById("divErr").innerHTML = data.MESSAGE;
						reloadPicCommentCaptcha();
				}
				$('ajaxLoader_viewpicture').hide();
		}
}

function handleCommentError(transport)
{
	document.getElementById("divErr").innerHTML = "Error processing: Please try later.";
	reloadPicCommentCaptcha();
	$('ajaxLoader_viewpicture').hide();
}
function reloadPicCommentCaptcha()
{
	document.getElementById("picCommentCaptcha").src="captcha.php?forsec=vpcom&"+Math.random();
}
// post report
function postReport(formObj,postUrl)
{
		$('ajaxLoader2').show();
		$(formObj).request({	
		action:postUrl,
	  method: 'post',
	  parameters: { actionType:'POST_REPORT'}, 
	  onSuccess:handleReportResponse, 
	  onException: handleReportError
	});
}	
function handleReportResponse(transport)
{
		data=transport.responseText.evalJSON();
		if(data.MESSAGE == "PAGE_RELOAD")
				location.reload();
		else
		{
				if(data.STATUS=="SUCCESS")
				{
						document.getElementById("divStatus").innerHTML = data.MESSAGE;
						//alert(data.MESSAGE);
						closeDivReport();
				}
				else
				{
						document.getElementById("divErr2").innerHTML = data.MESSAGE;
				}
				$('ajaxLoader2').hide();
		}
}

function handleReportError(transport)
{
	document.getElementById("divErr2").innerHTML = "Error processing: Please try later.";
	$('ajaxLoader2').hide();
}

function addToFavorite()
{
		formObj = document.getElementById('frmActions');
		postUrl = 'action_view_picture.php';
		$('ajxStatus').hide();
		$('ajxLoader').show();
		$(formObj).request({	
		action:postUrl,
	  method: 'post',
	  parameters: { actionType:'ADD_FAVORITE'}, 
	  onSuccess:handleActionResponse, 
	  onException: handleActionError
	});	
}

function handleActionResponse(transport)
{
		data=transport.responseText.evalJSON();
		if(data.MESSAGE == "PAGE_RELOAD")
				location.reload();
		else
		{
				if(data.STATUS=="SUCCESS")
				{
						document.getElementById("ajxStatus").innerHTML = "<font style='color:#e26600; font-weight:bold'>"+data.MESSAGE+"</font>";
				}
				else
				{
						document.getElementById("ajxStatus").innerHTML = data.MESSAGE;
				}
				$('ajxStatus').show();
				$('ajxLoader').hide();
		}
}

function handleActionError(transport)
{
	document.getElementById("ajxStatus").innerHTML = "Error processing: Please try later.";
		$('ajxStatus').show();
		$('ajxLoader').hide();	
}

function inappropriateRequest()
{
		formObj = document.getElementById('frmActions');
		postUrl = 'action_view_picture.php';
		$('ajxStatus').hide();
		$('ajxLoader').show();
		$(formObj).request({	
		action:postUrl,
	  method: 'post',
	  parameters: { actionType:'INAPPROPRIATE'}, 
	  onSuccess:handleActionResponse, 
	  onException: handleActionError
	});	
}

function featureRequest()
{
		formObj = document.getElementById('frmActions');
		postUrl = 'action_view_picture.php';
		$('ajxStatus').hide();
		$('ajxLoader').show();
		$(formObj).request({	
		action:postUrl,
	  method: 'post',
	  parameters: { actionType:'FEATURE'}, 
	  onSuccess:handleActionResponse, 
	  onException: handleActionError
	});	
}

function getNewCaptcha()
{
	var strURL = "get_captcha.php";
	var xmlHttpReq = false;
  var self = this;
  // Mozilla/Safari
  if (window.XMLHttpRequest) {
      self.xmlHttpReq = new XMLHttpRequest();
  }
  // IE
  else if (window.ActiveXObject) {
      self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
  }
  self.xmlHttpReq.open('POST', strURL, true);
  self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  self.xmlHttpReq.onreadystatechange = function() {
      if (self.xmlHttpReq.readyState == 4) {
          returnCaptcha(self.xmlHttpReq.responseText);
      }
 }
 self.xmlHttpReq.send();
}

function returnCaptcha(str)
{
	alert(str);
}
