// toggle check boxes
function toggleCheck(theField){
for (i = 0; i < theField.length; i++)
	if (theField[i].checked){
		theField[i].checked = false;
	}
	else {
		theField[i].checked = true;
	}
}

// is numeric
function isNumber(theField){
	var valid = "0123456789.,";
	var objForm = document.theForm;
	var formElementLength = objForm.length;
	for (xx=0;xx < formElementLength; xx++) {
		if (objForm[xx].name == eval('theField')) {
			if (eval('theField') != "") {
				for (yy=0;yy < objForm[xx].value.length; yy++){
					temp = "" + objForm[xx].value.substring(yy, yy+1);
					if (valid.indexOf(temp) == "-1") {
						alert("You must enter a number in this field");	
						setTimeout("document.theForm[xx].focus();",1);
						setTimeout("document.theForm[xx].select();",1);						
						return false;
						break;
					}
				}		
			}
		}
	}	
}

// option transfer cities/styles
function SelectMoveRows(SS1,SS2,valField,transField,howMany)
{
	var SelID='';
    var SelText='';
	var showMessage = false;
    // Move rows from SS1 to SS2 from bottom to top
    for (i=SS1.options.length - 1; i>=0; i--)
    {
        if (SS1.options[i].selected == true)
        {
            SelID=SS1.options[i].value;
            SelText=SS1.options[i].text;
			if(valField.options.length <= howMany -1){
				var newRow = new Option(SelText,SelID);
				SS2.options[SS2.length]=newRow;
				SS1.options[i]=null;
			}
			else{
				showMessage = true;
			}
        }
    }
	if (showMessage){
		alert("Please select only " + howMany + " items");
	}
    SelectSort(SS2);
	// build the list of items selected
	var valList = "";
	for (var z=0; z < valField.length; z++){
		valList += valField.options[z].value + ",";
	}
	transField.value = valList.replace(/,$/,"");
}
function SelectSort(SelList)
{
    var ID='';
    var Text='';
    for (x=0; x < SelList.length - 1; x++)
    {
        for (y=x + 1; y < SelList.length; y++)
        {
            if (SelList[x].text > SelList[y].text)
            {
                // Swap rows
                ID=SelList[x].value;
                Text=SelList[x].text;
                SelList[x].value=SelList[y].value;
                SelList[x].text=SelList[y].text;
                SelList[y].value=ID;
                SelList[y].text=Text;
				
            }
        }

    }
	//for (z=0; z < SelList.length; z++)
    //{
	//	SelList[z].selected = true;
   // }
}
// how many have been transferred
function itemCount(theCount,field1,field2,valField1,valField2){
	var howMany = field1.options.length + field2.options.length;
	//alert(howMany);
	if (howMany > theCount){
		alert("Please select only 5 cities");
		// remove options until only 5 left
		while (howMany > theCount){
			if (field2.options.length > 0){
				whichOne = field2.options.length - 1;
				field2.options[whichOne] = null;
				
				// rebuild the hidden filed list
				var valList = "";
				for (var z=0; z < field2.length; z++){
					valList += field2.options[z].value + ",";
				}
				valField2.value = valList.replace(/,$/,"");
				
			}
			else{
				whichOne = field1.options.length - 1;
				field1.options[whichOne] = null;
				// rebuild the hidden filed list
				var valList = "";
				for (var z=0; z < field1.length; z++){
					valList += field1.options[z].value + ",";
				}
				valField1.value = valList.replace(/,$/,"");
			}
			howMany = field1.options.length + field2.options.length;
		}
	}
}

// text area size
function getLength(theField,maxChars,resultField) {
	var objForm = document.theForm;
	var formElementLength = objForm.length;
	for (xx=0;xx < formElementLength; xx++) {
		if (objForm[xx].name == eval('theField')) {
			var fieldString = objForm[xx].value;
			if(fieldString != ""){
				fieldDescLength = fieldString.length + 1;
				if (fieldDescLength > maxChars) {
					alert("You are limited to " + maxChars + " characters in the " + theField + " field");	
					setTimeout("document.theForm[xx].focus();",1);
					return false;
				}
				//alert(fieldDescLength);
				document.getElementById(resultField).innerHTML = "Current characters: " + fieldDescLength;
			}
	
		}
	}		
}

// remove punctuation
function removePunct(theField){
	alert(theField);
	var objForm = document.theForm;
	var formElementLength = objForm.length;
	re = /[^0-9]/gi;
	for (xx=0;xx < formElementLength; xx++) {
		if (objForm[xx].name == eval('theField')) {
  			var fieldValue = objForm[xx].value;
			var fieldLength = fieldValue.length;
  			if (fieldLength > 0){
				newValue = fieldValue.replace(re, '') ;
			   	objForm[xx].value = newValue;
			}
		}
	}
}

function removePunct2(theField){
	var newValue = "";
	re2 = /[^0-9]/gi;
	var fieldLength = theField.length;
	if (fieldLength > 0){
		newValue = theField.replace(re2, '') ;
	}
	return newValue;
}

// format numbers with commas

// number formatting function
// copyright Stephen Chapman 24th March 2006, 22nd August 2008
// permission to use this function is granted provided
// that this copyright notice is retained intact http://javascript.about.com/library/blnumfmt.htm

function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {var x = Math.round(num * Math.pow(10,dec));if (x >= 0) n1=n2='';var y = (''+Math.abs(x)).split('');var z = y.length - dec; if (z<0) z--; for(var i = z; i < 0; i++) y.unshift('0'); if (z<0) z = 1; y.splice(z, 0, pnt); if(y[0] == pnt) y.unshift('0'); while (z > 3) {z-=3; y.splice(z,0,thou);}var r = curr1+n1+y.join('')+n2+curr2;return r;}




function addCommas(theField)
{
	var objForm = document.theForm;
	var formElementLength = objForm.length;
	var newValue = "";
	re = /[^0-9]/gi;
	for (xx=0;xx < formElementLength; xx++) {
		if (objForm[xx].name == eval('theField')) {
			var fieldValue = objForm[xx].value;
			var fieldLength = fieldValue.length;
  			if (fieldLength > 0){
				newValue = fieldValue.replace(re, '') ;
			}
			if (newValue.length > 2){
				newStr = formatNumber(newValue,0,',','','','','-','');
				objForm[xx].value = newStr;
			}
		}
	}
}


// compare values of two fields  second must be greater than first
function compareFields(theField1,theField2){
	var objForm = document.theForm;
	var formElementLength = objForm.length;
	re = /_/gi;
	// get value of the first field
	for (xx=0;xx < formElementLength; xx++) {
		if (objForm[xx].name == eval('theField1')) {
			var fieldValue1 = removePunct2(objForm[xx].value);
  			fieldValue1 = parseInt(fieldValue1);
			var fieldLength1 = fieldValue1.length;
			var fieldName1 = objForm[xx].name.replace(re, ' ');
			var xx1 = xx;
			break;
		}
	}	
	// get value of the second field
	for (yy=0;yy < formElementLength; yy++) {
		if (objForm[yy].name == eval('theField2')) {
			fieldValue2 = removePunct2(objForm[yy].value);
  			fieldValue2 = parseInt(fieldValue2);
			var fieldLength2 = fieldValue2.length;
			var fieldName2 = objForm[yy].name.replace(re, ' ');
			var yy1 = yy;
			break;
		}
	}
	// now compare them
	if (fieldLength2 != '' && fieldValue2 < fieldValue1){
		
		alert(fieldName2 + " must be larger than " + fieldName1);
		
	}
}

// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. 
// If you wish to share this code with others, please just point them
// to the URL instead.
// Please DO NOT link directly to my .js files from your site. Copy
// the files to your server and use them there. Thank you.
// ===================================================================

// HISTORY
// ------------------------------------------------------------------
// April 20, 2005: Fixed the removeSelectedOptions() function to 
//                 correctly handle single selects
// June 12, 2003: Modified up and down functions to support more than
//                one selected option
/*
DESCRIPTION: These are general functions to deal with and manipulate
select boxes. Also see the OptionTransfer library to more easily 
handle transferring options between two lists

COMPATABILITY: These are fairly basic functions - they should work on
all browsers that support Javascript.
*/


// -------------------------------------------------------------------
// hasOptions(obj)
//  Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}

// -------------------------------------------------------------------
// selectUnselectMatchingOptions(select_object,regex,select/unselect,true/false)
//  This is a general function used by the select functions below, to
//  avoid code duplication
// -------------------------------------------------------------------
function selectUnselectMatchingOptions(obj,regex,which,only) {
	if (window.RegExp) {
		if (which == "select") {
			var selected1=true;
			var selected2=false;
			}
		else if (which == "unselect") {
			var selected1=false;
			var selected2=true;
			}
		else {
			return;
			}
		var re = new RegExp(regex);
		if (!hasOptions(obj)) { return; }
		for (var i=0; i<obj.options.length; i++) {
			if (re.test(obj.options[i].text)) {
				obj.options[i].selected = selected1;
				}
			else {
				if (only == true) {
					obj.options[i].selected = selected2;
					}
				}
			}
		}
	}
		
// -------------------------------------------------------------------
// selectMatchingOptions(select_object,regex)
//  This function selects all options that match the regular expression
//  passed in. Currently-selected options will not be changed.
// -------------------------------------------------------------------
function selectMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"select",false);
	}
// -------------------------------------------------------------------
// selectOnlyMatchingOptions(select_object,regex)
//  This function selects all options that match the regular expression
//  passed in. Selected options that don't match will be un-selected.
// -------------------------------------------------------------------
function selectOnlyMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"select",true);
	}
// -------------------------------------------------------------------
// unSelectMatchingOptions(select_object,regex)
//  This function Unselects all options that match the regular expression
//  passed in. 
// -------------------------------------------------------------------
function unSelectMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"unselect",false);
	}
	
// -------------------------------------------------------------------
// sortSelect(select_object)
//   Pass this function a SELECT object and the options will be sorted
//   by their text (display) values
// -------------------------------------------------------------------
function sortSelect(obj) {
	var o = new Array();
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}
	if (o.length==0) { return; }
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
	}

// -------------------------------------------------------------------
// selectAllOptions(select_object)
//  This function takes a select box and selects all options (in a 
//  multiple select object). This is used when passing values between
//  two select boxes. Select all options in the right box before 
//  submitting the form so the values will be sent to the server.
// -------------------------------------------------------------------
function selectAllOptions(obj) {
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		obj.options[i].selected = true;
		}
	}
	
// -------------------------------------------------------------------
// moveSelectedOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  This function moves options between select boxes. Works best with
//  multi-select boxes to create the common Windows control effect.
//  Passes all selected values from the first object to the second
//  object and re-sorts each box.
//  If a third argument of 'false' is passed, then the lists are not
//  sorted after the move.
//  If a fourth string argument is passed, this will function as a
//  Regular Expression to match against the TEXT or the options. If 
//  the text of an option matches the pattern, it will NOT be moved.
//  It will be treated as an unmoveable option.
//  You can also put this into the <SELECT> object as follows:
//    onDblClick="moveSelectedOptions(this,this.form.target)
//  This way, when the user double-clicks on a value in one box, it
//  will be transferred to the other (in browsers that support the 
//  onDblClick() event handler).
// -------------------------------------------------------------------
function moveSelectedOptions(from,to) {
	// Unselect matching options, if required
	if (arguments.length>3) {
		var regex = arguments[3];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
			}
		}
	// Move them over
	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
			to.options[index] = new Option( o.text, o.value, false, false);
			}
		}
	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(from);
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	}

// -------------------------------------------------------------------
// copySelectedOptions(select_object,select_object[,autosort(true/false)])
//  This function copies options between select boxes instead of 
//  moving items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
function copySelectedOptions(from,to,transField) {
	var options = new Object();
	if (hasOptions(to)) {
		for (var i=0; i<to.options.length; i++) {
			options[to.options[i].value] = to.options[i].text;
			}
		}
	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text) {
				if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
				to.options[index] = new Option( o.text, o.value, false, false);
				}
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	// build the list of items selected
	var valList = "";
	for (var z=0; z < to.length; z++){
		valList += to.options[z].value + ",";
	}
	transField.value = valList.replace(/,$/,"");
	}

// -------------------------------------------------------------------
// moveAllOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  Move all options from one select box to another.
// -------------------------------------------------------------------
function moveAllOptions(from,to) {
	selectAllOptions(from);
	if (arguments.length==2) {
		moveSelectedOptions(from,to);
		}
	else if (arguments.length==3) {
		moveSelectedOptions(from,to,arguments[2]);
		}
	else if (arguments.length==4) {
		moveSelectedOptions(from,to,arguments[2],arguments[3]);
		}
	}

// -------------------------------------------------------------------
// copyAllOptions(select_object,select_object[,autosort(true/false)])
//  Copy all options from one select box to another, instead of
//  removing items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
function copyAllOptions(from,to) {
	selectAllOptions(from);
	if (arguments.length==2) {
		copySelectedOptions(from,to);
		}
	else if (arguments.length==3) {
		copySelectedOptions(from,to,arguments[2]);
		}
	}

// -------------------------------------------------------------------
// swapOptions(select_object,option1,option2)
//  Swap positions of two options in a select list
// -------------------------------------------------------------------
function swapOptions(obj,i,j) {
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
	}
	
// -------------------------------------------------------------------
// moveOptionUp(select_object)
//  Move selected option in a select list up one
// -------------------------------------------------------------------
function moveOptionUp(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			if (i != 0 && !obj.options[i-1].selected) {
				swapOptions(obj,i,i-1);
				obj.options[i-1].selected = true;
				}
			}
		}
	}

// -------------------------------------------------------------------
// moveOptionDown(select_object)
//  Move selected option in a select list down one
// -------------------------------------------------------------------
function moveOptionDown(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=obj.options.length-1; i>=0; i--) {
		if (obj.options[i].selected) {
			if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
				swapOptions(obj,i,i+1);
				obj.options[i+1].selected = true;
				}
			}
		}
	}

// -------------------------------------------------------------------
// removeSelectedOptions(select_object)
//  Remove all selected options from a list
//  (Thanks to Gene Ninestein)
// -------------------------------------------------------------------
function removeSelectedOptions(from,transField) { 
	if (!hasOptions(from)) { return; }
	if (from.type=="select-one") {
		from.options[from.selectedIndex] = null;
		}
	else {
		for (var i=(from.options.length-1); i>=0; i--) { 
			var o=from.options[i]; 
			if (o.selected) { 
				from.options[i] = null; 
				} 
			}
		}
	from.selectedIndex = -1; 
	transField.value = "";
	// build the list of items selected
	var valList = "";
	for (var z=0; z < from.length; z++){
		valList += from.options[z].value + ",";
	}
	transField.value = valList.replace(/,$/,"");
	} 


// -------------------------------------------------------------------
// removeAllOptions(select_object)
//  Remove all options from a list
// -------------------------------------------------------------------
function removeAllOptions(from) { 
	if (!hasOptions(from)) { return; }
	for (var i=(from.options.length-1); i>=0; i--) { 
		from.options[i] = null; 
		} 
	from.selectedIndex = -1; 
	} 

// -------------------------------------------------------------------
// addOption(select_object,display_text,value,selected)
//  Add an option to a list
// -------------------------------------------------------------------
function addOption(obj,text,value,selected) {
	if (obj!=null && obj.options!=null) {
		obj.options[obj.options.length] = new Option(text, value, false, selected);
		}
	}
	
// text area size
function textareaSize(theSize,whereIs){
	
	var goOn = true;
	var moveIt = true;
	var taSize = document.theForm.Description.value.length;
	if(taSize > theSize){
		alert("Your Description is too long.  You are limited to " + theSize + " characters.");
		goOn = false;
	}
	
	//if (goOn && whereIs == 0){
	//	moveIt = checkFields();
	//	alert(moveIt);
	//}
	if (goOn){
		return true;
	}
	else{
		if(document.getElementById('uploadMessage')){
			setVisibility('uploadMessage','hidden');
		}
		return false;
	}
}