/****************************/
// MAIN FUNCTIONS
/****************************/
// add entry to selected lists
// options are to be structured as 'textItem###optionItem'
// where textItem is added to the text_dst area, 
// and the S# or SEC# code added to the e3codes hidden text input item
// and optionItem is added to the option_dst area
function Add(form_name) {
//	alert('attempting...');
    var from = document.forms[form_name].option_src;
    for(var count = 0; count <= from.options.length-1; count++) {
        if(from.options[count].selected == true) {
            DoAdd(form_name, from.options[count].value);
        }
    }
    deselectall(document.forms[form_name].option_dst);
    var to = document.forms[form_name].option_dst;
    for(count = 0; count <= to.options.length-1; count++){
        if(to.options[count].value==""){
            to.options[count] = null;
        }
    }
}
// do the actual add
function DoAdd(form_name, addstr){
    if(addstr.indexOf("\$\$\$") == 0){
        addstr = addstr.substring(3);
    }
    if(document.forms[form_name].text_dst != null){
    	var text_to=document.forms[form_name].text_dst;
    	var e3codes_to=document.forms[form_name].e3codes;
    }
//    if(document.getElementById('symbols').innerHTML != null){
////		alert('inner: '+document.getElementById('symbols').innerHTML);
////    	var text_to=document.getElementById('symbols').innerHTML;
//   		var tmpStr = document.getElementById('symbols').innerHTML;
//    	
//    }
	
    var opts_to=document.forms[form_name].option_dst;
    deselectall(opts_to);
    if(addstr != ''){
        var addarr = addstr.split("\$\$\$");
    	if(document.forms[form_name].text_dst != null){
//		if(document.getElementById('symbols').innerHTML != null){
    		var tmpStr = text_to.value.replace(/,\s*$/, "");
    		var tmpCode = e3codes_to.value;
	        for(i=0; i<addarr.length; i++){
	            var info = addarr[i].split("###");
	            if(!alreadyInList(opts_to, info[0]+"###"+info[1])){
	                tmpCode = tmpCode + ", " + info[0];
	                tmpStr = tmpStr + ", " + info[1];
//	                addSelectOption(opts_to, info[1], info[0]);
	                addSelectOption(opts_to, info[0], info[1], info[2]);
//	                addSelectOption(opts_to, "a", "b", "c");
				} else { // ensure text version (sym) is there for something already in
////					if((tmpStr.indexOf(", " +info[1] + ",") == -1) && (tmpStr.indexOf(", " +info[1] + ",") == -1)){
    				var re = new RegExp("\\b"+info[1]+"\\b", "i");
					if(!(tmpStr.match(re))){
//						alert(info[1]+" add to: "+tmpStr);
//						alert(tmpStr.match(/re/));
	                	tmpStr = tmpStr + ", " + info[1];
					}
	            }
	        }
			text_to.value = tmpStr.replace(/^,+\s*/, "");
			e3codes_to.value = tmpCode.replace(/^,+\s*/, "");
//			alert(text_to.value);
//			document.getElementById('symbols').innerHTML = tmpStr.replace(/^, /, "");
		} else {
	        for(i=0; i<addarr.length; i++){
	            var info = addarr[i].split("###");
//	            if(!alreadyInList(opts_to, addarr[i].value)){
//	                addSelectOption(opts_to, addarr[i].value, addarr[i].value, addarr[i].value);
//	            }
	            if(!alreadyInList(opts_to, info[0]+"###"+info[1])){
//	                addSelectOption(opts_to, addarr[i].value, addarr[i].value, addarr[i].value);
	                addSelectOption(opts_to, info[0], info[1], info[2]);
//	                addSelectOption(opts_to, "A", "B", "C");
	            }
	        }
		}
    }
}
// remove entry from selected lists
function Remove(form_name){
    var opts_from=document.forms[form_name].option_dst;
    var i;
    for(i=opts_from.length - 1; i>=0; i--){
        if (opts_from.options[i].selected){
//        	alert(opts_from.options[i].value+","+opts_from.options[i].text);
    		if(document.forms[form_name].text_dst != null){
//			if(document.getElementById('symbols').innerHTML != null){
//            	RemoveText(form_name, opts_from.options[i].value);
	            var info = opts_from.options[i].value.split("###");
            	RemoveText(form_name, info[0], info[1]);
            }
//            if(opts_from.length == 1){
//                addSelectOption(opts_from, "", "", "")
//            }
            opts_from.remove(i);
        }
    }
    deselectall(document.forms[form_name].option_src);
}
// remove the ticker or sector edgesym from Selected Stocks:
function RemoveText(form_name, codeItem, textItem){
	var text_from=document.forms[form_name].text_dst;
	var code_from=document.forms[form_name].e3codes;
//	var text_from=document.getElementById('symbols').innerHTML;
//    if(document.getElementById('symbols').innerHTML != null){
//	alert(textItem.replace("%", "\\\%"));
//	alert(textItem + ' del from: ' + text_from.value);
////	alert(textItem.replace("%", "\\\%") + ' del from: ' + text_from.value);
//	alert(codeItem + ' del from: ' + code_from.value);
	var result;  
//	var pattern = new RegExp("\\b"+textItem.replace("%", "\\\%")+"\\b", "i");  
//	var pattern = new RegExp("\\b\\%WGI\\b", "i");  
//	(?:^|[^%])(%wgi)\b    //testing with symbol "%WGI"
//	var pattern = new RegExp("(?:^|[^%])(%WGI)\\b", "i");  //testing with symbol "%WGI"
//	var pattern = new RegExp("(?:^|[^%])(" + textItem + ")\\b", "i");  // ignore 1st tagged pattern, only 2nd tagged () capture
//	var pattern = new RegExp("(?:^|[^%])" + textItem + "\\b", "i");  // ignore 1st tagged pattern
	// /[^,\s\w\.\%]?[,\s](txt)\b/i;  // reformulate to avoid overzealous removal:  i.e. fix rm {U} stripping U from {SNP.U}
	var pattern = new RegExp("[^,\\s\\w\\.\\%]?(?:^|[,\\s])(" + textItem + ")\\b", "i"); 
//	alert(pattern);
	result = pattern.exec(text_from.value);
//	alert(result);
//	alert(result[0]);
//	alert(result[1]);
	if (result != null){
//		var re = new RegExp("\\b"+textItem.replace("%", "\%")+"\\b", "i");  
//		alert(re);
		// textItem = '%WGI'
	    // text_from.value = 'WES, %WGI, WPX'
//	    text_from.value = text_from.value.replace(re, "");
	    text_from.value = text_from.value.replace(pattern, "");
	    text_from.value = text_from.value.replace(/,\s*,/, ",");
	    text_from.value = text_from.value.replace(/^,+\s*/, "");
	    text_from.value = text_from.value.replace(/,$/, "");
//		alert(text_from.value);
		var re = new RegExp("\\b"+codeItem+"\\b", "i");
	    code_from.value = code_from.value.replace(re, "");
	    code_from.value = code_from.value.replace(/,\s*,/, ",");
	    code_from.value = code_from.value.replace(/^,+\s*/, "");
	    code_from.value = code_from.value.replace(/,$/, "");
//		alert(code_from.value);
	}
//	var textArr = text_from.value.split(", ");
//	var codeArr = code_from.value.split(", ");
////	var textArr = text_from.split(", ");
//	var newlist = "";
//	var newcodes = "";
////	var info = textItem.split("###");
//	// info[0]+"###"+info[1]+"###"+info[2]
//    for(j=0; j<textArr.length; j++){
//		if(textArr[j] != textItem){
//			newlist = newlist + ", " + textArr[j];
//		}
////		if(textArr[j] != info[1]){
////			newlist = newlist + ", " + textArr[j];
////		}
//    }
//    text_from.value = newlist.replace(/^, /, "");
//
//    for(j=0; j<codeArr.length; j++){
//		if(codeArr[j] != codeItem){
//			newcodes = newcodes + ", " + codeArr[j];
//		}
//    }
//    code_from.value = newcodes.replace(/^, /, "");
////    document.getElementById('symbols').innerHTML = text_from.value;
////    text_from = newlist.replace(/^, /, "");
////    document.getElementById('symbols').innerHTML = text_from;
}
/****************************/
// HELPER FUNCTIONS
/****************************/
// make sure we don't add things to the list more than once
function alreadyInList(which, val){
    for(j=0; j<which.length; j++){
        if(which.options[j].value == val){
            return true;
        }                
    }
    return false;
}
// deselect all from a specified select list
function deselectall(which){
    for(i=which.length - 1; i>=0; i--){
        which.options[i].selected=false;
    }
}
// create select box option - cross platform
function addSelectOption(add_to, code, sym, txt){
    var OptNew = document.createElement('option');
    OptNew.text = txt;
    OptNew.value = code + "###" + sym;
    try{
        add_to.add(OptNew, null); // standards compliant; doesn't work in IE
    }catch(ex){
        add_to.add(OptNew); // IE only
    }
}

// use as onClick event validation for submit buttons to ensure a required field has content
function validate_required(field, alerttxt){
	with (field){
		if (value==null||value==""){
			alert(alerttxt); return false;
		} else {
			return true;
		}
	}
}
