function valid(str)
{
	var i;
	for (i = 0; i < str.length; i++)
	{
		var charcode = str.charCodeAt(i);

		/* A-Z */
		if (charcode >= 0x41 && charcode <= 0x5A)
		{
			continue;
		}
		/* a-z */
		if (charcode >= 0x61 && charcode <= 0x7A)
		{
			continue;
		}
		/* 0-9 */
		if (charcode >= 0x30 && charcode <= 0x39)
		{
			continue;
		}
		/* . - _ 32*/
		if (charcode === 0x2D || charcode === 0x2E || charcode === 0x5F)
		{
			continue;
		}

		return false;
	}//end for(var i = 0; i < str.length; i++)
	return true;
}//end function valid

function testemail(obj)
{
	obj.value = obj.value.trim();
	var atpos = obj.value.indexOf("@");
	if (atpos === -1)
	{
		return false;
	}

	if (atpos === 0)
	{
		return false;
	}

	var dotpos = obj.value.indexOf('.', atpos + 2);

	if (dotpos === -1)
	{
		return false;
	}

	if (dotpos === (obj.value.length - 1))
	{
		return false;
	}

	if (obj.value.indexOf(".") === 0)
	{
		return false;
	}

	if (obj.value.indexOf("@")  === (obj.value.indexOf('.', obj.value.indexOf("@") + 1)) - 1)
	{
		return false;
	}

	if (obj.value.indexOf(".")  === (obj.value.indexOf('.', obj.value.indexOf(".") + 1)) - 1)
	{
		return false;
	}


	var fpart = obj.value.substring(0, atpos);
	var host = obj.value.substring(atpos + 1, dotpos);
	var domain = obj.value.substr(dotpos + 1);

	if (!(valid(fpart) && valid(host) && valid(domain)))
	{
		return false;
	}

	var afterat = obj.value.substr(atpos + 1);

	if (afterat.lastIndexOf('.') === (afterat.length - 1))
	{
		return false;
	}
	var i;
	for (i = 1; i < afterat.length; i++)
	{
		if (afterat.charAt(i) === '.' && afterat.charAt(i - 1) === '.')
		{
			return false;
		}
	}//end for(var i = 1; i < afterat.length; i++)

	return true;
}//end function testemail

function Capitalize(orig, re1)
{
	var result;
	if (re1 === "&")
	{
		result = "(amp)";
	}
	else if (re1 === "/")
	{
		result = "(xx)";
	}
	else if (re1 === "?")
	{
		result = "(p)";
	}
	else if (re1 === "\"")
	{
		result = "(quot)";
	}
	else if (re1 === "<")
	{
		result = "(lt)";
	}
	else if (re1 === ">")
	{
		result = "(gt)";
	}
	else if (re1 === "+")
	{
		result = "(pl)";
	}
	else if (re1 === "\\")
	{
		result = "(bk)";
	}
	return result;
}
function validLength(str)
{
	var mytool_arrayline = str.split("\n");
	for (var c = 0; c < mytool_arrayline.length; c++)
	{
		var mytool_array = mytool_arrayline[c].split(" ");
		for (var i = 0; i < mytool_array.length; i++)
		{
			if (mytool_array[i].length > 30)
			{
				return mytool_array[i].substring(0, 20) + "...";
			}
		}//end for(var i = 0; i < mytool_array.length; i++)
	}
	return 0;
}//end function valid

function viewComment(sText)
{
	if (sText !== 0)
	{
		document.getElementById('imageDIV').style.display = "none";
		document.getElementById('buttonDIV').style.display = "block";
		document.getElementById('msgDone').style.display = "block";
		document.getElementById('error_msg').style.display = "none";
		location.reload();
		//document.getElementById('buttonDIV').style.display = "block";
		document.comment_form.comment.value = '';
	}
}
function isIE()
{
	// only for Win IE 6+
	// But not in Windows 98, Me, NT 4.0, 2000
	var strBrwsr = navigator.userAgent.toLowerCase();
	if (strBrwsr.indexOf("msie") > -1 && strBrwsr.indexOf("mac") < 0)
	{
		if (parseInt(strBrwsr.charAt(strBrwsr.indexOf("msie") + 5)) < 6)
		{
			return false;
		}
		if (strBrwsr.indexOf("win98") > -1 ||
			strBrwsr.indexOf("win 9x 4.90") > -1 ||
			strBrwsr.indexOf("winnt4.0") > -1 ||
			strBrwsr.indexOf("windows nt 5.0") > -1)
		{
			return false;
		}
		return true;
	}
	else
	{
		return false;
	}
}
function validateData()
{
	document.getElementById('msgDone').style.display = "none";
	document.getElementById('error_msg').style.display = "none";
	var checkName = validLength(document.comment_form.name.value);
	if (checkName !== 0)
	{
		document.getElementById('error_msg').innerHTML = '<font color="#FF0000">كلمة ' + checkName + ' أطول من الحد المسموح به في الاسم </font>';
		document.getElementById('error_msg').style.display = "block";
		document.comment_form.name.focus();
		return false;
	}
	var checkComment = validLength(document.comment_form.comment.value);
	if (checkComment !== 0)
	{
		document.getElementById('error_msg').innerHTML = '<font color="#FF0000">كلمة ' + checkComment + ' أطول من الحد المسموح به في التعليق </font>';
		document.getElementById('error_msg').style.display = "block";
		document.comment_form.comment.focus();
		return false;
	}
	if (document.comment_form.name.value.trim() === "")
	{
		document.getElementById('error_msg').innerHTML = '<font color="#FF0000">الرجاء إدخال  الإسم </font>';
		document.getElementById('error_msg').style.display = "block";
		document.comment_form.name.focus();
		return false;
	}
	else if (document.comment_form.email.value.trim() === "")
	{
		document.getElementById('error_msg').innerHTML = '<font color="#FF0000">الرجاء إدخال  البريد الإلكتروني </font>';
		document.getElementById('error_msg').style.display = "block";
		document.comment_form.email.focus();
		return false;
	}
	else if (!testemail(document.comment_form.email))
	{
		document.getElementById('error_msg').innerHTML = '<font color="#FF0000">الرجاء التأكدّ من  البريد الإلكتروني </font>';
		document.getElementById('error_msg').style.display = "block";
		document.comment_form.email.focus();
		return false;
	}
	else if (document.comment_form.comment.value.trim() === "")
	{
		document.getElementById('error_msg').innerHTML = '<font color="#FF0000">الرجاء كتابة تعليقك</font>';
		document.getElementById('error_msg').style.display = "block";
		document.comment_form.comment.focus();
		return false;
	}
	else
	{
		return true;
	}
}//end function

function addComment(crumb)
{
	if (validateData())
	{
		document.getElementById('buttonDIV').style.display = "none";
		document.getElementById('imageDIV').style.display = "block";
		var comment = document.comment_form.comment.value.replace('/([&\//?"><+])/g', Capitalize);
		var name = document.comment_form.name.value.replace('/([&\//?"><+])/g', Capitalize);
		var chk = "";
		if (document.comment_form.trackComments.checked)
		{
			chk = "yes";
		}
		//var type = document.getElementById('typeArit').value;
		AJAXGet(base_url + "addComment.php?crumb=" + crumb + "&trackComment=" + chk + "&sender_id=" + document.comment_form.oid.value + "&type=0&comment=" + comment + "&email=" + document.comment_form.email.value + "&name=" + name + "&title=" + (document.tellForm ? document.tellForm.title.value.replace('/([?])/g', "(q)") : "") + "&link=" + (document.tellForm ? document.tellForm.link.value : ""), viewComment, false, false);
		setTimeout("viewComment('done')", 10000);
	}
}

