	function Show(id) {
	    obj = document.getElementsByTagName("div");
		obj[id].style.display = 'block';
	}
	
	function Hide(id) {
	    obj = document.getElementsByTagName("div");
		obj[id].style.display = 'none';
	}
	
	function popUp(URL, SCROLL, myWIDTH, myHEIGHT, isCenter) {
		if(window.screen)if(isCenter)if(isCenter=="true"){
		    var myLeft = (screen.width-myWIDTH)/2;
		    var myTop = (screen.height-myHEIGHT)/2;
		}
		window.open(URL, '', 'toolbar=0,scrollbars='+SCROLL+',location=0,statusbar=0,menubar=0,resizable=no,width='+myWIDTH+',height='+myHEIGHT+',left='+myLeft+',top='+myTop+', screenX=50,screenY=50,resizable=1');
	}
	
	function changeImages(img_name,img_src) {
		document[img_name].src=img_src;
	}
	

function replace(str, str1, str2) {
//replace str1 with str2 in original str
	var i;
	var ch;
	var ret = "";
	for (i = 0; i < str.length; i++) {
		ch = str.substring(i, i + 1);
		if (ch == str1) {
			ret = ret + str2;
		}
		else {
			ret = ret + ch;
		}
	}
	return ret;
} //replace()

function encode(str) {
//replace spaces with pluses
   return replace(str, ' ', '+');
} //encode()

function decode(str) {
//replace pluses with spaces
   return replace(str, '+', ' ');
} //decode()

function reverse(str) {
	var i;
	var ch;
	var ret = "";
	for (i = str.length - 1; i > -1; i--) {
		ch = str.substring(i, i + 1);
		ret = ret + ch;
	}
	return ret;
} //reverse()

function ltrim(str) {
	var nonspace = false;
	var i;
	var ch;
	var ret = "";
	for (i = 0; i < str.length; i++) {
		ch = str.substring(i, i + 1);
		if (ch != " ") {
			ret = ret + ch;
			nonspace = true;
		}
		else {
			if (nonspace) {
				ret = ret + ch;
			}
		}
	}
	return ret;
} //ltrim()

function rtrim(str) {
	return reverse(ltrim(reverse(str)));
} //rtrim()

function trim(str) {
	return ltrim(rtrim(str));
} //trim()

function isLetter(c) {
   var n = c.charCodeAt(0);
   if ((n >= 65 && n <= 90) || (n >= 97 && n <= 122)) {
      return true;
   }
   else {
      return false;
   }
} //isLetter()

function isDigit(c) {
   var n = c.charCodeAt(0);
   if (n >= 48 && n <= 57) {
      return true;
   }
   else {
      return false;
   }
} //isDigit()

function isInteger(str) {
	var ch;
	var ret = true;
	for (var i = 0; i < str.length; i++) {
		ch = str.substring(i, i + 1);
		if (!isDigit(ch)) {
			ret = false;
		}
	}
	return ret;
} //isInteger()

function isASCII(str) {
	var ch;
	var n;
	var ret = true;
	for (var i = 0; i < str.length; i++) {
		ch = str.substring(i, i + 1);
		n = ch.charCodeAt(0);
		if (n >= 32 && n <= 126) {
			ch = ch; //valid ranges - do nothing
		}
		else {
			ret = false;
		}
	}
	return ret;
} //isASCII()

function isDoubleQuote(str) {
	var ch;
	var ret = false;
	for (var i = 0; i < str.length; i++) {
		ch = str.substring(i, i + 1);
		if (ch != '"') {
			ch = ch; //valid range - do nothing
		}
		else {
			ret = true;
		}
	}
	return ret;
} //isDoubleQuote()

function isName(str) {
	var ch;
	var n;
	var ret = true;
	for (var i = 0; i < str.length; i++) {
		ch = str.substring(i, i + 1);
		n = ch.charCodeAt(0);
		//32=space, 45=hyphen, 38=ampersand, 39=apostroph
		if (isDigit(ch) || isLetter(ch) || n == 32 || n == 45 || n == 38 || n == 39) {
			ch = ch; //valid ranges - do nothing
		}
		else {
			ret = false;
		}
	}
	return ret;
} //isName()

function isKeyCode(str) {
	var ch;
	var n;
	var ret = true;
	for (var i = 0; i < str.length; i++) {
		ch = str.substring(i, i + 1);
		n = ch.charCodeAt(0);
		//45=hyphen
		if (isDigit(ch) || isLetter(ch) || n == 45) {
			ch = ch; //valid ranges - do nothing
		}
		else {
			ret = false;
		}
	}
	return ret;
} //isKeyCode()

function isCampaignName(str) {
	var ch;
	var n;
	var ret = true;
	for (var i = 0; i < str.length; i++) {
		ch = str.substring(i, i + 1);
		n = ch.charCodeAt(0);
		//32=space, 45=hyphen, 39=apostroph, 46=period
		if (isDigit(ch) || isLetter(ch) || n == 32 || n == 45 || n == 39 || n == 46) {
			ch = ch; //valid ranges - do nothing
		}
		else {
			ret = false;
		}
	}
	return ret;
} //isCampaignName()

function isDescription(str) {
	var ch;
	var n;
	var ret = true;
	for (var i = 0; i < str.length; i++) {
		ch = str.substring(i, i + 1);
		n = ch.charCodeAt(0);
		//38=ampersand, 34=doublequote, 94=tilda
		if (n == 38 || n == 34 || n == 94) {
			ret = false;
			break;
		}
	}
	return ret;
} //isDescription()

function isCompanyName(str) {
	var ch;
	var n;
	var ret = true;
	for (var i = 0; i < str.length; i++) {
		ch = str.substring(i, i + 1);
		n = ch.charCodeAt(0);
		//32=space, 45=hyphen, 38=ampersand, 39=apostroph, 58=colon
		if (isDigit(ch) || isLetter(ch) || n == 32 || n == 45 || n == 38 || n == 39 || n == 58) {
			ch = ch; //valid ranges - do nothing
		}
		else {
			ret = false;
		}
	}
	return ret;
} //isCompanyName()

function isUSACanadaZip(str) {
	var ch;
	var n;
	var ret = true;
	for (var i = 0; i < str.length; i++) {
		ch = str.substring(i, i + 1);
		n = ch.charCodeAt(0);
		//32=space
		if (isDigit(ch) || isLetter(ch) || n == 32) {
			ch = ch; //valid ranges - do nothing
		}
		else {
			ret = false;
		}
	}
	return ret;
} //isUSACanadaZip()

function isLogin(str) {
	var ch;
	var ret = true;
	for (var i = 0; i < str.length; i++) {
		ch = str.substring(i, i + 1);
		if (isDigit(ch) || isLetter(ch)) {
			ch = ch; //valid ranges - do nothing
		}
		else {
			ret = false;
		}
	}
	return ret;
} //isLogin()

function extraSpaces(str) {
	var currCh = "";
	var prevCh = "";
	var ret = false;
	for (var i = 0; i < str.length; i++) {
		currCh = str.substring(i, i + 1);
		if (currCh == " " && prevCh == " ") {
			ret = true;
		}
		prevCh = currCh;
	}
	return ret;
} //extraSpaces()

function isEmail(email) {
	var IndexOfAt = email.indexOf("@");
	var LastIndexOfAt = email.lastIndexOf("@");
	var badchar = false;

 		//email address has exactly one @ char
		if ((IndexOfAt == -1) || (IndexOfAt != LastIndexOfAt)) {
			return false;
		}

		//@ char in the email can not be the first or the last char
		if ((IndexOfAt == 0) || (LastIndexOfAt == email.length - 1)) {
			return false;
		}

		//must have at least one dot
		if (email.indexOf(".") == -1) {
			return false;
		}

		//dot can not be the last char
		if (email.lastIndexOf(".") == email.length - 1) {
			return false;
		}

		//illegal combinations: @. ..
		if (email.indexOf("@.") != -1 || email.indexOf("..") != -1) {
			return false;
		}

		//must be at least 5 chars
		if (email.length < 5) {
			return false;
		}

		// check for bad characters
		for (var i = 0; i < email.length; i++) {
			ch = email.substring(i, i + 1)
			if ((ch >= "A" && ch <= "Z") ||
				(ch >= "a" && ch <= "z") ||
				(ch == "@") || (ch == ".") || (ch == "'") ||
				(ch == "_") || (ch == "-") ||
				(ch >= "0" && ch <= "9"))
			{
				ch = ch; //valid ranges - do nothing
			}
			else {
				badchar = true;
			}

		}
		if (badchar) {
			return false;
		}

		//at this point we've passed all email validations successfully
		return true;
} //isEmail()

function isDate(str) {
//This function checks whether input argument
//represents a valid date.
//Returns empty string if ok, error msg otherwise.

    var i;                    //iterative variable used in for loops
    var c = '';               //current char used in a loop
    var aMonth;               //moth value
    var aDay;                 //day value
    var aYear;                //year value (two digits)
    var firstSlash;           //position of the first slash
    var secondSlash;          //position of the second slash
    var numberOfSlashes;      //total number of slashed in mydate
    var aDate;                //local copy of input arg

    //initialize
    aDate = trim(str);
    numberOfSlashes = 0;

    //date is optional
    if (aDate == '') {return true;}

    //check that there are two slashes
    for (var i = 0; i < aDate.length; i++) {
       c = str.substring(i, i + 1);
       if (c == '/') {
          numberOfSlashes++;
       }
    }
    if (numberOfSlashes != 2) {return false;}

    //get positions of both slashes
    firstSlash = aDate.indexOf('/');
    secondSlash = aDate.indexOf('/', firstSlash + 1);

    //make sure that slashes are positioned at valid positions
    if (firstSlash != 1 && firstSlash != 2) {return false;}
    if (secondSlash != 3 && secondSlash != 4 && secondSlash != 5) {return false;}
    if ((secondSlash - firstSlash != 2) && (secondSlash - firstSlash != 3)) {return false;}

    //obtain month, day and year values
    aMonth = aDate.substring(0, firstSlash);
    aDay = aDate.substring(firstSlash + 1, secondSlash);
    aYear = aDate.substring(secondSlash + 1, aDate.length);

    //make sure month, day, year are integers
    if (!isInteger(aMonth) || !isInteger(aDay) || !isInteger(aYear)) {return false;}

    iMonth = new Number(aMonth);
    iYear = new Number(aYear);
    iDay = new Number(aDay);

    //check if year is valid
    if ((iYear < 1800) || (iYear > 2100)) {return false;}

    //check if month is valid
    if ((iMonth < 1) || (iMonth > 12)) {return false;}

    //check if day is valid
    if (iMonth==1 || iMonth==3 || iMonth==5 || iMonth==7 || iMonth==8 || iMonth==10 || iMonth==12) {
       if ((iDay < 1) || (iDay > 31)) {return false;}
    }
    if (iMonth==4 || iMonth==6 || iMonth==9 || iMonth==11) {
       if ((iDay < 1) || (iDay > 30)) {return false;}
    }
    if (iMonth==2) {
       if ((iYear % 4) == 0) {                              //leap year
          if ((iDay < 1) || (iDay > 29)) {return false;}
       }
       else {                                               //regular year
          if ((iDay < 1) || (iDay > 28)) {return false;}
       }
    }

    return true;       //success

} //isDate()

function isTime(str) {
//This function checks whether input argument
//represents a valid time in format hh:mm AM/PM or hh:mmAM/PM
//Returns empty string if ok, error msg otherwise.

    var i;                    //iterative variable used in for loops
    var c = '';               //current char used in a loop
    var aHour;                //hour value
    var aMinute;              //minute value
    var aAMPM;                //AM/PM value
    var pColon;               //position of the colon
    var aTime;                //local copy of input arg

    //initialize
    aTime = trim(str);

    //time is optional
    if (aTime == '') {return true;}

    //make sure that time lenght is valid
    if (aTime.length < 6) {return false;}

    //check that there is the colon and get positions of colon
    pColon = aTime.indexOf(':');
    if (pColon == -1) {return false;}

    //make sure that colon is positioned at valid position
    if (pColon != 1 && pColon != 2) {return false;}

    //obtain hour, minute and AM/PM values
    aHour = aTime.substring(0, pColon);
    aMinute = aTime.substring(pColon + 1, pColon + 3);
    aAMPM = aTime.substring(pColon + 3, aTime.length);

    //make sure hour, minute is integer
    if (!isInteger(aHour) || !isInteger(aMinute)) {return false;}

    iHour = new Number(aHour);
    iMinute = new Number(aMinute);
    aAMPM = trim(aAMPM);
    aAMPM = aAMPM.toUpperCase();

    //check if hour is valid
    if ((iHour < 1) || (iHour > 12)) {return false;}

    //check if minute is valid
    if ((iMinute < 0) || (iMinute > 59)) {return false;}

    //check if AM/PM is valid
    if ((aAMPM != 'AM') && (aAMPM != 'PM')) {return false;}

    return true;       //success

} //isTime()

function formatDate(aDate) {
//accepts valid calendar date in the format m/d/yyyy
//returns date in the standard format mm/dd/yyyy

   var firstSlash;
   var secondSlash;
   var aMonth = '';
   var aDay = '';
   var aYear = '';

   //get positions of both slashes
   firstSlash = aDate.indexOf('/');
   secondSlash = aDate.indexOf('/', firstSlash + 1);

   //obtain month, day and year values
   aMonth = aDate.substring(0, firstSlash);
   aDay = aDate.substring(firstSlash + 1, secondSlash);
   aYear = aDate.substring(secondSlash + 1, aDate.length);

   if (aMonth.length < 2) {aMonth = '0'+ aMonth;}
   if (aDay.length < 2) {aDay = '0'+ aDay;}

   return (aMonth +'/'+ aDay +'/'+ aYear);

}

function controlExists(frm, ctlName) {

   var i;
   var found;

   found = false;
   for(i=0; i<frm.length; i++) {
      e = frm.elements[i];
      if (e.name == ctlName) {
         found = true;
         break;
      }
   }
   return found;

} //controlExists()

function listSelected(lst, val) {

   var i;
   var counter = 0;

   //count how many selections are made in the list
   for (i=0; i<lst.options.length; i++) {
      if (lst.options[i].selected == true && lst.options[i].value != val) {
         counter++;
      }
   }

   //return
   if (counter > 0) {
      return true;
   }
   else {
      return false;
   }

} //listSelected()

function listSelectAll(lst) {
//select all items in the list
   var i;
   for (i=0; i<lst.options.length; i++) {
      lst.options[i].selected = true;
   }
} //listSelectAll()

function checkRemove(frm, c) {

   if (c.checked == true) {
      for (i=0; i<frm.elements.length; i++) {
         if ((frm.elements[i].type == 'checkbox') &&
             (frm.elements[i].name.substring(0,9) == c.name.substring(0,9)) &&
             (frm.elements[i].name != c.name)) {
                frm.elements[i].checked = false;
         }
      }
   }

} //checkRemove()

function shuffleSimple(lst1, lst2, all, adminLocked) {
//removes selected entries from list1 into list2
//all=flag if all to be moved (as opposed to just the selected ones)
//adminLocked=flag if true, then do not shuffle admin user
   var i;
   var txt;
   var ival;
   var adminInd = -1;
   var adminEntry = false;

   //clear selections in target list
   for (i=0; i<lst2.options.length; i++) {
     lst2.options[i].selected = false;
   }

   //selected items in the source:
   //add it to target and select them
   for (i=0; i<lst1.options.length; i++) {
      if (all || lst1.options[i].selected) {
         txt = lst1.options[i].text;
         ival = lst1.options[i].value;
         //which item holds 'user, power'
         if (txt.substring(0) == 'user, power' || txt.substring(0,2) == 'g:' || ival == -1) {
            adminInd = i;
            adminEntry = true;
         }
         else {
            adminEntry = false;
         }
         //non-admin items are moved no matter what
         if (!adminEntry) {
            opt = new Option();
            opt.value = lst1.options[i].value;
            opt.text = txt;
            lst2.add(opt);
            lst2.options[lst2.options.length-1].selected = true;
         }
         //admin items are moved only if it is not locked
         else {
            if (!adminLocked) {
               opt = new Option();
               opt.value = lst1.options[i].value;
               opt.text = txt;
               lst2.add(opt);
               lst2.options[lst2.options.length-1].selected = true;
            }
         }
      }
   }


   //remove selected items from the source
   for (i=0; i<lst1.options.length; i++) {
      if (all || lst1.options[i].selected) {
           txt = lst1.options[i].text;
           ival = lst1.options[i].value;
           //which item holds 'admin'
           if (txt.substring(0) == 'user, power' || txt.substring(0,2) == 'g:' || ival == -1) {
              adminEntry = true;
           }
           else {
              adminEntry = false;
           }
           if (!adminEntry) {
              lst1.remove(i);
              i--;
           }
           else {
              if (!adminLocked) {
                 lst1.remove(i);
                 i--;
              }
           }

      }
   }

} //shuffleSimple()

function todaysDate() {
//returns todays date in m/d/yyyy format
   var d = new Date();
   return (d.getMonth()+1)+'/'+d.getDate()+'/'+d.getYear();
} //todaysDate()

function surveyControl(controlName,controlPrefix,controlRequired,controlSelected) {
   this.controlName = controlName;
   this.controlPrefix = controlPrefix;
   this.controlRequired = controlRequired;
   this.controlSelected = controlSelected;
} //constructor for surveyControl

function surveyOK(frm) {
//control name for dynamically generated controls has the following format:
//  1. underscore - marker of dynamically generated control
//  2. prefix - txt, lst, chk, rad
//  3. required or optional - either 1 or zero
//  4. control's id - integer value
//  5. undercore followed by integer - continuation of control's id in case of check box or radio button
//
   var controlName = '';
   var controlPrefix = '';
   var controlRemainder = '';
   var controlRequired = 0;
   var controlSelected = 0;
   var surveyControls = new Array();
   var x = -1;  //array index
   var found = false;
   var ret = true;

   for (i = 0; i < frm.elements.length; i++) {
      if (frm.elements[i].name.substring(0,1) == '_') {

         //for dynamically generated controls:
         //get their common name part and store in array with associated number of user selections/checks
         controlPrefix = frm.elements[i].name.substring(1,4);
         controlRequired = frm.elements[i].name.substring(4,5);
         controlRemainder = frm.elements[i].name.substring(5,frm.elements[i].name.length);
         pos = controlRemainder.indexOf('_');
         if (pos > -1) {
            controlRemainder = controlRemainder.substring(0,pos);
         }
         controlName = controlRequired + controlRemainder;

         //see if this control is checked/selected
         controlSelected = 0;
         if (controlPrefix == 'txt') {
            if (trim(frm.elements[i].value) != '') {
               controlSelected = 1;
            }
         }
         if (controlPrefix == 'chk') {
            if (frm.elements[i].checked) {
               controlSelected = 1;
            }
         }
         if (controlPrefix == 'rad') {
            if (frm.elements[i].checked) {
               controlSelected = 1;
            }
         }
         if (controlPrefix == 'lst') {
            if (listSelected(frm.elements[i], -1)) {
               controlSelected = 1;
            }
         }

         //see if this control is in the array already
         found = false;
         for (j = 0; j <= x; j++) {
            if (controlName == surveyControls[j].controlName) {
               found = true;
               foundIndex = j;
            }
         }

         //if controlName is not in array yet - add it
         //otherwise, just update its controlSelected property
         if (!found) {
            x++;
            surveyControls[x] = new surveyControl(controlName,controlPrefix,controlRequired,controlSelected);
         }
         else {
            surveyControls[foundIndex].controlSelected = surveyControls[foundIndex].controlSelected + controlSelected;
         }
      }
   }

   //at this point array contains all dynamically generated controls
   //each control is marked as either required or optional
   //each control has the number of times it was checked/selected by user

   //validate if all required controls has at least one selection made
   for (i = 0; i <= x; i++) {
      if (surveyControls[i].controlRequired == 1 && surveyControls[i].controlSelected == 0) {
         ret = false;
      }
   }

   return ret;

} //surveyOK

   function fillList(lstTarget, srvURL, srvFileName, srvArgString, selectOptionID) {

      var responseText = '';
      var url = '';
      var i;
      var arr;
      var a;
      var token;


      //obtain text returned by the service
      var oxh = new ActiveXObject("Microsoft.XMLHTTP");
      url = srvURL + srvFileName;
      if (trim(srvArgString) != '') {
         url = url +'?'+ srvArgString;
      }
      oxh.open("POST", url, false);
      oxh.send();
      responseText = oxh.responseText;

      //clear target list
      for (i=0; i<lstTarget.options.length; i++) {
         lstTarget.remove(i);
         i--;
      }

      //populate target list with response text
      arr = responseText.split("<br>");
      for (i=0; i<arr.length; i++) {
         token = trim(arr[i]);   //token has format id:value
         if (token != '') {
            a = token.split(":");
            opt = new Option();
            opt.value = a[0];
            opt.text = a[1];
            lstTarget.add(opt);
         }
      }

      //select specified entry
      if (lstTarget.options.length > 0) {
         if (selectOptionID > -1 && selectOptionID < lstTarget.options.length) {
            lstTarget.options[selectOptionID].selected = true;
         }
      }

   } //fillList()

   function fillListDynamically(lstSource, lstTarget, srvURL, srvFileName, srvArgName, selectValue, selectOptionID) {

      var sourceListIDs = '';
      var responseText = '';
      var url = '';
      var i;
      var arr;
      var a;
      var token;

      //accumulate selected entries in the source list box
      for (i=0; i<lstSource.options.length; i++) {
         if (lstSource.options[i].selected) {
            sourceListIDs = sourceListIDs + lstSource.options[i].value + ',';
         }
      }
      //remove last comma
      if (sourceListIDs.length > 0) {
         sourceListIDs = sourceListIDs.substring(0,sourceListIDs.length-1);
      }

      //obtain text returned by the service
      var oxh = new ActiveXObject("Microsoft.XMLHTTP");
      url = srvURL + srvFileName +'?'+ srvArgName +'='+ sourceListIDs;
      oxh.open("POST", url, false);
      oxh.send();
      responseText = oxh.responseText;

      //clear target list
      for (i=0; i<lstTarget.options.length; i++) {
         lstTarget.remove(i);
         i--;
      }

      //populate target list with response text
      arr = responseText.split("<br>");
      for (i=0; i<arr.length; i++) {
         token = trim(arr[i]);   //token has format id:value
         if (token != '') {
            a = token.split(":");
            opt = new Option();
            opt.value = a[0];
            opt.text = a[1];
            lstTarget.add(opt);
         }
      }

      //select specified entry: by VALUE
      for (i=0; i<lstTarget.options.length; i++) {
         if (lstTarget.options[i].value == selectValue) {
            lstTarget.options[i].selected = true;
         }
      }

      //select specified entry: by OPTION ID
      if (lstTarget.options.length > 0) {
         if (selectOptionID > -1 && selectOptionID < lstTarget.options.length) {
            lstTarget.options[selectOptionID].selected = true;
         }
      }

   } //fillListDynamically()
   
   function fillListDynamically2(lstSource1, lstSource2, lstTarget, srvURL, srvFileName, srvArgName1, srvArgName2) {

      var sourceListIDs1 = '';
      var sourceListIDs2 = '';
      var responseText = '';
      var url = '';
      var i;
      var arr;
      var a;
      var token;

      //accumulate selected entries in the source1 list box
      for (i=0; i<lstSource1.options.length; i++) {
         if (lstSource1.options[i].selected) {
            sourceListIDs1 = sourceListIDs1 + lstSource1.options[i].value + ',';
         }
      }
      //remove last comma
      if (sourceListIDs1.length > 0) {
         sourceListIDs1 = sourceListIDs1.substring(0,sourceListIDs1.length-1);
      }

      //accumulate selected entries in the source2 list box
      for (i=0; i<lstSource2.options.length; i++) {
         if (lstSource2.options[i].selected) {
            sourceListIDs2 = sourceListIDs2 + lstSource2.options[i].value + ',';
         }
      }
      //remove last comma
      if (sourceListIDs2.length > 0) {
         sourceListIDs2 = sourceListIDs2.substring(0,sourceListIDs2.length-1);
      }

      //obtain text returned by the service
      var oxh = new ActiveXObject("Microsoft.XMLHTTP");
      url = srvURL + srvFileName +'?'+ srvArgName1 +'='+ sourceListIDs1 +'&'+ srvArgName2 +'='+ sourceListIDs2;
      oxh.open("POST", url, false);
      oxh.send();
      responseText = oxh.responseText;

      //clear target list
      for (i=0; i<lstTarget.options.length; i++) {
         lstTarget.remove(i);
         i--;
      }

      //populate target list with response text
      arr = responseText.split("<br>");
      for (i=0; i<arr.length; i++) {
         token = trim(arr[i]);   //token has format id:value
         if (token != '') {
            a = token.split(":");
            opt = new Option();
            opt.value = a[0];
            opt.text = a[1];
            lstTarget.add(opt);
         }
      }

      //select the first entry in the target list
      if (lstTarget.options.length > 0) {
         lstTarget.options[0].selected = true;
      }

   } //fillListDynamically2()

    function isDate(str) {
    //This function checks whether input argument
    //represents a valid date.
    //Returns true of false
       var i;                    //iterative variable used in for loops
       var c = '';               //current char used in a loop
       var aMonth;               //moth value
       var aDay;                 //day value
       var aYear;                //year value (two digits)
       var firstSlash;           //position of the first slash
       var secondSlash;          //position of the second slash
       var numberOfSlashes;      //total number of slashed in mydate
       var aDate;                //local copy of input arg

          //initialize
          aDate = trim(str);
          numberOfSlashes = 0;

          //date is optional
          if (aDate == '') {return true;}

          //check that there are two slashes
          for (var i = 0; i < aDate.length; i++) {
             c = str.substring(i, i + 1);
             if (c == '/') {
                numberOfSlashes++;
             }
          }
          if (numberOfSlashes != 2) {return false;}

          //get positions of both slashes
          firstSlash = aDate.indexOf('/');
          secondSlash = aDate.indexOf('/', firstSlash + 1);

          //make sure that slashes are positioned at valid positions
          if (firstSlash != 1 && firstSlash != 2) {return false;}
          if (secondSlash != 3 && secondSlash != 4 && secondSlash != 5) {return false;}
          if ((secondSlash - firstSlash != 2) && (secondSlash - firstSlash != 3)) {return false;}

          //obtain month, day and year values
          aMonth = aDate.substring(0, firstSlash);
          aDay = aDate.substring(firstSlash + 1, secondSlash);
          aYear = aDate.substring(secondSlash + 1, aDate.length);

          //make sure month, day, year are integers
          if (!isInteger(aMonth) || !isInteger(aDay) || !isInteger(aYear)) {return false;}

          iMonth = new Number(aMonth);
          iYear = new Number(aYear);
          iDay = new Number(aDay);

          //check if year is valid
          if ((iYear < 1800) || (iYear > 2100)) {return false;}

          //check if month is valid
          if ((iMonth < 1) || (iMonth > 12)) {return false;}

          //check if day is valid
          if (iMonth==1 || iMonth==3 || iMonth==5 || iMonth==7 || iMonth==8 || iMonth==10 || iMonth==12) {
             if ((iDay < 1) || (iDay > 31)) {return false;}
          }
          if (iMonth==4 || iMonth==6 || iMonth==9 || iMonth==11) {
             if ((iDay < 1) || (iDay > 30)) {return false;}
          }
          if (iMonth==2) {
             if ((iYear % 4) == 0) {                              //leap year
                if ((iDay < 1) || (iDay > 29)) {return false;}
             }
          else {                                               //regular year
             if ((iDay < 1) || (iDay > 28)) {return false;}
             }
          }
       return true;       //success
    } //isDate()

    function yearInt(str) {
    //This function returns year in integer datatype
    //from a valid date.
       var i;                    //iterative variable used in for loops
       var c = '';               //current char used in a loop
       var aMonth;               //moth value
       var aDay;                 //day value
       var aYear;                //year value (two digits)
       var firstSlash;           //position of the first slash
       var secondSlash;          //position of the second slash
       var numberOfSlashes;      //total number of slashed in mydate
       var aDate;                //local copy of input arg

       //initialize
       aDate = trim(str);
       numberOfSlashes = 0;

       //check that there are two slashes
       for (var i = 0; i < aDate.length; i++) {
          c = str.substring(i, i + 1);
          if (c == '/') {
             numberOfSlashes++;
          }
       }
       //get positions of both slashes
       firstSlash = aDate.indexOf('/');
       secondSlash = aDate.indexOf('/', firstSlash + 1);

       //obtain year values
       aYear = aDate.substring(secondSlash + 1, aDate.length);
       iYear = new Number(aYear);
       return iYear;
    }//yearInt

    function listSelected(lst, val) {
       var i;
       var counter = 0;
       //count how many selections are made in the list
       for (i=0; i<lst.options.length; i++) {
          if (lst.options[i].selected == true && lst.options[i].value != val) {
             counter++;
          }
       }
       //return
       if (counter > 0) {
          return true;
       }
       else {
          return false;
       }
    } //listSelected()

    function isEmail(email) {
       var IndexOfAt = email.indexOf("@");
       var LastIndexOfAt = email.lastIndexOf("@");
       var badchar = false;
       var str = email.substring(IndexOfAt + 1, email.length);

       //email address has exactly one @ char
       if ((IndexOfAt == -1) || (IndexOfAt != LastIndexOfAt)) {
          return false;
       }

       //@ char in the email can not be the first or the last char
       if ((IndexOfAt == 0) || (LastIndexOfAt == email.length - 1)) {
          return false;
       }

       //must have at least one dot
       if (email.indexOf(".") == -1) {
          return false;
       }

       //dot can not be the last char
       if (email.lastIndexOf(".") == email.length - 1) {
          return false;
       }

       //illegal combinations: @. ..
       if (email.indexOf("@.") != -1 || email.indexOf("..") != -1) {
          return false;
       }

       //can not have apostriphy in domain name
       if (str.indexOf("'") != -1) {
          return false;
       }

       //must be at least 5 chars
       if (email.length < 5) {
          return false;
       }

       // check for bad characters
       for (var i = 0; i < email.length; i++) {
          ch = email.substring(i, i + 1)
          if ((ch >= "A" && ch <= "Z") ||
              (ch >= "a" && ch <= "z") ||
              (ch == "@") || (ch == ".") || (ch == "'") ||
              (ch == "_") || (ch == "-") ||
              (ch >= "0" && ch <= "9"))
          {
                 ch = ch; //valid ranges - do nothing
          }
          else {
             badchar = true;
          }
       }
       if (badchar) {
          return false;
       }

       //at this point we've passed all email validations successfully
       return true;
    } //isEmail()

    function isDoubleQuote(str) {
       var ch;
       var ret = false;
       for (var i = 0; i < str.length; i++) {
         ch = str.substring(i, i + 1);
         if (ch != '"') {
            ch = ch; //valid range - do nothing
         }
         else {
            ret = true;
         }
       }
       return ret;
    } //isDoubleQuote()

   function extraSpaces(str) {
      var currCh = "";
      var prevCh = "";
      var ret = false;
      for (var i = 0; i < str.length; i++) {
         currCh = str.substring(i, i + 1);
         if (currCh == " " && prevCh == " ") {
            ret = true;
         }
         prevCh = currCh;
      }
         return ret;
    } //extraSpaces()

    function reverse(str) {
       var i;
       var ch;
       var ret = "";

       for (i = str.length - 1; i > -1; i--) {
          ch = str.substring(i, i + 1);
          ret = ret + ch;
       }
       return ret;
    } //reverse()

    function ltrim(str) {
       var nonspace = false;
       var i;
       var ch;
       var ret = "";

       for (i = 0; i < str.length; i++) {
          ch = str.substring(i, i + 1);
             if (ch != " ") {
                ret = ret + ch;
                nonspace = true;
             }
            else {
               if (nonspace) {
                  ret = ret + ch;
               }
            }
       }
       return ret;
    } //ltrim()


    function rtrim(str) {
       return reverse(ltrim(reverse(str)));
    } //rtrim()


    function trim(str) {
       return ltrim(rtrim(str));
    } //trim()

    function isDigit(c) {
       var n = c.charCodeAt(0);
       if (n >= 48 && n <= 57) {
          return true;
       }
       else {
          return false;
       }
    } //isDigit()

	function isInteger(str) {
	   var ch;
	   var ret = true;
	   for (var i = 0; i < str.length; i++) {
          ch = str.substring(i, i + 1);
          if (!isDigit(ch)) {
             ret = false;
          }
       }
	   return ret;
    } //isInteger()

    function isLetter(c) {
       var n = c.charCodeAt(0);
       if ((n >= 65 && n <= 90) || (n >= 97 && n <= 122)) {
          return true;
       }
       else {
          return false;
       }
    } //isLetter()

	function NumberOfTimes(str, chr){
        //returns how many times char occurs within str
        var i;
        var c;
        var counter = 0;
        for (var i = 0; i < str.length; i++) {
            c = str.substring(i, i + 1);
            if (c == chr){
                counter = counter + 1;
            }
        }
        return counter;
    }//NumberOfTimes

    function IsPhone(str) {
    //returns error message when phone is invalid
    //returns empty string when success
    //only USA is implemented so far
      var ch;
      var n;
      var ret = true;
      var msg = '';
      var pos;
      var LDcounter = 0;
      var phone = trim(str);
      //may contain digits, letters, spaces, dots, dashes, parenthesis
      for (var i = 0; i < phone.length; i++) {
         ch = phone.substring(i, i + 1);
         n = ch.charCodeAt(0);
         //32=space, 39=apostrophe, 44=comma, 45=hyphen, 46=period
         if (isDigit(ch) || isLetter(ch) || n == 32 || n == 40  || n == 41 || n == 45 || n == 46) {
            ch = ch; //valid ranges - do nothing
         }
         else {
            msg = 'Phone may contain only digits, letters, spaces, dots, dashes and parenthesis. Suggested format is: (123) 456-7890.';
            return msg;
         }
      }

      //number of parenthesis is no more than one each
      if (NumberOfTimes(phone, ')') > 1 || NumberOfTimes(phone, '(') > 1) {
         msg = 'Phone format is invalid. Please check number of parenthesis. Suggested format is: (123) 456-7890.';
         return msg;
      }
      //number of dashes is no more than 2
      if (NumberOfTimes(phone, '-') > 2) {
         msg = 'Phone format is invalid. Please check number of dashes. Suggested format is: (123) 456-7890.';
         return msg;
      }
      //number of dots is no more than 2
      if (NumberOfTimes(phone, '.') > 2) {
         msg = 'Phone format is invalid. Please check number of dots. Suggested format is: 123.456.7890.';
         return msg;
      }
      //number of spaces is no more than 2
      if (NumberOfTimes(phone, ' ') > 2) {
         msg = 'Phone format is invalid. Please check number of spaces. Suggested format is: 123 456 7890.';
         return msg;
      }
      //if it has parenthesis, there must be two at specific positions
      pos = phone.indexOf('(');
      if (pos > -1) {
         if (pos != 0 || phone.indexOf(')') != 4) {
            msg = 'Phone format is invalid. Please check position of parenthesis. Suggested format is: (123) 456-7890.';
            return msg;
         }
      }
      pos = phone.indexOf(')');
      if (pos > -1) {
         if (pos != 4 || phone.indexOf('(') != 0) {
            msg = 'Phone format is invalid. Please check position of parenthesis. Suggested format is: (123) 456-7890.';
            return msg;
         }
      }
      //if it has dots, there must be two at specific positions
      pos = phone.indexOf('.');
      if (pos > -1) {
         if (pos != 3 || phone.indexOf('.', pos + 1) != 7) {
            msg = 'Phone format is invalid. Please check position of dots. Suggested format is: 123.456.7890.';
            return msg;
         }
      }
      //if it has dashes
      pos = phone.indexOf('-');
      if (pos > -1) {
         //parenthesis also exist
         if (phone.indexOf('(') > -1) {
            //space also exist: format must be (123) 456-8965
            if (phone.indexOf(' ') > -1) {
               if (phone.indexOf(' ') != 5 || pos != 9) {
                  msg = 'Phone format is invalid. Please check position of dashes. Suggested format is: (123) 456-7890.';
                  return msg;
               }
               else  {   //no space
                  //more than one dash: must be (123)-343-3434
                  if (phone.indexOf('-', pos + 1) > -1) {
                     if (pos != 5 || phone.indexOf('-', pos + 1) != 9) {
                        msg = 'Phone format is invalid. Please check position of dashes. Suggested format is: (123)-456-7890.';
                        return msg;
                     }
                     else {   //only one dash: must be (123)456-8965
                        if (pos != 8) {
                           msg = 'Phone format is invalid. Please check position of dashes. Suggested format is: (123)456-7890.';
                           return msg;
                         }
                     }
                  }
               }
            }
         }
         else { //no parenthesis
            //space also exists: format is 123 343-3434
            if (phone.indexOf(' ') > -1) {
               if (phone.indexOf(' ') != 3 || pos != 7) {
                  msg = 'Phone format is invalid. Please check position of dashes. Suggested format is: 123&nbsp;456-7890.';
                  return msg;
               }
               else {    //no space: format is 123-345-4545
                  if (pos != 3 || phone.indexOf('-', pos + 1) != 7) {
                     msg = 'Phone format is invalid. Please check position of dashes. Suggested format is: 123-456-7890.';
                     return msg;
                   }
               }
            }
         }
      }
      //total number of digits and/or letters must be 10
      for (var i = 0; i < phone.length; i++) {
         ch = phone.substring(i, i + 1);
         n = ch.charCodeAt(0);
         if ((n >= 48 && n <= 57) || (n >= 65 && n <= 90) || (n >= 97 && n <= 122)) {
            LDcounter = LDcounter + 1;
         }
      }
      if (LDcounter != 10) {
         msg = 'Phone format is invalid. Suggested format is: (123) 456-7890.';
         return msg;
      }
      return msg     //success by now
   }//IsPhone


   function isSnuggleName(str) {
      var ch;
      var n;
      var ret = true;

      for (var i = 0; i < str.length; i++) {
         ch = str.substring(i, i + 1);
         n = ch.charCodeAt(0);
         //32=space, 39=apostrophe, 44=comma, 45=hyphen, 46=period
         if (isLetter(ch) || n == 32 || n == 39 || n == 44 || n == 45 || n == 46) {
            ch = ch; //valid ranges - do nothing
         }
         else {
            ret = false;
         }
      }
      return ret;

   } //isSnuggleName()


   function isSnuggleAddress(str) {
      var ch;
      var n;
      var ret = true;

      for (var i = 0; i < str.length; i++) {
         ch = str.substring(i, i + 1);
         n = ch.charCodeAt(0);
         //32=space, 39=apostrophe, 44=comma, 45=hyphen, 46=period
         if (isDigit(ch) || isLetter(ch) || n == 32 || n == 39 || n == 44 || n == 45 || n == 46) {
            ch = ch; //valid ranges - do nothing
         }
         else {
            ret = false;
         }
      }
      return ret;

   } //isSnuggleAddress()


   function validate(frm) {

      //email from
      if (trim(frm.crm_email_address.value) == '') {
         alert("Please enter your e-mail.");
         frm.crm_email_address.focus();
         return false;
      }

      if (!isEmail(frm.crm_email_address.value)) {
         alert("Your e-mail is invalid. Please re-enter.");
         frm.crm_email_address.focus();
         return false;
      }
      //first name from
      if (trim(frm.crm_first_name.value) == '') {
         alert("Please enter your first name.");
         frm.crm_first_name.focus();
         return false;
      }
      if (isDoubleQuote(frm.crm_first_name.value)) {
         alert("Your first name can not contain double quotes. Please re-enter.");
         frm.crm_first_name.focus();
         return false;
      }      if (!isSnuggleName(frm.crm_first_name.value)) {
         alert('First name can contain only letters, spaces, dashes, apostrophes, commas and periods. Please re-enter.');
         frm.crm_first_name.focus();
         return false;
      }
      if (extraSpaces(trim(frm.crm_first_name.value))) {
         alert('Parts of first name should be separated by single space. Multiple spaces are not allowed. Please re-enter.');
         frm.crm_first_name.focus();
         return false;
      }

      //last name from
      if (trim(frm.crm_last_name.value) == '') {
         alert("Please enter your last name.");
         frm.crm_last_name.focus();
         return false;
      }
      if (isDoubleQuote(frm.crm_last_name.value)) {
         alert("Your last name can not contain double quotes. Please re-enter.");
         frm.crm_last_name.focus();
         return false;
      }      if (!isSnuggleName(frm.crm_last_name.value)) {
         alert('Last name can contain only letters, spaces, dashes, apostrophes, commas and periods. Please re-enter.');
         frm.crm_last_name.focus();
         return false;
      }
      if (extraSpaces(trim(frm.crm_last_name.value))) {
         alert('Parts of last name should be separated by single space. Multiple spaces are not allowed. Please re-enter.');
         frm.crm_last_name.focus();
         return false;
      }

      //address1
      if (trim(frm.crm_address1.value) == '') {
         alert("Please enter your address.");
         frm.crm_address1.focus();
         return false;
      }
      if (isDoubleQuote(frm.crm_address1.value)) {
         alert("Your address can not contain double quotes. Please re-enter.");
         frm.crm_address1.focus();
         return false;
      }      if (!isSnuggleAddress(frm.crm_address1.value)) {
         alert('Address can contain only digits, letters, spaces, dashes, apostrophes, commas and periods. Please re-enter.');
         frm.crm_address1.focus();
         return false;
      }
	  if (extraSpaces(trim(frm.crm_address1.value))) {
	     alert('Parts of address should be separated by single space. Multiple spaces are not allowed. Please re-enter.');
	     frm.crm_address1.focus();
	     return false;
      }

      //Address 2
      if (trim(frm.crm_address2.value) != '') {
        if (isDoubleQuote(frm.crm_address2.value)) {
         alert("Apt/suite address can not contain double quotes. Please re-enter.");
         frm.crm_address1.focus();
         return false;
        }
		if (!isSnuggleAddress(frm.crm_address2.value)) {
		   alert('Apt/suite address can contain only digits, letters, spaces, dashes, apostrophes, commas and periods. Please re-enter.');
		   frm.crm_address2.focus();
		   return false;
		}
		if (extraSpaces(trim(frm.crm_address2.value))) {
		   alert('Parts of apt/suite address should be separated by single space. Multiple spaces are not allowed. Please re-enter.');
		   frm.crm_address1.focus();
		   return false;
		}
      }

      //crm_city
      if (trim(frm.crm_city.value) == '') {
         alert("Please enter your city.");
         frm.crm_city.focus();
         return false;
      }
      if (isDoubleQuote(frm.crm_city.value)) {
         alert("Your city can not contain double quotes. Please re-enter.");
         frm.crm_city.focus();
         return false;
      }
      if (!isSnuggleName(frm.crm_city.value)) {
         alert('City can contain only letters, spaces, dashes, apostrophes, commas and periods. Please re-enter.');
         frm.crm_city.focus();
         return false;
      }
	  if (extraSpaces(trim(frm.crm_city.value))) {
	     alert('Parts of city should be separated by single space. Multiple spaces are not allowed. Please re-enter.');
	     frm.crm_city.focus();
	     return false;
	  }
      //crm_state
      if (!listSelected(frm.crm_state, 0)) {
         alert('Please select state.');
         frm.crm_state.focus();
         return false;
      }
      //crm_postal_code
      if (trim(frm.crm_postal_code.value) == '') {
         alert('Please enter zip code.');
         frm.crm_postal_code.focus();
         return false;
      }
      if (frm.crm_postal_code.value.length != 5) {
         alert("Format of zip code is invalid. Valid format is '12345'. Please re-enter.");
         frm.crm_postal_code.focus();
         return false;
      }
      if (!isInteger(trim(frm.crm_postal_code.value))) {
         alert("Format of zip code is invalid. Valid format is '12345'. Please re-enter.");
         frm.crm_postal_code.focus();
         return false;
      }
    //crm_daytime_phone
     if (trim(frm.crm_daytime_phone.value) != '') {
         if (IsPhone(frm.crm_daytime_phone.value) != '') {
            alert(IsPhone(frm.crm_daytime_phone.value));
            frm.crm_daytime_phone.focus();
            return false;
         }
     }
   //crm_evening_phone
   if (trim(frm.crm_evening_phone.value) != '') {
         if (IsPhone(frm.crm_evening_phone.value) != '') {
            alert(IsPhone(frm.crm_evening_phone.value));
            frm.crm_evening_phone.focus();
            return false;
         }
     }

    //crm_inquiry
    if (trim(frm.crm_inquiry.value) == '') {
         alert("Please enter your message.");
         frm.crm_inquiry.focus();
         return false;
   }

      //at this point all is valid
      return true;
   }

   function doClear(frm) {
      for (i = 0; i < frm.elements.length; i++) {
         if (frm.elements[i].type == 'text') {
            frm.elements[i].value = '';
         }
         else if (frm.elements[i].type == 'textarea') {
            frm.elements[i].value = '';
         }
      }
   }

   function doSubmit(frm) {
      if (validate(frm)) {
         frm.submit();
         return true;
      }
   }

	
