function hideSpan(spanId){	
	$(spanId).innerHTML='';
}

function on_err(id,span_id,err_id,errMsg,iserror){
	//$(span_id).style.color="red";
	//$(span_id).style.fontWeight="bold";
	$(span_id).className="MJLabel-error";
	$(err_id).innerHTML=errMsg;
	if (iserror==1){
		id.focus();
	}
}

function validate(){
	
	$('infoTable').style.display='none';
	var iserror=0;

	var spanfields=$('span_id').value.split(',');
	for (var i=0;i<spanfields.length;i++){
		var f=$(spanfields[i]);
		if (!f){ continue;}
		//f.style.color="#990000";
		//f.style.fontWeight="normal"; 
		f.className="MJLabel";
	}
	var errfields=$('err_span').value.split(',');
	for (var i=0;i<errfields.length;i++){
		var f=$(errfields[i]);
		if (!f){continue;}
		f.innerHTML="";
	}
	
	if (iserror>0){
		if (iserror == 1){
			error_msg=" There is 1 error in the form. Please correct the error and re-submit the page.";
		} else{
			error_msg=" There are "+iserror+" errors in the form. Please correct the errors and re-submit the page.";
		}
		
		$('span_head').style.color="red";
		$('span_head').style.fontWeight="bold";
		$('span_head').innerHTML=error_msg;
		$('span_head').style.display='';
		$('infoTable').style.display='';
		alert(error_msg);
		return false;
	} else{

		var countryCount = document.getElementById('countryCount').value;
		var locationCount = document.getElementById('joblocationCount').value;
		var orgTypeCount = document.getElementById('orgTypeCount').value;
		
		//var preferred_orgtype="";
		var preferred_location="";
		var jobpreforg1="";
		var jobpreforg2="";
		for(i = 0; i < parseInt(orgTypeCount); i++)
		{
			if(document.getElementById('org_nature1'+i).checked)
			jobpreforg1+=","+ document.getElementById('org_nature1'+i).value;
			//alert(jobpreforg1);
			
			if(document.getElementById('org_nature2'+i).checked)
			jobpreforg2+=","+ document.getElementById('org_nature2'+i).value;
				
		}
		
		document.getElementById('jobPref_orgn1').value = jobpreforg1;
		document.getElementById('jobPref_orgn2').value = jobpreforg2;
		
		for(i = 0; i < parseInt(countryCount); i++)
		{
			if(document.getElementById('oloc'+i).checked)
				preferred_location += ","+ document.getElementById('oloc'+i).value;
				
		}
		
		document.getElementById('pref_outside_nepal').value = preferred_location;
		preferred_location = "";
		
		for(i = 0; i < parseInt(locationCount); i++)
		{
			if(document.getElementById('loc'+i).checked)
				preferred_location += ","+ document.getElementById('loc'+i).value;
				
		}
		
		document.getElementById('pref_inside_nepal').value = preferred_location;

		return true;
	}
}

function isInteger(iNumber){
	var i;
	for (i=0;i<iNumber.length;i++){
		var c=iNumber.charAt(i);
		if (!isDigit(c)) 
				return false;
	}
	return true;
}

function isDigit(c){
	 return ((c>="0") && (c<="9"))
}

function trim(inputString){
   	if (typeof inputString != "string")
   		return inputString; 
    var retValue=inputString;
    var ch=retValue.substring(0, 1);
    while (ch == " " || ch =="\n"){
    	retValue=retValue.substring(1, retValue.length);
      	ch=retValue.substring(0, 1);
   	}
   	ch=retValue.substring(retValue.length-1, retValue.length);
  	while (ch == " "){
      	retValue=retValue.substring(0, retValue.length-1);
      	ch=retValue.substring(retValue.length-1, retValue.length);
   	}
   	while (retValue.indexOf("  ") != -1){
      	retValue=retValue.substring(0, retValue.indexOf("  "))+retValue.substring(retValue.indexOf("  ")+1, retValue.length);
   	}
   	return retValue;
}

function checkDate(day, month, year){
	if (day.substring(0, 1)==0)
    	day=day.substring(1, day.length);
	if (month.substring(0, 1)==0)
		month=month.substring(1, month.length);
	// since jan equals one and not zero, hence thirteen elements in the array.
	var no_of_days_in_month=new Array(0,31,28,31,30,31,30,31,31,30,31,30,31)
	if (month>=1 && month<=12 && day>= 1 && day<=31 && year>=0){
		//Handling february, special case.
		if (month == 2){
			if ( (year%4==0 && year%100 != 0) || year%400 == 0 ){
				no_of_days_in_month[month]=29
			}
		}
		if (day>=1 && day<=no_of_days_in_month[month]){
			return true;
		} else
			return false;
	} else
		return false;
}

function countLocIn(cb_id){
	var count=parseInt(document.MJReg1.locInsideNepal.value);
	if (document.MJReg1.optJobArea[0].checked){
		$(cb_id).checked=false;
		return;
	}
	if ($(cb_id).checked){
		count=count+1;
		if (count > 15){
			alert("You can select only 15 locations.");
			$(cb_id).checked=false;
			count=count -1;
		}
	}
	else 
		count=count - 1;
	document.MJReg1.locInsideNepal.value=count;
}

function countLocOut(cb_id){
	var count=parseInt(document.MJReg1.locOutsideNepal.value);
	if ($(cb_id).checked){
		count=count+1;
		if (count > 10){
			alert("You can select only 10 locations.");
			$(cb_id).checked=false;
			count=count -1;
		}
	}
	else
		count=count - 1;
	
	document.MJReg1.locOutsideNepal.value=count;
}

function countOT(cb_id){
	var count=parseInt(document.MJReg1.countOrgType.value);
	if ($(cb_id).checked){
		count=count+1;
		if (count > 10){
			alert("You can select only 10 options.");
			$(cb_id).checked=false;
			count=count - 1;
		}
	} else 
		count=count - 1;
	document.MJReg1.countOrgType.value=count;
}

function clearAllChkBoxes(){
	var countryCount=$('countryCount').value;
	var locationCount=$('joblocationCount').value;
	var orgTypeCount=$('orgTypeCount').value;
	
	for (i=0; i<=parseInt(orgTypeCount); i++){
		$('org'+i).checked=false;
	}
	for (i=0; i < parseInt(countryCount); i++){
		$('oloc'+i).checked=false;
	}
	for (i=0; i<=parseInt(locationCount); i++){
		$('loc'+i).checked=false;
	}
	$('locInsideNepal').value=0;
	$('locOutsideNepal').value=0;
	$('countOrgType').value=0;

	$('pref_other').disabled=true;
	$('pref_other').value="";
}

function clearChkBox(chkBox, count, countCheck){
	for (i=0; i<=parseInt(count); i++){
		$(''+chkBox+i).checked=false;
	}
	$(countCheck).value=0;
	return false;
}

function disableCB(chkBox, count){
	n=$(count).value;
	for (i=0; i<=n; i++){
		$(chkBox+i).disabled=true;
	}
}

function enableCB(chkBox, count){
	n=$(count).value;
	for (i=0; i<=n; i++){
		document.getElementById(chkBox+i).disabled=false;
	}
}

