﻿// Function explanations
//
// function setLookupFromFieldName(fieldName, value)
//    Sets the fieldname to be Value
//
// function setSelectedOption(select, value)
//    Select the option within the select object that matches value

// function getTagFromIdentifierAndTitle(tagName, identifier, title)
//   Return the tag value if the identifier and title match usen in setLookupFromFieldName

// function login(showhide)
//   Accepts show or hide.  Changes the visibility of the div tag "popupbox"

// function toggleDisplay(divid)
//   Accepts the div ID.  Toggles the display of the div block from none to "" and back

// function getUrlVars()
//  Returns a hash of the variables in the url string
//  Ex.  http://xbrl.us/default.aspx?this=one  => hash[0] = "this"

// Begin "Set Field Default"
// The section below allows you to set the default value for a field, either input or select
// use the following syntax: setLookupFromFieldName("PaperSubject", "Test");


function setLookupFromFieldName(fieldName, value) {
  if (value == undefined) return;

  var theSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);
// if theSelect is null, it means that the target list has more than
// 20 items, and the Lookup is being rendered with an input element

  if (theSelect == null) {
    var theInput = getTagFromIdentifierAndTitle("select","",fieldName);
    //ShowDropdown(theInput.id); //this function is provided by SharePoint
    var opt=document.getElementById(theInput.id);

    setSelectedOption(opt, value);
    OptLoseFocus(opt); //this function is provided by SharePoint
  } else {
    setSelectedOption(theSelect, value);

  }
}

function setSelectedOption(select, value) {
  var opts = select.options;

  var l = opts.length;
  if (select == null) return;

  for (var i=0; i < l; i++) {
    if (opts[i].value == value) {
      select.selectedIndex = i;
      return true;
    }
  }

  return false;

}

function getTagFromIdentifierAndTitle(tagName, identifier, title) {
  var len = identifier.length;
  var tags = document.getElementsByTagName(tagName);

  for (var i=0; i < tags.length; i++) {
    var tempString = tags[i].id;

    if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
      return tags[i];
    }
  }
  return null;
}

// End "Set Field Default"

// popup function for login 
    function login(showhide){
    //alert('a');
    if(showhide == "show"){
        //alert('b');
        document.getElementById('popupbox').style.visibility="visible"; /* If the function is called with the variable 'show', show the login box */
        //alert('b1');
    }else if(showhide == "hide"){
        //alert('c');
        document.getElementById('popupbox').style.visibility="hidden"; /* If the function is called with the variable 'hide', hide the login box */
        //alert('c1');
    }
    //alert('d');
  }

// Toggles div tag to visible/not visible and vice versa
/*function toggleDisplay(divid) {
	thiselement = document.getElementById(divid);
	if (thiselement.style.display == "") {
	  thiselement.style.display = "none";
	} else {
	  thiselement.style.display = "";
	}
}*/

// Toggles div tag to visible/not visible and vice versa
// divid can be a single div tag id or an array
// i.e. toggleDisplay('idname1') or toggleDisplay(['idname1',idname2'])
// action is optional.  
// If action doesn't exist then the function toggles the divid the single or each one in the array
// If action is "expand" then it makes each tag in divid visible.  If it's anything else (not blank)
//   then it collapses every tag in divid.
function toggleDisplay(divid,action) {
  try {
    switch (typeof(divid)) {
  	  case "string":
  				 thiselement = document.getElementById(divid);
  				 if (thiselement.style.display == "") {
  	  		 		thiselement.style.display = "none";
  				 } else {
  	  		 	  thiselement.style.display = "";
  				 }
  				 break;
  		case "object":
  				 if (action==null || action=="") {
    				 for (var item in divid) {
  				 	 	 thiselement = document.getElementById(divid[item]);
      				 if (thiselement.style.display == "") {
      	  		 		thiselement.style.display = "none";
      				 } else {
      	  		 	  thiselement.style.display = "";
      				 }
    				 }
  				 } else {
    				 for (var item in divid) {
  				 	 	 thiselement = document.getElementById(divid[item]);
    				   if (action=="expand") {
      	  		 		thiselement.style.display = "";
    					 } else {
      	  		 		thiselement.style.display = "none";
    					 }
    				 }
  				 }
  				 break;
  		default:
  				 alert("default");
  				 break;
  		}
		} catch (Exception) {}
}


function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
 
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
 
    return vars;
}

