/*
  $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 update_qte_services(name)
{
	for (var i = 0; i < document.period_search.elements.length; i++) {
	  if(document.period_search.elements[i].name != name) 
      {document.period_search.elements[i].checked = false;}
      else
      {document.period_search.elements[i].checked = true;}
  }
}

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

  TargetElement.select();
}

function openurl(url)
	{		
		window.open(url,"Details","scrollbars=yes,resizable=yes,width=800,height="+screen.availHeight,screenX=parseInt((parseInt(screen.availWidth)-800)/2)+",screenY=0,top=0,left="+parseInt((parseInt(screen.availWidth)-800)/2)+"");
	}	

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 MM_showHideLayers() { //v3.0 actif, début, fin
  var i,p,v,a,d,f,j,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=4)
  	{
  	ad=args[i];
	af=args[i+1];
	d=args[i+2];
	f=args[i+3];
	for (j=d; j<=f;j++)
	   {
	   if ((j>=ad)&&(j<=af)) v='show'; else v='hide';
	   jt = 'calque_' + j;
	   if ( (window.jt != "undefined") && ((obj=MM_findObj(jt)) !=null))
    	  {
	   	  if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    	  obj.visibility=v;
		  }
	   }
	}
}


function showhide(id,id2,id3)
	{
	if (document.getElementById(id).style.display=='block')
	document.getElementById(id).style.display='none';
	else
	document.getElementById(id).style.display='block';
	if (document.getElementById(id2).style.display=='block')
	document.getElementById(id2).style.display='none';
	else
	document.getElementById(id2).style.display='block';
	if (document.getElementById(id3).style.display=='block')
	document.getElementById(id3).style.display='none';
	else
	document.getElementById(id3).style.display='block';
	}

	function add_criteres_search()
		{
		agent.call('','add_criteres_search','add_criteres_search_return', 
						document.add_criteres.add_criteres_id.value,
						document.add_criteres.id.value,
						document.add_criteres.type.value,
						document.add_criteres.priority.value,
						document.add_criteres.search_id.value 
					);
		
		document.add_criteres.add_criteres_id.value = "";
		}
	
	function add_criteres_search_return(str)
		{
		var dih = new DomInnerHtml();
		dih.write(str,false,'search_criteres');
		}
	
	

function DomInnerHtml() { }

DomInnerHtml.prototype.init = function() {
    this.position = [0];
    this.depth = 0;
    this.htmlTree = this.createElement('div');
};

// copied from a function which was I think originally written by Simon Willison (http://simon.incutio.com) - I could be wrong though...
DomInnerHtml.prototype.createElement = function(element) {
    if (document.createElementNS) {
        return document.createElementNS('http://www.w3.org/1999/xhtml', element);
    }
    if (document.createElement) {
        return document.createElement(element);
    }
    return false;
};

DomInnerHtml.prototype.clean = function(obj) {
    while (obj.hasChildNodes()) {
        obj.removeChild(obj.firstChild);
    }
};

DomInnerHtml.prototype.walk = function(node) {
    ++this.depth;
    if (this.position[this.depth] == undefined) {
        this.position[this.depth] = 0;
    }

    if (node.nodeType == 1) {
        var el = this.createElement(node.nodeName);
        for (var i = 0; i < node.attributes.length; ++i) {
            el.setAttribute(node.attributes[i].name, node.attributes[i].value);
        }
    } else if (node.nodeType == 3) {
        var el = document.createTextNode(node.nodeValue);
    }

    this.buildDomTree(el);

    if (node.hasChildNodes()) {
        for (var i = 0; i < node.childNodes.length; ++i) {
            this.walk(node.childNodes[i]);
        }
    } 

    ++this.position[this.depth];
    this.position.splice(this.depth + 1, 1);
    --this.depth;
};

DomInnerHtml.prototype.buildDomTree = function(el) {
    var node = this.htmlTree;
    for (var i = 1; i < this.position.length - 1; ++i) {
        node = node.firstChild;
        for (var j = 0; j < this.position[i]; ++j) {
            node = node.nextSibling;
        }      
    }
    node.appendChild(el);
};

DomInnerHtml.prototype.write = function(str, replace, target) {
    this.init();

    if (typeof target == 'string') {
        var target = document.getElementById(target);
    } else if (target == undefined) {
        var target = document.getElementsByTagName('body')[0];
    }

    if (!target) {
        return false;
    }

    if (window.DOMParser) {
        try {
            var dom = (new DOMParser()).parseFromString('<div>' + str + '</div>', 'text/xml');
            if (dom.firstChild.nodeName == 'parsererror') {
			if (replace) {
            target.innerHTML = str;
        } else {
            target.innerHTML += str;
        }
			return false;
            }
			
        } catch(e) {
		return false;
        }

        this.walk(dom.firstChild);

        if (replace) {
            this.clean(target);
        }
        
        var i = this.htmlTree.firstChild.childNodes.length;
        var tmp = [];
        while (i--) {
            tmp.push(this.htmlTree.firstChild.childNodes[i]);
        }

        var i = tmp.length;
        while (i--) {
            target.appendChild(tmp[i]);
        }

    } else {
        if (replace) {
            target.innerHTML = str;
        } else {
            target.innerHTML += str;
        }
    }
    return true;
};