//\/////
//\  overLIB Anchor Plugin
//\  This file requires overLIB 4.10 or later.
//\
//\  overLIB 4.10 - You may not remove or change this notice.
//\  Copyright Andrew Bay 2005. All rights reserved.
//\  Modified from Anchor Plugin.
//\  See http://www.bosrup.com/web/overlib/ for details.
//   $Revision: 1.1 $                      $Date: 2006/03/01 13:52:55 $
//\/////
//\mini


////////
// PRE-INIT
// Ignore these lines, configuration is below.
////////
if (typeof olInfo == 'undefined' || typeof olInfo.meets == 'undefined' || !olInfo.meets(4.10)) ;//alert('overLIB 4.10 or later is required for the Anchor Plugin.');
else {
registerCommands('popform,popext,prefunc,afterfunc');

//pForm = Parent Form name, cForm = Child form name
//var olpForm, olcForm;

////////
// DEFAULT CONFIGURATION
// Settings you want everywhere are set here. All of this can also be
// changed on your html page or through an overLIB call.
////////
if (typeof ol_pForm ==  'undefined') var ol_pForm = '';
if (typeof ol_cForm ==  'undefined') var ol_cForm = '';
if (typeof ol_cFormExt ==  'undefined') var ol_cFormExt = '';
if (typeof ol_listTok ==  'undefined') var ol_listTok = ', ';
if (typeof ol_PlaceHolder ==  'undefined') var ol_PlaceHolder = 'ATB_PLACEHOLDER';
if (typeof ol_prefunc ==  'undefined') var ol_prefunc = null;
if (typeof ol_postfunc ==  'undefined') var ol_postfunc = null;

////////
// END OF CONFIGURATION
// Don't change anything below this line, all configuration is above.
////////





////////
// INIT
////////
// Runtime variables init. Don't change for config!
var o3_pForm = "";
var o3_cForm = "";
var o3_cFormExt = "";
var o3_already_saved = true;
var o3_PlaceHolder = "";
var o3_listTok = "";
var o3_prefunc = null;
var o3_postfunc = null;
////////
// PLUGIN FUNCTIONS
////////
function setFormVariables() {
	o3_pForm = ol_pForm;
	o3_cForm = ol_cForm;
	o3_cFormExt = ol_cFormExt;
	o3_PlaceHolder = ol_PlaceHolder;
	o3_listTok = ol_listTok;
	o3_prefunc = ol_prefunc;
	o3_postfunc = ol_postfunc;
}

// Parses Reference Mark commands
function parseFormExtras(pf,i,ar) {
	var v, k=i;

	if (k < ar.length) {
		if (ar[k] ==  POPFORM) { 
		  eval(pf + "pForm = '" + ar[++k] + "'"); 
		  eval(pf + "cForm = '" + ar[++k] + "'"); 
		  return k; 
		}
		if (ar[k] ==  POPEXT) { 
		  eval(pf + "cFormExt = '" + ar[++k] + "'"); 
		  return k; 
		}
		if (ar[k]==PREFUNC) { 
			if (typeof ar[k+1] != 'number') { 
				v = ar[++k]; 
				o3_prefunc=v;//(typeof v=='function' ? v : null); 
			}
			return k;
		}		
		if (ar[k]==AFTERFUNC) { 
			if (typeof ar[k+1] != 'number') { 
				v = ar[++k]; 
				o3_postfunc=v; //(typeof v=='function' ? v : null); 
			}
			return k;
		}		
	}

	return -1;
}



///////
// EXTERNAL SUPPORT FUNCTIONS TO HANDLE ANCHOR PROPERTIES
///////

// Stub function for the runHook routine
/*function anchorPreface() {
	if (!mrkObj) return;
	rmrkPosition = getAnchorLocation(mrkObj);
}*/



  function ol_getChoice(mySelect) {
	//alert(mySelect.name);
	var myValue = new Array('', '');
    for (var i = 0; i < mySelect.length; i++) {
        if (mySelect.options[i].selected == true) {
			myValue[0] = mySelect.options[i].value;			
			//alert(FindInnerText(mySelect.options[i]));
			myValue[1] = FindInnerText(mySelect.options[i]);
           return myValue;
        }
    }
	return myValue;
  }
  function ol_getChoices(mySelect) {
	//alert(mySelect.name);
	var myValue = new Array(ol_PlaceHolder, ol_PlaceHolder);
    for (var i = 0; i < mySelect.length; i++) {
        if (mySelect.options[i].selected == true) {
            myValue[0] += o3_listTok + mySelect.options[i].value;
			myValue[1] += o3_listTok + FindInnerText(mySelect.options[i]);
        }
    }
	return myValue;
  }

  function ol_setChoice(mySelect, myValue, startAt) {
	//alert(mySelect.name);
    for (var i = startAt; i < mySelect.length; i++) {
        if (mySelect.options[i].value == myValue) {
           mySelect.options[i].selected = true;
           return i;
        }
    }
  }

  function ol_setChoices(mySelect, myValue, startAt) {
	//alert(mySelect.name);
	var lastI;
    for (var i = startAt; i < mySelect.length; i++) {
		//alert(mySelect.options[i].value + "|" + myValue + "|" + AInList(mySelect.options[i].value, myValue) );
		if (AInList(mySelect.options[i].value, myValue) > 0 ){
			mySelect.options[i].selected = true;
			lastI = i;
		} 
		else {
			mySelect.options[i].selected = false;
        }
    }
	return lastI;
  }

  function ol_getRadioChoice(myFrm, myRadio) {
	//alert(mySelect.name);
     for (var i = 0; i < myFrm.length; i++) {
        if (myFrm.elements[i].name == myRadio) 
			if (myFrm.elements[i].checked)
				return myFrm.elements[i].value;
        
     }
  }
function ol_getElementType(myFrm, myName){
	var n, w;
	/*for (n=0; n < myFrm.length; n++){
		w = myFrm.elements[n];
		if (w.name == myName)
			return w.type;
	}*/
	w = getEleByName(myName, myFrm); //.elements[myName];
	if (w){
		//alert(w.length);
		if (w.type){
			return w.type;		
		} else {
			//alert(w.length + ":" + w[0].type + ":" + w.type);
			return w[0].type;
		}
	}
}

function TruncPlaceholder(myValue){
	if (!myValue) return myValue;
	if (myValue.substring(0, ol_PlaceHolder.length + o3_listTok.length) == ol_PlaceHolder + o3_listTok)
		return myValue.substring(ol_PlaceHolder.length + o3_listTok.length);
	else if (myValue.substring(0, ol_PlaceHolder.length) == ol_PlaceHolder)
		return myValue.substring(ol_PlaceHolder.length);
	return myValue;
}

// Writes to a layer
function changeText(node, txt) {
	txt += "\n";
	if (olNs4) {
		//I don't know if this will work...
		var lyr = node
		lyr.write(txt)
		lyr.close()
	} else if (typeof node.innerHTML != 'undefined') {
		if (olIe5 && isMac) node.innerHTML = '';
		node.innerHTML = txt;
	} else {
		//tested in Firefox 1.0.7
		range = document.createRange();
		range.setStartAfter(node);
		domfrag = range.createContextualFragment(txt);
		
		while (node.hasChildNodes()) {
			node.removeChild(node.lastChild);
		}
		
		node.appendChild(domfrag);
	}
}

function ol_setFormField(myFrm, myName, myValue){
	var myType = ol_getElementType(myFrm, myName);
	var w, n, resplit = false, w2;
	switch (myType){
		case 'hidden':
		case 'textarea':
		case 'text':			
			if (myValue[0].substring(0, ol_PlaceHolder.length) == ol_PlaceHolder)
				resplit = true;
			myValue[0] = TruncPlaceholder(myValue[0]);
			myValue[1] = TruncPlaceholder(myValue[1]);
			w = getEleByName(myName, myFrm);
			if (w) {
				if (w.length > 0)
					w = w[0];
				w.value = myValue[0];
				w = w.nextSibling;
				if (!w || (w.id != 'SPAN_' + myName))
					w = getRefById('SPAN_' + myName, document);
				if (w && w.tagName == "SPAN"){
					if (resplit)
						myValue[1] = myValue[1].replace(RegExp(o3_listTok, "gi"), "\n");					
					changeText(w, myValue[1].replace(/\n/g, "<br>"));
				}		
			}
			break;
		case 'select-one':
			myValue[0] = TruncPlaceholder(myValue[0]);
			myValue[1] = TruncPlaceholder(myValue[1]);
			w = getEleByName(myName, myFrm);
			//Length here means number of choices!
			if (w){
				//alert("Wha?: " + w.length);
				//if (w.length > 1)
				//	w = w[0];
				ol_setChoice(w, myValue[0], 0);
			}
			break;
		case 'select-multiple':
			w = getEleByName(myName, myFrm);			
			if (w){
				//if (w.length > 1)
				//	w = w[0];
				ol_setChoices(w, myValue[0], 0);
			}
			break;
		case 'radio':
			myValue[0] = TruncPlaceholder(myValue[0]);
			myValue[1] = TruncPlaceholder(myValue[1]);
			w2 = getEleByNameList(myName, myFrm);			
			//alert(w2.length + ":" + myValue[0] + ":" + myValue[1]);
			for (n=0; n < w2.length; n++){
				w = w2[n];
				if (w && w.name == myName)
					if (w.value == myValue[0]){
						w.checked = true;
						break; //for
					}
			}
			break;
		case 'checkbox':
			w2 = getEleByNameList(myName, myFrm);			
			for (n=0; n < w2.length; n++){
				w = w2[n];
				if (w && w.name == myName)
					if (AInList(w.value, myValue[0])){
						w.checked = true;
					} else {
						w.checked = false;
					}
			}
			break;
	}
	return true;
}

function ol_getFormField(myFrm, myName){
	var myType = ol_getElementType(myFrm, myName);
	var w, n, myValue = new Array('', ''), w2, wlist, mlen;
	//alert (myName + ":" + myType);
	switch (myType){
		case 'hidden':
		case 'textarea':
		case 'text':
			w = getEleByName(myName, myFrm);
			if (w)
				return new Array(w.value, w.value);
			break;
		case 'select-one':
			w = getEleByName(myName, myFrm);
			if (w)
				return ol_getChoice(w);
			break;
		case 'select-multiple':
			w = getEleByName(myName, myFrm);
			if (w){
				myValue = ol_getChoices(w);
				//alert(myValue);
				return myValue;
			}
			break;
		case 'radio':
			wlst = getEleByNameList(myName, myFrm);			
			//alert (wlst.name + ":" + wlst.id);
			//alert (wlst.item(1).name + ":" + wlst.item(1).id);
			for (n=0; n < wlst.length; n++){
				w = wlst[n];
				if (w && w.name == myName)
					if (w.checked){
						//oh boy...						
						w2 = getLabelByFor(w.id, myFrm);
						if (w2) {
							//alert (FindInnerText(w2));
							return new Array(w.value, FindInnerText(w2));
						} else
							return new Array(w.value, 'err');
						break; //for
					}
			}
			break;
		case 'checkbox':
			myValue[0] = ol_PlaceHolder;
			myValue[1] = ol_PlaceHolder;
			wlst = getEleByNameList(myName, myFrm);
			if (wlst)
			for (n=0; n < wlst.length; n++){
				w = wlst[n];
				//alert(w.name);
				if (w && w.name == myName){
					w2 = getLabelByFor(w.id, myFrm);
					if (w.checked){
						//Same oh boy...
						myValue[0] += o3_listTok + w.value;
						//alert(myValue[0]);
						if (w2){
							//alert(w.id + ":" + w.value + ":" + FindInnerText(w2));
							myValue[1] += o3_listTok + FindInnerText(w2);
						}
						else {
							myValue[1] += o3_listTok + 'Yes';
						}
					}
					else if (!w2) {
						myValue[0] += o3_listTok + '';
						myValue[1] += o3_listTok + 'No';
					}
				}
			}
			//alert(myValue[0] + ":" + myValue[1]);
			return myValue;
			break;			
		default:
			alert('Unknown type of form entry: ' + myType + "\nField Name: " + myName);		
	}
	return myValue;
}

function AddBit(mystr, mynew, mySep){
	if (mySep == undefined) mySep = o3_listTok;
    var tmpstr = mystr;
    if (mystr === ""){ 
		tmpstr = mynew;
    } else { 
		tmpstr += mySep + mynew;
    }
    return tmpstr;	
}

function AInList(what, MyList, mysep){
    var i, j, k, hold;
	if (mysep == undefined) 
		mysep = o3_listTok;
    var arrList;
    arrList = MyList.split(o3_listTok);
    for (k = 0; k < arrList.length; k++){
        if (what == arrList[k]){
			return (k + 1);
		}
    }
    return 0;
}


//Copy data from the base form to the pop-up form.
function doFormCopy(){
	if (!o3_pForm || !o3_cForm || !o3_already_saved) return;
	o3_already_saved = false;
	var pfm, cfm, pw, cw, fnm, n, m, hldVal, oName;
	pfm = getRefById(o3_pForm, document);
	cfm = getRefById(o3_cForm, document);
	//alert("P: " + o3_pForm + "..C: " + o3_cForm);
	//if (cfm) alert("Child: " + cfm.id);
	//if (pfm) alert("Parent Form: "+ pfm.id);
	//alert("test");
	oName = "-"
	if (cfm)
	for (n=0; n < cfm.length; n++){
		cw = cfm.elements[n];
		fnm = cw.name;
		//alert("Name: " + fnm + "\nType: "+ cw.type);
		//alert(":" + fnm.substring(0,4) + "|");
		if (fnm.substring(0,4).toUpperCase() == "POP_" || fnm.substring(0,7).toUpperCase() == "OLDPOP_"){
			if (fnm.substring(0,4).toUpperCase() == "POP_")
				fnm = fnm.substring(4) + o3_cFormExt;
			else
				fnm = "Old" + fnm.substring(7) + o3_cFormExt;
			if (oName != fnm){
				oName = fnm;
				hldVal = ol_getFormField(pfm, fnm);
				//alert(cw.name + ": " + hldVal);
				ol_setFormField(cfm, cw.name, hldVal);
			}
		}
	}
	//fi.value = 'woot';
	if(pfm && !pfm.onSubmit)
		pfm.onSubmit = olp_goodForm;
	if(cfm && !cfm.onSubmit)
		cfm.onSubmit = olp_badForm;
	if (o3_prefunc) 
		if (typeof o3_prefunc=='function')
			o3_prefunc();  //Function
		else
			eval(o3_prefunc); //String
	return;
}

function olp_goodForm(){
	cClick(); 
	return true;
};

function olp_badForm(){
	cClick(); 
	return false;
};

function doFormDownCopy(){
	if (!o3_pForm || !o3_cForm || o3_already_saved) return;
	o3_already_saved = true;
	var pfm, cfm, pw, cw, fnm, n, m, oName = '-', hldVal;
	pfm = getRefById(o3_pForm, document);
	cfm = getRefById(o3_cForm, document);
	//alert("P: " + o3_pForm + "..C: " + o3_cForm);
	//if (cfm) alert("Child: " + cfm.id);
	//if (pfm) alert("Parent Form: "+ pfm.id);
	//alert("test");
	if (cfm)
	for (n=0; n < cfm.length; n++){
		cw = cfm.elements[n];
		fnm = cw.name;
		//alert("Name: " + fnm + "\nType: "+ cw.type);
		//alert(":" + fnm.substring(0,4) + "|");
		if (fnm.substring(0,4).toUpperCase() == "POP_" || fnm.substring(0,7).toUpperCase() == "OLDPOP_"){
			if (fnm.substring(0,4).toUpperCase() == "POP_")
				fnm = fnm.substring(4) + o3_cFormExt;
			else
				fnm = "Old" + fnm.substring(7) + o3_cFormExt;
			if (oName != fnm){
				oName = fnm;
				hldVal = ol_getFormField(cfm, cw.name);
				ol_setFormField(pfm, fnm, hldVal);
			}
		}
	}
	//fi.value = 'woot';
	if (o3_postfunc) 
		if (typeof o3_postfunc=='function')
			o3_postfunc();  //Function
		else
			eval(o3_postfunc); //String
	return;
}

function getEleByName(l, d){
	var r = "", j;
	d = (d || document);
	eval("j = d." + l);
	if (j) 
		return (j);
	else
		for (j=0; j < d.length; j++) {
			if (l == d.elements[j].name)
				return d.elements[j];
		}

	return false;
}

function getEleByNameList(l, d){
	var r = "", j, w, c = 0;
	d = (d || document);
	eval("w = d." + l);
	if (w){
		if (!w.length) {	
			w = new Array(w);
		}
		return (w);	
	}
	else {  //Brute force
		w = new Array();
		for (j=0; j < d.length; j++) {
			if (l == d.elements[j].name){
				w[c++] = d.elements[j];
			}
		}
	}
	if (c > 0)
		return w;
	else
		return false;
}


function getLabelByFor(l, d){
	var r = "", j, el;

	d = (d || document);
	el = d.getElementsByTagName('LABEL');
	for (j=0; j < el.length; j++) {
		if (el[j].tagName == 'LABEL' && el[j].htmlFor == l)
			return el[j];
	}

	return;
}


//http://forums.aspfree.com/archive/t-50106/innerText-for-non-IE-browsers-JavaScript
function FindInnerText(objControl, innerText, nestingLevel){
	//maybe microsoft already done the job for us?
	if (typeof objControl.innerText != "undefined")
		return objControl.innerText;

	//standard browser. check recursion limit:
	if ((typeof nestingLevel != "undefined")&&(nestingLevel > 100))
		return innerText;

	//set defaults:
	if (typeof innerText == "undefined")
		innerText = "";
	if (typeof nestingLevel == "undefined")
		nestingLevel = 0;

	//stop condition:
	if (!objControl)
		return innerText;

	var text=objControl.nodeValue;
	if (!text)
		text = "";

	//take into consideration line breaks:
	if (objControl.nodeName.toLowerCase() == "br")
		return "\n";

	//iterate child elements:
	for (var i=0; i<objControl.childNodes.length; i++)
	{
		text += FindInnerText(objControl.childNodes[i], objControl.childNodes[i].nodeValue, nestingLevel+1);
	}

	//all done.
	return text;
}

////////
// PLUGIN REGISTRATIONS
////////
registerRunTimeFunction(setFormVariables);
registerCmdLineFunction(parseFormExtras);
//registerPostParseFunction(checkAnchorObject);
//Copy the data BEFORE you show the pop-up so it doesn't change beofre the user's eyes.
registerHook("showObject", doFormCopy, FBEFORE);
registerHook("hideObject", doFormDownCopy, FBEFORE);
//if(olInfo.meets(4.10)) registerNoParameterCommands('noanchorwarn');
}