/* 
 * sops.js
 * SOPS javascript file
 */
 // Function for View Flyer
var flyerWin=null; 
function flyerOpener(personId) {
	var url = "/offender/flyer.do?personId="+personId;
	if((flyerWin==null)||(flyerWin.closed)){
		flyerWin = window.open(url,"OffenderFlyer","width=680,height=680,screenX=0,screenY=0,top=0,left=0,scrollbars,toolbar,menubar");
	} else {
		flyerWin.location.href = url;
	}
	flyerWin.focus();
}

function openFlyerList() {
	if ((flyerWin==null)||(flyerWin.closed))
		flyerWin = window.open("","OffenderFlyerList","width=680,height=680,screenX=0,screenY=0,top=0,left=0,scrollbars,toolbar,menubar");
	document.flyerListForm.elements['commaSeparatedPersonIds'].value
			 = formName.elements['commaSeparatedPersonIds'].value;
	document.flyerListForm.target='OffenderFlyerList';
	var set_timeout=setTimeout("document.flyerListForm.submit();",100);
	flyerWin.focus();
}

function popLegalStatusList() {
	var url = "/offender/LegalStatusList.jsp";
	window.open(url,"LegalStatus","width=680,height=620,screenX=0,screenY=0,top=0,left=0,scrollbars,toolbar,menubar");
}

function popLegalStatusListNew() {
	var url = "/offender/LegalStatusListNew.jsp";
	window.open(url,"LegalStatus","width=680,height=620,screenX=0,screenY=0,top=0,left=0,scrollbars,toolbar,menubar");
}

function popReRegistrationInfo(url) {
	window.open(url,"ReRegistration","width=680,height=620,screenX=0,screenY=0,top=0,left=0,resizable,scrollbars,toolbar,menubar");
}

function popReRegistrationInfo(url) {
	window.open(url,"ReRegistration","width=680,height=620,screenX=0,screenY=0,top=0,left=0,resizable,scrollbars,toolbar,menubar");
}

// Returns true if character c is a digit 
// (0 .. 9).
function isDigit (c){
   return ((c >= "0") && (c <= "9"))
}

//returns true if value is a number, else false
function isNumeric(varValue) {
	var varNewValue = trim(varValue);
	return (!isNaN(varNewValue));
}
 
function anyDigitPresent(inputName) {
   	for (i=0; i<inputName.length; i++){
    	var c = inputName.charAt(i);
		if (isDigit(c)) return true;
    }
    return false;
}

  //returns the parameter with leading spaces removed
function lTrim(strValue){
	for (i=0;strValue.charAt(i)==" ";i++);
	return strValue.substr(i);
}

	//returns the parameter with trailing spaces removed
function rTrim(strValue){
	for (j=strValue.length - 1;strValue.charAt(j)==" ";j--);
	return strValue.substr(0, j + 1);
}

	//returns the parameter with leading and trailing spaces removed
function trim(strValue){
	return rTrim(lTrim(String(strValue)));
}


//function to get address map
function getMap(address, city, state, zip,message)
{
	var mapURL = "http://www.mapquest.com/maps/map.adp?searchtype=address&country=US&address="+address+
	    "&city="+city+"&state="+state+"&zipcode="+zip;

	if (confirm(message)){
		window.open(mapURL,"_New","width=764, height=480,scrollbars");
		      }
} 


 // Expand and Collapse
 
 function Toggle(r_strTaskBoxID) {
	var l_objTaskBox = document.getElementById(r_strTaskBoxID);
	var l_objTaskBoxImg = document.getElementById(r_strTaskBoxID + 'Link');
	if(l_objTaskBox) {
		if(l_objTaskBox.style.display=='none') {
			l_objTaskBox.style.display='';
		}
		else {
			l_objTaskBox.style.display='none';
		}
	}
	
	if(l_objTaskBoxImg) { 
		if(Right(l_objTaskBoxImg.src, 13)  == 'collapsed.gif') {
			l_objTaskBoxImg.src = './images/expanded.gif';
		}
		else {
			l_objTaskBoxImg.src = './images/collapsed.gif';
		}
	}
}
function Right(str, n)
{
	if (n <= 0)     // Invalid bound, return blank string
		return "";
  else if (n > String(str).length)   // Invalid bound, return
		return str;                     // entire string
  else { // Valid bound, return appropriate substring
		var iLen = String(str).length;
    return String(str).substring(iLen, iLen - n);
  }
}
 
 // End -- Expand and Collapse
 function securityAlert() {
	alert("You do not have permissions to access this functionality!");
}
 
// Launch a help window

function LaunchHelp(url)
{
	helpWindow = window.open(url, 'Help', 'width=600, height=400, resizable, scrollbars');
	helpWindow.focus();
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
/*-----Add/Remove Box functions------ */

/* 
DESCRIPTION: This widget is used to easily and quickly create an interface
where the user can transfer choices from one select box to another. For
example, when selecting which columns to show or hide in search results.
This object adds value by automatically storing the values that were added
or removed from each list, as well as the state of the final list. 

COMPATABILITY: Should work on all Javascript-compliant browsers.

USAGE:
// Create a new OptionTransfer object. Pass it the field names of the left
// select box and the right select box.
var ot = new OptionTransfer("from","to");

// Optionally tell the lists whether or not to auto-sort when options are 
// moved. By default, the lists will be sorted.
ot.setAutoSort(true);

// Optionally set the delimiter to be used to separate values that are
// stored in hidden fields for the added and removed options, as well as
// final state of the lists. Defaults to a comma.
ot.setDelimiter("|");

// You can set a regular expression for option texts which are _not_ allowed to
// be transferred in either direction
ot.setStaticOptionRegex("static");

// These functions assign the form fields which will store the state of
// the lists. Each one is optional, so you can pick to only store the
// new options which were transferred to the right list, for example.
// Each function takes the name of a HIDDEN or TEXT input field.

// Store list of options removed from left list into an input field
ot.saveRemovedLeftOptions("removedLeft");
// Store list of options removed from right list into an input field
ot.saveRemovedRightOptions("removedRight");
// Store list of options added to left list into an input field
ot.saveAddedLeftOptions("addedLeft");
// Store list of options radded to right list into an input field
ot.saveAddedRightOptions("addedRight");
// Store all options existing in the left list into an input field
ot.saveNewLeftOptions("newLeft");
// Store all options existing in the right list into an input field
ot.saveNewRightOptions("newRight");

// IMPORTANT: This step is required for the OptionTransfer object to work
// correctly.
// Add a call to the BODY onLoad="" tag of the page, and pass a reference to
// the form which contains the select boxes and input fields.
BODY onLoad="ot.init(document.forms[0])"

// ADDING ACTIONS INTO YOUR PAGE
// Finally, add calls to the object to move options back and forth, either
// from links in your page or from double-clicking the options themselves.
ot.transferRight();
ot.transferAllRight();
ot.transferLeft();
ot.transferAllLeft();

*/ 

/* SOURCE FILE: selectbox.js */
function hasOptions(obj){if(obj!=null && obj.options!=null){return true;}return false;}
function selectUnselectMatchingOptions(obj,regex,which,only){if(window.RegExp){if(which == "select"){var selected1=true;var selected2=false;}else if(which == "unselect"){var selected1=false;var selected2=true;}else{return;}var re = new RegExp(regex);if(!hasOptions(obj)){return;}for(var i=0;i<obj.options.length;i++){if(re.test(obj.options[i].text)){obj.options[i].selected = selected1;}else{if(only == true){obj.options[i].selected = selected2;}}}}}
function selectMatchingOptions(obj,regex){selectUnselectMatchingOptions(obj,regex,"select",false);}
function selectOnlyMatchingOptions(obj,regex){selectUnselectMatchingOptions(obj,regex,"select",true);}
function unSelectMatchingOptions(obj,regex){selectUnselectMatchingOptions(obj,regex,"unselect",false);}
function sortSelect(obj){var o = new Array();if(!hasOptions(obj)){return;}for(var i=0;i<obj.options.length;i++){o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;}if(o.length==0){return;}o = o.sort(
function(a,b){if((a.text+"") <(b.text+"")){return -1;}if((a.text+"") >(b.text+"")){return 1;}return 0;});for(var i=0;i<o.length;i++){obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);}}
function selectAllOptions(obj){if(!hasOptions(obj)){return;}for(var i=0;i<obj.options.length;i++){obj.options[i].selected = true;}}
function moveSelectedOptions(from,to){
if(arguments.length>3){var regex = arguments[3];if(regex != ""){unSelectMatchingOptions(from,regex);}}if(!hasOptions(from)){return;}for(var i=0;i<from.options.length;i++){var o = from.options[i];if(o.selected){if(!hasOptions(to)){var index = 0;}else{var index=to.options.length;}to.options[index] = new Option( o.text, o.value, false, false);}}for(var i=(from.options.length-1);i>=0;i--){var o = from.options[i];if(o.selected){from.options[i] = null;}}if((arguments.length<3) ||(arguments[2]==true)){sortSelect(from);sortSelect(to);}from.selectedIndex = -1;to.selectedIndex = -1;
}
function copySelectedOptions(from,to){var options = new Object();if(hasOptions(to)){for(var i=0;i<to.options.length;i++){options[to.options[i].value] = to.options[i].text;}}if(!hasOptions(from)){return;}for(var i=0;i<from.options.length;i++){var o = from.options[i];if(o.selected){if(options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text){if(!hasOptions(to)){var index = 0;}else{var index=to.options.length;}to.options[index] = new Option( o.text, o.value, false, false);}}}if((arguments.length<3) ||(arguments[2]==true)){sortSelect(to);}from.selectedIndex = -1;to.selectedIndex = -1;}
function moveAllOptions(from,to){selectAllOptions(from);if(arguments.length==2){moveSelectedOptions(from,to);}else if(arguments.length==3){moveSelectedOptions(from,to,arguments[2]);}else if(arguments.length==4){moveSelectedOptions(from,to,arguments[2],arguments[3]);}}
function copyAllOptions(from,to){selectAllOptions(from);if(arguments.length==2){copySelectedOptions(from,to);}else if(arguments.length==3){copySelectedOptions(from,to,arguments[2]);}}
function swapOptions(obj,i,j){var o = obj.options;var i_selected = o[i].selected;var j_selected = o[j].selected;var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);o[i] = temp2;o[j] = temp;o[i].selected = j_selected;o[j].selected = i_selected;}
function moveOptionUp(obj){if(!hasOptions(obj)){return;}for(i=0;i<obj.options.length;i++){if(obj.options[i].selected){if(i != 0 && !obj.options[i-1].selected){swapOptions(obj,i,i-1);obj.options[i-1].selected = true;}}}}
function moveOptionDown(obj){if(!hasOptions(obj)){return;}for(i=obj.options.length-1;i>=0;i--){if(obj.options[i].selected){if(i !=(obj.options.length-1) && ! obj.options[i+1].selected){swapOptions(obj,i,i+1);obj.options[i+1].selected = true;}}}}
function removeSelectedOptions(from){if(!hasOptions(from)){return;}for(var i=(from.options.length-1);i>=0;i--){var o=from.options[i];if(o.selected){from.options[i] = null;}}from.selectedIndex = -1;}
function removeAllOptions(from){if(!hasOptions(from)){return;}for(var i=(from.options.length-1);i>=0;i--){from.options[i] = null;}from.selectedIndex = -1;}
function addOption(obj,text,value,selected){if(obj!=null && obj.options!=null){obj.options[obj.options.length] = new Option(text, value, false, selected);}}


/* SOURCE FILE: OptionTransfer.js */

function OT_transferLeft(){
moveSelectedOptions(this.right,this.left,this.autoSort,this.staticOptionRegex);this.update();}
function OT_transferRight(){
moveSelectedOptions(this.left,this.right,this.autoSort,this.staticOptionRegex);this.update();
}
function OT_transferAllLeft(){
moveAllOptions(this.right,this.left,this.autoSort,this.staticOptionRegex);this.update();}
function OT_transferAllRight(){
moveAllOptions(this.left,this.right,this.autoSort,this.staticOptionRegex);this.update();}
function OT_saveRemovedLeftOptions(f){
this.removedLeftField = f;}
function OT_saveRemovedRightOptions(f){
this.removedRightField = f;}
function OT_saveAddedLeftOptions(f){
this.addedLeftField = f;}
function OT_saveAddedRightOptions(f){
this.addedRightField = f;}
function OT_saveNewLeftOptions(f){
this.newLeftField = f;}
function OT_saveNewRightOptions(f){
this.newRightField = f;}
function OT_update(){
var removedLeft = new Object();var removedRight = new Object();var addedLeft = new Object();var addedRight = new Object();var newLeft = new Object();var newRight = new Object();for(var i=0;i<this.left.options.length;i++){var o=this.left.options[i];newLeft[o.value]=1;if(typeof(this.originalLeftValues[o.value])=="undefined"){addedLeft[o.value]=1;removedRight[o.value]=1;}}for(var i=0;i<this.right.options.length;i++){var o=this.right.options[i];newRight[o.value]=1;if(typeof(this.originalRightValues[o.value])=="undefined"){addedRight[o.value]=1;removedLeft[o.value]=1;}}if(this.removedLeftField!=null){this.removedLeftField.value = OT_join(removedLeft,this.delimiter);}if(this.removedRightField!=null){this.removedRightField.value = OT_join(removedRight,this.delimiter);}if(this.addedLeftField!=null){this.addedLeftField.value = OT_join(addedLeft,this.delimiter);}if(this.addedRightField!=null){this.addedRightField.value = OT_join(addedRight,this.delimiter);}if(this.newLeftField!=null){this.newLeftField.value = OT_join(newLeft,this.delimiter);}if(this.newRightField!=null){this.newRightField.value = OT_join(newRight,this.delimiter);}}
function OT_join(o,delimiter){
var val;var str="";for(val in o){if(str.length>0){str=str+delimiter;}str=str+val;}return str;}
function OT_setDelimiter(val){
this.delimiter=val;}
function OT_setAutoSort(val){
this.autoSort=val;}
function OT_setStaticOptionRegex(val){
this.staticOptionRegex=val;}
function OT_init(theform){
this.form = theform;if(!theform[this.left]){
return false;}if(!theform[this.right]){return false;}this.left=theform[this.left];this.right=theform[this.right];for(var i=0;i<this.left.options.length;i++){this.originalLeftValues[this.left.options[i].value]=1;}for(var i=0;i<this.right.options.length;i++){this.originalRightValues[this.right.options[i].value]=1;}if(this.removedLeftField!=null){this.removedLeftField=theform[this.removedLeftField];}if(this.removedRightField!=null){this.removedRightField=theform[this.removedRightField];}if(this.addedLeftField!=null){this.addedLeftField=theform[this.addedLeftField];}if(this.addedRightField!=null){this.addedRightField=theform[this.addedRightField];}if(this.newLeftField!=null){this.newLeftField=theform[this.newLeftField];}if(this.newRightField!=null){this.newRightField=theform[this.newRightField];}this.update();}
function OptionTransfer(l,r){
this.form = null;this.left=l;this.right=r;this.autoSort=true;this.delimiter=",";this.staticOptionRegex = "";this.originalLeftValues = new Object();this.originalRightValues = new Object();this.removedLeftField = null;this.removedRightField = null;this.addedLeftField = null;this.addedRightField = null;this.newLeftField = null;this.newRightField = null;this.transferLeft=OT_transferLeft;this.transferRight=OT_transferRight;this.transferAllLeft=OT_transferAllLeft;this.transferAllRight=OT_transferAllRight;this.saveRemovedLeftOptions=OT_saveRemovedLeftOptions;this.saveRemovedRightOptions=OT_saveRemovedRightOptions;this.saveAddedLeftOptions=OT_saveAddedLeftOptions;this.saveAddedRightOptions=OT_saveAddedRightOptions;this.saveNewLeftOptions=OT_saveNewLeftOptions;this.saveNewRightOptions=OT_saveNewRightOptions;this.setDelimiter=OT_setDelimiter;this.setAutoSort=OT_setAutoSort;this.setStaticOptionRegex=OT_setStaticOptionRegex;this.init=OT_init;this.update=OT_update;}

/*-----End Add/Remove Box functions------ */


function openMapWindow(url,windowName){

	var width = (screen.width * .8);
	var height = (screen.height * .8);
    var left = (window.screen.width/2) - (width/2 + 10);
    var top = (window.screen.height/2) - (height/2 + 50);

    var windowFeatures = "menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top;
    
    window.open(url, windowName, windowFeatures);
}


function loadInParentWindow(url){
	
	//OffenderFlyer is the name of window as specified in the window.open function call
	if(window.name == 'OffenderFlyer') {
		loadSleep(self);
		self.opener.location = url;
	} else {
	 	window.location = url;
	}
}


function loadSleep(windowElem) {
	setTimeout('closeWindow()',1000);
}


function closeWindow() {
	self.close();
}
