var authorID;
$(document).ready(function(){
	$('#lnkShowAllCompPhones').click(function () {
		$.ajax({
			type: "POST",
			url: HTTP_SERVER+DIR_WS_HTTP+"ajaxListing.php",
			data: "actionType=COMPATIBLE_PHONES&modelIDs=" + this.name+"&type=" + this.title,
			success: function(msg){
				var txt = msg;
				alertBox(txt, false);
			}
		});
	});
});

function addToMyCollection(type, typeID)
{
	strParams = "&actionType=" + type +"&typeID="+typeID;
	$.ajax({
				url: HTTP_SERVER+DIR_WS_HTTP+'ajaxAddToCollection.php',
				type: 'POST',
				data: strParams,
				timeout: 3000,
				error: function(strReponseResult){},
				success: function(strReponseResult){
					if (strReponseResult == 0)
					{
						var txt = '<img src="'+DIR_WS_IMAGES+'icons/Help.png" width="64" height="64" border="0" alt="" align="absmiddle">Mysql Error.';
						alertBox(txt, false);
					}
					if (strReponseResult == 1)
					{
						var txt = '<img src="'+DIR_WS_IMAGES+'icons/check.png" width="64" height="64" border="0" alt="" align="absmiddle">Successfully Added to your collection.';
						alertBox(txt, false);
					}
					else if (strReponseResult == 2)
					{
						var txt = '<img src="'+DIR_WS_IMAGES+'icons/Help.png" width="64" height="64" border="0" alt="" align="absmiddle">Already in your collection.';
						alertBox(txt, false);
					}
				}
		});
}

function postComment(objForm)
{
	if (trim(objForm.txtarComments.value) == '')
	{
		alert("Please enter your comments");
		objForm.txtarComments.focus();
		return false;
	}
	else
	{
		return true;
	}
}


function getComments(itemID, type, page)
{
	if (page == undefined)
	{
		page = 0;
	}

	$.ajax({
		type: "POST",
		url: HTTP_SERVER+DIR_WS_HTTP+"ajaxDownloadSingle.php",
		data: "actionType=GET_MY_COMMENTS_SETTINGS&itemID="+itemID+"&type="+type+"&authorID="+authorID+"&page="+page,
		success: function(msg){
			$('#getComments').empty();
			$('#getComments').append(msg);
			if (document.getElementById('txtarUserComments'))
			{
				edToolbar('txtarUserComments', 'edToolbar');
			}
		}
	});

	//
}

function deleteComment(obj, itemID, type)
{
	var txt = "Are you sure you want to hide this comment?";
	$.prompt(txt,{
		buttons:{Ok:-1,Cancel:0},
		submit:function(v,m,f){
			if(v == 0)
			{
				jQuery.ImpromptuClose();
			}
			else if(v == -1)
			{
				$.ajax({
					type: "POST",
					url: HTTP_SERVER+DIR_WS_HTTP+"ajaxDownloadSingle.php",
					data: "actionType=REMOVE_COMMENT&itemID="+obj.id,
					success: function(msg){
						jQuery.ImpromptuClose();
						if (msg == '1')
						{
							var txt = '<img src="'+DIR_WS_IMAGES+'icons/check.png" width="64" height="64" border="0" alt="" align="absmiddle">Comment hidden successfully!!!';
							alertBox(txt, false);
							getComments(itemID, type);
						}
					}
				});
			}
			return false;
		}
	});
}

function showComment(obj, itemID, type)
{
	var txt = "Are you sure you want to show this comment visible?";
	$.prompt(txt,{
		buttons:{Ok:-1,Cancel:0},
		submit:function(v,m,f){
			if(v == 0)
			{
				jQuery.ImpromptuClose();
			}
			else if(v == -1)
			{
				$.ajax({
					type: "POST",
					url: HTTP_SERVER+DIR_WS_HTTP+"ajaxDownloadSingle.php",
					data: "actionType=SHOW_COMMENT&itemID="+obj.id,
					success: function(msg){
						jQuery.ImpromptuClose();
						if (msg == '1')
						{
							var txt = '<img src="'+DIR_WS_IMAGES+'icons/check.png" width="64" height="64" border="0" alt="" align="absmiddle">Comment is now visible';
							alertBox(txt, false);
							getComments(itemID, type);
						}
					}
				});
			}
			return false;
		}
	});
}

function submitUserComments(itemID, type)
{
	$('#btnComments').attr("disabled","disabled");

	var objUserComments = document.getElementById('frmUserComments');

	if(objUserComments.txtarUserComments.value == "")
	{
		/*
		var txt = '<img src="'+DIR_WS_IMAGES+'icons/Help.png" width="64" height="64" border="0" alt="" align="absmiddle">Please enter the comment.';
		alertBox(txt, false);
		*/
		var txt = '<img src="'+DIR_WS_IMAGES+'icons/Help.png" width="64" height="64" border="0" alt="" align="absmiddle">Please enter the comment.';
		$.prompt(txt,{
			buttons:{Ok:-1},
			submit:function(v,m,f){
				if(v == -1)
				{
					jQuery.ImpromptuClose();
					$('#btnComments').removeAttr("disabled");
				}
				return false;
			}
		});
	}
	else
	{
		//var data = ajaxPost(objUserComments);
		var data = objUserComments.txtarUserComments.value;
		var actionType = "ADD_COMMENTS";

		/*----- Replacing "&" with "and" ------ */
		replaceString = /&/g;
		data = data.replace(replaceString, "and");
		/* ----------- */

		$.ajax({
			type: "POST",
			url: HTTP_SERVER+DIR_WS_HTTP+"ajaxDownloadSingle.php",
			data: "actionType="+actionType+"&itemID="+itemID+"&type="+type+"&authorID="+authorID+"&txtarUserComments="+data,
			success: function(msg){
				if (msg == 'success')
				{
					/*
					var txt = '<img src="'+DIR_WS_IMAGES+'icons/check.png" width="64" height="64" border="0" alt="" align="absmiddle">Comment Added Successfully.';
					alertBox(txt, false);
					*/
					var txt = '<img src="'+DIR_WS_IMAGES+'icons/check.png" width="64" height="64" border="0" alt="" align="absmiddle">Comment Added Successfully.';
					$.prompt(txt,{
						buttons:{Ok:-1},
						submit:function(v,m,f){
							if(v == -1)
							{
								jQuery.ImpromptuClose();
								getComments(itemID, type);
							}
							return false;
						}
					});					
				}
				else if (msg == 'errorSql')
				{
					/*
					var txt = "Error executing sql query.";
					alertBox(txt, true);
					$('#btnComments').removeAttr("disabled");
					*/
					var txt = "Error executing sql query.";
					$.prompt(txt,{
						buttons:{Ok:-1},
						submit:function(v,m,f){
							if(v == -1)
							{
								jQuery.ImpromptuClose();
								$('#btnComments').removeAttr("disabled");
							}
							return false;
						}
					});
				}
			}
		});
	}
}

/* Report Content Start */
function reportContent(type, itemID)
{
	var txt = "";
	$.ajax({
		type: "POST",
		url:HTTP_SERVER+DIR_WS_HTTP+"ajaxDownloadSingle.php",
		data: "actionType=REPORT_CONTENT_FORM&type="+type+"&itemID="+itemID,
		success: function(msg){
			txt = msg;
			$.prompt(txt,{
				buttons:{Ok:-1,Cancel:0},
				top:'10%',
				submit:function(v,m,f){
					if(v == 0)
					{
						jQuery.ImpromptuClose();
					}
					else if(v == -1)
						saveReportContent(v,m,f,type,itemID);
					return false;
				}
			});
		}
	});
}

function saveReportContent(v,m,f,type,itemID)
{
	if (v)
	{
		var actionType = "SAVE_REPORT_CONTENT";
		var ObjForm = document.getElementById('frmReportContent');
		var strData = ajaxPost(ObjForm);
		$.ajax({
			type: "POST",
			url:HTTP_SERVER+DIR_WS_HTTP+"ajaxDownloadSingle.php",
			data: "actionType="+actionType+"&"+strData,
			success: function(msg)
			{
				if (msg == 'success')
				{
					var txt = '<img src="../images/icons/check.png" width="64" height="64" border="0" alt="" align="absmiddle">The '+ type +' is reported to the administrator, and action will be taken accordingly.';
					alertBox(txt, false);
					getProfileCustText(strUserID);
				}
				else if (msg == 'errorSql')
				{
					var txt = "Error executing sql query.";
					alertBox(txt, true);
					getProfileCustText(strUserID);
				}
			}
		});
		jQuery.ImpromptuClose();
	}
}

/* Report Content End */

function postReply(commentID, itemID, type)
{
	var aa = $('#postReply').html();
	$('#postReply').html("");						

	$.prompt(aa,{
			buttons:{Ok:-1,Cancel:0},
			submit:function(v,m,f){
				if(v == 0)
				{
					$('#postReply').html(aa);
					jQuery.ImpromptuClose();
				}
				else if(v == -1)
				{
					jQuery.ImpromptuClose();
					var objUserComments = document.getElementById('frmPostCommentReply');

					if(objUserComments.txtarUserCommentsReply.value == "")
					{
						var txt = '<img src="'+DIR_WS_IMAGES+'icons/Help.png" width="64" height="64" border="0" alt="" align="absmiddle">Please enter the comment.';
						alertBox(txt, false);
					}
					else
					{
						//var data = ajaxPost(objUserComments);
						var data = objUserComments.txtarUserCommentsReply.value;
						var actionType = "ADD_COMMENTS";

						/*----- Replacing "&" with "and" ------ */
						replaceString = /&/g;
						data = data.replace(replaceString, "and");
						/* ----------- */

						$.ajax({
							type: "POST",
							url: HTTP_SERVER+DIR_WS_HTTP+"ajaxDownloadSingle.php",
							data: "actionType="+actionType+"&itemID="+itemID+"&type="+type+"&authorID="+authorID+"&txtarUserComments="+data+"&parentID="+commentID.id,
							success: function(msg){
								if (msg == 'success')
								{
									var txt = '<img src="'+DIR_WS_IMAGES+'icons/check.png" width="64" height="64" border="0" alt="" align="absmiddle">Comment Added Successfully.';
									$.prompt(txt,{
										buttons:{Ok:-1},
										submit:function(v,m,f){
											if(v == -1)
											{
												jQuery.ImpromptuClose();
												getComments(itemID, type);
											}
											return false;
										}
									});					
								}
								else if (msg == 'errorSql')
								{
									var txt = "Error executing sql query.";
									$.prompt(txt,{
										buttons:{Ok:-1},
										submit:function(v,m,f){
											if(v == -1)
											{
												jQuery.ImpromptuClose();
											}
											return false;
										}
									});
								}
							}
						});
					}
					$('#postReply').html(aa);
				}
				return false;
			}
		});
		$('.jqiclose').hide();
}