// JavaScript Document for roboutique
/*--------------------------------------------------------------------------------------------
 * Popup window for terms and privacy
 *--------------------------------------------------------------------------------------------*/
function openTerms(strFilename,strWindowName,intLeft,intTop) {
	var mywindow = null;
	var strWindowStyle = "height=400,width=740,left="+intLeft+",top="+intTop+",resizable=yes,scrollbars=yes,status=yes";
	try {
		mywindow = window.open(strFilename,strWindowName,strWindowStyle); 
	} catch(e) {
		// error opening window - return true so that link will open instead
		return true;
	}
	// set focus to window
	mywindow.focus();
	return false;
}
/*--------------------------------------------------------------------------------------------
 * User Functionality - general
 *--------------------------------------------------------------------------------------------*/
function representingClicked(status,include_sales) {
	var objCompany = findObj("company");
	var objExchange = findObj("exchange");
	var objTicker = findObj("ticker");
	var objSales = null;
	if (include_sales) {
		objSales = findObj("sales");
	}
	var disabled = (status == "S");
	setTextBoxState(objCompany, disabled);
	setTextBoxState(objExchange, disabled);
	setTextBoxState(objTicker, disabled);
	if (objSales) {
		setTextBoxState(objSales, disabled);
	}
}

function setTextBoxState(objText,disabled) {
	objText.readOnly = disabled;
	objText.disabled = disabled;
	objText.style.backgroundColor = disabled ? "#EEEEEE" : "#FFFFFF";
}

/*--------------------------------------------------------------------------------------------
 * User Functionality - for add/remove select box pairs
 *--------------------------------------------------------------------------------------------*/
var browserOK = true;

//
// moveOption - move options from one select box to another
//
function moveOption(tolist,fromlist) {
	var selectTo = findObj(tolist);
	var selectFrom = findObj(fromlist);
	try {
		if (selectFrom.selectedIndex == -1) {
			alert("Please select an item from the list");
		} else {
			var arrRemove = new Array();
			// add selected options to other select box, save indeces for removal
			for (var i=0; i<selectFrom.options.length; i++) {
				if (selectFrom.options[i].selected) {
					addOption(selectTo,selectFrom.options[i].text,selectFrom.options[i].value);
					arrRemove.push(i);
				}
			}
			// remove selected options
			for (var i=arrRemove.length-1; i>=0; i--) {
				selectFrom.remove(arrRemove[i]);
			}
		}
	} catch (e) {
		alert("Failed: "+e.message);
	}
}

//
// addOption - browser specific code to add options to select box
//
function addOption(objSelect,optionName,optionValue,selected) {
	var newOption = document.createElement("option");
	newOption.text = optionName;
	newOption.value = optionValue;
	// find order of options
	var newOptionIndex = 0;
	var nextOption = null;
	for (var i=0; i<objSelect.options.length; i++) {
		if (objSelect.options[i].text < newOption.text) {
			newOptionIndex = i+1; // for IE
		} else {
			nextOption = objSelect.options[i]; // for Netscape
			break;
		}
	}
	switch(navigator.appName) {
		case "Microsoft Internet Explorer":
			objSelect.add(newOption,newOptionIndex);
			break;
		case "Netscape":
			objSelect.add(newOption,nextOption);
			break;
	}
}

//
// submitRegistrationForm - function called during onSubmit method of form to select user selected options
//
function submitRegistrationForm() {
	selectAll('sectors[]');
	selectAll('specialities[]');
	selectAll('categories[]');
	return true;
}

//
// submitRobotsForm - function called during onSubmit method of form to select user selected options
//
function submitRobotsForm() {
	selectAll('sectors[]');
	return true;
}

//
// submitSchoolsForm - function called during onSubmit method of form to select user selected options
//
function submitSchoolsForm() {
	selectAll('levels[]');
	return true;
}


//
// selectAll - selects all options in the specified select box
//
function selectAll(selectname) {
	var objSelect = findObj(selectname);
	objSelect.multiple = true;
	for (var i=0; i < objSelect.options.length; i++) {
		objSelect.options[i].selected = true;
	}
}

//
// initOptions - function called during onLoad to remove the browser message
// and ensure that the add/remove buttons will work for the multi-select boxes
//
function initOptions() {
	// try to remove first (temporary) option
	try {
		removeTempOption('fromspecialities[]');
		removeTempOption('fromsectors[]');
		removeTempOption('fromcategories[]');
		removeTempOption('specialities[]');
		removeTempOption('sectors[]');
		removeTempOption('categories[]');
	} catch (e) {
		alert(e.message);
		browserOK = false;
	}
	
}

//
// initSectors - function called during onLoad to remove the browser message
// and ensure that the add/remove buttons will work for the multi-select boxes
//
function initSectors() {
	// try to remove first (temporary) option
	try {
		removeTempOption('fromsectors[]');
		removeTempOption('sectors[]');
	} catch (e) {
		alert(e.message);
		browserOK = false;
	}
	
}

//
// initSchoolLevels - function called during onLoad to remove the browser message
// and ensure that the add/remove buttons will work for the multi-select boxes
//
function initSchoolLevels() {
	// try to remove first (temporary) option
	try {
		removeTempOption('fromlevels[]');
		removeTempOption('levels[]');
	} catch (e) {
		alert(e.message);
		browserOK = false;
	}
	
}


//
// removeTempOption - called by initOptions to remove an individual temporary option
//
function removeTempOption(selectname) {
	var objSelect = findObj(selectname);
	if (objSelect) {
		objSelect.remove(0);
		if (objSelect.length > 0) {
			browserOK = false;
		}
	}
}

/*--------------------------------------------------------------------------------------------
 * General Functionality
 *--------------------------------------------------------------------------------------------*/
// find an object
// Example: obj = findObj("image1");
function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}
