/////////////////////////////////////////////////
// Function to show or hide a block of HTML
// Example: <a href="javascript:void(0)" onclick="showHide('myElement');">Toggle me</a>
//   ...
//   <tbody id="myElement" style="display: none;">     would initially hide this element
//   <tr>
//     <td>
//       Content to hide
//     </td>
//   </tr>
//   </tbody>
//

function isDefined(type) {
  return (type != 'undefined' && type != 'unknown');
}

function childNodes(element) {
  if (isDefined(typeof element.childNodes)) {
    return element.childNodes;
  } else if (isDefined(typeof element.children)) {
    return element.children;
  }
}

function getElementById(element_name) {
  if (isDefined(typeof document.getElementById)) {
    return document.getElementById(element_name);
  } else if(typeof isDefined(document.all)) {
    return document.all[element_name];
  }
}

function setTextContent(elem, content) {
  if (isDefined(typeof elem.innerText)) {
    elem.innerText = content; 
  } else if (isDefined(typeof elem.textContent)) {
    elem.textContent = content;
  }
}

function clearStatus() {
  var elm = getElementById('status');
  elm.innerHTML = '';
}

function showHide(thisID) {
  var thisElement = document.getElementById(thisID);
  
  if( thisElement.style.display == "none")
    thisElement.style.display = "";
  else
    thisElement.style.display = "none"
}

function hideElement(thisID) {
  var thisElement = document.getElementById(thisID);

  thisElement.style.display = "none"
}

function showElement(thisID) {
  var thisElement = document.getElementById(thisID);
  
  thisElement.style.display = "";
}

function disableElement(thisID) {
  var thisElement = document.getElementById(thisID);
  
  thisElement.disabled = true;
}

function valueElement(thisID, newvalue) {
  var thisElement = document.getElementById(thisID);
  
  thisElement.value = newvalue;
}


/////////////////////////////////////////////////
// Function to returns values of selected elements in a select multiple
// Example: getMutipleSelectedValues('ownerid');
//
function getMutipleSelectedValues(element_name){
  var el = document.getElementById(element_name)
  var thisList = '';
  for (var i=0; i < el.options.length; i++)
    if ( el.options[i].selected) {
      if ( thisList.length > 0 )
        thisList += ',';
      thisList +=  el.options[i].value;
    }
  return thisList;
}

/////////////////////////////////////////////////
// Function to Add rows to a textArea
// Example: <a href="##" onClick="addTextAreaRow('textarea1',2);">Add rows</a>
//
function addTextAreaRow(thisID,rowsToAdd) {
  var thisElement = document.getElementById(thisID);
  thisElement.rows = thisElement.rows + rowsToAdd;
}


/////////////////////////////////////////////////
// Function to clear a form element when user clicks on it if it matches the default text, erasing the default text
// Example: <input type="text" value="Required" onFocus="clearDefault(this,'Required');">
//
function clearDefault(el,defaultText) {
	if (el.defaultValue==el.value || el.value==defaultText) el.value = ""
}

/////////////////////////////////////////////////
// http://www.qodo.co.uk/blog/javascript-select-all-options-for-a-select-box/
// EXAMPLE: <input type="button" name="Button" value="All" onclick="selectAll('selectbox1',true)" />
function selectAll(selectBox,selectAll) {
	// have we been passed an ID
	if (typeof selectBox == "string") {
		selectBox = document.getElementById(selectBox);
	}
	// is the select box a multiple select box?
	if (selectBox.type == "select-multiple") {
		for (var i = 0; i < selectBox.options.length; i++) {
			selectBox.options[i].selected = selectAll;
		}
	}
}

/////////////////////////////////////////////////
// Function to add a new option in an HTML <select> form element
// Example: <a href="javascript:void(0)" onclick="addNewOption('howContacted_0_11','Add new Location',0)">add</a>
//  <cfset thisJSID = replace(createUUID(),'-','','all')>  then id="#thisJSID#" 
//
function addNewOption(thisID, promptText,position){

  var thisElement = document.getElementById(thisID);
  var tempvar = prompt(promptText,"");

  if (tempvar != null && tempvar != 'undefined') {
   myList = document.getElementById('prefix');
   thisElement.options.add(new Option(tempvar, tempvar), position);
   thisElement.selectedIndex = position;
   thisElement.value = tempvar;
  }  
}

var submitcount=0;

function checkFields() {            
   if (submitcount == 0)
      {
      submitcount++;
      return true;
      }
   else 
      {
      alert("Please wait... This form has already been submitted and is being processed. Thanks You.");
      return false;
      }
}

function popUp(URL,w,h) {
    winl = (screen.width - w) / 2;
    wint = (screen.height - h) / 2;

    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+w+",height="+h+",top="+wint+",left="+winl+"');");
}
    
//http://www.mediacollege.com/internet/javascript/form/limit-characters.html
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function limitTextNotEnfoced(limitField, limitCount, limitNum) {
  limitCount.value = limitNum - limitField.value.length;
  if (limitCount.value < 0) {
    limitCount.style.backgroundColor = 'red';
    limitCount.style.color = 'white';
    limitCount.style.fontWeight = 'bold';
  }
  else if (limitCount.value < 7) {
    limitCount.style.backgroundColor = 'yellow';
    limitCount.style.color = 'black';
    limitCount.style.fontWeight = 'normal';
  }
  else {
    limitCount.style.backgroundColor = 'white';
    limitCount.style.color = 'black';
    limitCount.style.fontWeight = 'normal';
  }
   
}
