// Training finder script
function courseFinder(){
	var type =null;
	var category=document.getElementById("category").value;
	var trainingType=document.getElementById("trainingType").value;
	var param = "";
	if(trainingType != "ALL"){
		var param = "web_type="+trainingType;
	} 
	if(category != "null"){
		if(param != ""){param = param + "&";}
		var param = param + "category_id="+category;
	}
	document.getElementById("webForm").action="/ecommerce/control/compliance-training-search?"+param;
	document.getElementById("webForm").submit();
	return true;
}

//Compliance search function
function searchtx(){
	var type =null;
	var j = 0;
	var radio_buttons = new Array();
	var the_form = window.document.getElementById("coSearch");
	var query = document.getElementById("query").value;
	if(query.length < 1){
		alert("Enter the search string");
		document.getElementById("query").focus();
		return false;
	}else{
		document.getElementById("source").value = "training_view";
		document.getElementById("project").value = "Compliance-online-training"; 
		document.getElementById("coSearch").action="http://search.complianceonline.com/search/cgi-bin/query-meta?v:project=Compliance-online-training&query="+query;
		document.getElementById("coSearch").submit(); 
		return true;
	}
}

// Login Validation
function validateForm(){
	if(document.formDetails.USERNAME.value==""){
		alert("Please enter your Email ID.")
		document.formDetails.USERNAME.focus();
		return false;
	}else if(document.formDetails.PASSWORD.value==""){
		alert("Please enter your Password.")
		document.formDetails.PASSWORD.focus();
		return false;
	}else{
		//writeSessionCookie ("username", document.formDetails.USERNAME.value);
		document.formDetails.action ="/ecommerce/control/login";
		document.formDetails.submit();
		return true;
	}
}

// Forgot password
function forgot(){
	if(document.formDetails.USERNAME.value==""){
		alert("Please enter your Email ID.")
		document.formDetails.USERNAME.focus();
		return false;
	}else{
		var confirmStatus = confirm("Your password will be reset and sent to your Email ID");		
		if(!confirmStatus) return false;
		//document.formDetails.action = "http://www.complianceonline.com/ecommerce/control/../../cms/cms/system/modules/org.opencms.portal/jsptemplates/forgotpassword.jsp?ref=forcedlogin";
		alert("Please wait for the confimation. Do not Refresh/Reload the page. Press OK to continue");
		document.formDetails.action = "/ecommerce/control/forgotpassword_send";
	}
	document.formDetails.submit();
}

// Search box function
function clearBox(field){
	var x = document.getElementById(field);
	if(field=="query"){
		y = "Compliance Search";
	}else{
		y = "Search Answers";
	}
	if(x.value==y){
		x.value="";
	}else{
		x.select();
	}
	return true;
}

// Search box function
function fillBox(field){
	var x = document.getElementById(field);
	if(field=="query"){
		y = "Compliance Search";
	}else{
		y = "Search Answers";
	}
	if(x.value==""){
		x.value=y;
	}
	return true;
}

// Pagination

function nextPage(Obj){
var x = Obj.options[Obj.selectedIndex].value;
document.pagForm.action=x;
document.pagForm.submit();
return false;
}
var tagsLimit = 5; // Maximum tags limit
function validatePostYourQuestionForm(objForm) {
	if(objForm.question.value == "") {
		alert('Enter your question');
		objForm.question.focus();
		return false;
	}
	if(objForm.question_category_id.value == -1) {
		alert('Choose the category');
		return false;
	}
	if( !checkTagsLength(objForm.tags) ) {
		objForm.tags.focus();
		return false;
	}
/*	if(objForm.description.value == "") {
		alert('Enter details for your question');
		objForm.description.focus();
		return false;
	}*/
	
	if(document.frmPostQuestion.oPoint.value!=""){
		if(numCheck(document.frmPostQuestion.oPoint.value)){
			alert("Please enter a number");
			document.getElementById('oPoint').select();
			return false;
		}
	}
	
	if(!checkRadio2()){
	
	alert("Please select/enter a point");
	document.getElementById('oPoint').focus();
	return false;
	}	
}
function checkRadio2(){
	frmName = "frmPostQuestion";
	rbGroupName = "points";
	var radios = document[frmName].elements[rbGroupName];
	for (var i=0; i <radios.length; i++) {
		if (radios[i].checked) {
			if(i==4){
				if(document.frmPostQuestion.oPoint.value==""){
					document.frmPostQuestion.oPoint.select();
					return false;
				}else{
					document.frmPostQuestion.point.value=document.frmPostQuestion.oPoint.value;
					document.frmPostQuestion.submit();
					return true;
				}
			}else{
			document.frmPostQuestion.point.value=radios[i].value;
			document.frmPostQuestion.submit();
			return true;
			}
		}
	}
	return false;
}

function limitCharLength (objTextBox, limit, keyCode) {
	if(keyCode>0) return true;

	var content = objTextBox.value;
	window.status = content.length;
	var remainingLimit = (limit - content.length)
	if(remainingLimit <= 0)
		return false;
	return true;
}

function updateLimitOnUI (objTextBox, spanName, limit) {
	if(limit == undefined) limit=110;

	var content = objTextBox.value;
	window.status = content.length;

	var spanObj = document.getElementById(spanName);
	var remainingLimit = (limit - content.length);
	if(remainingLimit < 0 ) {
		var temp = content.substring(0,limit);
		objTextBox.value = temp;
		remainingLimit = 0;
	}

	spanObj.innerHTML = remainingLimit;
}

// initialize

if(document.getElementById("question") != null)
	updateLimitOnUI(document.getElementById("question"), "divQuestionLimit", 110);

if(document.getElementById("description") != null)
	updateLimitOnUI(document.getElementById("description"), "divQDescriptionLimit", 1000);


function checkTagsLength(obj){
	//var len = obj.value.split(/[\s]+/);
	var len = obj.value.split(/,+/);
	if(len.length > tagsLimit){
		alert("You cannot tag more than "+tagsLimit+" tags.");
		//obj.focus();
		return false;
	}
	return true;
}

function showSampleTags(objSelectBox) {
	var spanObj = document.getElementById("sampleTags");
	spanObj.innerHTML = "";
	if(objSelectBox.value == "-1") return ;
	var selectedValue = "cat_" + objSelectBox.value;
	var tagsValue = eval("" + selectedValue);

	if(tagsValue != "")
		spanObj.innerHTML = "(sample tags: " + tagsValue + ")";
}
function numCheck(str)
{	
	var string = str.length;
	var iChars = "1234567890";
	for (var i = 0; i < string; i++) {
	if (iChars.indexOf(str.charAt(i)) != -1)
		return false;
	}
	return true;
}
function validatePostAnswerForm(objForm) {
	if(objForm.answer.value == "") {
		alert('Enter your answer');
		objForm.answer.focus();
		return false;
	}
	return true;
}

function checkRadio (frmName, rbGroupName) {
 var radios = document[frmName].elements[rbGroupName];
 for (var i=0; i <radios.length; i++) {
  if (radios[i].checked) {
  
  document.pointForm.point.value=radios[i].value;
  document.pointForm.submit();
  }
 }
 return false;
}

function valFrm() {
 if (!checkRadio("pointForm","points")){
 if(document.pointForm.oPoint.value==""){
  alert("Please select/enter a point");
  }
  else{  document.pointForm.point.value=document.pointForm.oPoint.value;
  document.pointForm.submit();
  }
  }
return false;  
}
function deleteAns(ans){
if(confirm("Are you sure to delete?")){
document.deleteForm.answer_id.value=ans;
document.deleteForm.submit();
}
return false;
}
function searchAns(){
var query = document.getElementById('ansQuery');
	if(query.value==""){
		alert("Please enter the words to search");
		query.select();
		return false;
	}
}

