/*********************************jp_functions.js***********************************
Created by	:	Vishnu Kumar Agarwal
Created on	:	10th Nov 2005
Modified On	:	14 Oct 2006
Description	:	contains all the javascript functions for the online Job Posting.
************************************************************************************/

//	Description	:	
function jp_limitCounrtyCity(id,str)
{
	var Ele = document.getElementById(id);
	var count = 0;
	for(var i = 1; i < Ele.length; i++)
	{
		if(Ele[i].selected)
		count++;
		if(count > 3)
		Ele[i].selected = false;
	}

	if(count > 3)
	alert("You cannot select more than 3 "+str);

	jp_LocationFromCity();
}

//	Description	:	Finds the cities values & labels from the countries' values
function jp_getCountryCity(formCountryId, formCityId, selectedCities)
{
	var countryEle = document.getElementById(formCountryId);
	var optCount = 0;
	var cityElement = document.getElementById(formCityId);
	var optSelect = new Option();
	var selCntry;
	var vallbl;

	var selCity_str = '';
	
	for(var i=1; i<cityElement.length; i++)
		if(cityElement[i].selected)
			selCity_str += cityElement[i].value + ",";	//	selected city at the same form

	if(selectedCities != "")	//	selected cities from the edit flow
		selCity_str += selectedCities + ",";
			
	optSelect.text = "-- Select --";
	optSelect.value = "select";
	cityElement.length = 0;
	cityElement[0] = optSelect;

	var showFlag = 'false';
	var countryCount = 0;
	for(var count = 1; count < countryEle.length; count++)
	{
		if(countryEle[count].selected)
		{
			countryCount++;
			cityval_arr = common_city_arr[countryEle[count].value].split("#");//countryEle[count].value.split("|X|");
			//country count is included here as to stop the country dd to populate the cities corresponding to more than three countries
			for (var i = 1; i <= cityval_arr.length && countryCount <= 3; ++i)
			{
				var opt = new Option();
				vallbl = cityval_arr[i-1].split("$");
				opt.text = vallbl[1];
				
				opt.value = vallbl[1];
				
				if(eval(selCity_str.indexOf(opt.value + ",")) > -1)
				{
					opt.selected = "selected";
					
					if(vallbl[0] == "1000")
						showFlag = 'true';
				}

				optCount = optCount+1;

				cityElement[optCount] = opt;
			}
		}
	}
	
	if(showFlag == 'true')
		jp_showHide('OTHER_CITY_ID', '');
	else
	{
		document.getElementById('OTHER_ID').value = '';
		jp_showHide('OTHER_CITY_ID', 'none');
	}
}

//	Description	:	show or hide the Other location box according to city selected
function jp_LocationFromCity()
{
	var cityElement = document.getElementById('CITIES_ID');
	var showFlag = 'false';

	if(eval(cityElement.length) == 1)
	{
		jp_showHide('OTHER_CITY_ID', 'none');
	}

	for(var count = 0; count < cityElement.length; count++)
	{
		if(cityElement[count].selected)
		{
			if((cityElement[count].text).match("Other"))
			{
				jp_showHide('OTHER_CITY_ID', '');
				showFlag = 'true';
				break;
			}
			else if(showFlag == 'false')
			{
				document.getElementById('OTHER_ID').value = '';
				jp_showHide('OTHER_CITY_ID', 'none');
			}
		}
	}
}

//	Description	:	Shows or hide the element
function jp_showHide(id, flag)
{
	document.getElementById(id).style.display = flag;
}

//	Description	:	Finds the address details from the # seperated values of address dropdown.
function jp_setAddressDetails(theForm)
{
	var addDetEle = document.getElementById('SELADDR_ID');
	if(addDetEle.value != "select")
	{
		det_arr = addDetEle.value.split("|X|");//		det_arr = det[1].split("#");

		document.form_prev.COUNTRY_TXT.value = det_arr[1];
		document.form_prev.CITY_TXT.value = det_arr[2];
		document.form_prev.STREET_TXT.value = det_arr[3];
		document.form_prev.POSTAL_TXT.value = det_arr[4];
	}
	else
	{
		document.form_prev.COUNTRY_TXT.value = '';
		document.form_prev.CITY_TXT.value = '';
		document.form_prev.STREET_TXT.value = '';
		document.form_prev.POSTAL_TXT.value = '';
	}
}

//	Description	:	Validates the postal code field.
function jp_isValidPostalCode(name)
{
	str = name;
	str = str.replace(/[a-z0-9 -]/ig,"");
	return str;
}


function je_validate_subscription(theForm,msg)
{
	if((theForm.rj.checked) && (theForm.jc.checked))
	{
		alert(msg);
		return false;
	}
	else
	{
		return true;
	}
}


//	Description	:	Limits max. nationalities/cand locations to 4 -- Added By Nishant on 21 une 2006
function jp_limitFields(id,str)
{
	var Ele = document.getElementById(id);
	var count = 0;

	for(var i = 1; i < Ele.length; i++)
	{
		if(Ele[i].selected)
		count++;
		if(id == 'GRADCOURSE_ID' || id == 'PGCOURSE_ID' || id == 'PPGCOURSE_ID')
		{
			if(count > 2)
			Ele[i].selected = false;
		}
		else
		if(count > 4)
		Ele[i].selected = false;
	}
	if(id == 'GRADCOURSE_ID' || id == 'PGCOURSE_ID' || id == 'PPGCOURSE_ID')
	{
		if(count > 2)
		alert("You cannot select more than 2 "+str);
	}
	else
	if(count > 4)
	alert("You cannot select more than 4 "+str);

}

//	Description	:	populates the spec corresponding to course
function jp_getGradCourseSpec(formCourseId, formSpecId, courseFlag, selectedSpec)
{
	var coursEle = document.getElementById(formCourseId);
	var optCount = 0;
	var specElement = document.getElementById(formSpecId);
	var optSelect = new Option();
	var selCourse;
	var vallbl;
	
	if(courseFlag == 'GRAD')
		common_spec_arr = common_gradspec_arr;
		
	else if(courseFlag == 'PG')
		common_spec_arr = common_pgspec_arr;
		
	else if(courseFlag == 'PPG')
		common_spec_arr = common_ppgspec_arr;
	
	else
		return false;

	var selSpec_str = '';
	for(var i = 1; i<specElement.length; i++)
		if(specElement[i].selected)
			selSpec_str += specElement[i].value + ",";	//	selected Spec at the same form

	if(selectedSpec != "")	//	selected course from the edit flow
		selSpec_str += selectedSpec + ",";

	optSelect.text = "-- Select --";
	optSelect.value = "select";
	specElement.length = 0;
	specElement[0] = optSelect;

	var showFlag = 'false';
	var courseCount = 0;

	for(var count = 1 ; count < coursEle.length; count++)
	{
		if(coursEle[count].selected)
		{
			courseCount++;
			specval_arr = common_spec_arr[coursEle[count].value].split("#");

			for (var i = 1; i <= specval_arr.length && courseCount <= 4; ++i)
			{
				var opt = new Option();
				vallbl = specval_arr[i-1].split("$");
				
				if(i == 1)
				{
					var optSelect = new Option();
					optSelect.text = "-- " + coursEle[count].text + " --";
					optSelect.value =  "select";
					
					if(optCount == 0)
					{
						specElement[0] = optSelect;
					}
					else
					{
						optCount = optCount + 1;
						specElement[optCount] = optSelect;
					}
				}
				
				//	if any, value2 => remove any
				if(specval_arr.length == 2)
				{
					if(i == 1)
						continue;
					
					if(i == 2)
						opt.selected = "selected";
				}
				
//				if((specval_arr.length != 2) && (eval(selSpec_str.indexOf(coursEle[count].value + "." + vallbl[0])) <= -1))
				//	this conndition is here to select 'Any' option
				if((specval_arr.length != 2) && (eval(selSpec_str.indexOf(coursEle[count].value + ".")) <= -1))
				{
					if(i == 1)
						opt.selected = "selected";
				}
				
				opt.text = vallbl[1];
				opt.value = coursEle[count].value + "." + vallbl[0];

				if(eval(selSpec_str.indexOf(opt.value + ',')) > -1)
					opt.selected = "selected";
	
				optCount = optCount + 1;
	
				specElement[optCount] = opt;
			}
		}
	}
}

function jp_checkcount(formSpecId,formCourseId)
{
	var specEle = document.getElementById(formSpecId);

	var tempArr = new Array();
	var temp = "";
	var prev = "";
	var cnt = 1;
	var toShow = false;

	for(count = 1 ; count < specEle.length; count++)
	{
		if(specEle[count].selected)
		{
			tempArr = specEle[count].value.split(".");
			temp = tempArr[0];

			if(prev == temp)
				cnt++;
			else
				cnt = 1;

			if(cnt > 2)
			{
				toShow = true;
				specEle[count].selected = false;
			}

			prev = temp;
		}
	}

	if(toShow)
		alert("You cannot select more than 2 specializations for a particular " + formCourseId);
}

function jp_ShowHideDetails(response_for)
{
	var isChecked;
	
	if(response_for =="response_at_address")
		isChecked = document.form_prev.ADDRESS_CHK.checked;
		
	else if(response_for =="response_at_contact")
		isChecked = document.form_prev.CONTACT_CHK.checked;

	else if(response_for =="response_at_email" && document.form_prev.EMAIL_CHK)
		isChecked = document.form_prev.EMAIL_CHK.checked;

	if(isChecked == true)
		document.getElementById(response_for).style.display = ''; // display will be there
	
	else if(isChecked == false)
		document.getElementById(response_for).style.display = 'none';	//hide

}

function jp_showHideCourse(chkid, courseid)
{
	
	if(document.getElementById(chkid).checked == true)
		document.getElementById(courseid).style.display = '';
	else
		document.getElementById(courseid).style.display = 'none';
}

function jp_ShowHideSpec(dofor)
{
	if(dofor == "grad_hide, pg_hide, ppg_hide")
	{
		//GRAD
		var courseEle = document.getElementById('GRADCOURSE_ID');
		var specEle = document.getElementById('GRADSPEC_ID');
		count_spec_grad = 0;
		//PG
		var pgcourseEle = document.getElementById('PGCOURSE_ID');
		var pgspecEle = document.getElementById('PGSPEC_ID');
		count_spec_pg = 0;
		//PPG
		var ppgcourseEle = document.getElementById('PPGCOURSE_ID');
		var ppgspecEle = document.getElementById('PPGSPEC_ID');
		count_spec_ppg = 0;
	
		//GRAD spec
		for(count = 0; count < specEle.length; count++)
			if(specEle[count].selected)
				++count_spec_grad;
		//PG spec
		for(count = 0; count < pgspecEle.length; count++)
			if(pgspecEle[count].selected)
				++count_spec_pg;
		//PPG spec
		for(count = 0; count < ppgspecEle.length; count++)
			if(ppgspecEle[count].selected)
				++count_spec_ppg;
	
		if( count_spec_grad == 0)
		{
			document.getElementById("span_gradspec").style.display = 'none';
			document.getElementById("GRADSPEC_ID").style.display = 'none';
		}
		
		if( count_spec_pg == 0)
		{
			document.getElementById("span_pgspec").style.display = 'none';
			document.getElementById("PGSPEC_ID").style.display = 'none';
		}
		
		if( count_spec_ppg == 0)
		{
			document.getElementById("span_ppgspec").style.display = 'none';
			document.getElementById("PPGSPEC_ID").style.display = 'none';
		}
	}

	if(dofor == 'grad')
	{
		document.getElementById("span_gradspec").style.display = '';
		document.getElementById("GRADSPEC_ID").style.display = '';
	}

	if(dofor == 'pg')
	{
		document.getElementById("span_pgspec").style.display = '';
		document.getElementById("PGSPEC_ID").style.display = '';
	}

	if(dofor == 'ppg')
	{
		document.getElementById("span_ppgspec").style.display = '';
		document.getElementById("PPGSPEC_ID").style.display = '';
	}

}

function jp_removeEmploymentDetails(param)
{
	if(param == 'click' && document.form_prev.COMPENSATION.value== "Any Other Employment Benefits")
		document.form_prev.COMPENSATION.value = "";
		
	else if(param == 'blur' && document.form_prev.COMPENSATION.value == "")
		document.form_prev.COMPENSATION.value = "Any Other Employment Benefits";
}

function jp_HideOtherCity(param)
{
	if(param == 'click' && document.form_prev.OTHER_CITY.value== "Enter Your Location")
		document.form_prev.OTHER_CITY.value = "";

	else if(param == 'blur' && document.form_prev.OTHER_CITY.value == "")
		document.form_prev.OTHER_CITY.value = "Enter Your Location";
}

function jp_deselect(id)
{
	if(document.getElementById(id))
	{
		var i;
		var ele = document.getElementById(id);
		var length = ele.length;
		
		for(i = 0; i < length; i++)
			ele[i].selected = false;
	}
}

function jp_setColor(id_arr, color)
{
	var len = id_arr.length;
	var i = 0;
	
	for(; i < len; i++)
		document.getElementById(id_arr[i]).style.color = color;
}

function jp_fillFilter(obj, id)
{
	if(ele = document.getElementById(id))
	{
		txt = '';
		if(obj.options[obj.selectedIndex].value != "select")
			txt += obj.options[obj.selectedIndex].text;
		else
			txt += "No value selected";
		ele.innerHTML = ": " + txt;
	}
		
}

function jp_expfillFilter(obj, id)
{
	if(ele = document.getElementById(id))
	{
		var error = '';
		return_arr = jp_validateExp(obj, error, 0, 1);
		
		if(return_arr[0] != '')
		{	
			ele.innerHTML = ": No value selected";
			return;
		}
		
		var dd1 = document.getElementById("id_minexpdd");
		var dd2 = document.getElementById("id_maxexpdd");
		var minval = dd1[dd1.selectedIndex].value;
		var maxval = dd2[dd2.selectedIndex].value;	
		
		if(minval == "30plus")
			minval = "30+";
			
		if(maxval == "30plus")
			maxval = "30+";
			
		if(minval == maxval)
			exptxt = maxval + " years";
		
		else if(minval != maxval)
			exptxt = minval + " - " + maxval + " years";
			
		ele.innerHTML = ": " + exptxt;
	}
}
function jp_multifillFilter(obj, id)
{
	if(ele = document.getElementById(id))
	{
		var txt = '';
		var len = obj.options.length;
		
		for (i = 0; i< len; i++) 
		{
	    	if (obj.options[i].selected && obj.options[i].value != "select")
		       txt += obj.options[i].text + ", ";
		}
		
		if(txt.length > 1)
			txt = txt.substring(0, txt.length - 2);
		else
			txt += "No value selected";
		ele.innerHTML = ": " + txt;
	}
}


