// JavaScript Document

/* Popup Window Using a Link */
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos)
{
	if(pos=="random")
	{
		LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
	}

	if(pos=="center")
	{
		LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;
	} 
	else if((pos!="center" && pos!="random") || pos==null)
	{
		LeftPosition=0;TopPosition=20
	}
	
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';win=window.open(mypage,myname,settings);
}

/* Use This For Mailing List Form Submit */
function submitFormPopup(myForm, windowName)
{
	if (! window.focus)return true;
	window.open('', windowName, 'height=400,width=500,scrollbars=yes');
	myForm.target=windowName;
	return true;
}

/** print page **/
function printpage() {
	window.print();  
}

/** toggle div **/
function toggleDisplay(targetId)
{
    if (document.getElementById) 
	{
        target = document.getElementById(targetId);
    	if (target.style.display == "none")
		{
    		target.style.display = "";
    	} else {
    		target.style.display = "none";
    	}
    }
}

// toggle visibility 
function toggleVisibility(targetId) 
{
    if (document.getElementById) 
	{
        target = document.getElementById(targetId);
    	if (target.style.visibility == "hidden")
		{
    		target.style.visibility = "visible";
    	} else {
    		target.style.visibility = "hidden";
    	}
    }
}

// Jump Select (Dropdown) Menu
function JumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function popitup(url, w, h)
{
	newwindow=window.open(url,'name','height=h,width=w');
	if (window.focus) {newwindow.focus()}
	return false;
}

/** place focus on first input of form **/
function placeFocus() {
if (document.forms.length > 0) {
	var field = document.forms[0];
	for (i = 0; i < field.length; i++) {
		if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
			document.forms[0].elements[i].focus();
		break;
         }
      }
   }
}

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_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_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.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];}
}

/*** Used in the Application form. To disable Permanenet Address fields if same as mailing address **/
function perm() {

  if (document.getElementById('permSame').checked == true) {

    document.getElementById('permAddressLine1').value='';
    document.getElementById('permAddressLine2').value='';
    document.getElementById('permCity').value='';
    document.getElementById('permProvState').value='';
    document.getElementById('permPostalCode').value='';
    document.getElementById('permCountry').value='';
    
    document.getElementById('permAddressLine1').disabled=true;
    document.getElementById('permAddressLine2').disabled=true;
    document.getElementById('permCity').disabled=true;
    document.getElementById('permProvState').disabled=true;
    document.getElementById('permPostalCode').disabled=true;
    document.getElementById('permCountry').disabled=true;
  }
  else {
    
    document.getElementById('permAddressLine1').disabled=false;
    document.getElementById('permAddressLine2').disabled=false;
    document.getElementById('permCity').disabled=false;
    document.getElementById('permProvState').disabled=false;
    document.getElementById('permPostalCode').disabled=false;
    document.getElementById('permCountry').disabled=false;
  }
}

/**
 * Copyright (c)2005-2008 Matt Kruse (javascripttoolbox.com)
 * 
 * Dual licensed under the MIT and GPL licenses. 
 * This basically means you can use this code however you want for
 * free, but don't claim to have written it yourself!
 * Donations always accepted: http://www.JavascriptToolbox.com/donate/
 * 
 * Please do not link to the .js files on javascripttoolbox.com from
 * your site. Copy the files locally to your server instead.
 * 
 */
// Constructor
function CheckBoxGroup() {
	this.controlBox=null;
	this.controlBoxChecked=null;
	this.maxAllowed=null;
	this.maxAllowedMessage=null;
	this.masterBehavior="all";
	this.formRef=null;
	this.checkboxWildcardNames=new Array();
	this.checkboxNames=new Array();
	this.totalBoxes=0;
	this.totalSelected=0;
	// Public methods
	this.setControlBox=CBG_setControlBox;
	this.setMaxAllowed=CBG_setMaxAllowed;
	this.setMasterBehavior=CBG_setMasterBehavior;	// all, some
	this.addToGroup=CBG_addToGroup;
	// Private methods
	this.expandWildcards=CBG_expandWildcards;
	this.addWildcardCheckboxes=CBG_addWildcardCheckboxes;
	this.addArrayCheckboxes=CBG_addArrayCheckboxes;
	this.addSingleCheckbox=CBG_addSingleCheckbox;
	this.check=CBG_check;
	}
	
CheckBoxGroup.$VERSION = 1.01;

// Set the master control checkbox name
function CBG_setControlBox(name) { this.controlBox=name; }

// Set the maximum number of checked boxes in the set, and optionally
// the message to popup when the max is reached.
function CBG_setMaxAllowed(num,msg) {
	this.maxAllowed=num;
	if (msg!=null&&msg!="") { this.maxAllowedMessage=msg; }
	}

// Set the behavior for the checkbox group master checkbox
//	All: all boxes must be checked for the master to be checked
//	Some: one or more of the boxes can be checked for the master to be checked
function CBG_setMasterBehavior(b) { this.masterBehavior = b.toLowerCase(); }

// Add checkbox wildcards to the checkboxes array
function CBG_addToGroup() {
	if (arguments.length>0) {
		for (var i=0;i<arguments.length;i++) {
			this.checkboxWildcardNames[this.checkboxWildcardNames.length]=arguments[i];
			}
		}
	}

// Expand the wildcard checkbox names given in the addToGroup method
function CBG_expandWildcards() {
	if (this.formRef==null) {alert("ERROR: No form element has been passed.  Cannot extract form name!"); return false; }
	for (var i=0; i<this.checkboxWildcardNames.length;i++) {
		var n = this.checkboxWildcardNames[i];
		var el = this.formRef[n];
		if (n.indexOf("*")!=-1) { this.addWildcardCheckboxes(n); }
		else if(CBG_nameIsArray(el)) { this.addArrayCheckboxes(n); }
		else { this.addSingleCheckbox(el); }
		}
	}


// Add checkboxes to the group which match a pattern
function CBG_addWildcardCheckboxes(name) {
	var i=name.indexOf("*");
	if ((i==0) || (i==name.length-1)) {
		var searchString= (i)?name.substring(0,name.length-1):name.substring(1,name.length);
		var els = this.formRef.elements;
		var l = els.length;
		for (var j=0;j<l;j++) {
			var currentElement = els[j];
			if (currentElement.type && currentElement.type=="checkbox" && currentElement.name) {
				var currentElementName=currentElement.name;
				var partialName = (i)?currentElementName.substring(0,searchString.length) : currentElementName.substring(currentElementName.length-searchString.length,currentElementName.length);
				if (partialName==searchString) {
					if(CBG_nameIsArray(currentElement)) this.addArrayCheckboxes(currentElement);
					else this.addSingleCheckbox(currentElement);
					}
				}
			}
		}
	}

// Add checkboxes to the group which all have the same name
function CBG_addArrayCheckboxes(name) {
	if((CBG_nameIsArray(this.formRef[name])) && (this.formRef[name].length>0)) {
		for (var i=0; i<this.formRef[name].length; i++) { this.addSingleCheckbox(this.formRef[name][i]); }
		}
	}

function CBG_addSingleCheckbox(obj) {
	if (obj != this.formRef[this.controlBox]) {
		this.checkboxNames[this.checkboxNames.length]=obj;
		this.totalBoxes++;
		if (obj.checked) {
			this.totalSelected++;
			}
		}
	}

// Runs whenever a checkbox in the group is clicked
function CBG_check(obj) {
	var checked=obj.checked;
	if (this.formRef==null) {
		this.formRef=obj.form;
		this.expandWildcards();
		if (this.controlBox==null || obj.name!=this.controlBox) {
			this.totalSelected += (checked)?-1:1;
			}
		}
	if (this.controlBox!=null&&obj.name==this.controlBox) {
		if (this.masterBehavior=="all") {
			for (i=0;i<this.checkboxNames.length;i++) { this.checkboxNames[i].checked=checked; }
			this.totalSelected=(checked)?this.checkboxNames.length:0;
			}
		else {
			if (!checked) {
				obj.checked = (this.totalSelected>0)?true:false;
				obj.blur();
				}
			}
		}
	else {
		if (this.masterBehavior=="all" && this.controlBox!=null) {
			if (!checked) {
				this.formRef[this.controlBox].checked=false;
				this.totalSelected--;
				}
			else { this.totalSelected++; }
			if (this.controlBox!=null) {
				this.formRef[this.controlBox].checked=(this.totalSelected==this.totalBoxes)?true:false;
				}
			}
		else {
			if (!obj.checked) { this.totalSelected--; }	
			else { this.totalSelected++; }
			if (this.controlBox!=null) {
				this.formRef[this.controlBox].checked=(this.totalSelected>0)?true:false;
				}
			if (this.maxAllowed!=null) {
				if (this.totalSelected>this.maxAllowed) {
					obj.checked=false;
					this.totalSelected--;
					if (this.maxAllowedMessage!=null) { alert(this.maxAllowedMessage); }
					return false;
					}
				}
			}
		}
	}

function CBG_nameIsArray(obj) {
	return ((typeof obj.type!="string")&&(obj.length>0)&&(obj[0]!=null)&&(obj[0].type=="checkbox"));
}

// usage
// <img id="ticket_{$ticket.id}" src="/images/star_off.gif" onclick="updateStar(this.id)" align="absmiddle"> 
function updateStar(id) {
   //var imgsrc = (document.getElementById(id).src == "/images/star_off.gif") ? "/images/star_on.gif" : "/images/star_off.gif";
   if(document.getElementById(id).src == "/images/star_off.gif") {
   	imgsrc = "/images/star_on.gif";
   	alert('turn on');
   } else {
    imgsrc = "/images/star_off.gif";
       	alert('turn off');
   }
   
   var sendId = id.split('_')[1];
   //var sendStar = (imgsrc == "/images/star_off.gif") ? '0' : '1';
   
   //if( imgsrc == "/images/star_off.gif") {
   if (document.getElementById(id).src == "/images/star_off.gif") {
   	sendStar = 1;
   	document.getElementById(id).src = '/images/star_off.gif';
   } else {
   	sendStar = 0;
   	document.getElementById(id).src = '/images/star_on.gif';
   }
   
   var objXml = new XMLHttpRequest();
   var datasource = "/tickets/set_starred.php";
   var params = "id=" + sendId + "&star=" + sendStar;
   objXml.open("GET", datasource + "?" + params, true);
   objXml.onreadystatechange=function() {
      if ((objXml.readyState==4) && (objXml.status==200)) {
         //alert('status changed. params=' + params );
      } 
   }
   objXml.send(null);
}
