
// Global
var myRemote;
var navURL;

  
function launch(newURL, newName, newFeatures, orgName) {
remote = open(newURL, newName, newFeatures);
  if (remote.opener == null)
    remote.opener = window;

  remote.opener.name = orgName;
  return remote;
}

function openWindow(loc) {
  myRemote = launch(loc,"FreeSurveys1","height=640,width=750,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0","Help");
  window.myRemote.focus();  
}

function openPreview(loc) {
  myRemote = launch(loc,"FreeSurveys1","height=640,width=800,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0","Help");
  window.myRemote.focus();  
}

function openWindowWithMenu(loc) {
  myRemote = launch(loc,"WindowWithMenu","height=640,width=800,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=1,resizable=1,scrollbars=1,status=0,toolbar=1","Help");
  window.myRemote.focus();  
}


function openSlideShow(loc) {
  myRemote = launch(loc,"SlideShow1","height=640,width=750,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0","Help");
  window.myRemote.focus();  
}

function miniPopup(loc) {
  myRemote = launch(loc,"FreeSurveys2","height=300,width=450,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0","Help");
  window.myRemote.focus();  
}


function gotoLoc (loc) {
  myRemote = launch(loc,"FreeSurveys","height=640,width=750,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0","Help");
  window.myRemote.focus();
}

function helpWindow(loc) {
  myRemote = launch(loc,"FreeSurveys3","height=640,width=700,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0","Help");
  window.myRemote.focus();
}


function addQuestion(form , base) {
   base += "?location=";
   base += form.location.options[form.location.selectedIndex].value;
   base += "&questionCategory=";
   base += form.questionCategory.options[form.questionCategory.selectedIndex].value;

   gotoLoc(base);
}


function refreshForm(form) {
    opener.document.forms[form].submit();
    window.close();
}

function refreshBuilder() {
    opener.document.DummyForm.submit();
    window.close();
}

function refreshLocation(loc) {
  if (opener.document) {
    opener.document.location=loc;
    window.close();
  } else {
    document.location=loc;
  }
}

function refreshLocationWithAlert(loc, msg) {
  opener.document.location=loc;
  alert(msg);
  window.close();
}


function openWindowConditionalAlert(loc, condition, falseMessage) {
  if(condition == "true" ) {
    openWindow(loc);        
  } else {
    alert(falseMessage);
  }
}

function openPreviewConditionalAlert(loc, condition, falseMessage) {
  if(condition == "true" ) {
    openPreview(loc);        
  } else {
    alert(falseMessage);
  }
}


function closeWindow() {
  window.close();
}

function refreshAdmin() {
  refreshLocation('showUpdate.do');
}

function refreshList() {
  refreshLocation('listSurveys.do');
}

function deleteWindow (loc) {
    if (confirm("Are You Sure ?")) {
        document.location = loc;
    }
}

function confirmDelete(loc, message) {
    if (confirm(message)) {
        document.location = loc;        
    }
}


function resetLoginForm() {
  document.LoginForm.username.value="";
  document.LoginForm.password.value="";
}

function DropDownMenu(entered) {
  with (entered) {
  ref=options[selectedIndex].value;
  document.location=ref;
  }
}


function DropDownMenuWithPrefix(prefix, entered) {
  ref=entered.options[entered.selectedIndex].value;
  prefix += ref;
  document.location=prefix;
}



function SetChecked(val) {
  dml=document.form[0];
  len = dml.elements.length;
  var i=0;
  for( i=0 ; i<len ; i++) {
    if (dml.elements[i].name=='questionID') {
      dml.elements[i].checked=val;
    }
  }
}


function lookupAndClose(elementName, value) {
    opener.document.forms[0].elements[elementName].value = value;
    window.close();
}

function transferValueFromPopUp(openerFormName, openerFormElementName, value, loc) {
        
        loc += "&overrideStylesheet=";
        loc += value;

        top.frames["main"].document.location = loc;
        top.opener.document.forms[openerFormName].elements[openerFormElementName].value = value;
}


function goBack() {
  window.history.go(-1);
}

function setCookie(name, value) {
  var expFromNow = 60*24*60*60*1000;  // Expires in 60 Days
  var exp = new Date(); 
  exp.setTime(exp.getTime() + expFromNow);
  document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}


function getCookie(Name) 
{
        var search = Name + "="   
        if (document.cookie.length > 0) 
        { 
                offset = document.cookie.indexOf(search);
                if (offset != -1) //DOES COOKIE EXIST
                { 
                        offset += search.length;
                        end = document.cookie.indexOf(";", offset);
                        if (end == -1);
                                end = document.cookie.length;
                        end = offset + 1; //SINCE THE VALUE IS 0, IT HAS A LENGTH OF 1
                        return unescape(document.cookie.substring(offset, end));
                }    
        }
}

function checkSearchQuery(field){
		
	txt = field.value;
	
	if(txt == null || txt.length < 1){
		alert("The search field cannot be empty. Please enter a query.");
		return false;
	}//if
	else{
		return true;		
	}//else

}

function checkForNotNull(field, message){
	txt = field.value;	
	if(txt == null || txt.length < 1){
		alert(message);
		return false;
	}
	else{
		return true;		
	}

}


function logSiteRef() {
  var siteRef;          
  var loc = document.location.href;
  index = loc.indexOf("siteRef=");
  if ( index >= 0) {
    siteRef=loc.substring(index+8);
    setCookie("siteRef", siteRef);        
  }
}

function closeFrame() {
  top.document.location=top.frames['main'].location;
}

function processAlerts(str){
	alert(str);
}


function modifyText(id, text) {
        if(document.getElementById && text != '') {
          obj = document.getElementById(id);
          obj.childNodes[0].data = text;
        }
}

function modifyTextNoCheck(id, text) {
        if(document.getElementById) {
          obj = document.getElementById(id);
          obj.childNodes[0].data = text;
        }
}

function modifyDivColor(id, selectBox) {
        if (document.getElementById) {
           obj = document.getElementById(id);
           if (selectBox.selectedIndex >= 0) {
             obj.style.color=selectBox.options[selectBox.selectedIndex].value;
           }
        }
}

function modifyDivBackgroundColor(id, selectBox) {
        if (document.getElementById) {
           obj = document.getElementById(id);
           if (selectBox.selectedIndex >= 0) {
             obj.style.backgroundColor=selectBox.options[selectBox.selectedIndex].value;
           }
        }
}

function modifyDivBorderColor(id, selectBox) {
        if (document.getElementById) {
           obj = document.getElementById(id);
           if (selectBox.selectedIndex >= 0) {
             obj.style.borderColor=selectBox.options[selectBox.selectedIndex].value;
           }
        }
}

function changeTextClass(id, stylesheetClass) {
        if(document.getElementById) {        
          obj = document.getElementById(id);
          obj.className=stylesheetClass;
        }        
}

function changeDualTextClass(id1, id2, stylesheetClass1, stylesheetClass2) {
        changeTextClass(id1, stylesheetClass1);
        changeTextClass(id2, stylesheetClass2);
}



function printLocation(loc) {
  myRemote = launch(loc,"FreeSurveys1","height=700,width=600,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0","Help");
  window.myRemote.focus();  
  if (window.print) {
    window.print();
  }

}

function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function getSel() {
	if (document.getSelection) txt = document.getSelection();
	else if (document.selection) txt = document.selection.createRange().text;
	else return;
        
        return txt;
}


function checkSelection(typeVal) {
  mySelection = getSel();
  if (mySelection) {
        document.forms[0].decorationType.value = typeVal;
        document.forms[0].decorationValue.value = mySelection;
        document.forms[0].submit();
  } else {
        alert ("Please Select Text and then Click on the Icon");
  }
}

function changeMode(form, value) {

  form.singleDisplayMode.value = value;
  form.changeQuestionType.value = "true";
  form.submit();
}



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.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];}
}

function MM_nbGroup(event, grpName) { //v3.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : args[i+1];
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    if ((nbArr = document[grpName]) != null)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = args[i+1];
      nbArr[nbArr.length] = img;
  }
 }
}


function applyText(val, control) {
  control.value = val;
}


function warnForInactivity(expiryTimeInMinutes) {
  alert ("Your Session Will Expire in " + expiryTimeInMinutes + " minutes due to Inactivity. Please Respond to the Survey!");
}

function openURLWithAlert(alertMessage, url) {
  alert(alertMessage);
  document.location = url;
}


function submitAction(form, name, value) {
  form.elements[name].value = value;
  form.submit();
}


function showCollapse(id) {
        if(document.getElementById) {
          obj = document.getElementById(id);

          if (obj.style.display == 'inline') {
            obj.style.display='none';
		  document.images[id+"_sign"].src='/images/plus.gif';
	       
          } else {
            obj.style.display='inline';        
		  document.images[id+"_sign"].src='/images/minus.gif';
		  
          }

        }
}

function showCollapseConditional(val, comparison, id) {
      if(document.getElementById) {
          obj = document.getElementById(id);
          if (val == comparison) {
            obj.style.display='inline';        
          } else {
            obj.style.display='none';
          }
      }
}

function showCollapseConditionalCheckbox(checkboxElement, id) {
      if(document.getElementById) {
          obj = document.getElementById(id);
          if (checkboxElement.checked) {
            obj.style.display='inline';        
          } else {
            obj.style.display='none';
          }
      }
}



function fillSelectList(selectInput, targetSelectBox, myArray) {
  var target = selectInput.form.elements[targetSelectBox];
        
  while(target.options.length) {
        target.options[0] = null;
  }

  var arr = myArray[selectInput.selectedIndex];

  for(i = 0; i < arr.length; ++i) {
        target.options[i] = arr[i];
  }

}

function showHelp(selectInput) {
        if (selectInput.selectedIndex >= 0) {
                var index = selectInput.selectedIndex;
                docLocation = "/help/" + selectInput.options[index].value + "-window.html";
                gotoLoc(docLocation);
        }
}


function enableNewSurveyBox(doc){

	if(doc.uploadSurveyForm.createNewSurvey.checked){
		doc.uploadSurveyForm.newSurveyName.disabled=false;
	}
	else{
		doc.uploadSurveyForm.newSurveyName.disabled=true;
	}

}

function enableTextbox(textBox, checkBoxState){

	
	if(checkBoxState==true){
		textBox.disabled=false;
	}
	else{
		textBox.disabled=true;
	}

}



function populateConversionSurveys(url){
	document.location = url
}

function openSmallWindow (loc) {
  myRemote = launch(loc,"FreeSurveys","height=100,width=250,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=0,status=0,toolbar=0","Help");
  window.myRemote.focus();
}


function navPopup(id, title, itemTitle, itemURL) {
   var mye,mye2,menu;
   var open=0;
   // Set the navURL;
   navURL = itemURL;

   var startx=0,starty=0;
   if (!id || id == "") {
        id = 0;
   }

   if ((mye = document.getElementById(id)) == null) return;


   for(var p = mye; p && p.tagName!='BODY'; p = p.offsetParent){
      startx += p.offsetLeft;
      starty += p.offsetTop;
   }

   if ((mye2 = document.getElementById("navcontrol_title")) == null) return;
   mye2.innerHTML = title;

   if ((menu = document.getElementById("navcontrol")) == null) return;
   menu.style.left = (startx-3)+"px";
   menu.style.top  = (starty-3)+"px";

   
   for (i = 0; i < itemTitle.length; ++i) {
        mye = document.getElementById("navcontrol_" + i);
        if (mye == null) {
          return;
        }
        mye.innerHTML = itemTitle[i];

        mye2 = document.getElementById("navcontrol_" + i + "_row");
        if (mye2 == null) {
          return;
        }
        mye2.style.display = "";
   }


   menu.style.display="";
   return;
}

function navClick(optionNumber) {
  if (navURL != null && optionNumber < navURL.length) {
    document.location = navURL[optionNumber];
  }
}


function navClose() {
  var mye2;
  if ((mye2 = document.getElementById("navcontrol")) == null) return;
  mye2.style.display="none";
}

function clearExistingData(mForm, hAct){

        //alert("hact: " +hAct);
        if(hAct ==2){
                delConfirm(mForm);
        }else{
                mForm.hAction.value = hAct;
                mForm.submit();
        }
}

function delConfirm(mForm){
        mForm.hAction.value = 2;
        retVal = confirm("Once data is deleted there is no way to recover it.\r\n"+
                        "Are you sure you want to delete the data?");
        if(retVal == true){
                mForm.submit();
        }

}


