/*
  $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;
}

/*function img_zoom(){
	var images = document.images;
	for(i=0;i<images.length;i++){
		if(typeof(images[i]) == 'object'){
			attributs = images[i].attributes;
			for(j=0;j<attributs.length;j++){
				if(attributs[j].nodeName == 'width' && attributs[j].nodeValue > 10){
					//images[i].onmouseover = function(){};
					//document.getElementById('infos').innerHTML = images[i].id = 'test';
					//images[i].setAttributeNode('id');
					images[i].setAttribute('id', 'img'+i+j);
					images[i].onmouseover = function(){ show(this.id, true); }
					images[i].onmouseout = function(){ show(this.id, false); }
				}
			}
		}
	}
}*/

/**
* affichage du zoom
* id: id de la balise à afficher dans le zoom
* display: true si afficher, false si cacher
**/
function show(id, display){
	var zoom = document.getElementById('zoom');
	
	/*zoom.onclick = function(){
		disparition('zoom', 100, 0, 10);
	}*/
	
	var img = document.getElementById(id);
	//var txt = document.getElementById('span'+id);
	//zoom.innerHTML = '<img src="'+img.src+'" alt="zoom" style="background:url('+img.src+');" />';
	zoom.src = img.src;
	//if(!zoom.style.opacity || zoom.style.opacity <= 0) apparition('zoom', 0, 100, 10);
	if(display){
		if(zoom.dis) clearTimeout(zoom.dis);
		apparition('zoom', 0, 100, 50);
	}
	else{
		if(zoom.app) clearTimeout(zoom.app);
		disparition('zoom', 100, 0, 50);
	}
	//centrer('zoom');
}

/**
* affiche un objet
* id: l'id de la balise
* op_min: opacité de départ en %
* op_max: opacité d'arrivé en %
* variation: vitesse d'affichage
**/
function apparition (id, op_min, op_max, variation) {
	el = document.getElementById(id);
	
	Nom_Browser = navigator.appName;
	Version_Browser = navigator.appVersion;
	if(Nom_Browser == "Netscape") {
		el.variation = variation / 100;
		el.op_max = op_max / 100;
		el.op_min = op_min / 100;
	}
	else{
		el.op_max = op_max;
		el.op_min = op_min;
		el.variation = variation;
	}
	
	if(el.op_min < el.op_max){
		if(el.style.display == 'none') el.style.display = 'block';
		
		el.op_min = el.op_min + el.variation;
		if(el.op_min > el.op_max) el.op_min = el.op_max;
		
		el.style.opacity = el.op_min;
		el.style.filter = 'alpha(opacity='+el.op_min+')';
		
		if(Nom_Browser == "Netscape"){
			el.op_min = el.op_min * 100;
			el.op_max = el.op_max * 100;
		}
		
		el.app = setTimeout('apparition("'+id+'", '+el.op_min+', '+el.op_max+', '+variation+')',30);
	}
}

/**
* cache un objet
* id: l'id de la balise
* op_min: opacité de départ en %
* op_max: opacité d'arrivé en %
* variation: vitesse
**/
function disparition (id, op_max, op_min, variation) {
	el = document.getElementById(id);
	
	Nom_Browser = navigator.appName;
	Version_Browser = navigator.appVersion;
	if(Nom_Browser == "Netscape"){
		el.variation = variation / 100;
		el.op_min = op_min / 100;
		el.op_max = op_max / 100;
	}
	else{
		el.op_max = op_max;
		el.op_min = op_min;
		el.variation = variation;
	}
	
	if(el.op_max > el.op_min){
		el.op_max = el.op_max - el.variation;
		if(el.op_max < el.op_min) el.op_max = el.op_min;
		
		el.style.opacity = el.op_max;
		el.style.filter = 'alpha(opacity='+el.op_max+')';
		
		if(Nom_Browser == "Netscape"){
			el.op_min = el.op_min * 100;
			el.op_max = el.op_max * 100;
		}
		
		el.dis = setTimeout('disparition("'+id+'", '+el.op_max+', '+el.op_min+', '+variation+')',30);
	}
	else if(el.op_min <= 0) el.style.display = 'none';
}

function GetId(id){
	return document.getElementById(id);
}

function debug(txt){
	obj = GetId('infos');
	obj.innerHTML = txt;
}

function getScreenSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  /*window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );*/
  return [ myWidth, myHeight ];
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
 
/**
* fais coller le zoom au curseur
**/
function move(e) {
	var w = GetId("zoom").offsetWidth;
	var h = GetId("zoom").offsetHeight;
	
	// pointeur
	var x = 10; //décalage X par rapport au pointeur
	var y = -(h/2); //décalage Y par rapport au pointeur
	
	// écran
	var h_header = 140; //marge top
	var w_screen = getScreenSize()[0];
	var h_screen = getScreenSize()[1];
	var h_scroll = getScrollXY()[1];
	
	if (navigator.appName!="Microsoft Internet Explorer") {
		var y_pos = e.pageY;
		var x_pos = e.pageX;
		
		//calcul du placement de l'axe x
		var x_tmp = x_pos + w - w_screen - x;
		
		if(x_tmp > 0) x_marge = x_pos - w - x;
		else x_marge = x_pos + x;
		
		
		//calcul du placement de l'axe y
		h_header = h_header - h_scroll;
		if(h_header < 0) h_header = 0;
		
		if( (y_pos + h/2) > (h_screen + h_scroll) ) y_marge = h_scroll + h_screen - h; //trop bas
		else if( (y_pos - h/2) < (h_header + h_scroll)) y_marge = h_header + h_scroll; //trop haut
		else y_marge = y_pos + y;
	}
	else {
		var y_pos = event.y;
		var x_pos = event.x;
		
		//calcul du placement de l'axe x
		var x_tmp = x_pos + w - w_screen - x;
		
		if(x_tmp > 0) x_marge = x_pos - w - x;
		else x_marge = x_pos + x;
				
		
		//calcul du placement de l'axe y
		if(y_pos + h/2 > h_screen) y_marge = y_pos + h_scroll + y + (h_screen-(y_pos+h/2)); //position trop basse
		else if(y_pos - h/2 - h_header < 0 && h_scroll <= 0) y_marge = h_scroll + h_header; //position trop haute par rapport au haut de la page
		else if(y_pos - h/2 < (h_header - h_scroll) && h_scroll <= h_header) y_marge = h_scroll + (h_header - h_scroll); //position trop haute par rapport au haut de la page avec un bout du header présent
		else if(y_pos - h/2 < 0) y_marge = h_scroll; //position trop haute lors d'un scroll
		else y_marge = y_pos + h_scroll + y; //position normal
	}
			
	GetId("zoom").style.left = x_marge +"px";
	GetId("zoom").style.top = y_marge +"px";
}
document.onmousemove=move;
