<!-- Form Manager Script (Written By Robert Norton - Robert@SophMedia.com) -->
function addTextOption( formId, optionName )
{
 if( formId > 0 )
 {
  addOptionsList = this.document.getElementById( optionName + "List" );
  addOptionsListAddTextOption =  this.document.getElementById( optionName + "AddTextOption" );
  addOptionsListEditItemKey =  this.document.getElementById( optionName + "EditItemKey" );

  if( addOptionsListAddTextOption.value != "" )
  {
   if( addOptionsList.options[0].value == "" )
   {
    addOptionsList.options[0] = null;
   }
   
   if( addOptionsListEditItemKey.value != '' )
   {
    addOptionsList.options[addOptionsListEditItemKey.value].value = addOptionsListAddTextOption.value;
    addOptionsList.options[addOptionsListEditItemKey.value].text = addOptionsListAddTextOption.value;
    addOptionsListAddTextOption.value = "";
    addOptionsListEditItemKey.value = "";
   }else
   {
    addOptionsList.options[addOptionsList.options.length] = new Option( addOptionsListAddTextOption.value, addOptionsListAddTextOption.value );
    addOptionsListAddTextOption.value = "";    
   }

   selectAllOptions( optionName );
  }else{alert( "Specified value is invalid");}
 }else{alert( "formId is invalid!" );}
}

function moveTextOption( formId, optionName, moveDirection )
{
 if( formId > 0 )
 {
  if( moveDirection != '' )
  {
   addOptionsList = this.document.getElementById( optionName + "List" );
   addOptionsListAddTextOption =  this.document.getElementById( optionName + "AddTextOption" );
   addOptionsListEditItemKey =  this.document.getElementById( optionName + "EditItemKey" );
   currentOption = addOptionsList.options[addOptionsList.options.selectedIndex];
   newOptionListArray = new Array( addOptionsList.options.length );
   newOptionIndex = 0;

   if( addOptionsList.options.selectedIndex > -1 && addOptionsList.options[addOptionsList.options.selectedIndex].value != "" )
   {
    if( moveDirection == 'down' )
    {
     currentOption = null;
     
     if( addOptionsList.options.selectedIndex < ( addOptionsList.options.length - 1 ) )
     {
      for( var i = 0; i < ( addOptionsList.options.selectedIndex ); i++ )
      {
       newOptionListArray[i] = new Object();
       newOptionListArray[i].value = addOptionsList.options[i].value;
       newOptionListArray[i].text = addOptionsList.options[i].text;
      }
     
      newOptionListArray[addOptionsList.options.selectedIndex] = new Object();
      newOptionListArray[addOptionsList.options.selectedIndex].value = addOptionsList.options[addOptionsList.options.selectedIndex + 1].value;
      newOptionListArray[addOptionsList.options.selectedIndex].text = addOptionsList.options[addOptionsList.options.selectedIndex + 1].text;
     
      newOptionListArray[addOptionsList.options.selectedIndex + 1] = new Object();
      newOptionListArray[addOptionsList.options.selectedIndex + 1].value = addOptionsList.options[addOptionsList.options.selectedIndex].value;
      newOptionListArray[addOptionsList.options.selectedIndex + 1].text = addOptionsList.options[addOptionsList.options.selectedIndex].text;  
      newOptionIndex = addOptionsList.options.selectedIndex + 1;

      for( var i = addOptionsList.options.selectedIndex + 2; i < addOptionsList.options.length; i++ )
      {
       newOptionListArray[i] = new Object();
       newOptionListArray[i].value = addOptionsList.options[i].value;
       newOptionListArray[i].text = addOptionsList.options[i].text;
      }
     
      addOptionsListEditItemKey.value = "";
     }else
     {            
      for( var i = 0; i < addOptionsList.options.selectedIndex; i++ )
      {
       newOptionListArray[i + 1] = new Object();
       newOptionListArray[i + 1].value = addOptionsList.options[i].value;
       newOptionListArray[i + 1].text = addOptionsList.options[i].text;
      }
      
      newOptionIndex = 0;
      
      newOptionListArray[0] = new Object();
      newOptionListArray[0].value = addOptionsList.options[addOptionsList.options.selectedIndex].value;
      newOptionListArray[0].text = addOptionsList.options[addOptionsList.options.selectedIndex].text;
     }
    }else if( moveDirection == 'up' )
    {  
     if( addOptionsList.options.selectedIndex > 0 )
     {
      for( var i = 0; i < ( addOptionsList.options.selectedIndex - 1); i++ )
      {
       newOptionListArray[i] = new Object();
       newOptionListArray[i].value = addOptionsList.options[i].value;
       newOptionListArray[i].text = addOptionsList.options[i].text;
      }
     
      newOptionListArray[addOptionsList.options.selectedIndex] = new Object();
      newOptionListArray[addOptionsList.options.selectedIndex].value = addOptionsList.options[addOptionsList.options.selectedIndex - 1].value;
      newOptionListArray[addOptionsList.options.selectedIndex].text = addOptionsList.options[addOptionsList.options.selectedIndex - 1].text;
     
      newOptionListArray[addOptionsList.options.selectedIndex - 1] = new Object();
      newOptionListArray[addOptionsList.options.selectedIndex - 1].value = addOptionsList.options[addOptionsList.options.selectedIndex].value;
      newOptionListArray[addOptionsList.options.selectedIndex - 1].text = addOptionsList.options[addOptionsList.options.selectedIndex].text;  
      newOptionIndex = addOptionsList.options.selectedIndex - 1;

      for( var i = addOptionsList.options.selectedIndex + 1; i < addOptionsList.options.length; i++ )
      {
       newOptionListArray[i] = new Object();
       newOptionListArray[i].value = addOptionsList.options[i].value;
       newOptionListArray[i].text = addOptionsList.options[i].text;
      }
     
      addOptionsListEditItemKey.value = "";
     }else
     {            
      for( var i = ( addOptionsList.options.length - 1 ); i > 0; i-- )
      {
       newOptionListArray[i - 1] = new Object();
       newOptionListArray[i - 1].value = addOptionsList.options[i].value;
       newOptionListArray[i - 1].text = addOptionsList.options[i].text;
      }
      
      newOptionIndex = newOptionListArray.length - 1;
      newOptionListArray[newOptionIndex] = new Object();
      newOptionListArray[newOptionIndex].value = addOptionsList.options[0].value;
      newOptionListArray[newOptionIndex].text = addOptionsList.options[0].text;
     }
    } 
    
    if( newOptionListArray.length > 0 )
    {
     for( var i = 0; i < newOptionListArray.length; i++ )
     {
      addOptionsList.options[i].text = newOptionListArray[i].text;
      addOptionsList.options[i].value = newOptionListArray[i].value;
     } 
    } 
    
    addOptionsList.options.selectedIndex = newOptionIndex;

    selectAllOptions( optionName );
   }else
   {
    alert( "Please select a valid item to Move." );
   }
  }else
  {
   alert( "Please specify a valid direction." );
  }   
 }else{alert( "formId is invalid!" );}
}

function deleteTextOption( formId, optionName, defaultValue )
{
 addOptionsList = this.document.getElementById( optionName + "List" );
 addOptionsListAddTextOption =  this.document.getElementById( optionName + "AddTextOption" );
 addOptionsListEditItemKey =  this.document.getElementById( optionName + "EditItemKey" );

 if( addOptionsList.options.selectedIndex > -1 && addOptionsList.options[addOptionsList.options.selectedIndex].value != "" )
 {
  addOptionsList.options[addOptionsList.options.selectedIndex] = null;  
  addOptionsListEditItemKey.value = "";

  if( addOptionsList.options.length == 0 )
  {
   addOptionsList.options[addOptionsList.options.length] = new Option( defaultValue, "" );
  }


  selectAllOptions( optionName );
 }else
 {
  alert( "Please select a valid item to Delete." );
 }
}

function editTextOption( formId, optionName, defaultValue )
{
 addOptionsList = this.document.getElementById( optionName + "List" );
 addOptionsListAddTextOption =  this.document.getElementById( optionName + "AddTextOption" );
 addOptionsListEditItemKey =  this.document.getElementById( optionName + "EditItemKey" );

 if( addOptionsList.options.selectedIndex > -1 && addOptionsList.options[addOptionsList.options.selectedIndex].value != "" )
 {
  addOptionsListEditItemKey.value = addOptionsList.options.selectedIndex;
  addOptionsListAddTextOption.value = addOptionsList.options[addOptionsList.options.selectedIndex].value;

  selectAllOptions( optionName );
 }else
 {
  alert( "Please select a valid item to Edit." );
 }
}

function addSelectOption( formId, optionName )
{
 if( formId > 0 )
 {
  addOptionsList = this.document.getElementById( optionName + "List" );
  addOptionsListAddSelectOption =  this.document.getElementById( optionName + "AddSelectOption" );

  if( addOptionsListAddSelectOption.options[addOptionsListAddSelectOption.options.selectedIndex].value != "" )
  {
   if( addOptionsList.options.length > 0 )
   {
    if( addOptionsList.options[0].value == "" )
    {
     addOptionsList.options[0] = null;
    }
   }

   addOptionsList.options[addOptionsList.options.length] = new Option( addOptionsListAddSelectOption.options[addOptionsListAddSelectOption.options.selectedIndex].text, addOptionsListAddSelectOption.options[addOptionsListAddSelectOption.options.selectedIndex].value );
   addOptionsListAddSelectOption.options[addOptionsListAddSelectOption.options.selectedIndex] = null;

   selectAllOptions( optionName );
  }else{alert( "Specified value is invalid");}
 }else{alert( "formId is invalid!" );}
}

function deleteSelectOption( formId, optionName, defaultValue )
{
 addOptionsList = this.document.getElementById( optionName + "List" );
 addOptionsListAddSelectOption =  this.document.getElementById( optionName + "AddSelectOption" );

 if( addOptionsList.options.selectedIndex > -1 && addOptionsList.options[addOptionsList.options.selectedIndex].value != "" )
 {
  if( addOptionsList.options.length == 1 )
  {
   addOptionsList.options[addOptionsList.options.length] = new Option( defaultValue, "" );
  }

  addOptionsListAddSelectOption.options[addOptionsListAddSelectOption.options.length] = new Option( addOptionsList.options[addOptionsList.options.selectedIndex].text, addOptionsList.options[addOptionsList.options.selectedIndex].value );

  addOptionsList.options[addOptionsList.options.selectedIndex] = null;
  selectAllOptions( optionName );
 }else
 {
  alert( "Please select a valid item to Delete." );
 }
}

function selectAllOptions( optionName )
{
 addOptionsList = this.document.getElementById( optionName + "List" );
 addOptions = this.document.getElementById( optionName );
 addOptions.value = "";

 returnedValuesArray = new Array( addOptionsList.length );

 for( var i = 0; i < addOptionsList.options.length; i++ )
 {
  returnedValuesArray[i] = addOptionsList.options[i].value;
 }

 addOptions.value = serializeArray( returnedValuesArray );
 
}

function getPosition( e )
{
 e = e || window.event;
 var cursor = {x:0, y:0};
 if (e.pageX || e.pageY) {
     cursor.x = e.pageX;
     cursor.y = e.pageY;
 }else
 {
     var de = document.documentElement;
     var b = document.body;
     cursor.x = e.clientX +
         (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
     cursor.y = e.clientY +
         (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
 }

 return cursor;
}

function hideElement( objName, mousePosition )
{
 elementLeft = document.getElementById( objName ).style.left;
 elementLeft = elementLeft.replace( 'px', '' );

 elementTop = document.getElementById( objName ).style.top;
 elementTop = elementTop.replace( 'px', '' );

 elementHeight = document.getElementById( objName ).offsetHeight;

 elementWidth = document.getElementById( objName ).offsetWidth;

 if( mousePosition.x < elementLeft || mousePosition.x > ( ( elementLeft * 1 ) + ( elementWidth * 1 ) ) || mousePosition.y < elementTop || mousePosition.y > ( ( elementTop * 1 ) + ( elementHeight * 1 ) ) )
 {
  setTimeout( "hideElementDisplay( " + objName + " );", 100 );
 }

 return;
}

function hideElementDisplay( objName )
{
 objName.style.display = "none";

 return;
}

function showElement( objName, eventObj )
{
 document.getElementById( objName ).style.display = "block";
 document.getElementById( objName ).style.left = ( getPosition( eventObj ).x - 10 );
 document.getElementById( objName ).style.top = ( getPosition( eventObj ).y - 10 );

 return;
}

function buildInputCalendar( month, year, day, inputFieldID, currentValue, elementContainer, maxValue )
{
 if( currentValue != '' )
 {
  currentValue = String( currentValue );
  splitCurrentDate = currentValue.split( '/' );
  month = splitCurrentDate[0];
  day = splitCurrentDate[1];
  year = splitCurrentDate[2];
 }

 month = parseInt( month );
 day = parseInt( day );
 year = parseInt( year );

 var monthsArray = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];
 var dim = [ 31 , 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
 var originalDate = new Date( year, month-1, 1);
 originalDate.originalDate = originalDate.getDay() + 1;

 if( month > 0 && month <= 9 )
 {
   month = "0" + stripZeros( month );
 }else
 {
  month = stripZeros( month );
 }

 var todaysDate = new Date();
 var scanForToday = ( year == todaysDate.getFullYear() && month==todaysDate.getMonth()+1)? todaysDate.getDate() : 0

 dim[1]=(((originalDate.getFullYear()%100!=0)&&(originalDate.getFullYear()%4==0))||(originalDate.getFullYear()%400==0))?29:28;
 var calendarTable = '<div class="dropDownCalendarMain"><table class="dropDownCalendarMain" cols="7" cellpadding="0" cellspacing="1" width="100%"><tr align="center">';

 calendarTable += '<td align="center" class="dropDownCalendarMonth"><img src="images/icons/date_previous.png" border="0" height="16" width="16" style="cursor: pointer;" onclick="calendarNavigate( \'' + month + '\', \'' + year + '\', \'' + day + '\', \'' + inputFieldID + '\', \'' + currentValue + '\', \'' + elementContainer + '\', \'' + maxValue + '\', \'-1\' );" /></td><td colspan="5" align="center" class="dropDownCalendarMonth">'+monthsArray[month-1]+' - '+year+'</td><td align="center" class="dropDownCalendarMonth"><img src="images/icons/date_next.png" border="0" height="16" width="16"  style="cursor: pointer;" onclick="calendarNavigate( \'' + month + '\', \'' + year + '\', \'' + day + '\', \'' + inputFieldID + '\', \'' + currentValue + '\', \'' + elementContainer + '\', \'' + maxValue + '\', \'1\' );" /></td></tr><tr align="center">';

 for( s = 0; s < 7; s++)
 {
  calendarTable += '<td class="dropDownCalendarDaysOfWeek">'+"SMTWTFS".substr(s,1)+'</td>';
 }

 calendarTable += '</tr><tr align="center">';

 for( i = 1; i <= 42;i++ )
 {
  var x = ( ( i - originalDate.originalDate >= 0 ) && ( i - originalDate.originalDate < dim[month-1] ) ) ? i - originalDate.originalDate + 1 : '';


  if( x > 0 && x <= 9 )
  {
   x = "0" + stripZeros( x );
  }

  dayShown = x;

  calendarTable += '<td class="';

  if( ( x == scanForToday && day == '' ) || ( day != '' && x == day ) )
  {
   calendarTable += 'dropDownCalendarDaysCurrent';
  }else
  {
   calendarTable += 'dropDownCalendarDays';
  }

  calendarTable += '">';

  if( x != '' )
  {
   calendarTable += '<a href="#" onclick="document.getElementById(\'' + inputFieldID + '\').value=\'' + month + '/' + x + '/' + year + '\'">'+ dayShown +'</a>';
  }else
  {
   calendarTable += dayShown;
  }

  calendarTable += '</td>';

  if(((i)%7==0)&&(i<36))
  {
   calendarTable += '</tr><tr align="center">';
  }
 }

 return calendarTable += '</tr></table></div>';
}

function buildInputCalendarTime( month, year, day, hour, minute, inputFieldID, currentValue, elementContainer, maxValue )
{
 if( currentValue != '' )
 {
  currentValue = String( currentValue );
  splitCurrentDate = currentValue.split( '/' );
  month = splitCurrentDate[0];
  day = splitCurrentDate[1];
  remaining = splitCurrentDate[2];
		
  splitCurrentDateRemaining = remaining.split( ' ' );
  year = splitCurrentDateRemaining[0];
		
		time = splitCurrentDateRemaining[1];
  splitCurrentTime = time.split( ':' );
  hour = splitCurrentTime[0];
		calendarTimeHour = hour;
  minute = splitCurrentTime[1];
		calendarTimeMinute = minute;		
 }

 month = parseInt( month );
 day = parseInt( day );
 year = parseInt( year );

 var monthsArray = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];
 var dim = [ 31 , 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
 var originalDate = new Date( year, month-1, 1);
 originalDate.originalDate = originalDate.getDay() + 1;

 if( month > 0 && month <= 9 )
 {
   month = "0" + stripZeros( month );
 }else
 {
  month = stripZeros( month );
 }
	
	if( hour > 0 && hour <= 9 )
 {
   hour = "0" + stripZeros( hour );
 }else
 {
  hour = stripZeros( hour );
 }
	
	if( minute > 0 && minute <= 9 )
 {
   minute = "0" + stripZeros( minute );
 }else
 {
  minute = stripZeros( minute );
 }

 var todaysDate = new Date();
 var scanForToday = ( year == todaysDate.getFullYear() && month==todaysDate.getMonth()+1)? todaysDate.getDate() : 0

 dim[1]=(((originalDate.getFullYear()%100!=0)&&(originalDate.getFullYear()%4==0))||(originalDate.getFullYear()%400==0))?29:28;
 var calendarTable = '<div class="dropDownCalendarTimeMain"><table class="dropDownCalendarTimeMain" cols="7" cellpadding="0" cellspacing="1" width="100%"><tr align="center">';

 calendarTable += '<td align="center" class="dropDownCalendarMonth"><img src="images/icons/date_previous.png" border="0" height="16" width="16" style="cursor: pointer;" onclick="calendarTimeNavigate( \'' + month + '\', \'' + year + '\', \'' + day + '\', \'' + hour + '\', \'' + minute + '\', \'' + inputFieldID + '\', \'' + currentValue + '\', \'' + elementContainer + '\', \'' + maxValue + '\', \'-1\' );" /></td><td colspan="5" align="center" class="dropDownCalendarMonth">'+monthsArray[month-1]+' - '+year+'</td><td align="center" class="dropDownCalendarMonth"><img src="images/icons/date_next.png" border="0" height="16" width="16"  style="cursor: pointer;" onclick="calendarTimeNavigate( \'' + month + '\', \'' + year + '\', \'' + day + '\',  \'' + hour + '\',  \'' + minute + '\', \'' + inputFieldID + '\', \'' + currentValue + '\', \'' + elementContainer + '\', \'' + maxValue + '\', \'1\' );" /></td></tr><tr align="center">';

 for( s = 0; s < 7; s++)
 {
  calendarTable += '<td class="dropDownCalendarDaysOfWeek">'+"SMTWTFS".substr(s,1)+'</td>';
 }

 calendarTable += '</tr><tr align="center">';

 for( i = 1; i <= 42;i++ )
 {
  var x = ( ( i - originalDate.originalDate >= 0 ) && ( i - originalDate.originalDate < dim[month-1] ) ) ? i - originalDate.originalDate + 1 : '';


  if( x > 0 && x <= 9 )
  {
   x = "0" + stripZeros( x );
  }

  dayShown = x;

  calendarTable += '<td class="';

  if( ( x == scanForToday && day == '' ) || ( day != '' && x == day ) )
  {
   calendarTable += 'dropDownCalendarDaysCurrent';
  }else
  {
   calendarTable += 'dropDownCalendarDays';
  }

  calendarTable += '">';

  if( x != '' )
  {
		 calendarTable += '<a href="#" onclick="document.getElementById(\'' + inputFieldID + '\').value=\'' + month + '/' + x + '/' + year + ' \' + calendarTimeHour + \':\' + calendarTimeMinute + \'\';">'+ dayShown +'</a>';
  }else
  {
   calendarTable += dayShown;
  }

  calendarTable += '</td>';

  if(((i)%7==0)&&(i<36))
  {
   calendarTable += '</tr><tr align="center">';
  }
 }
	
 return calendarTable += '</tr><tr><td colspan="6" align="center"><input type="text" size="2" maxlength="2" style="width: 20px; text-align: center;" id="calendarTimeHour" value="' + calendarTimeHour + '" onchange="calendarTimeHour = this.value;" />:<input type="text" size="2" maxlength="2" style="width: 20px;" value="' + calendarTimeMinute + '" id="calendarTimeMinute" onchange="calendarTimeMinute = this.value;" /></td><td align="center" valign="center"><a href="#"  onclick="document.getElementById(\'' + inputFieldID + '\').value=\'' + month + '/' + day + '/' + year + ' \' + calendarTimeHour + \':\' + calendarTimeMinute + \'\';">[X]</a></td></tr></table></div>';
}

function calendarNavigate( month, year, day, inputFieldID, currentValue, elementContainer, maxValue, moveDirection )
{
 month = stripZeros( month );

 if( moveDirection == -1 )
 {
  if( parseInt( month ) == 1 )
  {
   month = 12;
   year = parseInt( year ) - 1;
  }else
  {

   month = parseInt( month ) - 1;
  }
 }else if( moveDirection == 1)
 {
  if( parseInt( month ) == 12 )
  {
   month = 1;
   year = parseInt( year ) + 1;
  }else
  {
   month = parseInt( month ) + 1;
  }
 }

	document.getElementById( elementContainer ).innerHTML = '' + buildInputCalendar( parseInt ( month ), parseInt( year ), parseInt( day ), inputFieldID, '', elementContainer );			
}

function calendarTimeNavigate( month, year, day, hour, minute, inputFieldID, currentValue, elementContainer, maxValue, moveDirection )
{
 month = stripZeros( month );

 if( moveDirection == -1 )
 {
  if( parseInt( month ) == 1 )
  {
   month = 12;
   year = parseInt( year ) - 1;
  }else
  {

   month = parseInt( month ) - 1;
  }
 }else if( moveDirection == 1)
 {
  if( parseInt( month ) == 12 )
  {
   month = 1;
   year = parseInt( year ) + 1;
  }else
  {
   month = parseInt( month ) + 1;
  }
 }
	
	document.getElementById( elementContainer ).innerHTML = '' + buildInputCalendarTime( parseInt ( month ), parseInt( year ), parseInt( day ), parseInt( hour ), parseInt( minute ), inputFieldID, '', elementContainer );			
}

function updateCalendar( theselection )
{
 var themonth=parseInt(theselection[theselection.selectedIndex].value)+1
 var calendarstr=buildCal(themonth, curyear, "main", "month", "daysofweek", "days", 0)
 if (document.getElementById)
 document.getElementById("calendarspace").innerHTML=calendarstr
}

function stripZeros( strValue )
{
 strValue = String( strValue );
 strValue = strValue.replace( /^[0]+/g, "" );
 return strValue;
}

function serializeArray( arrayValuesObj )
{
 var serializeArray = "";
 var totalElements = 0;

 for( var arrayKey in arrayValuesObj )
 {
  ++ totalElements;
  serializeArray = serializeArray + "s:" +
  String( arrayValuesObj[arrayKey] ).length + ":\"" + String( arrayValuesObj[arrayKey] ) + "\";s:" +
  String( arrayValuesObj[arrayKey] ).length + ":\"" + String( arrayValuesObj[arrayKey] ) + "\";";
 }

 serializeArray = "a:" + totalElements + ":{" + serializeArray + "}";

 return serializeArray;
}
