/////////////////////////////////////////////////
// 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 showHide(thisID) {
  var thisElement = document.getElementById(thisID);
  
  if( thisElement.style.display == "none")
    thisElement.style.display = "inline";
  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 = "inline";
}

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 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 = ""
}



/////////////////////////////////////////////////
//This function accepts "obj" as an object's ID (not name).  The "html" parameter accepts the text to insert.
//
// Example: <textarea id="txt"></textarea> 
//          <input type="button" onclick="javascript:insertText('txt','hey there');">
function insertText(obj,html){
if(document.selection){
document.getElementById(obj).focus();
sel=document.selection.createRange();
sel.text=html;
sel.select();
}
else if(document.getElementById(obj).selectionStart || document.getElementById(obj).selectionStart=='0'){
var startPos=document.getElementById(obj).selectionStart;
var endPos=document.getElementById(obj).selectionEnd;
document.getElementById(obj).value=document.getElementById(obj).value.substring(0,startPos) + html + document.getElementById(obj).value.substring(endPos,document.getElementById(obj).value.length);
document.getElementById(obj).focus();
document.getElementById(obj).selectionStart=endPos+html.length;
document.getElementById(obj).selectionEnd=endPos+html.length;
}
else{
document.getElementById(obj).value+=html;
document.getElementById(obj).focus();
}
}



/////////////////////////////////////////////////
// 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 PopPictureWindow(url,id,wh,ww) {
	var sheight=screen.height;
	var swidth=screen.width;
	var wheight=wh;
	var wwidth=ww;
	var wleft=((swidth-wwidth)/2);
	var wtop=((sheight-wheight)/2);
	var winprops="location=no,scrollbars=yes,menubars=no,toolbars=no,directories=no,status=no,resizable=no" +
	                       ",height=" + wheight + ",width=" + wwidth + ",left=" + wleft + ",top=" +wtop;
	window.open(url,id,winprops);
}
//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';
  }
   
}
