//- function isWhitespace(s) 
//- function isEmpty(s)
//- function moveSelected(o) 
//- function displayChildNodes(o)
//==============================================================================
function isWhitespace (s) {
//==============================================================================
    var whitespace = " \t\n\r";
    var i;
    
    if (isEmpty(s)) {
        return true;
    } 
    
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (whitespace.indexOf(c) != -1) {
            return true;
        }
    }
    
    return false;
    
}
//==============================================================================
function isEmpty(s) {
//==============================================================================
        return ((s == null) || (s.length == 0))
}
//==============================================================================
function isNumeric(s) {
//==============================================================================
    if (s.indexOf(".") != -1) {
        digits=s.split(".");
        
        dLen=digits[1].length;
        
        while (dLen>=1 && digits[1].substr(dLen-1,1) == 0) {
//            alert("dLen = "+dLen+"\ndigit = "+digits[1].substr(dLen-1,1)+"\nnumber = "+digits[1].substring(0,dLen-1));
            digits[1]=digits[1].substring(0,dLen-1);
            dLen=digits[1].length;

        }
        if (dLen==0) {
            s=digits[0];
        } else {
            s=digits[0]+"."+digits[1];
        }
            
    }
    str1=s;
    str1Len=str1.length;
    num1=parseFloat(s);
    str2=new String(num1);
    str2Len=str2.length;
//    window.alert("String 1a = |"+str1+"|\nString 2a = |"+str2+"|\n");
    if (str1==str2) {
        return true;
    } else {
        return false;
    }
}
function formatDecimal(s,i) {

    if (isNumeric(s)) {

      dig=s.split(".");

      if (dig.length>1) {
          if (dig[1].length>i) {
//             alert("|"+dig[1].substr((i+1),1)+"|");
             if(dig[1].substr((i+1),1)>4) {
                dig[1]=parseInt(dig[1].substring(0,i)) + 1;
             } else {
                 dig[1]=dig[1].substring(0,i);
             }
          } else if (dig[1].length<i) {
             numPads=i-dig[1].length;
             for(j=0;j<numPads;j++) {
                dig[1]+="0";
             }
          } else {
             fs=s;
          }
          fs=dig[0]+"."+dig[1];
      } else if (dig.length==1){
          dec="";
          for(j=0;j<i;j++) {
             dec+="0";
          }
          fs=dig[0]+"."+dec;
      } else {
         fs=s;
      }
      return fs;
    } else {

      return false;
    }

}
//==============================================================================
function isValidEmail(s) {
//==============================================================================
    var at="@";
    var dot=".";
    var atPos, dotPos
    
    if (isEmpty(s)) {
        return false;
    } 
    
    atPos = s.indexOf(at);
    dotPos = s.lastIndexOf(dot);

    if (atPos > 0 && dotPos > atPos + 1 && dotPos < s.length -1) {    
        return true;
    } else {
        return false;
    }
}
function setSelect(o,v) {
//    alert("Object = "+o+"\nValue= "+v);
    var l=o.length;

    for (var i=0;i<l;i++) {
        if (o.options[i].value == v) {
            o.options[i].selected=true;
        } else {
            o.options[i].selected=false;
        }
    }
}
function setSelectName(n,v) {
//    alert("Object = "+n+"\nValue= "+v);

    o=getElementById(n);
    var l=o.length;

    for (var i=0;i<l;i++) {
        if (o.options[i].value == v) {
            o.options[i].selected=true;
        } else {
            o.options[i].selected=false;
        }
    }
}

function isCreditCard(st) {
    if (st.length > 19)
        return false
        sum = 0; mul = 1; l = st.length
        for (x = 0; x < l; x++) {
            digit = st.substring(l-x-1,l-x)
            tproduct = parseInt(digit ,10)*mul
            if (tproduct >= 10) 
                sum += (tproduct % 10) + 1
            else 
                sum += tproduct
            if (mul == 1) 
                mul++
            else 
                mul--
        }
        if ((sum % 10) == 0) 
            return true
        else 
            return false
}

function HelpWindow(f)
{
helpwindow=window.open(f,'helpwindow','scrollbars,location=no,height=400,width=450');helpwindow.focus();
}
//-===========================================================================
//-
//-===========================================================================
function extractToArray(s) {

    var pi = new Array;
    var tmpArray = new Array;

    ta=s.split("|");
        
    for(i=0;i<ta.length;i++) {
        tmpArray=ta[i].split("=");
        pi[tmpArray[0]]=tmpArray[1];
    }
    
    return pi;
}

function getRadioValue(o) {

    rLen=o.length;

    rValue=false;
    for(i=0;i<rLen;i++) {

        if (o[i].checked) {
            rValue=o[i].value;
        }
    }

    return rValue;
}

function setProcessingLabel (id) {

    o=document.getElementById(id);
//    window.alert(o.id);
    if (document.all) {
        lbl=o.innerText;
    } else {
        lbl=o.innerHTML;
    }
    
    if (lbl.search(/PROCESSING/)==-1) {
      lbl="PROCESSING";
    }
    if (lbl.length<20) {
        lbl+=">";
        lbl="<"+lbl;
    } else {
        lbl="PROCESSING";
    }
    if (document.all) {
        o.innerText=lbl;
    } else {
        o.innerHTML=lbl;
    }

}
function fn_clearInterval(id) {

//    window.alert("clearInterval id = |"+id+"|");
    intId=document.getElementById(id);
    
    if (intId.value == "") {
        return;
    } else {
        window.clearInterval(intId.value);
        intId.value="";
        return;
    }
    
}
//-===========================================================================
//-
//-===========================================================================
function removeNodeById(nid) {

    targetNode=document.getElementById(nid);

    if (targetNode) {
        parentNode=targetNode.parentNode;
        parentNode.removeChild(targetNode);
    }

}

function moveSelected(o) {

    oName=o.name;
//    window.alert("Object Name : |"+oName+"|\n");
    aO=oName.split("_");
    
    sBoxName="dd_"+aO[2]+"[]";
    sBoxSelectedName="dd_"+aO[2]+"_Selected[]";
    fromBox=document.getElementById(sBoxName);
    toBox=document.getElementById(sBoxSelectedName);
    
//- If the ADD Button is pushed.
    if (aO[1]=="Add") {
        if (fromBox.options[0].selected && fromBox.options[0].text=="SELECT ALL") {
            //- Clear the toBox before moving SELECT ALL option
            moveOptions(toBox, fromBox);
            moveSingleOption(fromBox, toBox, 0);
        } else {
            moveSelectedOptions(fromBox, toBox);
        }
        
    } else if (aO[1]=="Remove") {
//        alert("toBox length = |"+toBox.options.length+"|");
        if (toBox.options.length>0) {
	        if (toBox.options[0].selected && toBox.options[0].text=="SELECT ALL") {
    	        moveOptions(fromBox,toBox);
        	    moveOptions(toBox,fromBox);
	        } else {
    	        moveSelectedOptions(toBox, fromBox);
        	}
        }
    } else {
        return false;
    }
	
	addRemoveSort(toBox);
}
function moveSelectedOptions(fromBox, toBox) {

    for (var i=0;i<fromBox.length;i++) {
        if (fromBox.options[i].selected) {
            textValue=fromBox.options[i].text;
            oNewOption = new Array(textValue,textValue,false,false);
            addOptions(toBox,oNewOption)
            fromBox.options[i]=null;
            i--;
        }
    }
    
    return;
}

function moveOptions(fromBox, toBox) {

    for (var i=0;i<fromBox.length;i++) {
        textValue=fromBox.options[i].text;
        oNewOption = new Array(textValue,textValue,false,false);
        addOptions(toBox,oNewOption);
        fromBox.options[i]=null;
        i--;
    }
    
    return;
}
function copyOptions(fromBox, toBox) {

    for (var i=0;i<fromBox.length;i++) {
        textValue=fromBox.options[i].text;
        oNewOption = new Array(textValue,textValue,false,false);
        addOptions(toBox,oNewOption);
    }
    
    return;
}

function moveSingleOption(fromBox, toBox, i) {

    textValue=fromBox.options[i].text;
    oNewOption = new Array(textValue,textValue,false,false);
    addOptions(toBox,oNewOption)
    fromBox.options[i]=null;

    return;
}

function addOptions(o,ar) {
//- o - object
//- ar - Array containing options attributes
//ar=new Array(value,textValue,false,select=>false);

    oOption=document.createElement("OPTION");

    if (document.all) {
        oOption.innerText=ar[0];
    } else {
        oOption.text=ar[0];
    }
	oOption.value=ar[1];
    o.appendChild(oOption);

    if (ar[2]==true) {
        oOption.defaultSelected=true;
    } else {
        oOption.defaultSelected=false;
	}

    if (ar[3]==true) {
        oOption.selected=true;
    } else {
        oOption.selected=false;
	}

    return;
}

function selectAllOptions(o) {

    selBox=document.getElementById(o);
    
    for (var i=0;i<selBox.length;i++) {
        selBox.options[i].selected=true;
    }
    
    return;

}
function displayChildNodes(o) {
    var ol, ocn, i;
    ol=o.length;
//    alert("ocn.length = "+ol);
    ocn = o(i);
    for(i=0;i<ol;i++) {
        ocn = o(i);
        document.write(ocn.nodeName+" : ");
        document.write(ocn.text+"<br>");
//        alert(ocn.nodeName+" has |"+ocn.hasChildNodes()+"|");
        if (ocn.hasChildNodes() && ocn.childNodes.length>1) {
           displayChildNodes(ocn.childNodes);
        }
    }
    return;
}

function findStyleRule(styleName) {
  for (i = 0; i < document.styleSheets.length; i++) { 
    for (j = 0; j < document.styleSheets(i).rules.length; j++) {
//    alert(document.styleSheets(i).rules(j).selectorText);
      if (document.styleSheets(i).rules(j).selectorText == styleName) {
        return document.styleSheets(i).rules(j);
      }
    }
  }
}
function addRemoveSort(o){
	var mode, optExists, optName, canEnable;
	var isSelected= new Array;
    arO=o.name.split("_");
	optName=arO[1];
	canEnable=true;

	mode=o.length > 0 ? "add" : "remove" ;
	
	for (i=1;i<=5;i++) {
		optExists=false;
		isSelected[i]=false;

		sBox=document.getElementById("sort_select_"+i);
		boxReset=sBox.selectedIndex>0 ? false : true ;
		
//        alert("Sbox " + i + " length = " + sBox.length);
		for (j=0;j<sBox.length;j++) {

			if (sBox.options[j].text==optName) {
				optExists=true;
	   			if (mode=="remove") {
					sBox.options[j]=null;
				} 
			}
		}

		if (mode=="add" && optExists==false) {
	        oNewOption = new Array(optName,optName,false,false);
	        addOptions(sBox,oNewOption);

		}
		
		if(boxReset) {sBox.selectedIndex=0;}

		if ( (i==1 && sBox.length>0) || (i > 1 && canEnable) ) {
		    if(sBox.disabled) {
			  sBox.disabled=false;
			  sButtons=document.getElementsByName("sort_radio_"+i);

			  for(j=0;j<sButtons.length;j++) {
			  	sButtons[j].disabled=false;
			  }
			}
		} else {
		  sBox.selectedIndex=0;
		  sBox.disabled=true;
		  sButtons=document.getElementById("sort_radio_"+i);
		  for(j=0;j<sButtons.length;j++) {
		  	sButtons[j].disabled=true;
		  }
		}
//		alert(i + " isSelected " + isSelected[i] + "\n" + sBox.selectedIndex);
		if (isSelected[i]==false) {
			canEnable=false;
		}
	}
	
	return;
	
}
function sortChange(o) {
	var numCriteria, boxNum, nextBoxNum;
	oArr=o.name.split("_");
	boxNum=parseInt(oArr[2]);
	nextBoxNum=boxNum+1;
	
//	alert(o.name + "\n" + o.selectedIndex + "\nBoxNum = " +boxNum+"\nNext = "+nextBoxNum);
	fl=new Array("viewlocation","city","state","category","relationship");
	numCriteria=0;
	for(i=0;i<fl.length;i++) {
		arrLen=document.getElementById("dd_"+fl[j]+"_Selected[]").length;
		if(arrLen>0) { numCriteria++; }
	}
	
	if(o.selectedIndex>0 && oArr[2] < o.length-1) {
		nextBox=document.getElementById(oArr[0]+"_"+oArr[1]+"_"+nextBoxNum);
        if(nextBox.disabled) {
	        nextBox.disabled=false;
            nextBox.selectedIndex=0;
            nextButtons=document.getElementsByName("sort_radio_"+nextBoxNum);
            for(j=0;j<nextButtons.length;j++) {
		        nextButtons[j].disabled=false;
            }
		}

	} else {
		for(k=5;k>boxNum;k--) {
            nextBox=document.getElementById(oArr[0]+"_"+oArr[1]+"_"+k);
            nextBox.selectedIndex=0;
            nextBox.disabled=true;
            nextButtons=document.getElementsByName("sort_radio_"+k);
            for(j=0;j<nextButtons.length;j++) {
			    nextButtons[j].disabled=true;
            }
		}	
	}
}