// JavaScript Document
/*DOM Functions*/
var form_functions = true;
function goto_contact_info(url){
	var back_url = escape(url);
	window.location = "/login/profiles/Rep_Contact.asp";
}

function uf(field,value,emptyAllowed){ //update field value
	if(value != "" || emptyAllowed){
		try{
			document.getElementById(field).value = unescape(value);
		}catch(err){
			return false;
		}
		return true;
	}
}
function u2(field,value,emptyAllowed){ //update field value
	if(value !== "" || emptyAllowed){
		try{
			if(document.getElementById(field).type == 'checkbox'){
				document.getElementById(field).checked = !!(value);
			}else{
				document.getElementById(field).value = unescape(value);
			}
		}catch(err){
			return false;
		}
		return true;
	}
}

var hide_debug = "";
function show_hide(obj_id,emptyObjID){
	hide_debug = obj_id;
	el = document.getElementById(obj_id);
	if(el.style.display=="none"){
		el.style.display="block";
		hide_debug += "block";
	}else{
		el.style.display="none";
		hide_debug += " none";
	}
	if(emptyObjID != null && emptyObjID != ""){
		redrawScreen(emptyObjID);
	}
}
function redrawScreen(emptyObjID){
	document.getElementById(emptyObjID).innerHTML = "<div id='redrawSucka'></div>";//trick browsers(particularly IE) into redrawing to properly show updates
	document.getElementById(emptyObjID).innerHTML = "";
}
debugsetDisplay = '';
function setDisplay(obj_id,type){
	debugsetDisplay = obj_id;
	el = document.getElementById(obj_id).style.display=type;
}
function limitLength(field,limit,displayID){
	var charCount = field.value.length;
	/*if(charCount > limit){
		field.value = field.value.substring(0,limit);
	}
	charCount = field.value.length;*/
	document.getElementById(displayID).innerHTML = limit - charCount;
}
function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		//selectbox.options.remove(i);
		selectbox.remove(i);
	}
}
function addOption(selectbox, value, text )
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;

	selectbox.options.add(optn);
}
function populateMultiDD(DD,optionsArray){
	//PARAMETERS:
	//DD: the Drop Down to be modified. All of it's current options will be dropped. Pass the object itself, like this: document.getElementById("theObjectIDvalue")
	//optionsArray: the new options for the DD as a multi-dimensional array.
	/* var options = [	["Part1 Name","Part1 Title","Part1 Description"],
								["Part2 Name","Part2 Title","Part2 Description"],
								["Part3 Name","Part3 Title","Part3 Description"]];
	This array would give options like this:
	option1: "Part1 Name :Part1 Title :Part1 Description"
	option2: "Part2 Name :Part2 Title :Part2 Description"
	*/
	removeAllOptions(DD);
	if(optionsArray.length ==0){
		return;	
	}
	populateDebug = '';
	var i;
	var pad = "\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0";
	/*get maximum length of first value to pad them all to the same width 
	title      : value
	title 2    : value
	title long : value //max length
	*/
	var maxLens = [];
	for(s=0;s<optionsArray[0].length;s++){
		maxLens[s] = 0;
	}
	for(i=0; i<optionsArray.length; i++){
		for(s=0;s<optionsArray[i].length;s++){
			maxLens[s] = Math.max(optionsArray[i][s].length,maxLens[s]);
			if(maxLens[s] > pad.length){
				maxLens[s] = pad.length;
			}
		}
	}
	//populate
	var optionTxt = '';
	var	delimet = '';
	for(i=0;i<optionsArray.length; i++){
		optionTxt = '';
		delimet = '';
		for(s=1;s<optionsArray[i].length;s++){
			populateDebug += "i: " + i + "; s:"+s+"; optionsArray[i][s]:"+optionsArray[i][s]+" maxLens[s]:"+maxLens[s]+"; delimet:"+delimet+"\n";
			optionTxt += delimet + optionsArray[i][s];
			delimet = pad.substring(optionsArray[i][s].length,maxLens[s]) + " : ";
		}
		addOption(DD, optionsArray[i][0], optionTxt);
	}
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}
var addChangeEvent_debug = '';
function addChangeEvent(func,id) { 
addChangeEvent_debug = id;
  var el = document.getElementById(id);
  var oldonchange = el.onchange; 
  if (typeof el.onchange != 'function') { 
    el.onchange = func; 
  } else { 
    el.onchange = function() { 
      if (oldonchange) { 
        oldonchange(); 
      } 
      func(); 
    } 
  } 
} 
function addBlurEvent(func,id) { 
  addBlurEvent.debug = id;
  var el = document.getElementById(id);
  var oldonblur = el.onblur; 
  if (typeof el.onblur != 'function') { 
    el.onblur = func; 
  } else { 
    el.onblur = function() { 
      if (oldonblur) { 
        oldonblur(); 
      } 
      func(); 
    } 
  } 
} 

function scrollElement(el,inc){
	el.scrollTop -= inc;
}
function getMultipleSelect(el){
	var out = [],i;
	for(i=0;i<el.length;i++){
		if(el[i].selected){
			out[out.length] = el[i].value;
		}
	}
	return out;
}

function buildElement(el_data){//takes data and builds elements matching the hierarchy of the data
	//el_data = {"tag":"select","attributes":{"id":"type_0":"type","type":"select"},"sub_elements":[{"tag":"option","attributes":{"value":"0"},"inner_text":"First"},{"tag":"option","attributes":{"value":"1"},"inner_text":"Shuffle"},]}
/*
var table_example = {"tag":"table","attributes":{"class":"results_table","border":"1","cellpadding":"3","cellspacing":"0"},"sub_elements":[
		{"tag":"thead","sub_elements":[
			{"tag":"tr","sub_elements":[
				{"tag":"td",inner_text:"foo row"},
				{"tag":"td",inner_text:"foo row"}
			]}
		]},
		{"tag":"tbody","sub_elements":[
			{"tag":"tr","sub_elements":[
				{"tag":"td",inner_text:"foo row"},
				{"tag":"td",inner_text:"foo row"}
			]},
			{"tag":"tr","sub_elements":[
				{"tag":"td",inner_text:"foo row"},
				{"tag":"td",inner_text:"foo row"}
			]},
			{"tag":"tr","sub_elements":[
				{"tag":"td",inner_text:"foo row"},
				{"tag":"td",inner_text:"foo row"}
			]}
		]}
	]};
*/
	var i,sub_el_count,text,el = document.createElement(el_data.tag);
	for(key in el_data.attributes){
		el.setAttribute(key,el_data.attributes[key]);
	}
	if(el_data.inner_text){
		text = document.createTextNode(el_data.inner_text);
		el.appendChild(text);
	}
	for(key in el_data.properties){
		el[key]=el_data.properties[key];
	}
	sub_el_count = (typeof(el_data.sub_elements) == 'object') ? el_data.sub_elements.length : 0;
	for(i=0;i<sub_el_count;i++){
		el.appendChild(buildElement(el_data.sub_elements[i]));
	}
	return el;
}
