function togglePR(id){
	if ($(id).value != "SINGAPORE"){
		$('PR').show();
		$('PR_LABEL').show();
	} else {
		$('PR').hide();
		$('PR_LABEL').hide();
	}
}
function updateFields(target){
	if ($(target).value == "Primary / Secondary School / 'O' Levels" || $(target).value == "Higher Secondary / Pre-U / 'A' Levels / College"){
		$('fos').hide();
		$('add1').show();
		$('add2').hide();
	} else {
		$('fos').show();
		$('add1').hide();
		$('add2').show();
	}
}
function updateDropDown(target,dest){
	removeAllOptions($(dest));
	var t = $(target).value;
	var tv = t.toLowerCase();
	for(var i=0;i<fos[tv].length;i++){
		addOption($(dest),fos[tv][i],fos[tv][i],false);
	}
}
function setDOB(){
	var date_of_birth = $('birthyear').value+"-"+$('birthmonth').value+"-"+$('birthday').value;
	$('date_of_birth').value = date_of_birth;
}
/*
function AddSelectOption(selectObj, text, value, isSelected) 
{
    if (selectObj != null && selectObj.options != null)
    {
        selectObj.options[selectObj.options.length] = 
            new Option(text, value, false, isSelected);
    }
}
*/


/*
function calAmount(){
	//alert("debug: check");
	var so = $('salary_offered').value;
	var br = $('billing_rate').value;
	so = so.replace(',','');
	br = br.replace(',','');
		
	var salary_offered = Number(so);
	var billing_rate = Number(br);
	if ($('billing_type').value != 'Temp / Contract'){
		$('calAmountLink').show();
		$('billing_rate').show();		
		if (!isEmpty(billing_rate)){			
			var multiplier = Number(billing_rate/100);
			var amount = salary_offered*multiplier;
			$('billing_amount').value =  amount.toFixed(2);
			$('salary_offered').value = salary_offered.toFixed(2);			
		}	
	} else {
		$('calAmountLink').hide();
		$('billing_rate').hide();
		$('billing_rate').value = '';
		
	}
	if ($('order_status').value == 'Closed Won'){
		if ($('billing_type').value == 'Per Annum' || $('billing_type').value == 'Per Month'){
			addToValidate('EditView', 'billing_rate', 'float', true,'Billing Rate' );
			addToValidate('EditView', 'salary_offered', 'currency', true,'Salary Offered' );
		} else {
			removeFromValidate('EditView','salary_offered');
			removeFromValidate('EditView','billing_rate');
		}
	}
}
function alertBillAmt(){
	if ($('billing_type').value != 'Temp / Contract'){	
		alert('Billing Amount is computed based on Salary Offered and Billing Rate');
		$('salary_offered').focus();
	}
}
*/
function isEmpty(value){
	value = Number(value);
	if (value == "" || value == null || value == 0){
		return true;
	}
	return false;
}
/*
function checkClosedWon(t,team_id,team_name){
	var so = $('salary_offered').value;
	var ba = $('billing_amount').value;
	if (so!=""){
		so = so.replace(/,/g,'');
		var salary_offered = parseInt(so);
		$('salary_offered').value = salary_offered.toFixed(2);
	}	
	if (ba !=""){	
		ba = ba.replace(/,/g,'');
		var billing_amt = parseInt(so);	
		$('billing_amount').value = billing_amt.toFixed(2);
	}
	//addToValidate(formname, name, type, required, msg)
	if (validate['EditView']){
		removeFromValidate('EditView','date_of_commencement');
		removeFromValidate('EditView','selected_candidate_c');		
		removeFromValidate('EditView','billing_type');
		if ($('billing_type' != 'Temp / Contract')){
			removeFromValidate('EditView','salary_offered');
			removeFromValidate('EditView','billing_rate');
		}
		removeFromValidate('EditView','billing_amount');
		removeFromValidate('EditView','billing_detail');
	}
	if (t.value == "Closed Won"){
		$('team_id').value = team_id;
		$('team_name').value = team_name;
		addToValidate('EditView', 'date_of_commencement', 'date', true,'Date of Commencement' );
		addToValidate('EditView', 'selected_candidate_c', 'relate', true,'Selected Candidate' );
		
		addToValidate('EditView', 'billing_type', 'enum', true,'Billing Type' );
		if ($('billing_type' != 'Temp / Contract')){
			addToValidate('EditView', 'billing_rate', 'float', true,'Billing Rate' );
			addToValidate('EditView', 'salary_offered', 'currency', true,'Salary Offered' );
		}
		addToValidate('EditView', 'billing_amount', 'currency', true,'Billing Amount' );
		addToValidate('EditView', 'billing_detail', 'text', true,'Billing Detail' );			
		//alert(validate['EditView']);
	} else {
		$('team_id').value = 1;
		$('team_name').value = 'Everyone';
	}
	hideBilling();
	calAmount();
}
function hideBilling(){
	//alert("debug: check");
	if ($('order_status').value == 'Closed Won'){
		$('LBL_PANEL2').show();
	} else {
		$('LBL_PANEL2').hide();
	}
}

//Event.observe('EditView', 'submit', calAmount);

/*
document.observe('dom:loaded', function() { 
           
             //first arg   : div to update 
             //second arg  : interval to poll in seconds 
             //third arg   : file to get data 
              
             var visitorCounter = new updater('billing_amount', 1, 'cal.php',$('billing_type').value); 
             visitorCounter.getUpdate(); 
         }); 
*/
// -------------------------------------------------------------------
// hasOptions(obj)
//  Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}

// -------------------------------------------------------------------
// selectUnselectMatchingOptions(select_object,regex,select/unselect,true/false)
//  This is a general function used by the select functions below, to
//  avoid code duplication
// -------------------------------------------------------------------
function selectUnselectMatchingOptions(obj,regex,which,only) {
	if (window.RegExp) {
		if (which == "select") {
			var selected1=true;
			var selected2=false;
			}
		else if (which == "unselect") {
			var selected1=false;
			var selected2=true;
			}
		else {
			return;
			}
		var re = new RegExp(regex);
		if (!hasOptions(obj)) { return; }
		for (var i=0; i<obj.options.length; i++) {
			if (re.test(obj.options[i].text)) {
				obj.options[i].selected = selected1;
				}
			else {
				if (only == true) {
					obj.options[i].selected = selected2;
					}
				}
			}
		}
	}
		
// -------------------------------------------------------------------
// selectMatchingOptions(select_object,regex)
//  This function selects all options that match the regular expression
//  passed in. Currently-selected options will not be changed.
// -------------------------------------------------------------------
function selectMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"select",false);
	}
// -------------------------------------------------------------------
// selectOnlyMatchingOptions(select_object,regex)
//  This function selects all options that match the regular expression
//  passed in. Selected options that don't match will be un-selected.
// -------------------------------------------------------------------
function selectOnlyMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"select",true);
	}
// -------------------------------------------------------------------
// unSelectMatchingOptions(select_object,regex)
//  This function Unselects all options that match the regular expression
//  passed in. 
// -------------------------------------------------------------------
function unSelectMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"unselect",false);
	}
	
// -------------------------------------------------------------------
// sortSelect(select_object)
//   Pass this function a SELECT object and the options will be sorted
//   by their text (display) values
// -------------------------------------------------------------------
function sortSelect(obj) {
	var o = new Array();
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}
	if (o.length==0) { return; }
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
	}

// -------------------------------------------------------------------
// selectAllOptions(select_object)
//  This function takes a select box and selects all options (in a 
//  multiple select object). This is used when passing values between
//  two select boxes. Select all options in the right box before 
//  submitting the form so the values will be sent to the server.
// -------------------------------------------------------------------
function selectAllOptions(obj) {
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		obj.options[i].selected = true;
		}
	}
	
// -------------------------------------------------------------------
// moveSelectedOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  This function moves options between select boxes. Works best with
//  multi-select boxes to create the common Windows control effect.
//  Passes all selected values from the first object to the second
//  object and re-sorts each box.
//  If a third argument of 'false' is passed, then the lists are not
//  sorted after the move.
//  If a fourth string argument is passed, this will function as a
//  Regular Expression to match against the TEXT or the options. If 
//  the text of an option matches the pattern, it will NOT be moved.
//  It will be treated as an unmoveable option.
//  You can also put this into the <SELECT> object as follows:
//    onDblClick="moveSelectedOptions(this,this.form.target)
//  This way, when the user double-clicks on a value in one box, it
//  will be transferred to the other (in browsers that support the 
//  onDblClick() event handler).
// -------------------------------------------------------------------
function moveSelectedOptions(from,to) {
	// Unselect matching options, if required
	if (arguments.length>3) {
		var regex = arguments[3];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
			}
		}
	// Move them over
	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
			to.options[index] = new Option( o.text, o.value, false, false);
			}
		}
	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(from);
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	}


	

// -------------------------------------------------------------------
// moveAllOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  Move all options from one select box to another.
// -------------------------------------------------------------------
function moveAllOptions(from,to) {
	selectAllOptions(from);
	if (arguments.length==2) {
		moveSelectedOptions(from,to);
		}
	else if (arguments.length==3) {
		moveSelectedOptions(from,to,arguments[2]);
		}
	else if (arguments.length==4) {
		moveSelectedOptions(from,to,arguments[2],arguments[3]);
		}
	}

// -------------------------------------------------------------------
// copyAllOptions(select_object,select_object[,autosort(true/false)])
//  Copy all options from one select box to another, instead of
//  removing items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
function copyAllOptions(from,to) {
	selectAllOptions(from);
	if (arguments.length==2) {
		copySelectedOptions(from,to);
		}
	else if (arguments.length==3) {
		copySelectedOptions(from,to,arguments[2]);
		}
	}

// -------------------------------------------------------------------
// swapOptions(select_object,option1,option2)
//  Swap positions of two options in a select list
// -------------------------------------------------------------------
function swapOptions(obj,i,j) {
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
	}
	
// -------------------------------------------------------------------
// moveOptionUp(select_object)
//  Move selected option in a select list up one
// -------------------------------------------------------------------
function moveOptionUp(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			if (i != 0 && !obj.options[i-1].selected) {
				swapOptions(obj,i,i-1);
				obj.options[i-1].selected = true;
				}
			}
		}
	}

// -------------------------------------------------------------------
// moveOptionDown(select_object)
//  Move selected option in a select list down one
// -------------------------------------------------------------------
function moveOptionDown(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=obj.options.length-1; i>=0; i--) {
		if (obj.options[i].selected) {
			if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
				swapOptions(obj,i,i+1);
				obj.options[i+1].selected = true;
				}
			}
		}
	}

// -------------------------------------------------------------------
// removeSelectedOptions(select_object)
//  Remove all selected options from a list
//  (Thanks to Gene Ninestein)
// -------------------------------------------------------------------
function removeSelectedOptions(from,valuebox) { 
	if (!hasOptions(from)) { return; }
	if (from.type=="select-one") {
		from.options[from.selectedIndex] = null;
		}
	else {
		for (var i=(from.options.length-1); i>=0; i--) { 
			var o=from.options[i]; 
			if (o.selected) { 
				from.options[i] = null; 
				} 
			}
		}
		selectAllOptions(from)
	$(valuebox).value = from.getValue();
	from.selectedIndex = -1; 
	} 

// -------------------------------------------------------------------
// removeAllOptions(select_object)
//  Remove all options from a list
// -------------------------------------------------------------------
function removeAllOptions(from) { 
	if (!hasOptions(from)) { return; }
	for (var i=(from.options.length-1); i>=0; i--) { 
		from.options[i] = null; 
		} 
	from.selectedIndex = -1; 
	} 

// -------------------------------------------------------------------
// addOption(select_object,display_text,value,selected)
//  Add an option to a list
// -------------------------------------------------------------------
function addOption(obj,text,value,selected) {
	if (obj!=null && obj.options!=null) {
		obj.options[obj.options.length] = new Option(text, value, false, selected);
		}
	}

// -------- custom ///
function setOptionValues(to,valuebox){
	selectAllOptions(to)
	valuebox.value = to.getValue();
	to.selectedIndex = -1; 
	
}


// -------------------------------------------------------------------
// copySelectedOptions(select_object,select_object[,autosort(true/false)])
//  This function copies options between select boxes instead of 
//  moving items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
// custom 
function copySelectedOptions(from,to,valuebox) {
	
	
	var options = new Object();
	if (hasOptions(to)) {
		for (var i=0; i<to.options.length; i++) {
			//options[to.options[i].value] = to.options[i].text;
			//alert("1");
			}
		}
	
	if (!hasOptions(from)) { return; }
	
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text) {
				if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
				add_text = o.text
				if (hasOptions(arguments[3])){
					var from1 = arguments[3];
					if (from1.value !="" && from1.value !="undefined" && from1.value !=undefined){
						add_text += " in "+from1.value;
					}
				}
				if (hasOptions(arguments[4])){
					var from2 = arguments[4];
					if (from2.value !="" && from2.value !="undefined" && from2.value !=undefined){
						add_text += " ("+from2.value+")";
					}
				}
				
				

				/*
				if (from1.value != null && from1.value != "undefined" && from1.value != 0) {
					add_text += " in "+from1.value;
				}				
				if (from2.value != null && from2.value != "undefined" && from2.value != 0) {
					add_text += " in "+from1.value+from2.value;
				}
				*/
				to.options[index] = new Option( add_text, add_text, false, false);

				
				}
			}
			
		}
		/*
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(to);
		}
		*/
	//from.selectedIndex = -1;
	
	selectAllOptions(to)
	valuebox.value = to.getValue();
	to.selectedIndex = -1; 

	}
function hideEmptyList(){
	
	if ($('field_of_study_list').options.length<=0) {
		$('from1').hide();
		$('from1btn').hide();
		$('from0btn').show();
	} else {		
		$('from1').show();
		$('from1btn').hide();
		$('from0btn').hide();
		
	}
	if ($('sub_field_of_study_list').options.length<=0) {
		$('from2').hide();
		$('from2btn').hide();
		$('from0btn').show();
	} else {
		$('from2').show();
		$('from2btn').show();
		$('from0btn').hide();
	}
}
