/*
  $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;
}
//********************* fonctions de rollover *********************
// The following functions were pulled from lib.js 
// by Caio Chassot (http://v2studio.com/k/code/)
Array.prototype.find = function(value, start) {
	start = start || 0;
	for (var i=start; i<this.length; i++) 
		if (this[i]==value)
			return i;
	return false; 
}

Array.prototype.count = function(value) {
	var pos, start = 0, count = 0;
	while ((pos = this.find(value, start))!==false) {
		start = pos + 1;
		count++;
	}
	return count;
}

function isUndefined(v) { 
	var undef;
	return v===undef;
}

function undef(v) {  return  isUndefined(v) }
function isdef(v) {  return !isUndefined(v) }

function map(list, fn) {
	if (typeof(fn)=='string') return map(list, __strfn('item,idx,list', fn));

	var result = [];
	fn = fn || function(v) {return v};
	for (var i=0; i < list.length; i++) result.push(fn(list[i], i, list)); 
	return result;
}

function filter(list, fn) { 
	if (typeof(fn)=='string') return filter(list, __strfn('item,idx,list', fn));

	var result = [];
	fn = fn || function(v) {return v};
	map(list, function(item,idx,list) { if (fn(item,idx,list)) result.push(item) } );
	return result;
}

String.prototype.insert = function(idx,value) { 
	return this.slice(0,idx) + value + this.slice(idx);
}

function getElem(elem) { 
	if (document.getElementById) {
		if (typeof elem == "string") {
			elem = document.getElementById(elem);
			if (elem===null) throw 'cannot get element: element does not exist';
		} else if (typeof elem != "object") {
			throw 'cannot get element: invalid datatype';
		}
	} else throw 'cannot get element: unsupported DOM';
	return elem;
}

function getElementsByClass(className, tagName, parentNode) { 
	parentNode = isdef(parentNode)? getElem(parentNode) : document;
	if (undef(tagName)) tagName = '*';
	return filter(parentNode.getElementsByTagName(tagName), 
		function(elem) { return hasClass(elem, className) });
}

function hasClass(elem, className) { 
	return getElem(elem).className.split(' ').count(className);
}

// ATTACH ROLLOVERS
// This function loops through every image and input tag
// looking for the class "rollover", if found it then preloads
// the rollover image and attaches mouse events for the rollover
// by Tim Walling

// User Defined Variables
var erClassName = "rollover";
var erImageName = "_over";

function newImageName(oldName) {
	var newName = oldName.insert (oldName.length-4,erImageName);
	return newName;
}

function oldImageName(curName) {
	var re = new RegExp (erImageName, 'gi') ;
	var oldName = curName.replace(re, '') ;
	return oldName;
}

function attachRollovers(){
	var btnsTemp = new Array();
	var origSrc = new Array();
	var newSrc = new Array();
	var btnsImage = getElementsByClass(erClassName,'img'); // for images
	var btnsInput = getElementsByClass(erClassName,'input'); // for inputs
	var btns = btnsImage.concat(btnsInput); // combine into one array
	
	for (var i=0; i<btns.length; i++){
		origSrc[i] = btns[i].src;
		newSrc[i] = newImageName(origSrc[i]);
		btnsTemp[i] = new Image();
        btnsTemp[i].src = newSrc[i];
        btns[i].onmouseover = function() {
			var newSrcString = newImageName(this.src);
            this.setAttribute('src',newSrcString)
        }
        btns[i].onmouseout = function() {
			var oldSrcString = oldImageName(this.src);
            this.setAttribute('src',oldSrcString)
        }
	}
}
window.onload = attachRollovers;

