/**
 * lowercaseSpecial
 *
 * Convierte la cedena recibida como parámetro a lowercase manteniendo el primer caracter en uppercase
 */
function lowercaseSpecial (theField) {
	if ($(theField)) {
		var form = $('sf_admin_edit_form');
		var input = form[theField];
		
		val = Form.Element.getValue(input);

		val = val.substring(0,1).toUpperCase() + val.substring(1).toLowerCase();

		Form.Element.setValue(form[theField], val);
	}
}

/**
 * clearField
 *
 * Vacía el value del input recibido como parámetro
 */
 function clearField (theInput) {
	theInput.value = '';
}

/**
 * cleanEan
 *
 * Vuelca en el campo EAN real el campo filtrado del EAN falso
 */
function cleanEan (theEan) {
	theEan = theEan.replace(/-/g, "");
	theEan = theEan.replace(/ /g, "")
	
	Form.Element.setValue('ean', theEan);
	checkEan (Form.Element.getValue('ean'));
}

/**
 * checkEan
 *
 * Vuelca en el campo EAN CHK el dígito de control calculado
 */
function checkEan(theEan){
	Form.Element.setValue('ean_chk', getEanChk(theEan));
}
	
/**
 * getEanChk
 *
 * Retorna el dígito de control del EAN recibido
 */
function getEanChk (theEan) {
	if (theEan.length != 12) return 'E';
	
	var value_0 = "0".charCodeAt(0);
	var sum = 0;
	var oddParity = true;
	
	for (var i=theEan.length-1; i>=0; i--) {
		if (oddParity) {
			sum += 3 * (theEan.charCodeAt(i) - value_0);
		}
		else {
			sum += theEan.charCodeAt(i) - value_0;
		}
		oddParity = !oddParity;
	}
	
	var checkDigit = 10 - (sum % 10);
	if (checkDigit == 10) checkDigit = 0;	
	
	return String.fromCharCode (checkDigit + value_0 );
}


/**
 * checkTerms
 *
 * Check if the user has checked the box with the shopping terms
 */
function checkTerms (theMessage) {
	if (document.validaForm.terms.checked) {
		return true;
	}
	else {
		alert (theMessage);
		return false;
	}
}

function fastCheckTerms (theMessage) {
	if (document.fastForm.terms.checked) {
		return true;
	}
	else {
		alert (theMessage);
		return false;
	}
}

/**
 * rotateBooks
 *
 * Makes a rotation to the right between selects
 */
function rotateBooks () {
  var form = $('updateForm');
  var element;
  
  for (var i = 1; i <= 5; i++) {
  	src = form.elements['sel' + i];
  	dest = form.elements['sel' + (i - 1)];
  	
  	for (var j = 0; j < src.options.length; j++) {
      dest.options[dest.length] = new Option(src.options[j].text, src.options[j].value);
      src.options[j] = null;
      --j;
  	}
  }
}

/**
 * generateHiddenList
 *
 * Dumps the content of <ul> into a hiddent field to submit through POST
 */
function generateHiddenList () {
	document.forms["hotForm"].sortlist.value = Sortable.serialize('list');
}

/**
 * double_list_move + double_list_submit
 *
 * Copia de las funciones por defecto de Symfony
 */
function custom_double_list_move(src, dest)
{
  for (var i = 0; i < src.options.length; i++)
  {
    if (src.options[i].selected)
    {
      dest.options[dest.length] = new Option(src.options[i].text, src.options[i].value);
      src.options[i] = null;
      --i;
    }
  }
}

function custom_double_list_submit(form_name)
{
  // default id to allow using a custom form id
  if( ! form_name ) {
    var form_name = 'sf_admin_edit_form';
  }

  var form = $(form_name);
  var element;

  // find multiple selects with name beginning 'associated_' and select all their options
  for (var i = 0; i < form.elements.length; i++)
  {
    element = form.elements[i];

    if (element.type == 'select-multiple')
    {
      if (element.className == 'update_select')
      {
        for (var j = 0; j < element.options.length; j++)
        {
          element.options[j].selected = true;
        }
      }
    }
  }
}

