/*
  $Id: general.js,v 1.3 2003/02/10 22:30:55 hpdl Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

function SetFocus(TargetFormName) {
  var target = 0;
  if (TargetFormName != "") {
    for (i=0; i<document.forms.length; i++) {
      if (document.forms[i].name == TargetFormName) {
        target = i;
        break;
      }
    }
  }

  var TargetForm = document.forms[target];
    
  for (i=0; i<TargetForm.length; i++) {
    if ( (TargetForm.elements[i].type != "image") && (TargetForm.elements[i].type != "hidden") && (TargetForm.elements[i].type != "reset") && (TargetForm.elements[i].type != "submit") ) {
      TargetForm.elements[i].focus();

      if ( (TargetForm.elements[i].type == "text") || (TargetForm.elements[i].type == "password") ) {
        TargetForm.elements[i].select();
      }

      break;
    }
  }
}

function RemoveFormatString(TargetElement, FormatString) {
  if (TargetElement.value == FormatString) {
    TargetElement.value = "";
  }

  TargetElement.select();
}

function CheckDateRange(from, to) {
  if (Date.parse(from.value) <= Date.parse(to.value)) {
    return true;
  } else {
    return false;
  }
}

function IsValidDate(DateToCheck, FormatString) {
  var strDateToCheck;
  var strDateToCheckArray;
  var strFormatArray;
  var strFormatString;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var intDateSeparatorIdx = -1;
  var intFormatSeparatorIdx = -1;
  var strSeparatorArray = new Array("-"," ","/",".");
  var strMonthArray = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
  var intDaysArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  strDateToCheck = DateToCheck.toLowerCase();
  strFormatString = FormatString.toLowerCase();
  
  if (strDateToCheck.length != strFormatString.length) {
    return false;
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strFormatString.indexOf(strSeparatorArray[i]) != -1) {
      intFormatSeparatorIdx = i;
      break;
    }
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strDateToCheck.indexOf(strSeparatorArray[i]) != -1) {
      intDateSeparatorIdx = i;
      break;
    }
  }

  if (intDateSeparatorIdx != intFormatSeparatorIdx) {
    return false;
  }

  if (intDateSeparatorIdx != -1) {
    strFormatArray = strFormatString.split(strSeparatorArray[intFormatSeparatorIdx]);
    if (strFormatArray.length != 3) {
      return false;
    }

    strDateToCheckArray = strDateToCheck.split(strSeparatorArray[intDateSeparatorIdx]);
    if (strDateToCheckArray.length != 3) {
      return false;
    }

    for (i=0; i<strFormatArray.length; i++) {
      if (strFormatArray[i] == 'mm' || strFormatArray[i] == 'mmm') {
        strMonth = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'dd') {
        strDay = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'yyyy') {
        strYear = strDateToCheckArray[i];
      }
    }
  } else {
    if (FormatString.length > 7) {
      if (strFormatString.indexOf('mmm') == -1) {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mm'), 2);
      } else {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mmm'), 3);
      }

      strDay = strDateToCheck.substring(strFormatString.indexOf('dd'), 2);
      strYear = strDateToCheck.substring(strFormatString.indexOf('yyyy'), 2);
    } else {
      return false;
    }
  }

  if (strYear.length != 4) {
    return false;
  }

  intday = parseInt(strDay, 10);
  if (isNaN(intday)) {
    return false;
  }
  if (intday < 1) {
    return false;
  }

  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) {
    for (i=0; i<strMonthArray.length; i++) {
      if (strMonth == strMonthArray[i]) {
        intMonth = i+1;
        break;
      }
    }
    if (isNaN(intMonth)) {
      return false;
    }
  }
  if (intMonth > 12 || intMonth < 1) {
    return false;
  }

  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    return false;
  }
  if (IsLeapYear(intYear) == true) {
    intDaysArray[1] = 29;
  }

  if (intday > intDaysArray[intMonth - 1]) {
    return false;
  }
  
  return true;
}

function IsLeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0) {
      return true;
    }
  } else {
    if ((intYear % 4) == 0) {
      return true;
    }
  }

  return false;
}

//############### AJAX HTTPHIDDEN ###############
function httpHidden(filename,data,functionWait,functionName,method,asynchrone,xmlAnswer) {
	var xhr_object 	= null;

	if(window.XMLHttpRequest) // Firefox   
		xhr_object = new XMLHttpRequest();   
	else if(window.ActiveXObject) // Internet Explorer   
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");   
	else { // XMLHttpRequest non supporté par le navigateur   
		alert("Your navigator is not XMLHttpRequest capable");   
		return;
	}   	
	
	if(method == "GET" && data != null) {   
		filename += "?"+data;   
		data      = null;   
	}  
	
	xhr_object.open(method, filename, asynchrone);   
	
	xhr_object.onreadystatechange = function() {   
		if(xhr_object.readyState == 4) {
			if (xhr_object.status == 200) {
				if(xmlAnswer==0) var received = xhr_object.responseText;
				else var received = xhr_object.responseXML;
				if(functionName !="") eval(functionName);
				//alert(received) ;
			}
			else{
				if(xhr_object.status != 0) {
					//alert("Une erreur est survenue.\nCode de l'erreur : "+xhr_object.status);
				}
			}
		}
		else {
			if(functionWait != "") eval(functionWait);
		}			
	}
	if(method == "POST") xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");   
	xhr_object.send(data);
}
//############### AJAX HTTPHIDDEN ###############
/**
 * Rechargement d'une page en JS
 * @param URL
 * @return NULL
 */
function goToProduct(received) {
	document.location = received ;
}

/**
 * Chargement dynamique de les marque en fonction des sites
 * @param id_template
 * @return
 */
function loadProduct(id_template) {
	
}

/**
 * 
 * @param id_manufacturer
 * @return
 */
function loadManufacturer(id_manufacturer) {
	
}

/**
 * Ecrire dans une DIV
 * @param div_id
 * @param html
 */
function writeDiv(div_id,contenu) {
	 document.getElementById(div_id).innerHTML = contenu;
}

/**
 * 
 * @param type (0 = marque, 1=type, 2=couleur, 3=prix)
 * @param id_magasin
 * @param id_manufacturer
 * @param input
 * @return
 */
function checkMenu(type,id_magasin,id, input, oscsid, all) {
	
	if(oscsid == '') oscsid = 0 ;
	
	var nbMarque 				= document.getElementById('nbInputManufacturer').value ;
	var nbType	 				= document.getElementById('nbInputType').value ;
	var nbColor	 				= document.getElementById('nbInputColor').value ;	
	var nbPrice	 				= document.getElementById('nbInputPrix').value ;
	
	var nbOption 		  	= 0 ;
	var array_manufacturer	= new Array() ;
	var array_type			= new Array() ;
	var array_color			= new Array() ;	
	var array_prix			= new Array() ;	
	
	loading() ;
	
	if(all == "1") {
		allManufacturer = document.getElementById('nbManufacturer').value ;
		httpHidden("ajax_marques.php",'id_magasin='+id_magasin+'&manufacturer_id='+allManufacturer+"&type_id=&color_id=&price_id=&osCsid="+oscsid+"&sort=4d","loading();",'loadPage(received)','GET',true,false);
	}else{
		if(input == '-1') {
			if(type == 0) {
				if(document.getElementById('allManufacturer').value == '0') {
					document.getElementById('allManufacturer').value = '1' ;	
					document.getElementById('li_m_All').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce_cochee.gif")' ;	
					var allManufacturer = document.getElementById('allManufacturer').value ;
					array_manufacturer = document.getElementById('nbManufacturer').value ;					
					var aMarque = array_manufacturer.split('|');
					for(i=0;i<nbMarque;i++) {		
						document.getElementById('li_m_'+i+'').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce_cochee.gif")' ;	
						document.getElementById("li_m_i_"+i).value = aMarque[i] ;				
					}		
				}else{
					document.getElementById('allManufacturer').value = '0' ;
					document.getElementById('li_m_All').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce.gif")' ;						
					var allManufacturer = document.getElementById('allManufacturer').value ;
					array_manufacturer = '0' ;				
					var aMarque = array_manufacturer.split('|');
					for(i=0;i<nbMarque;i++) {		
						document.getElementById('li_m_'+i+'').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce.gif")' ;	
						document.getElementById("li_m_i_"+i).value = '0' ;				
					}	
				}
			}		
			if(type == 1) {
				if(document.getElementById('allType').value == '0') {
					document.getElementById('allType').value = '1' ;
					document.getElementById('li_t_All').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce_cochee.gif")' ;	
					var allType = document.getElementById('allType').value ;	
					array_type = document.getElementById('nbType').value ;						
					var aType = array_type.split('|');
					for(i=0;i<nbType;i++) {				
						document.getElementById('li_t_'+i+'').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce_cochee.gif")' ;	
						document.getElementById("li_t_i_"+i).value = aType[i] ;					
					}		
				}else{
					document.getElementById('allType').value = '0' ;	
					document.getElementById('li_t_All').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce.gif")' ;						
					var allType = document.getElementById('allType').value ;	
					array_type = '0' ;						
					var aType = array_type.split('|');
					for(i=0;i<nbType;i++) {				
						document.getElementById('li_t_'+i+'').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce.gif")' ;	
						document.getElementById("li_t_i_"+i).value = 0 ;					
					}						
				}
			}				
			if(type == 2) {
				if(document.getElementById('allColor').value == '0') {
					document.getElementById('allColor').value = '1' ;	
					
					var allColor = document.getElementById('allColor').value ;	
					array_color = document.getElementById('nbColor').value ;							
					var aColor = array_color.split('|');
					for(i=0;i<nbColor;i++) {				
						document.getElementById('li_c_'+i).style.borderColor = 'FFFFFF' ;
						document.getElementById("li_c_i_"+i).value = aColor[i] ;				
					}	
				}else{
					document.getElementById('allColor').value = '0' ;	
					
					var allColor = document.getElementById('allColor').value ;	
					array_color = '0' ;							
					var aColor = array_color.split('|');
					for(i=0;i<nbColor;i++) {				
						document.getElementById('li_c_'+i).style.borderColor = '545F73' ;
						document.getElementById("li_c_i_"+i).value = 0 ;				
					}						
				}
			}			
			if(type == 3) {
				if(document.getElementById('allPrix').value == '0') {
					document.getElementById('allPrix').value = '1' ;	
					document.getElementById('li_p_All').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce_cochee.gif")' ;	
					var allPrix = document.getElementById('allPrix').value ;
					array_prix = document.getElementById('nbPrix').value ;							
					var aPrix = array_prix.split('|');
					for(i=0;i<nbPrice;i++) {				
						document.getElementById('li_p_'+i+'').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce_cochee.gif")' ;	
						document.getElementById("li_p_i_"+i).value = aPrix[i] ;				
					}	
				}else{
					document.getElementById('allPrix').value = '0' ;	
					document.getElementById('li_p_All').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce.gif")' ;						
					var allPrix = document.getElementById('allPrix').value ;
					array_prix = '0' ;							
					var aPrix = array_prix.split('|');
					for(i=0;i<nbPrice;i++) {				
						document.getElementById('li_p_'+i+'').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce.gif")' ;	
						document.getElementById("li_p_i_"+i).value = '0' ;				
					}						
				}
			}

			for(i=0;i<nbMarque;i++) {
				if(document.getElementById("li_m_i_"+i).value != 0) {
					array_manufacturer += document.getElementById("li_m_i_"+i).value+'|' ;
					nbOption++ ;
				}
			}	
			for(i=0;i<nbType;i++) {		
				if(document.getElementById("li_t_i_"+i).value != 0) {
					array_type += document.getElementById("li_t_i_"+i).value+'|' ;
					nbOption++ ;
				}
			}		
			for(i=0;i<nbColor;i++) {		
				if(document.getElementById("li_c_i_"+i).value != 0) {
					array_color += document.getElementById("li_c_i_"+i).value+'|' ;
					nbOption++ ;
				}
			}		
			for(i=0;i<nbPrice;i++) {	
				if(document.getElementById("li_p_i_"+i).value != 0) {
					array_prix += document.getElementById("li_p_i_"+i).value+'|' ;
					nbOption++ ;
				}
			}			
			//alert(array_manufacturer) ;
			httpHidden("ajax_marques.php",'id_magasin='+id_magasin+'&manufacturer_id='+array_manufacturer+"&type_id="+array_type+'&color_id='+array_color+'&prix_id='+array_prix+"&osCsid="+oscsid,"",'loadPage(received)','GET',true,false);		
		
		}else{
			if(type == 0) {
				if(document.getElementById("li_m_i_"+input).value == 0) {
					document.getElementById('li_m_'+input+'').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce_cochee.gif")' ;	
					document.getElementById("li_m_i_"+input).value = id ;
				}else{
					document.getElementById('li_m_'+input+'').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce.gif")' ;		
					document.getElementById("li_m_i_"+input).value = 0 ;
				}
			}
			if(type == 1) {		
				if(document.getElementById("li_t_i_"+input).value == 0) {
					document.getElementById('li_t_'+input+'').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce_cochee.gif")' ;	
					document.getElementById("li_t_i_"+input).value = id ;
				}else{
					document.getElementById('li_t_'+input+'').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce.gif")' ;		
					document.getElementById("li_t_i_"+input).value = 0 ;
				}		
			}
			if(type == 2) {
				if(document.getElementById("li_c_i_"+input).value == 0) {
					document.getElementById('li_c_'+input).style.borderColor = 'FFFFFF' ;
					document.getElementById("li_c_i_"+input).value = id ;
				}else{
					document.getElementById('li_c_'+input).style.borderColor = '545F73' ;	
					document.getElementById("li_c_i_"+input).value = 0 ;
				}		
			}
			if(type == 3) {
				if(document.getElementById("li_p_i_"+input).value == 0) {
					document.getElementById('li_p_'+input+'').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce_cochee.gif")' ;	
					document.getElementById("li_p_i_"+input).value = id ;
				}else{
					document.getElementById('li_p_'+input+'').style.backgroundImage = 'url("images/css/tousenchemise/menu/menu_puce.gif")' ;		
					document.getElementById("li_p_i_"+input).value = 0 ;
				}		
			}	

			for(i=0;i<nbMarque;i++) {
				if(document.getElementById("li_m_i_"+i).value != 0) {
					array_manufacturer += document.getElementById("li_m_i_"+i).value+'|' ;
					nbOption++ ;
				}
			}	
			for(i=0;i<nbType;i++) {		
				if(document.getElementById("li_t_i_"+i).value != 0) {
					array_type += document.getElementById("li_t_i_"+i).value+'|' ;
					nbOption++ ;
				}
			}		
			for(i=0;i<nbColor;i++) {		
				if(document.getElementById("li_c_i_"+i).value != 0) {
					array_color += document.getElementById("li_c_i_"+i).value+'|' ;
					nbOption++ ;
				}
			}		
			for(i=0;i<nbPrice;i++) {	
				if(document.getElementById("li_p_i_"+i).value != 0) {
					array_prix += document.getElementById("li_p_i_"+i).value+'|' ;
					nbOption++ ;
				}
			}
			
			if(nbOption == 0) {
				checkMenu('0','2','0','0','','1');
				//httpHidden("ajax_load_page.php",'products_id=0&template_id='+id_magasin+"&osCsid="+oscsid,"",'goToProduct(received)','GET',true,false);
			}else{
				httpHidden("ajax_marques.php",'id_magasin='+id_magasin+'&manufacturer_id='+array_manufacturer+"&type_id="+array_type+'&color_id='+array_color+'&price_id='+array_prix+"&osCsid="+oscsid,"loading();",'loadPage(received)','GET',true,false);
			}		
			
		}	
	}
}

/**
 * Ecrire du div de chargement
 * @return
 */
function loading() {	
	writeDiv("pagePrincipal",'<div class="page_content"><div class="page_content_short" style="align:center; color:#2e5f85; text-align:center;"><div class="breadcrumb"></div><br><br><br><br><br><img src="images/ajax-loader1.gif"><br><br><b>Chargement de votre sélection</b></div></div>') ;	
}

/**
 * Ecriture du HTML dans le div
 * @param html
 * @return
 */
function loadPage(html) {
	writeDiv('pagePrincipal', html);
}

/**
 * Tri sur le listing produit
 * @param id_magasin
 * @param array_manufacturer
 * @param type (0 = prix, 1 = marque)  
 * @param pages
 * @return
 */
function sortBy(id_magasin,type,pages,array_manufacturer, array_type, array_color, array_price, oscsid) {
	if(pages > 0) {
		httpHidden("ajax_marques.php",'id_magasin='+id_magasin+'&manufacturer_id='+array_manufacturer+'&type_id='+array_type+'&color_id='+array_color+'&price_id='+array_price+'&page='+pages+"&osCsid="+oscsid,"",'loadPage(received)','GET',true,false);		
	}else{
		if(document.getElementById('tri_'+type).value == 'a') {
			sort = type+""+document.getElementById('tri_'+type).value ;
		}else{
			sort = type+""+document.getElementById('tri_'+type).value ;
		}
		httpHidden("ajax_marques.php",'id_magasin='+id_magasin+'&manufacturer_id='+array_manufacturer+'&type_id='+array_type+'&color_id='+array_color+'&price_id='+array_price+'&sort='+sort+"&osCsid="+oscsid,"",'loadPage(received)','GET',true,false);
	}
}

/**
 * Ajoute le site au favori
 * @return
 */
function favoris() {
      if ( navigator.appName != 'Microsoft Internet Explorer' ){
          window.sidebar.addPanel("[Moteur de Mode]","http://www.moteurdemode.com/","");
      }
      else {
          window.external.AddFavorite("http://www.moteurdemode.com/","[Moteur de Mode]");
      }
}

/**
 * Affichage des autre colour.
 * @return
 */
function showOtherColor() {	
	document.getElementById('libOtherColor').style.display = 'none' ;	
	if(document.getElementById('otherColor').style.display == 'none') {
		document.getElementById('otherColor').style.display = 'block' ;
	}else{
		document.getElementById('otherColor').style.display = 'none' ;
	}	
}

function stopCarousel() {
	//glob_carousel.stopAuto();
	//document.getElementById('productCaroussel').parentNode.removeChild(document.getElementById('productCaroussel'));
}
