/**
 * updates html fields with passed json values
 *
 * @param Object request object
 * @param Object json object
 */
function updateJSON(request, json)
{
	var responses = json;
	if (!json){
	  //if you don't use the json tips then evaluate the renderedText instead
	  var responses = eval('(' + request.responseText + ')');
	}
	var resSize = responses.length;
	for (var i = 0; i < resSize; i++)
	{
	   Element.update(responses[i][0], responses[i][1]);
	}
}

/**
 * fills main search form select boxes with parsed request or json object depending on whats passed
 *
 * @param string form_name form to fill select boxes in
 * @param Object request object
 * @param Object json object
 */
function fillForm(form_name, request, json)
{
	var data = request.responseText.evalJSON();
	
	keys = Object.keys(data);
	values = Object.values(data);
	
	selected_keys = Object.keys(data.selected);
	selected_values = Object.values(data.selected);
	
	//loop through object values
	for (var i=0; i<keys.length; i++)
	{
		if (keys[i] != 'selected' && keys[i] != 'hidden' && keys[i] != 'veiling_id')
		{
			//First enable form field
			name = form_name + '_' + keys[i];
		
			//disable while filling select box
			$(name).disable();

			//empty select box
			for(var k=$(name).options.length-1; k>=0; k--)
			{
				$(name).options[k]=null;
			}
		
			//Set keys/values of object
			element_object = values[i];
			element_keys = Object.keys(element_object);
			element_values = Object.values(element_object);
			element_length = element_values.length;
		
			//get selectedIndex
			selectedIndex = false;
			for (l=0; l<selected_keys.length; l++)
			{
				if (keys[i] == selected_keys[l])
				{
					selectedIndex = selected_values[l];
				}
			}
			
			/**
			 * Fill in select box. Make sure to set selectedIndex if it's returned in json
			 */
			for (var j=0; j< element_length; j++)
			{
				if ($(name) != null && $(name).options != null)
				{
					if (element_keys[j] == selectedIndex)
					{
						$(name).options[$(name).options.length] = new Option(element_values[j], element_keys[j], true, true);
					}
					else
					{
						$(name).options[$(name).options.length] = new Option(element_values[j], element_keys[j], false, false);
					}
				}
			}
			
			/**
			 * only enable if select box has more than 1 (the standard) option
			*/
			if(element_length > 1)
			{
				$(name).enable();
			}
		}
	}
}

/** 
 * Enables/disables form fields according to which category is chosen
 * @param int category_id
 */
function setOptional(category_id)
{
	var optional_elements = new Array("product_km", "product_draaiuren", "product_hp", "product_kw", "product_euro_norm_id", "product_tachograaf_id", "product_versnellingsbak", "product_tow_hitch", "product_total_combination_weight", "product_pen_thickness_coupling", "product_vangmuil_schotel");

	if(category_id == 4)
	{
		for(i in optional_elements)
		{
			if(!$("row_" + optional_elements[i]))
			{
				$("row_" + optional_elements[i]).hide();
			}			
		}
	}
	else
	{
		for(i in optional_elements)
		{
			if($("row_" + optional_elements[i]))
			{
				$("row_" + optional_elements[i]).show();
			}		
		}
	}
}


/** TIMERS **/

/**
 * Returns diff between two times
 */
function calcage(secs, num1, num2) {
	LeadingZero = true;
  s = ((Math.floor(secs/num1))%num2).toString();
  if (LeadingZero && s.length < 2)
    s = "0" + s;
  return s;
}

/**
 * Does the actual formatting
 */
function CountBack(secs, CountStepper, SetTimeOutPeriod, CountActive) {
	
	if (secs >= 86400)
	{
		var DisplayFormat = "%%D%%d:%%H%%u:%%M%%m:%%S%%s";
	}
	else if (secs >= 3600)
	{
		var DisplayFormat = "%%H%%u:%%M%%m:%%S%%s";
	}
	else
	{
		var DisplayFormat = "%%M%%m:%%S%%s";
	}
	
	var FinishMessage = "Bod voorbij!";
  if (secs < 0) 
	{
    return FinishMessage;
  }
  DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
  DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
  DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
  DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));

  if (CountActive)
    setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
	
	return DisplayStr;
}

/**
 * Returns date diff in secs
 */
function getSecs(date, interval)
{
	//Put dates in date objects
	var dthen = new Date(date);
  var dnow = new Date();

	// Set countstepper & timeout period
	var CountStepper = 0-interval;
  var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
	var CountActive = true;

  if(CountStepper>0)
	{
    ddiff = new Date(dnow-dthen);
	}
  else
	{
    ddiff = new Date(dthen-dnow);
	}
  
	gsecs = Math.floor(ddiff.valueOf()/1000);
	return gsecs;
}

/**
 * Countdown timer
 */
function countdown(date, interval)
{
	//Put dates in date objects
	var dthen = new Date(date);
  var dnow = new Date();

	// Set countstepper & timeout period
	var CountStepper = 0-interval;
  var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
	var CountActive = true;

  if(CountStepper>0)
	{
    ddiff = new Date(dnow-dthen);
	}
  else
	{
    ddiff = new Date(dthen-dnow);
	}
	
	gsecs = getSecs(date, interval);
	
  return CountBack(gsecs, CountStepper, SetTimeOutPeriod, CountActive);
}

/**LOGIN**/

function moveToPrevious(width)
{
  new Effect.Move('imageBoxInside', { x: width, y: 0, transition: Effect.Transitions.sinoidal });
} 

function moveToNext(width)
{
  new Effect.Move('imageBoxInside', { x: -width, y: 0, transition: Effect.Transitions.sinoidal });
}

