// 31-10-2008 - AL - created file
function showDiv(div_id) {
	document.getElementById(div_id).style.visibility = 'visible';
}

function hideDiv(div_id) {
	document.getElementById(div_id).style.visibility = 'hidden';
}

function selectTerminology(selected) {
	document.getElementById("k").value = selected;
}

function changeBGColor(id, bgcolor, textcolor) {
	document.getElementById(id).style.backgroundColor  = bgcolor;
	document.getElementById(id).style.color  = textcolor;
}

function getTerminologies() {
	var Terminology = document.getElementById("k").value;
	var url = '/inc/get_terminologies.php?key=' + Terminology;
	var httpRequest;
	if(Terminology.length > 2) {
		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
			httpRequest = new XMLHttpRequest();
			if (httpRequest.overrideMimeType) {  httpRequest.overrideMimeType('text/xml'); }
		} 
		else if (window.ActiveXObject) { // IE
			try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); } 
				catch (e) {
				   try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } 
				   catch (e) {}
				}
		}
	
		if (!httpRequest) {
			alert('Giving up :( Cannot create an XMLHTTP instance');
			return false;
		}
	
		httpRequest.onreadystatechange = function() { showterminologies(httpRequest); };
		httpRequest.open('GET', url, true);
		httpRequest.send(null);
	}
}

function showterminologies(httpRequest) {

	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			if (httpRequest.responseText == "") { 
				document.getElementById('terminologies_tip').innerHTML = '<div class="tip">ابحث بالعربي أو بلإنجليزي</div>'; 
			} else {
				document.getElementById('terminologies_tip').innerHTML = httpRequest.responseText;
			}
			showDiv('terminologies_tip');
		}
	}

}

function hideOnClick(id1, id2) {

	if(document.main_search.k.value == 'ابحث هنا') { 
		document.main_search.k.value = '';
	}
	if (document.addEventListener){
	  document.addEventListener('click', function() { hideOnCall(id1, id2); }, false); 
	} else if (document.attachEvent){
	  document.attachEvent('onclick', function() { hideOnCall(id1, id2); });
	}
	
}

var clickedElements = '';
function hideOnCall(id1, id2) {
	elements = document.getElementsByTagName('*');
	for(i=0; i<elements.length; i++)
	{
		if (!elements[i].onclick) { // if element doesn't have an onclick handler 
			elements[i].onclick = function() {
									clickedElements += '_' + this.id;
								  } 
		}
	} 
	if(clickedElements.indexOf(id1) == -1) { hideDiv(id2); }
	clickedElements = '';
}


function checkForm()
{
	//name=document.questionForm.question_name.value;
	//email=document.questionForm.question_email.value;
	question=document.questionForm.question_question.value;
	
	/*
	if(name=="") {
		 alert("Please enter your name");
		 document.questionForm.question_name.focus();	
		 return false;
	}
	
	for(var a=0;a<name.length;a++) {
		if((name.charAt(a)== '<') || (name.charAt(a)== '>'))
		{
		
		 alert(" Please don't use the following signs: '>', '<'");
		document.questionForm.question_name.focus();	
		return false;
		}
	}
	
	if(email=="") {
		 alert("Please enter your email");
		 document.questionForm.question_email.focus();	
		 return false;
	}
	
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i=0; i < email.length ;i++)
	{
		if(ok.indexOf(email.charAt(i))<0) 
		{ 
			document.questionForm.question_email.focus();
			alert("The email address you have entered is not valid");
			return false;
		}	
	} 
	*/
	
	if(question=="") {
		 alert("الرجاء إدخال السؤال");
		 document.questionForm.question_question.focus();	
		 return false;
	}
		
	for(var j=0;j<question.length;j++) {
		if((question.charAt(j) == '<') || (question.charAt(j) == '>'))
		{
		 alert("يرجى عدم استخدام العلامات التالية: '>', '<'");
		document.questionForm.question_question.focus();	
		return false;
		
		}
	}
		
	if(question.length > 4000)
	{
		alert('السؤال الذي أدخلته تجاوز الحد المسموح به وهو ٨٠٠ كلمة');
		document.questionForm.question_question.focus();	
		return false;
	}
	
	document.questionForm.submit();

}


function submitUserAnswer()
{
	answer=document.answerForm.answer_answer.value;
	
	if(answer=="") {
		 alert("الرجاء إدخال الإجابة");
		 document.answerForm.answer_answer.focus();	
		 return false;
	}
		
	for(var j=0;j<answer.length;j++) {
		if((answer.charAt(j) == '<') || (answer.charAt(j) == '>'))
		{
		alert("يرجى عدم استخدام العلامات التالية: '>', '<'");
		document.answerForm.answer_answer.focus();	
		return false;
		
		}
	}
		
	if(answer.length > 4000)
	{
		alert('الإجابة التي ادخلتها تجاوزت الحد المسموح به وهو ٨٠٠ كلمة');
		document.answerForm.answer_answer.focus();	
		return false;
	}
	
	document.answerForm.submit();

}


function textCounter(field, maxlimit) {
	if (field.value.length> maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);		
}