// Dynamic Version by: Nannette Thacker
// http://www.shiningstar.net -->
// Original by :  Ronnie T. Moore -->
// Web Site:  The JavaScript Source -->
// Use one function for multiple text areas on a page -->
// Limit the number of characters per textarea -->

startYear = 1913

function textCounter(field, cntfield, maxlimit)
{
  // if too long...trim it!
  if (field.value.length > maxlimit)
    field.value = field.value.substring(0, maxlimit);
  // otherwise, update 'characters left' counter
  else
    cntfield.value = maxlimit - field.value.length;
}


swapFields = new Array()

function prepareSave()
{
  for (index = 0; index < swapFields.length; index++)
  {
    divSection = document.getElementById(swapFields[index] + '_div');
		alert(divSection.contentEditable);
    textObject = eval("document.Form1." + swapFields[index]);
    textObject.value = divSection.innerHTML
  }

  return false;
}

function addToSwapList(swapField)
{
  swapFields[swapFields.length] = swapField
}

function checkField(Which)
{
	var el = Which + 'ModeB';

	d = document.getElementById(el);

	if (d != null)
	{
		d.checked = true;
	}
}

function validateField(Which)
{
  DayObject = eval("document.Form1." + Which + "Day");
  MonthObject = eval("document.Form1." + Which + "Month");
  YearObject = eval("document.Form1." + Which + "Year");
  HourObject = eval("document.Form1." + Which + "Hour");
  MinuteObject = eval("document.Form1." + Which + "Minute");

  Day = DayObject[DayObject.selectedIndex].text;
  Month = MonthObject[MonthObject.selectedIndex].text;
  Year = YearObject[YearObject.selectedIndex].text;
  Hour = HourObject[HourObject.selectedIndex].text;
  Minute = MinuteObject[MinuteObject.selectedIndex].text;

  TextObject = eval("document.Form1." + Which)
  TextObject.value = MonthObject.selectedIndex+1
}

//set todays date
Now = new Date();
NowDay = Now.getDate();
NowMonth = Now.getMonth();
NowYear = Now.getYear();
NowHour = Now.getHours();
NowMinute = Now.getMinutes();
if (NowYear < 2000) NowYear += 1900; //for Netscape

//function for returning how many days there are in a month including leap years
function DaysInMonth(WhichMonth, WhichYear)
{
  var DaysInMonth = 31;
  if (WhichMonth == "Apr" || WhichMonth == "Jun" || WhichMonth == "Sep" || WhichMonth == "Nov") DaysInMonth = 30;
  if (WhichMonth == "Feb" && (WhichYear/4) != Math.floor(WhichYear/4))  DaysInMonth = 28;
  if (WhichMonth == "Feb" && (WhichYear/4) == Math.floor(WhichYear/4))  DaysInMonth = 29;
  return DaysInMonth;
}

//function to change the available days in a months
function ChangeOptionDays(Which)
{
  DaysObject = eval("document.Form1." + Which + "Day");
  MonthObject = eval("document.Form1." + Which + "Month");
  YearObject = eval("document.Form1." + Which + "Year");

  Month = MonthObject[MonthObject.selectedIndex].text;
  Year = YearObject[YearObject.selectedIndex].text;

  DaysForThisSelection = DaysInMonth(Month, Year);
  CurrentDaysInSelection = DaysObject.length;
  if (CurrentDaysInSelection > DaysForThisSelection)
  {
    for (i=0; i<(CurrentDaysInSelection-DaysForThisSelection); i++)
    {
      DaysObject.options[DaysObject.options.length - 1] = null
    }
  }
  if (DaysForThisSelection > CurrentDaysInSelection)
  {
    for (i=0; i<(DaysForThisSelection-CurrentDaysInSelection); i++)
    {
      NewOption = new Option(DaysObject.options.length + 1);
      DaysObject.add(NewOption);
    }
  }
  if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;

  validateField(Which)
}

//function to set options to today
function SetToToday(Which)
{

  DayObject = eval("document.Form1." + Which + "Day");
  MonthObject = eval("document.Form1." + Which + "Month");
  YearObject = eval("document.Form1." + Which + "Year");
  HourObject = eval("document.Form1." + Which + "Hour");
  MinuteObject = eval("document.Form1." + Which + "Minute");

  YearObject[ NowYear - startYear].selected = true;
  MonthObject[NowMonth].selected = true;

  HourObject[NowHour].selected = true;
  MinuteObject[NowMinute].selected = true;

  ChangeOptionDays(Which);

  DayObject[NowDay-1].selected = true;
}

//function to set options to today
function SetDate(Which, year, month, day, hour, minute)
{

  DayObject = eval("document.Form1." + Which + "Day");
  MonthObject = eval("document.Form1." + Which + "Month");
  YearObject = eval("document.Form1." + Which + "Year");
  HourObject = eval("document.Form1." + Which + "Hour");
  MinuteObject = eval("document.Form1." + Which + "Minute");

  if (year < startYear )
    year = NowYear;

  YearObject[ year - startYear].selected = true;
  MonthObject[month-1].selected = true;

  HourObject[hour].selected = true;
  MinuteObject[minute].selected = true;

  ChangeOptionDays(Which);

  DayObject[day-1].selected = true;
}



//function to write option years plus x
function WriteYearOptions(YearsAhead)
{
  line = "";

  for (i=0; i<YearsAhead; i++)
  {
    line += "<OPTION>";
    line += startYear + i;
  }
  return line;
}

//function to write option years plus x
function WriteHourOptions(YearsAhead)
{
  line = "";
  for (i=1; i<25; i++)
  {
    line += "<OPTION>";
    line += i;
  }
  return line;
}

function WriteRangeOptions(start, end)
{
  line = "";
  for (i=start; i <= end; i++)
  {
    line += "<OPTION>";
    if (i < 10)
      line += '0';
    line += i;
  }
  return line;
}

function dateSelect(Which)
{
  document.write('<SELECT name="' + Which + 'Day" onchange="checkField(\'' + Which + '\'); validateField(\'' + Which + '\')">');
  document.write(WriteRangeOptions(1, 31));
  document.write('</SELECT>');

  document.write('<SELECT SELECT name="' + Which + 'Month" onchange="checkField(\'' + Which + '\'); ChangeOptionDays(\'' + Which +'\')">');
  document.write('<OPTION value="1">Januar');
  document.write('<OPTION value="2">Februar');
  document.write('<OPTION value="3">Mars');
  document.write('<OPTION value="4">April');
  document.write('<OPTION value="5">Mai');
  document.write('<OPTION value="6">Juni');
  document.write('<OPTION value="7">Juli');
  document.write('<OPTION value="8">August');
  document.write('<OPTION value="9">September');
  document.write('<OPTION value="10">Oktober');
  document.write('<OPTION value="11">November');
  document.write('<OPTION value="12">Desember');
  document.write('</SELECT>');

  document.write('<SELECT SELECT name="' + Which + 'Year"  onchange="checkField(\'' + Which + '\'); ChangeOptionDays(\'' + Which +'\')">');
  document.write(WriteYearOptions(100));
  document.write('</SELECT> ');


  document.write('<SELECT name="' + Which + 'Hour" onchange="checkField(\'' + Which + '\'); validateField(\'' + Which +'\')">');
  document.write(WriteRangeOptions(0, 23));
  document.write('</SELECT>');


  document.write('<SELECT name="' + Which + 'Minute" onchange="checkField(\'' + Which + '\'); validateField(\'' + Which +'\')">');
  document.write(WriteRangeOptions(0, 59));
  document.write('</SELECT>');

  document.write('<input type="hidden" name="' + Which + '" value="">');
}

function swapImage(imageUrl, category)
{
  document.images.mainImage.src = imageUrl;
  textArea = document.getElementById("categoryText");
  if (textArea != null)
  {
    textArea.innerHTML = category;
  }
}

function SwapImage(imageUrl, category)
{
  el = document.getElementById('mainimage');
  if (el != null)
  {
    el.src = imageUrl;
    textArea = document.getElementById("maincategory");
    if (textArea != null)
    {
      textArea.innerHTML = category;
    }
  }
}

thisURL  = this.location;

function openWindow(url) {
 myWin = window.open(url, 'OriginalSizeImage', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=550,height=400,left=20,top=20');
}


/* progress bar */

var rpi                    = false;

function InsertProgressBar(title, id, url, classname)
{
  if (url != null && url != '')
    url = ' src="' + url + '"';
  else
    url = '';

  if (classname == null)
    classname = 'progress';

  var output = '' +
               '   <di' + 'v class="lime_' + classname + 'bar" id="' + id + '_progressbar">' +
               '     <di' + 'v class="title" id="' + id + '_progress_title">' + title + '<\/di' + 'v>' +
               '     <di' + 'v class="bar"><di' + 'v class="meter" id="' + id + '_progress_bar"><\/di' + 'v><\/di' + 'v>' +
               '     <di' + 'v class="amount" id="' + id + '_progress_num" onclick="toggleProgressDebug(\'' + id + '\');"><\/di' + 'v>' +
               '     <di' + 'v class="text" id="' + id + '_progress_text"><\/di' + 'v>' +
               '     <ifram' + 'e class="lime_progressbuffer"' + url + ' style="display: none;" name="' + id + '" id="' + id + '" width="100%"><\/ifram' + 'e>' +
               '   <\/di' + 'v>';
  document.write(output);
}

function toggleProgressDebug(id)
{
	//UpdateProgressBar(id, 40, '', '', null, null);
	var el = document.getElementById(id);
	if (el)
	{
		var s = el.style.display;
		if (s == 'block')
			el.style.display = 'none';
		else
			el.style.display = 'block';
	}
}

//
//

function EnableProgressBar(id)
{
  var el = document.getElementById(id + '_progressbar')
  {
    if (el != null)
      el.style.display = 'block';
  }
}

//
//

function UpdateProgressBar(id, progress, status, url, millis, method)
{
  timer = null;
  clearTimeout(timer);

  if (millis == null)
    millis = 5000;

  var el = document.getElementById(id + '_progressbar');
  if (el != null)
  {
    el.style.display = 'block';
  }

  el = document.getElementById(id + '_progress_bar');
  if (el != null)
  {
    el.style.width = progress + '%';
  }

  el = document.getElementById(id + '_progress_num');
  if (el != null)
  {
    el.innerHTML = progress + '%';
  }

  // update the text window
  if (status != '')
  {
    el = document.getElementById(id + '_progress_text');
    if (el != null)
    {
      el.innerHTML = status;
    }
  }

  // prepare for redirect
  if (url != null && url != '')
  {
    timer = setTimeout("moveOn(url)", parseInt(millis));
  }

  if (progress >= 100)
  {
    if (millis > 0)
      timer = setTimeout("HideProgressBar('" + id + "')", parseInt(millis));

    if (method != null && method['call'] != '')
    {
      eval(method['call']);
    }
  }

  // clear frame buffer
  el = document.getElementById(id);
  if (el != null) window[id].window.document.clear;
}

function UpdateProgressTitle(id, header)
{
  var el = document.getElementById(id + '_progress_title');
  if (el) el.innerHTML = header;
}

function moveOn(URL)
{
  if (redirect != '');
    document.location.href = URL;
}

function reloadPage(URL)
{
	if (!rpi)
	{
		timer = setTimeout('_reloadPage("' + URL + '")', 1500);
		rpi = true;
	}
}

function _reloadPage(URL)
{
  document.location.href = document.location.href;

  clearTimeout(timer);
  rpi = false;
}

function reloadParent()
{
	timer = setTimeout('_reloadParent()', 5000);
}

function _reloadParent()
{
	document.location.href = document.location.href;
}

function openDialog(id, text)
{
  alert(text);
  HideProgressBar(id);
}

function HideProgressBar(id)
{
  var el = document.getElementById(id + '_progressbar');
  if (el != null)
  {
    el.style.display = 'none';
  }

  el = document.getElementById(id + '_progress_bar');
  if (el != null)
  {
    el.style.width = '0%';
  }

  el = document.getElementById(id + '_progress_num');
  if (el != null)
  {
    el.innerHTML = '';
  }

  // clear the text window
  if (status != '')
  {
    el = document.getElementById(id + '_progress_text');
    if (el != null)
    {
      el.innerHTML = '';
    }
  }
}


