/*
  $Id: general.js 1498 2007-03-29 14:04:50Z hpdl $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2005 osCommerce

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License v2 (1991)
  as published by the Free Software Foundation.
*/

function updateDatePullDownMenu(objForm, fieldName) {
  var pdmDays = fieldName + "_days";
  var pdmMonths = fieldName + "_months";
  var pdmYears = fieldName + "_years";

  time = new Date(objForm[pdmYears].options[objForm[pdmYears].selectedIndex].text, objForm[pdmMonths].options[objForm[pdmMonths].selectedIndex].value, 1);

  time = new Date(time - 86400000);

  var selectedDay = objForm[pdmDays].options[objForm[pdmDays].selectedIndex].text;
  var daysInMonth = time.getDate();

  for (var i=0; i<objForm[pdmDays].length; i++) {
    objForm[pdmDays].options[0] = null;
  }

  for (var i=0; i<daysInMonth; i++) {
    objForm[pdmDays].options[i] = new Option(i+1);
  }

  if (selectedDay <= daysInMonth) {
    objForm[pdmDays].options[selectedDay-1].selected = true;
  } else {
    objForm[pdmDays].options[daysInMonth-1].selected = true;
  }
}

function rowOverEffect(object) {
  if (object.className == 'moduleRow') object.className = 'moduleRowOver';
}

function rowOutEffect(object) {
  if (object.className == 'moduleRowOver') object.className = 'moduleRow';
}

function checkBox(object) {
  document.account_newsletter.elements[object].checked = !document.account_newsletter.elements[object].checked;
}

function popupWindow(url, name, params) {
  window.open(url, name, params).focus();
}

function change_price() {
		/**
		 * @declaration
		 */
		var id_attributes;
		var price_value;
		var reg;
		var tableau_price;
		var price_intial_modifier = price_initial;
		var price_display = document.getElementById('price');
		var modif_price = false;
		
		// On recupere les id des attributs
		var reg_options=new RegExp("[,]+", "g");
		if (document.getElementById('list_options')) {
			var list_options = document.getElementById('list_options').value.split(reg_options);
			
			for (var i = 0; i < list_options.length; ++i) {
				// On recupere les valeurs pour chaque choix
				id_attributes = 'attributesprice_' + document.getElementById("attributes[" + list_options[i] + "]").value;
				if (document.getElementById(id_attributes)) {
					modif_price = true;
					price_value = document.getElementById(id_attributes).value;
					reg = new RegExp("[_]+", "g");
					tableau_price = price_value.split(reg);
					
					if (tableau_price[1] == '+') 
						price_intial_modifier = parseFloat(price_intial_modifier) + parseFloat(tableau_price[0]);
					if (tableau_price[1] == '-') 
						price_intial_modifier = parseFloat(price_intial_modifier) - parseFloat(tableau_price[0]);
				}
				
			}
			if (Math.round(price_intial_modifier) == price_intial_modifier && modif_price) price_intial_modifier = price_intial_modifier + ".00";
			price_display.innerHTML = currency + price_intial_modifier;
		}
	}
	
function verif_stock() {
	// On recupere la liste des attributs selectionner
	var product_id = document.getElementById('products_id').value;
	var reg_options=new RegExp("[,]+", "g");
	var id_attributes = '';
	var list_attributs_stock = false;
	if (document.getElementById('list_options')) {
		var list_options = document.getElementById('list_options').value.split(reg_options);
		
		for (var i = 0; i < list_options.length; ++i) {
			// On recupere les valeurs pour chaque choix
			if (document.getElementById("attributes[" + list_options[i] + "]").value != '0') {
				list_attributs_stock = true;
				if (id_attributes != "") id_attributes += "__";
				id_attributes += 'attributesstock_' + list_options[i] + '_' + document.getElementById("attributes[" + list_options[i] + "]").value;
			}			
		}
		
	}
	
	if (list_attributs_stock) {
		new Ajax.Request("rpc.php?action=products_stocks&pID=" + product_id + "&attributs=" + id_attributes, { onSuccess: handleHttpVerifStock });
	}
	else {
		if (document.getElementById('products_qty').value == 0) {
			document.getElementById('icone_stock').src = 'images/icons/banner_stock_orange.png';
			document.getElementById('text_stock').innerHTML = products_stock_reappro;
		}
		else {
			document.getElementById('icone_stock').src = 'images/icons/banner_stock_green.png';
			document.getElementById('text_stock').innerHTML = products_stock_ok;
		}
	}
		

}

function handleHttpVerifStock(http) {
    var result = /\[\[([^|]*?)(?:\|([^|]*?)){0,1}\]\]/.exec(http.responseText);
    result.shift();
	 
    if (result[0] == '1') {
		if (result[1] == 0) {
			document.getElementById('icone_stock').src = 'images/icons/banner_stock_orange.png';
			document.getElementById('text_stock').innerHTML = products_stock_reappro;
		}
		else {
			document.getElementById('icone_stock').src = 'images/icons/banner_stock_green.png';
			document.getElementById('text_stock').innerHTML = products_stock_ok;
		}
	}
	else {
		document.getElementById('icone_stock').src = 'images/icons/banner_stock_red.png';
		document.getElementById('text_stock').innerHTML = products_stock_rupture;
	}
	
}

