var locationsCountry = new Array();
var locationsState = new Array();
var locationsCountryName = new Array();
var locationsStateName = new Array();
var nLocations=0;
function addPreferedLocation(countryControlId,stateControlId){

	var i;
	var separator='';	
	var combinedName;
	
	if (nLocations >= 3){
		document.getElementById('max-reached-loc').style.visibility  = 'visible';
		return;
	}
	
	i=0;
	while((document.getElementById('countryId' + (i+1)).value>0) && (i < 3)){
		locationsCountry[i] =  document.getElementById('countryId' + (i+1)).value;
		locationsState[i] = document.getElementById('stateId' + (i+1)).value
		combinedName = document.getElementById('location' + (i+1)).innerHTML;
		locationsCountryName[i] = combinedName.split("/")[0];
		locationsStateName[i] = combinedName.split("/")[1];
		++i;
	}
	nLocations = i;
	
	countryControl = document.getElementById(countryControlId)
	countryId = countryControl.value;
	countryName = countryControl.options[countryControl.selectedIndex].text;
	
	stateControl = document.getElementById(stateControlId);	
	stateId = stateControl.value;
	stateName = stateControl.options[stateControl.selectedIndex].text;
	
	
	locationsCountry[nLocations] = countryId;
	locationsState[nLocations] = stateId;
	locationsCountryName[nLocations] = countryName;
	locationsStateName[nLocations] = stateName;	
	++nLocations;
	
	//document.getElementById('desiredCountry').value = '';
	//document.getElementById('desiredState').value = '';
	
	for (i=0; i < nLocations; ++i){

		(i)?separator = ',':separator = '';
		document.getElementById('location'+(i+1)).innerHTML =  locationsCountryName[i] + '/' + locationsStateName[i]
		document.getElementById('locationDelete'+(i+1)).innerHTML = '<a href="" style="" onclick="deleteLocation('+ i + ');event.returnValue=false;return(false);">Delete</a>';	
		document.getElementById('countryId'+(i+1)).value = locationsCountry[i];
		document.getElementById('stateId'+(i+1)).value = locationsState[i];	
	}
	
	return(false);
}


function deleteLocation(locationIndex){
	var i;
	var combinedName;
	
	
	i=0;
	while((i < 3) && (document.getElementById('countryId' + (i+1)).value>0)){
		locationsCountry[i] =  document.getElementById('countryId' + (i+1)).value;
		locationsState[i] = document.getElementById('stateId' + (i+1)).value
		combinedName = document.getElementById('location' + (i+1)).innerHTML;
		locationsCountryName[i] = combinedName.split("/")[0];
		locationsStateName[i] = combinedName.split("/")[1];
		++i;
	}
	nLocations = i;
	
	 if (!confirm('Delete ' + locationsCountryName[locationIndex] + '/' + locationsStateName[locationIndex] + ' ?')) return;
	
	for(i=0; i <= nLocations; ++i){
		if (i == locationIndex){
			document.getElementById('location'+(i+1)).innerHTML ='';
			document.getElementById('locationDelete'+(i+1)).innerHTML = '';		
			document.getElementById('countryId'+(i+1)).value = '';		
			for (j=i; j < nLocations-1; ++j){
				locationsCountry[j] = locationsCountry[j+1];
				locationsState[j] = locationsState[j+1];
				locationsCountryName[j] = locationsCountryName[j+1];
				locationsStateName[j] = locationsStateName[j+1];	
				document.getElementById('location'+(j+2)).innerHTML ='';
				document.getElementById('locationDelete'+(j+2)).innerHTML = '';	
				document.getElementById('countryId'+(j+2)).value = '';					
			}
		}
	}
	--nLocations;
	
	//document.getElementById('country').value = '';
	//document.getElementById('state').value = '';
	
	if (nLocations == 0){
		document.getElementById('location1').innerHTML ='None Selected';
	}
	else{
		for (i=0; i < nLocations; ++i){
			document.getElementById('location'+(i+1)).innerHTML =  locationsCountryName[i] + '/' + locationsStateName[i]
			document.getElementById('locationDelete'+(i+1)).innerHTML = '<a href="" style="" onclick="deleteLocation('+ i + ');event.returnValue=false;return(false);">Delete</a><br />';	
			document.getElementById('countryId'+(i+1)).value = locationsCountry[i];
			document.getElementById('stateId'+(i+1)).value = locationsState[i];		
		}
	}
	
	document.getElementById('max-reached-loc').style.visibility  = 'hidden';
}

function jobCategorySelect(controlId, maximum){
	var i,nSelect=0;
	var lastOptionSelected=0;
	
	var control = document.getElementById(controlId);
	
	for (i=0; i < control.length; ++i){
		if (control.options[i].selected) ++nSelect;
	}
	if (nSelect > maximum){
		document.getElementById('max-reached-jobcat').style.visibility  = 'visible';
		control.options[control.selectedIndex].selected = false;
	}
	else{
		document.getElementById('max-reached-jobcat').style.visibility  = 'hidden';		
	}
}

function jobTypeSelect(controlId){
	try {
		var i,nSelect=0;
		var lastOptionSelected=0;
		
		var control = document.getElementById(controlId);
		
		for (i=0; i < control.length; ++i){
			if (control.options[i].selected) ++nSelect;
		}
		if (nSelect > 3){
			document.getElementById('max-reached-jobtype').style.visibility  = 'visible';
			control.options[control.selectedIndex].selected = false;
		}
		else{
			document.getElementById('max-reached-jobtype').style.visibility  = 'hidden';				
		}
	} 
	catch(e) 
	{ }
}

function checkSelectEmpty(control){
	var isempty = true;
	if(control.length >= 1){
		for (i = 0; i < control.length; ++i){
			if(control.options[i].selected && control.options[i].value != '' ){
				isempty = false;
				return isempty;
				
			}
		}
	}
	return isempty;

}

$(document).ready(function(){					   
	//$('#resumPref').submit(function() {
	$('#resumPref').submit(function() {
		var haserror = false;
		var $target;
		$('#alert').html(" ");
		$('#errors').html(" ");
		$('#jobtype').attr('style','');
		$('#category').attr('style','');
		$('#education').attr('style','');
		$('#career').attr('style','');
		$('#salary').attr('style','');
		$('#countryList').attr('style','');
		//check the jobtype
		if( checkSelectEmpty(document.getElementById('jobtype') ) ){
			$('#errors').append('<span class="error">Please select a Job Type</span><br/>');
			$('#jobtype').addClass('has-error');
			$('#jobtype').attr('style','border: 1px #ff0000 solid;');
			haserror = true;
		}
		
		//check the category
		if( checkSelectEmpty(document.getElementById('category') ) ){
			$('#errors').append('<span class="error">Please select a Job Category</span><br/>');
			$('#category').addClass('has-error');
			haserror = true;
			$('#category').attr('style','border: 1px #ff0000 solid;')
		}
		
		if( checkSelectEmpty( document.getElementById('education' ) ) ){
			$('#errors').append('<span class="error">Please select your Degree</span><br/>');
			$('#education').addClass('has-error');
			$('#education').attr('style','border: 1px #ff0000 solid;')
			haserror = true;
		}
		
		if( checkSelectEmpty( document.getElementById('career' ) ) ){
			$('#errors').append('<span class="error">Please select your Career Level</span><br/>');
			$('#career').addClass('has-error');
			$('#career').attr('style','border: 1px #ff0000 solid;')
			haserror = true;
		}
		
		if( checkSelectEmpty( document.getElementById('salary' ) ) ){
			$('#errors').append('<span class="error">Please select your Desired Salary</span><br/>');
			$('#salary').addClass('has-error');
			$('#salary').attr('style','border: 1px #ff0000 solid;')
			haserror = true;
		}
		
		
		if( $('#countryList').val() == '-1' && $('#countryList2').val() == '-1' && $('#countryList3').val() == '-1' ){
			$('#errors').append('<span class="error">Please select a Desired Work Location</span><br/>');
			$('#countryList').addClass('has-error');
			$('#countryList').attr('style','border: 1px #ff0000 solid;');
			//$('#countryList2').attr('style','border: 1px #ff0000 solid;')
			//$('#countryList3').attr('style','border: 1px #ff0000 solid;')
			haserror = true;
		}
		if(haserror) {
			return false;
		} else {
			return true;
		}
	});
});
