//====================================================================
//    SHOW OR HIDE AN ELEMENT BY ID
//====================================================================
function showMe(id) {
	var e = document.getElementById(id);
	e.style.display = 'inline';
}
function hideMe(id) {
	var e = document.getElementById(id);
	e.style.display = 'none';
}







//====================================================================
//    CLEAR TEXT AND REPLACE DEFAULT VALUE, AS NEEDED
//====================================================================
function clearText(thefield) {
	if (thefield.defaultValue == thefield.value)
		 thefield.value = ""
	else if (thefield.value == "")
		 thefield.value = "";
	else thefield.value = thefield.value
}





//====================================================================
//    CLEAR A RANGE OF TEXTBOXES THAT ALL HAVE THE SAME NAME
//====================================================================
function checkAll(range,action) {
	var tags = document.getElementsByName(range);
	for (var i = 0; i < tags.length; i++) {
		if (action=="uncheck") {
			tags[i].checked = false;
		} else {
			tags[i].checked = true;
		}
	}
}






//====================================================================
//    AUTO TAB FUNCTION -- TAB WHEN FORM FIELD COMPLETE
//====================================================================
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Cyanide_7 |  */
var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}









//====================================================================
//    DISABLE RIGHT-CLICK
//====================================================================
<!-- Edit the message as you wish -->
var msg_box ="Disabled";
function dis_rightclickIE(){
	if (navigator.appName == 'Microsoft Internet Explorer' && (event.button == 2 || event.button == 3))
		alert(msg_box)
}
function dis_rightclickNS(e){
	if ((document.layers||document.getElementById&&!document.all) && (e.which==2||e.which==3))
	{
	alert(msg_box)
	return false;
	}
}


/* UNCOMMENT THIS SECTION TO TURN THIS FEATURE ON

if (document.layers){
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown=dis_rightclickNS;
}
else if (document.all&&!document.getElementById){
	document.onmousedown=dis_rightclickIE;
}
	document.oncontextmenu=new Function("alert(msg_box);return false")
*/










//====================================================================
//    LIMIT CHARACTERS 
//====================================================================
<!-- Limit the type of characters that can be used in form fields, when this function is called
// add onKeyUp="limitChars(this,<<name of type>>);" to fields
var decimal = "-0123456789.";
var phone = "()- 0123456789";
var numb = "0123456789";
var date = "/-0123456789";
var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
var alphanum = "0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
var web_safe = "0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var email_safe = "0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@.-";

function limitChars(t,v){
var w = "";
for (i=0; i < t.value.length; i++) {
x = t.value.charAt(i);
if (v.indexOf(x,0) != -1)
w += x;
}
t.value = w;
}
// End -->






//====================================================================
//    FIND CHARACTER POSITIONS
//====================================================================
function getSelectionStart(o) {
    if (o.createTextRange) {
        var r = document.selection.createRange().duplicate();
        r.moveEnd("character", o.value.length);
        if (r.text == "") {
            return o.value.length;
        }
        else {
            return o.value.lastIndexOf(r.text);
        }
    }
    else {
        return o.selectionStart;
    }
}
function getSelectionEnd(o) {
    if (o.createTextRange) {
        var r = document.selection.createRange().duplicate();
        r.moveStart("character", -o.value.length);
        return r.text.length;
    }
    else {
        return o.selectionEnd;
    }
}







//====================================================================
//    CONVERT FORM FIELDS INTO FLAT TEXT (DIV TAGS)
//    create a div tag beside each form field with name="flat_<<field_name>>"
// 	  the form fields will still exist -- they'll just be hidden, so
//    keep that in mind for form submission purposes
//====================================================================
function toggleFormFieldsFlat(formID) {
	var form = document.getElementById(formID);
	var divs = form.getElementsByTagName('div');
	var elem = form.elements;
	for (var i=0; i<elem.length; i++) {
		var elemObject = elem[i];
		var elemType = elem[i].type;
		var elemName = elem[i].name;
		var elemVal = elem[i].value;
		//handle checkboxes & radio buttons to only show an "X" for their value, if checked
		if (elemType=="checkbox" || elemType=="radio") {
			if (elem[i].checked) {
				elemVal = "X";
			} else {
				elemVal = "";
			}
		}
		//handle select boxes: show text instead of value
		if (elemType=="select-one") {
			elemVal = elem[i].options[elem[i].options.selectedIndex].text;
		}		
		//now update the div tags, where present
		var flatDiv = "flat_" + elemName;
		var flatDiv = getElementsByName_iefix('div',flatDiv);
		if (flatDiv.length>0) {
			//how many divs have this name?  if more than one, let's figure out which one corresponds with this 
			if (flatDiv.length>1) {
				//we'll loop through all the elements on the page with the corresponding name, and then we'll pair it with the correct div tag
				var allMatchingElems = document.getElementsByName(elemName);
				for (var x=0; x<allMatchingElems.length; x++) {
					if (allMatchingElems[x]===elemObject) {
						flatDiv = flatDiv[x];
					}
				}
			} else {
				flatDiv = flatDiv[0];
			}
			//add the element value to the flat div tag
			flatDiv.innerHTML = elemVal;
			//toggle the display of the fields

			if (elem[i].style.display=="none") {
				elem[i].style.display = "inline";
				flatDiv.style.display = "none";
			} else {
				elem[i].style.display = "none";
				flatDiv.style.display = "inline";
			}			
		}
	}
}









//====================================================================
//	TAKE THREE SEPARATE PHONE FIELDS AND 
//  IMPLODE THEM INTO ONE HIDDEN FIELD IN THIS FORMAT (xxx - xxx - xxxx) 
//====================================================================
function updatePhoneField(partsName,fieldID) {
	var parts = document.getElementsByName(partsName);
	var ph = parts[0].value + "-" + parts[1].value + "-" +  parts[2].value;
	ph = ph.replace(/--/, "-");
	if (ph=="-") { ph = "" }
	//update the hidden field
	document.getElementById(fieldID).value = ph;
}



//====================================================================
//	ROUND A NUMBER WITH DECIMALS
//====================================================================
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	//add trailing zeros
	result = result.toFixed(2);
	
	return result;
}