//DO NOT EDIT THIS FILE, IT MAY BE OVER-WRITTEN.

var supported = (document.getElementById || document.all); 

if (supported)
{
	document.write("<STYLE TYPE='text/css'>");
	document.write(".para {display: none}");
	document.write("</STYLE>");

	var max = 7;
	var shown = new Array();
	for (var i=1;i<=max;i++)
	{
		shown[i+1] = false;
	}
}

function popupWSize(url,h,w)
{
	if (! window.focus)return true;
	window.open(url, "", "width=" + w + ",height=" + h + ",status=yes,resizable=yes,scrollbars=yes,toolbar=yes");
	return false;
}

function popupWithProperties(mylink, windowname, WinProperties)
{
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
	{
   		href=mylink;
	}else
	{
  	 	href=mylink.href;
	}

   	if (href.indexOf('popup_params/') != -1)
   	{
   		var index = href.indexOf('popup_params/') + 13;
   		WinProperties = href.substring(index);
   		href = href.substring(0,href.indexOf('popup_params/'));
   	}
	window.open(href, '', WinProperties);
	return false;
}


function popup(mylink, windowname)
{
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
   		href=mylink;
	else
   		href=mylink.href;
	window.open(href, windowname, 'width=725,height=800,status=yes,resizable=yes,scrollbars=yes,toolbar=yes');
	return false;
}


function popupViewer(mylink, windowname)
{
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
   		href=mylink;
	else
   		href=mylink.href;
	window.open(href, '', 'width=320,height=420,status=yes,resizable=yes,scrollbars=no');
	return false;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
   

//FUNCTION USED TO TOGGLE SPANS VISIBILITY
function blocking(i)
{
	if (!supported)
	{
		alert('This link does not work in your browser.');
		return;
	}
	var plusLocation = serverRoot+ "/images/plus.jpg";
	var minusLocation = serverRoot+ "/images/minus.jpg";
	
	imgShow = (shown[i]) ? minusLocation : plusLocation;

	var imgCode = 'IMG' + i;
	
	if (document.getElementById)
	{
	 	//COMMON 
		var current = document.getElementById('span'+i).style.display;
		if (current == 'block')
		{
			document.getElementById('span'+i).style.display = 'none';
			document.images[imgCode].src= plusLocation;
		}else{
			document.getElementById('span'+i).style.display = 'block';
			document.images[imgCode].src= minusLocation;
		}
	}
	else if (document.all)
	{
		var current = document.all['span'+i].style.display;
		if (current == 'block')
		{
			document.all['span'+i].style.display = 'none';
			document.images[imgCode].src= plusLocation;
		}else{
			document.all['span'+i].style.display = 'block';
			document.images[imgCode].src= minusLocation;
		}
	}
	
	return false;
}

//REDIRECTION SCRIPTS
//SHOW ITEM REPORT
function rpt(exptId,displayElement,searchField)
{
	if (! window.focus)return true;
	var link = serverRoot+ "/app/action/DisplayItemAction";
	link = link + "/search_value/" + exptId.toString() + "/search_element/" + displayElement.toString() + "/search_field/" + searchField;
	if(window.projectScope){
		link=link +"/project/" + window.projectScope;
	}
	if(window.isPopup){
		link=link + "/popup/true";
		window.location=link;
	}else{
		//window.open(link, '','width=800,height=800,status=yes,resizable=yes,scrollbars=yes,toolbar=yes');
		window.location=link;
	}
	return false;
}

//SHOW ITEM EDIT PAGE
function edit(exptId,displayElement,searchField)
{
	if (! window.focus)return true;
	var link = serverRoot+ "/app/action/EditItemAction/popup/true";
	link = link + "/search_value/" + exptId.toString() + "/search_element/" + displayElement.toString() + "/search_field/" + searchField;
	if(window.projectScope){
		link=link +"/project/" + window.projectScope;
	}
	window.open(link, '','width=600,height=800,status=yes,resizable=yes,scrollbars=yes,toolbar=yes');
	return false;
}

//SHOW ITEM IMAGE VIEWER
function viewer(sessionId)
{
	if (! window.focus)return true;
	var link = serverRoot+ "/app/action/ShowViewerAction/popup/true";
	link = link + "/search_element/xnat:mrSessionData/search_field/xnat:mrSessionData.ID/search_value/" + sessionId.toString();
	window.open(link, '', 'width=320,height=420,status=yes,resizable=yes,scrollbars=no');
	return false;
}

//SHOW ITEM IMAGE VIEWER WITH PRESET DEFUALT EXPERIMENT
function view(sessionId,exptCode) {
	if (exptCode==''){
		viewer(sessionId);
	}	
	else {
		var link = serverRoot+ "/app/action/ShowViewerAction/popup/true";
		link = link + "/search_element/xnat:mrSessionData/search_field/xnat:mrSessionData.ID/search_value/" + sessionId.toString() + "/startDisplayWith/" + exptCode.toString();
		window.open(link, '', 'width=320,height=420,status=yes,resizable=yes,scrollbars=no');
	}
	return false;	
}

//SHOW ITEM IMAGE VIEWER FOR SUBJECT
function viewPart(part_id) {
	var link = serverRoot+ "/app/action/ShowViewerAction/popup/true";
	link = link + "/skipq/true/id/" + part_id.toString();
	window.open(link, '', 'width=320,height=420,status=yes,resizable=yes,scrollbars=no');
	return false;	
}

//SHOW EMAIL SCREEN
function email(toAddress)
{
	if (! window.focus)return true;
	var link = serverRoot+ "/app/template/XDATScreen_email.vm";
	link = link + "/emailTo/" + toAddress;
	window.location = link;
	return false;
}

//SHOW ITEM XML
function displayXML(searchValue,displayElement,searchField)
{
	if (! window.focus)return true;
	var link = serverRoot+ "/app/action/DisplayXMLAction/popup/true";
	link = link + "/search_value/" + searchValue.toString() + "/search_element/" + displayElement.toString() + "/search_field/" + searchField;
	window.open(link, '','width=600,height=800,status=yes,resizable=yes,scrollbars=yes,toolbar=yes');
	return false;
}

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function concealContent(message){
    document.getElementById("layout_content2").className="warning";
    document.getElementById("layout_content2").innerHTML=message;
    document.getElementById("layout_content2").style.display="block";
    document.getElementById("layout_content").style.display="none";
}



function ArrayIndexOf(array,item){
	var index =-1;
	for(var i=0;i<array.length;i++){
		if (array[i]==item){
			index=i;
			break;
		}
	}
	return index;
}

function ArrayIndexById(array,id){
	var index =-1;
	for(var i=0;i<array.length;i++){
		if (array[i].getId()==item){
			index=i;
			break;
		}
	}
	return index;
}

function ArrayIndexByName(array,name){
	var index =-1;
	for(var i=0;i<array.length;i++){
		if (array[i].getName()==item){
			index=i;
			break;
		}
	}
	return index;
}

function stringTrim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}
function stringCamelCaps(val)
{
        var temp = val.replace(/^\s*|\s*$/g,"");
        temp = temp.replace(/[-]/," ");
        temp = temp.replace(/[&]/," ");
        temp = temp.replace(/[?]/," ");
        temp = temp.replace(/[<]/," ");
        temp = temp.replace(/[>]/," ");
        temp = temp.replace(/[(]/," ");
        temp = temp.replace(/[)]/," ");
        var newVal = '';
        temp = temp.split(' ');
        for(var c=0; c < temp.length; c++) {
              if (c==0)
                newVal += temp[c].substring(0,1) +
temp[c].substring(1,temp[c].length);
              else
                newVal += temp[c].substring(0,1).toUpperCase() +
temp[c].substring(1,temp[c].length);
        }
        
        return newVal;
}
String.prototype.trim = function () {
	return this.replace(/^\s*|\s*$/g,"");
}


String.prototype.startsWith = function(str) {
	if (this.indexOf(str)==0){
		return true;
	}else{	
		return false;
	}
}

String.prototype.endsWith = function(str) {
	if (this.lastIndexOf(str)==(this.length-str.length)){
		return true;
	}else{	
		return false;
	}
}

Array.prototype.contains = function(str) {
	for(var containsCount=0;containsCount<this.length;containsCount++){
		if (this[containsCount]==str){
			return true;
		}
	}
	
	return false;
}


function emptyChildNodes(element) {
	while(element.childNodes.length>0){
		element.removeChild(element.childNodes[0]);
	}
}

function log(msg){
	if (DEBUG){
		var logBox = document.getElementById("DEBUG_OUT");
		if (logBox==undefined){
			logBox = document.createElement("DIV");
			logBox.Id="DEBUG_OUT";
			logBox.style.position="absolute";
			logBox.style.right="0px";
			logBox.style.bottom="0px";
			logBox.style.color="FFFFFF";
			document.childNodes.push(logBox);
		}
		logBox.innerHTML=logBox.innerHTML + "<BR>" +msg;
	}
}

function isValidDate(dateStr, format) {
   if (format == null) { format = "MDY"; }
   format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { // If the year is first
      var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/;
      var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/;
   } else if (format.substring(1, 2) == "Y") { // If the year is second
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/;
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/;
   } else { // The year must be third
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/;
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
   }
   // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
   if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) { return false; }
   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
   return true;
}


function validateDate(sel){
   var childNodes = sel.parentNode.getElementsByTagName("SELECT");
   var img_div = sel.parentNode.getElementsByTagName("DIV")[0];
   if(img_div!=undefined)img_div.innerHTML="";
   var month =0;
   var day = 0;
   var year=0;
   for(var childNodeCount=0;childNodeCount<childNodes.length;childNodeCount++)
   {
      if (childNodes[childNodeCount].name.indexOf(".month")>0){
       if(childNodes[childNodeCount].selectedIndex==0)
       {
        return;
       }else{
        month= childNodes[childNodeCount].options[childNodes[childNodeCount].selectedIndex].value;
		monthInt = parseInt(month) + 1;
		month = monthInt;
	   }
      }else if (childNodes[childNodeCount].name.indexOf(".date")>0){
       if(childNodes[childNodeCount].selectedIndex==0)
       {
        return;
       }else{
        day= childNodes[childNodeCount].options[childNodes[childNodeCount].selectedIndex].value;
       }
      }else if (childNodes[childNodeCount].name.indexOf(".year")>0){
       if(childNodes[childNodeCount].selectedIndex==0)
       {
        return;
       }else{
        year= childNodes[childNodeCount].options[childNodes[childNodeCount].selectedIndex].value;
       }
      }
   }
   
   if(!isValidDate(month+"/" + day + "/" + year))
   {
     for(var childNodeCount=0;childNodeCount<childNodes.length;childNodeCount++)
     {
       childNodes[childNodeCount].selectedIndex=0;
       if (childNodes[childNodeCount].name.indexOf(".date")>0){
        childNodes[childNodeCount].focus();
       }
     }
     alert("Please select a valid date.");
   }else{
     if(img_div!=undefined)img_div.innerHTML="<img src=\"" + serverRoot+ "/images/checkmarkGreen.gif\"/>";
   }
   
  }
  
  /**********************************************
   * Inserts a YUI Calendar to handle date validation & selection
   */
  function insertCalendar(input,_title){
  	//CREATE BUTTON
  	var button = document.createElement("input");
  	button.type="button";
  	button.value=">";
  	button.id="cal_"+input.id;
  	
  	//CREATE CALENDAR CONTAINER
	var calendarContainer = document.createElement("DIV");
	calendarContainer.className="yui-skin-sam";
	calendarContainer.style.display="inline";
	
	//INSERT INTO DOM
	if(input.nextSibling==undefined){
		input.parentNode.appendChild(calendarContainer);
	}else{
		input.parentNode.insertBefore(calendarContainer,input.nextSibling);
	}
	
	calendarContainer.appendChild(button);
	
	var calendarDIV= document.createElement("DIV");
	calendarContainer.appendChild(calendarDIV);
	calendarDIV.style.position="absolute";
	
	var cal1 = new YAHOO.widget.Calendar("cal1",calendarDIV,{context:["cal_"+input.id,"tr","tl"],title:_title,navigator:true,close:true,visible:false}); 

    cal1.text_input = input;
    input.calendar=cal1;
    
    button.calendar=cal1;
    button.onclick=function(){
    	this.calendar.show();
    	return false;
    }
    
    cal1.hider=button;
        
    cal1.handleSelect = function(type,args,obj) { 
        var dates = args[0]; 
		var date = dates[0]; 
		var year = date[0], month = date[1], day = date[2]; 
		
		this.text_input.value = month + "/" + day + "/" + year;
		this.text_input.calendar.hide();
    }
    
    
   input.onchange=function(){
   	   if(this.value!=""){
   	   	this.value=this.value.replace(/[-]/,"/");
   	   	this.value=this.value.replace(/[.]/,"/");
   	   	if(isValidDate(this.value)){
   	   	 this.calendar.select(this.value); 
         var selectedDates = this.calendar.getSelectedDates(); 
         if (selectedDates.length > 0) { 
            var firstDate = selectedDates[0]; 
            this.calendar.cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + firstDate.getFullYear()); 
            this.calendar.render(); 
         } else { 
            alert("Invalid date. MM/DD/YYYY"); 
         } 
   	   	}else{
   	   		alert("Invalid date. MM/DD/YYYY");
   	   		this.value="";
   	   		this.focus();
   	   	}
   	   }
	}
 					    
 	cal1.selectEvent.subscribe(cal1.handleSelect, cal1, true);
 
    cal1.render(); 
    cal1.hide();
    
    if(input.value!=""){
   	   	 input.onchange();
	}
  }

  
  function parseForm(formId){
  	var oForm,oElement, oName, oValue, oDisabled,
            data='', item = 0,
            i,len,j,jlen,opt;
  	//TAKEN FROM yui/build/connection/connection.js
  		if(typeof formId == 'string'){
			// Determine if the argument is a form id or a form name.
			// Note form name usage is deprecated by supported
			// here for legacy reasons.
			oForm = (document.getElementById(formId) || document.forms[formId]);
		}
		else if(typeof formId == 'object'){
			// Treat argument as an HTML form object.
			oForm = formId;
		}
		else{
			return;
		}
  		
  		// Iterate over the form elements collection to construct the
		// label-value pairs.
		for (i=0,len=oForm.elements.length; i<len; ++i){
			oElement  = oForm.elements[i];
			oDisabled = oElement.disabled;
            oName     = oElement.name;

			// Do not submit fields that are disabled or
			// do not have a name attribute value.
			if(!oDisabled && oName)
			{
                oName  = encodeURIComponent(oName)+'=';
                oValue = encodeURIComponent(oElement.value);

				switch(oElement.type)
				{
                    // Safari, Opera, FF all default opt.value from .text if
                    // value attribute not specified in markup
					case 'select-one':
                        if (oElement.selectedIndex > -1) {
                            opt = oElement.options[oElement.selectedIndex];
                            if(item++ >0)
                              data+='&';
                            data += oName + encodeURIComponent(
                                (opt.attributes.value && opt.attributes.value.specified) ? opt.value : opt.text);
                        }
                        break;
					case 'select-multiple':
                        if (oElement.selectedIndex > -1) {
                            for(j=oElement.selectedIndex, jlen=oElement.options.length; j<jlen; ++j){
                                opt = oElement.options[j];
                                if (opt.selected) {
		                            if(item++ >0)
		                              data+='&';
                                    data += oName + encodeURIComponent(
                                        (opt.attributes.value && opt.attributes.value.specified) ? opt.value : opt.text);
                                }
                            }
                        }
						break;
					case 'radio':
					case 'checkbox':
						if(oElement.checked){
                            if(item++ >0)
                              data+='&';
                            data += oName + oValue;
						}
						break;
					case 'file':
						// stub case as XMLHttpRequest will only send the file path as a string.
					case undefined:
						// stub case for fieldset element which returns undefined.
					case 'reset':
						// stub case for input type reset button.
					case 'button':
						// stub case for input type button elements.
						break;
					case 'submit':
						break;
					default:
                        if(item++ >0)
                          data+='&';
                        data += oName + oValue;
				}
			}
		}
		
		return data.toString();
  }


function appendImage(obj,img_name){
if(obj!=null){
	  if(typeof obj == 'string'){
			// Determine if the argument is a form id or a form name.
			// Note form name usage is deprecated by supported
			// here for legacy reasons.
			obj = (document.getElementById(obj));
	  }
	  
	  if(obj.appendedImage==undefined){
	    obj.appendedImage = document.createElement("img");
	    obj.appendedImage.style.marginLeft="5pt";
	    if(obj.nextSibling==null)
	    {
	    	obj.parentNode.insertBefore(obj.appendedImage,obj.nextSibling);
	    }else{
	    	obj.parentNode.appendChild(obj.appendedImage);
	    }
	  }
	  obj.appendedImage.src=serverRoot + img_name;
	}
}

function removeAppendImage(obj){
if(obj!=null){
	  if(typeof obj == 'string'){
			// Determine if the argument is a form id or a form name.
			// Note form name usage is deprecated by supported
			// here for legacy reasons.
			obj = (document.getElementById(obj));
	  }
	  
	  if(obj.appendedImage!=undefined){
	  	obj.appendedImage.parentNode.removeChild(obj.appendedImage);
	    obj.appendedImage=null;
	  }
	}
}

window.modals=new Object();  
  
function openModalPanel(id,msg,parentPanel){
	  if(id==undefined)id="wait";
	  if(window.modals[id]==undefined){
		  window.modals[id]=new YAHOO.widget.Panel(id,     
	          { 
	          	   width:"240px",    
	               fixedcenter:true,    
	               close:true,    
	               draggable:false,    
	               zindex:4,   
	               modal:true,   
	               visible:false  
	          }    
	      );   
	      if(parentPanel!=undefined){
	   	      parentPanel.hide();
	          window.modals[id].parentPanel=parentPanel;
	      }
	      window.modals[id].setHeader(msg);
	      window.modals[id].setBody('<img src="' + serverRoot + '/images/rel_interstitial_loading.gif" />');   
	      window.modals[id].render(document.body);	  	
	  }
	  
	  window.modals[id].show();
}

function closeModalPanel(id){
	if(id==undefined)id="wait";
	if(window.modals[id]!=undefined){
		var parentPanel=window.modals[id].parentPanel;
		window.modals[id].destroy();
		window.modals[id]=null;
		if(parentPanel!=undefined){
			parentPanel.show();
		}
	}
}

function displayError(msg){
    alert(msg);	
}
