var result_div;

var session_check = 'heck||||||||||valid||||||||login';

var session_check_replace = 'check||||||||||valid||||||||login';

var captcha;



//functions for get codes

function callAjax(path, div_id)

	{

		result_div = div_id;

		new AG_ajax(path,'ajaxResult');		

		return false;

	}



function ajaxResult(data)

	{

		data = unescape(data);

		var obj = document.getElementById(result_div);

		obj.style.display = 'block';

		if(data.indexOf(session_check)>=1)

			{

				data = data.replace(session_check_replace,'');

			}

		else

			{

				obj.innerHTML = '<span class="clsExpired" id="selExpired" style="align:right;">'+viewphoto_session_expired+'</span>';				

				return;

			}

		obj.innerHTML = '<h2><span>'+viewphoto_codes_to_display+'</span></h2>'+'<p class="clsAlignRight" id="close"> <a onClick=codeWindowClose("codeBlock")>'+viewphoto_close_code+'</a></p>'+data;

	}



function RegularExpressionReplace(expression, subject, replaced) 

	{

	  var re = new RegExp(expression, "g");

	  return subject.replace(re, replaced);

	}



function changeWidth(max_width_value)

	{		

		var width_value = document.formGetCode.image_width.value;

		if(parseInt(max_width_value)>=width_value && (!isNaN(width_value)))

			{

				var expression = 'width="([0-9]+)"';

				var subject = document.formGetCode.image_code.value;

				var replaced = 'width="'+width_value+'"';				

				document.formGetCode.image_code.value = RegularExpressionReplace(expression, subject, replaced);

			}

	}



function changeWidthOld()

	{		

		var width_value = document.formGetCode.image_width.value;

		if(max_width_value>=width_value && (!isNaN(width_value)))

			{

				var code_value = document.formGetCode.image_code.value;

				var code_arr = code_value.split('width="');

				code_arr[1] = code_arr[1].substring(code_arr[1].indexOf('"'));

				var new_code = code_arr[0]+'width="'+width_value+code_arr[1];		

				document.formGetCode.image_code.value = new_code;

			}

	}

	

function codeWindowClose(div_id)

	{

		var obj = document.getElementById(div_id);

		obj.innerHTML = '';

		obj.style.display = 'none';

	}



//functions for rating

var rate_click = true;

function callAjaxRate(path, div_id)

	{

		if(!rate_click)

			return false;

		rate_click = false;

		result_div = div_id;

		new AG_ajax(path,'ajaxResultRate');

		setTimeout('changeRatingStatus()',2000);

		return false;

	}

function changeRatingStatus(){

		rate_click = true;

}

function ajaxResultRate(data)

	{

		data = unescape(data);

		var obj = document.getElementById(result_div);

		obj.style.display = 'block';

		if(data.indexOf(session_check)>=1)

			{

				data = data.replace(session_check_replace,'');

			}

		else

			{

				return;				

			}

		

		if(data)

			{

				obj.innerHTML = data;

				obj.style.top = getAbsoluteOffsetTopConfirmation(document.getElementById('dAltMulti')) + 'px';

			}

	}

var img_src = new Array();

function mouseOver(count)

	{

		for(var i=1; i<=count; i++)

			{

				var obj = document.getElementById('img'+i);				

				img_src[i] = obj.src;

				obj.src = site_url+'images/icon-ratehover.gif';

			}

		for(; i<=total_images; i++)

			{

				var obj = document.getElementById('img'+i);	

				img_src[i] = obj.src;

				obj.src = site_url+'images/icon-rate.gif';

			}

	}



function mouseOut(count)

	{

		for(var i=1; i<=total_images; i++)

			{

				var obj = document.getElementById('img'+i);				

				obj.src = img_src[i];

			}

	}



//paging(next/previous link)

function callAjaxPaging(path, div_id)

	{

		result_div = div_id;

		new AG_ajax(path,'ajaxResultPaging');		

		return false;

	}

	

function ajaxResultPaging(data)

	{

		data = unescape(data);	

		var obj = document.getElementById(result_div);

		obj.style.display = 'block';

		if(data.indexOf(session_check)>=1)

			{

				data = data.replace(session_check_replace,'');

			}

		else

			{

				location.replace(replace_url);

				return;

			}

		data = data.split('***--***!!!');		

		captcha = data[1];

		data = data[0].split('+++++--++++!+++++++');

		obj.innerHTML = data[0];

		flagged_status = data[1];		

		hideFlaggedContent();

	}

function hideFlaggedContent()

	{

	if(flagged_status=="Yes")

		{

			var obj = '';

			if(obj = document.getElementById('membersAlbums'))

				{

					obj.style.display = 'none';

				}

			var obj = document.getElementById('addFavorite');

			obj.style.display = 'none';

			var obj = document.getElementById('selflaggedContent');

			obj.style.display = 'none';

			var obj = document.getElementById('confirmationDiv');

			obj.style.display = 'block';

		}

	}

function showFlaggedContent()

	{

		var obj = '';

			if(obj = document.getElementById('membersAlbums'))

				{

					obj.style.display = 'block';

				}

		var obj = document.getElementById('addFavorite');

		obj.style.display = 'block';

		var obj = document.getElementById('selflaggedContent');

		obj.style.display = 'block';

		var obj = document.getElementById('confirmationDiv');

		obj.style.display = 'none';

		return false

	}



function callAjaxAddGroups(path, div_id)

	{

		result_div = div_id;

		new AG_ajax(path,'ajaxResultAddGroups');		

		return false;

	}



function ajaxResultAddGroups(data)

	{

		data = unescape(data);		

		var obj = document.getElementById(result_div);		

		obj.style.display = 'block';

		showHideScreen(result_div);

		if(data.indexOf(session_check)>=1)

			{

				data = data.replace(session_check_replace,'');

			}

		else

			{				

				return;

			}		

		obj.innerHTML = data;

		obj.style.top = getAbsoluteOffsetTopConfirmation(document.getElementById('dAltMulti')) + 'px';

	}



function restoreFirstValue(id)

	{

		/*var obj = document.getElementById(id);		

		obj.style.display = 'none';

		obj.innerHTML = '';*/

		hideAllBlocks();

		return false;

	}



var addToGroup = function()

	{		

		if(arguments[1])

			addGroupsUrl = arguments[1];



		var group_ids = '';

		var frm = document.addGroups;		

		for (var i=0;i<frm.elements.length;i++)

			{

				var e=frm.elements[i];

				if (e.type=='checkbox' && e.checked)

					{

						group_ids += e.value + ',';

					}

			}

		var currpath = addGroupsUrl+'&group_ids='+group_ids;		

		callAjaxUpdateGroups(currpath,'selEditPhotoComments');

		return false

	}



function callAjaxUpdateGroups(path, div_id)

	{

		result_div = div_id;

		new AG_ajax(path,'ajaxResultUpdateGroups');		

	}



function ajaxResultUpdateGroups(data)

	{

		data = unescape(data);

		var obj = document.getElementById(result_div);

		obj.style.display = 'block';

		if(data.indexOf(session_check)>=1)

			{

				data = data.replace(session_check_replace,'');

			}		

		restoreFirstValue(result_div);

		document.getElementById('selFavoriteLink').style.display = 'none';

		//alert(data);

	}

	

//favorites

var favorite_count = true;

function callAjaxFavoriteGroups(path, div_id)

	{

		//if(!favorite_count)

		if(0)

			return false;

			

		result_div = div_id;

		new AG_ajax(path,'ajaxResultAddGroups');		

		return false;

	}

var addToFavorite = function()

	{

		if(arguments[1])

			addFavoritesUrl = arguments[1];



		var favorite_id = '';

			

		var frm = document.addFavorites;		

		for (var i=0;i<frm.elements.length;i++)

			{

				var e=frm.elements[i];

				if (e.type=='checkbox' && e.checked)

					{

						favorite_id += e.value + ',';

					}

			}

		var currpath = addFavoritesUrl+'&favorite_id='+favorite_id;

		//alert(currpath);return;

		callAjaxUpdateGroups(currpath,'selEditPhotoComments');

		

		var obj;		

		if(obj = document.getElementById('totalFavorite'))

			obj.innerHTML = parseInt(obj.innerHTML)+1;		

		favorite_count = false;

		

		return false

	}

//flag

function callAjaxFlagGroups(path, div_id)

	{

		result_div = div_id;

		new AG_ajax(path,'ajaxResultAddGroups');		

		return false;

	}



var addToFlag = function()

	{

		if(arguments[1])

			addFlagUrl = arguments[1];



		var flag = '';

			

		var frm = document.addFavorites;		

		for (var i=0;i<frm.elements.length;i++)

			{

				var e=frm.elements[i];

				if (e.type=='checkbox' && e.checked)

					{

						flag += e.value + ',';

					}

			}

		var currpath = addFlagUrl+'&flag='+flag;

		//alert(currpath);return;

		callAjaxUpdateGroups(currpath,'selEditPhotoComments');

		return false

	}

	

//Add Comments

function callAjaxAddComments(path, div_id)

	{		

		restoreFirstValue('selEditMainComments');

		//var obj = document.getElementById('selViewPostComment');

		//obj.style.display = 'none';

		result_div = div_id;

		new AG_ajax(path,'ajaxResultAddComments');		

		return false;

	}

function ajaxResultAddComments(data)

	{

		data = unescape(data);		

		var obj = document.getElementById(result_div);		

		obj.style.display = 'block';

		if(data.indexOf(session_check)>=1)

			{

				data = data.replace(session_check_replace,'');

			}

		else

			{				

				return;

			}

		data = data.split('***--***!!!');

		obj.innerHTML = data[0];

		captcha = data[1];		

	}

	

var addToComment = function()
	{		

		if(arguments[0])

			{

				if(!dontUse)

					addCommentsUrl = arguments[0];

			}

		

		var f = '';

			

		var frm = document.addComments;		

		for (var i=0;i<frm.elements.length;i++)

			{

				var e=frm.elements[i];

				if (e.type!='button' && e.name!='captcha_value')

					{

						var ovalue = Trim(e.value);

						if(ovalue)

							{

								ovalue = replace_string(ovalue, '\n', '<br />');

								f += ovalue;

							}

						else

							{

								e.value = '';

								e.focus();

								return false;

							}

					}

			}
/*
		if(captcha!=frm.captcha_value.value)

			{				

				alert_manual('invalid captcha', 'alertHyperLink', 1000, 500);

				return;

			}
*/
		var currpath = addCommentsUrl+'&f='+escape(f);

		//alert(currpath);return;

		callAjaxComment(currpath,'selCommentBlock');

		return false

	}



function clearComment()

	{

		var obj = document.getElementById('selAddComments');

		obj.style.display = 'none';

		//var obj = document.getElementById('selViewPostComment');

		//obj.style.display = 'block';

	}



function callAjaxComment(path, div_id)

	{

		//result_div = div_id;

		new AG_ajax(path,'ajaxResultComment');		

		return false;

	}



function ajaxResultComment(data)

	{

		data = unescape(data);		

		var obj = document.getElementById('selCommentBlock');		

		obj.style.display = 'block';

		if(data.indexOf(session_check)>=1)

			{

				data = data.replace(session_check_replace,'');

			}

		else

			{

				return;

			}

		data = data.split('***--***!!!');		

		obj.innerHTML = data[1];

		setEditTimerValue(data[0]);

		//if(obj = document.getElementById('selViewPostComment'))		

			//obj.style.display = 'block';

		if(comment_approval){

			if(obj = document.getElementById('totalComments'))

				obj.innerHTML = parseInt(obj.innerHTML)+1;

		}

		captcha = data[2];

		return;

	}



//delete comment

var tr_delete;

function deleteCommand(path, div_id)

	{		

		if(confirm(deleteConfirmation))

			{

				tr_delete = div_id;

				beforeDeleteResult();

				new AG_ajax(path,'deleteResult');

			}

		return false;		

	}



function beforeDeleteResult()

	{

		var obj;

		

		if(obj = document.getElementById(tr_delete))

			obj.style.display = 'none';

			

		if(obj = document.getElementById(tr_delete+'_1'))

			obj.style.display = 'none';

			

		if(obj = document.getElementById('total_comments1'))

			{			

				total_comments = parseInt(obj.innerHTML)-1;

				obj.innerHTML = total_comments;

			}

		

		if(obj = document.getElementById('total_comments2'))

			{

				total_comments = parseInt(obj.innerHTML)-1

				obj.innerHTML = total_comments;

			}

			

		if(total_comments && total_comments<=minimum_counts)

			{

				if(obj = document.getElementById('view_all_comments'))			

					obj.style.display = 'none';

			}

		

		if(obj = document.getElementById('totalComments'))

			obj.innerHTML = parseInt(obj.innerHTML)-1;	

	}



function deleteResult(data)

	{		

		return;		

	}



//group video comments

function callAjaxAddCommentsGroup(path, div_id)

	{		

		result_div = div_id;

		new AG_ajax(path,'ajaxResultAddComments');		

		return false;

	}



function clearCommentGroup()

	{

		var obj = document.getElementById('selAddComments');

		obj.style.display = 'none';		

	}



//view Video

var downloadCount = true;

function increaseDownload(block_name)

	{		

		return true;

		if(!downloadCount)

		return false;

		

		var obj;

		if(obj = document.getElementById(block_name))

			{

				download_count = download_count+1;

				obj.innerHTML = download_count;

			}

		downloadCount = false;

		return true;

	}



//Reply comment



var addToReply = function()

	{

		var comment_id = arguments[0];



		if(arguments[1])

			{

				if(!dontUse)

					addCommentsUrl = arguments[1];

			}



		var f = '';

			

		var frm = eval("document.addReply_"+comment_id);		

		for (var i=0;i<frm.elements.length;i++)

			{

				var e=frm.elements[i];

				if (e.type!='button')

					{

						var ovalue = Trim(e.value);

						if(ovalue)

							{

								ovalue = replace_string(ovalue, '\n', '<br />');

								f += ovalue;

							}

						else

							{

								e.value = '';

								e.focus();

								return false;

							}

					}

			}			

		var currpath = addCommentsUrl+'&comment_id='+comment_id+'&f='+escape(f);	

		callAjaxReplyFinal(currpath,'selCommentBlock');

		return false

	}

	

function callAjaxReplyFinal(path, block)

	{

		

		path = path;

		new AG_ajax(path,'ajaxResultReplyFinal');		

		return false;

	}

	

function ajaxResultReplyFinal(data)

	{

		data = unescape(data);	

		//alert(data);	

		var obj = document.getElementById('selCommentBlock');		

		obj.style.display = 'block';

		if(data.indexOf(session_check)>=1)

			{

				data = data.replace(session_check_replace,'');

			}

		else

			{

				return;

			}

		data = data.split('***--***!!!');		

		obj.innerHTML = data[1];

		setEditTimerValue(data[0]);

		return;

	}

	

function callAjaxReply(path, comment_id)

	{

		//result_div = div_id;

		path = path+'&comment_id='+comment_id;		

		new AG_ajax(path,'ajaxResultReply');		

		return false;

	}



function ajaxResultReply(data)

	{

		data = unescape(data);		

		if(data.indexOf(session_check)>=1)

			{

				data = data.replace(session_check_replace,'');

				

				data = data.split('***--***!!!');				

				var obj = document.getElementById('selAddComments_'+data[0]);

				obj.style.display = 'block';				

				obj.innerHTML = data[1];

						

				obj = document.getElementById('selViewPostComment_'+data[0]);

				obj.style.display = 'none';

				return true;

			}

		return false;

	}



function discardReply(comment_id)

	{		

		var obj = document.getElementById('selAddComments_'+comment_id);

		obj.style.display = 'none';

		var obj = document.getElementById('selViewPostComment_'+comment_id);

		obj.style.display = 'block';

	}



/*******for ediit comment functions started***********/

function callAjaxEdit(path, comment_id)

	{

		//result_div = div_id;

		path = path+'&type=edit&comment_id='+comment_id;		

		new AG_ajax(path,'ajaxResultEdit');		

		return false;

	}



function ajaxResultEdit(data)

	{

		data = unescape(data);	

		if(data.indexOf(session_check)>=1)

			{

				var obj;

				data = data.replace(session_check_replace,'');

				

				data = data.split('***--***!!!');

				

				if(obj = document.getElementById('selEditCommentTxt_'+data[0]))

					obj.style.display = 'none';					

				

				obj = document.getElementById('selEditComments_'+data[0]);

				obj.style.display = 'block';				

				var txt = replace_string(data['1'], '<br>', '\n');

				txt = replace_string(txt, '<br />', '\n');

				txt = trim(txt);

				obj.innerHTML = txt;

						

				obj = document.getElementById('selViewEditComment_'+data[0]);

				obj.style.display = 'none';

				return true;

			}

		return false;

	}



function discardEdit(comment_id)

	{		

		var obj;

		

		if(obj = document.getElementById('selEditCommentTxt_'+comment_id))

			obj.style.display = '';

		

		if(obj = document.getElementById('selEditComments_'+comment_id))

			obj.style.display = 'none';	

		

		if(obj = document.getElementById('selViewEditComment_'+comment_id))

			obj.style.display = '';				

	}



var addToEdit = function()

	{

		comment_id = arguments[0];



		if(arguments[1])

			addCommentsUrl = arguments[1];



		var f = '';

			

		var frm = eval("document.addEdit_"+comment_id);		

		for (var i=0;i<frm.elements.length;i++)

			{

				var e=frm.elements[i];

				if (e.type!='button')

					{

						var ovalue = Trim(e.value);

						if(ovalue)

							{

								ovalue = replace_string(ovalue, '\n', '<br />');

								f += ovalue;								

							}

						else

							{

								e.value = '';

								e.focus();

								return false;

							}

					}

			}

		f = escape(f);		

		var currpath = addCommentsUrl+'&comment_id='+comment_id+'&type=edit&f='+escape(f);	

		callAjaxUpdate(currpath,'selCommentBlock');

		return false

	}

	

function callAjaxUpdate(path, block)

	{

		

		path = path;

		new AG_ajax(path,'callAjaxUpdateResponse');		

		return false;

	}

	

function callAjaxUpdateResponse(data)

	{

		data = unescape(data);

		if(data.indexOf(session_check)>=1)

			{

				data = data.replace(session_check_replace,'');

			}

		else

			{

				return;

			}

		data = data.split('***--***!!!');

		

		if(obj = document.getElementById('selEditCommentTxt_'+data['0']))

			obj.innerHTML = data['1'];

			

		discardEdit(data['0']);

		return;

	}

/************ edit comments end ********/

function changeTimer(){	

	if(enabled_edit_fields_comment.length){

		doTimerFunction();

		setTimeout('changeTimer()',1000);

	}

}



function setEditTimerValue(comment_id){

	enabled_edit_fields_comment[enabled_edit_fields_comment.length] = comment_id;

	enabled_edit_fields_time[comment_id] = max_timer;

}



function doTimerFunction(){

	var val;

	var comment_id;

	for(var i in enabled_edit_fields_comment){

		comment_id = enabled_edit_fields_comment[i];

		if(i!='undefined' && i!='has' && i!='find'){

		val = enabled_edit_fields_time[comment_id];

		if(val<=1)

			hideDeleteEditLinks(comment_id);

		else if(val!=null)

			decrementTime(comment_id);

		}

	}			

}



function decrementTime(comment_id){	

	var obj;

	var val = enabled_edit_fields_time[comment_id];

	if(obj = document.getElementById('selViewTimerComment_'+comment_id)){

		obj.innerHTML = val-1;

		obj.innerHTML = obj.innerHTML+' Sec';

	}

	enabled_edit_fields_time[comment_id] = val-1;	

}

function hideDeleteEditLinks(comment_id){	

	var obj;

	var val = enabled_edit_fields_time[comment_id];	

	if(obj = document.getElementById('selViewDeleteComment_'+comment_id))

		obj.style.display = 'none';

	if(obj = document.getElementById('selViewEditComment_'+comment_id))

		obj.style.display = 'none';

	if(obj = document.getElementById('selViewTimerComment_'+comment_id))

		obj.style.display = 'none';

	if(obj = document.getElementById('cmd'+comment_id))

		obj.className = 'clsNotEditable';

	enabled_edit_fields_time[comment_id] = null;

}