/*
  $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;
}

	var bLoaded = false;
	var bSubHold = false;
	var currSub = '';
	var isIE = navigator.appName.indexOf('Microsoft')>-1 && navigator.userAgent.indexOf('Opera')==-1 && navigator.platform == 'Win32' ? true : false;
	
	function newWin( width, height, url, title, bCentered, x, y )
	{
		newWin2=null;
		var Options = '';

		var strX = x.toString();
		var strY = y.toString();

		if ( bCentered )
		{
			strY = (screen.availHeight/2 - height/2 ).toString();
			strX = (screen.availWidth/2 - width/2 ).toString();
		}

		Options += "toolbar=no,width=" + width + ",height=" + height + ",left=" + strX;
		Options += ",top=" + strY + ",screenX=" + strX + ",screenY=" + strY +",resizable=yes,scrollbars=yes"

		newWin2 = window.open( url, title, Options );

		newWin2.focus();
		return newWin2;
	}
	
	function showSubMenu(elem,evt) {
		var sub;
		var ifr;
		var subBot;
		var bot;
		
		hideCurrSubMenu();
		tophideCurrSubMenu();
		elem.style.color = "#8F413D";		
		currSub = elem;
		
		if (navigator.userAgent.indexOf('MSIE 5.0')>-1) return;
		if (navigator.userAgent.indexOf('Safari/85')>-1) return;

		sub = document.getElementById('sub' + elem.id.slice(3));	
		if(isIE)ifr = document.getElementById('ifr');		

			if(navigator.platform == 'Win32') {
				if(elem.offsetParent && (elem.offsetParent != '[object HTMLDivElement]')) {
					sub.style.top = elem.offsetParent.offsetTop + 64 - elem.offsetParent.scrollTop;
					if(isIE)ifr.style.top = elem.offsetParent.offsetTop + 64 - elem.offsetParent.scrollTop;
				}
				else {
					sub.style.top = parseInt(evt.clientY)-10;
					if(isIE)ifr.style.top = parseInt(evt.clientY)-10;
				}
			}
			else {
				sub.style.top = elem.offsetTop + 64 - elem.scrollTop - 2;
				if(isIE)ifr.style.top = elem.offsetTop + 64 - elem.scrollTop - 2;
				sub.style.width = 350;
				if(isIE)ifr.style.width = 350;
			}
		
		if(isIE)ifr.style.width = sub.offsetWidth;
		if(isIE)ifr.style.height = sub.offsetHeight;

		subBot = parseInt(sub.style.top) + sub.offsetHeight
		bot = document.getElementById('bottombar')
			
		if(bot && subBot) {
			if(subBot > bot.offsetHeight + parseInt(bot.style.top)) {
				sub.style.top = bot.offsetHeight + parseInt(bot.style.top) - sub.offsetHeight;
				if(isIE)ifr.style.top = bot.offsetHeight + parseInt(bot.style.top) - sub.offsetHeight;
			}
			if(subBot > document.body.clientHeight) {
				sub.style.top = document.body.clientHeight - sub.offsetHeight;
				if(isIE)ifr.style.top = document.body.clientHeight - sub.offsetHeight;
			}
		}
			
		sub.style.visibility = 'visible';
		if(isIE)ifr.style.visibility = 'visible';
	
	}
	
	function hideSubMenu(elem) {
		if(currSub!=elem) {
			var sub = document.getElementById('sub' + elem.id.slice(3))
			var ifr = document.getElementById('ifr')
			if(currSub!='')currSub.style.color = "#000099";
			sub.style.visibility = 'hidden';
			if(isIE)ifr.style.visibility = 'hidden';
			currSub = '';
		}
	}
	function highlightCat(elem) {
		elem.style.color = "#8F413D";
		hideCurrSubMenu();
	}
	function lowlightCat(elem) {
		elem.style.color = "#000099";
	}
	function hideCurrSubMenu() {
		if(currSub!='') {
			currSub.style.color = "#000099";
			var sub = document.getElementById('sub' + currSub.id.slice(3))
			var ifr = document.getElementById('ifr')
			sub.style.visibility = 'hidden';
			if(isIE)ifr.style.visibility = 'hidden';
			currSub = '';
		}
	}
	function checkSubStatus() {
		if(!bSubHold && currSub!='') {
			document.getElementById('sub' + currSub.id.slice(3)).style.visibility = 'hidden';
			document.getElementById('ifr').style.visibility = 'hidden';
			currSub.style.color = "#000099";
		}
		setTimeout('checkSubStatus()', 1000);
	}

	var btopSubHold = false;
	var topcurrSub = '';
	
	function topshowSubMenu(elem,evt) {
		var sub;

		tophideCurrSubMenu();
		hideCurrSubMenu();
		elem.style.color = "#934E49";
		sub = document.getElementById('topsub' + elem.id.slice(3));

		if(navigator.platform == 'Win32') {
			if(elem.offsetParent && (elem.offsetParent != '[object HTMLDivElement]')) {
				sub.style.left = elem.offsetParent.offsetLeft + 129 - elem.offsetParent.scrollLeft;
			}
			else sub.style.left = parseInt(evt.clientX)-10;
		}
		else {
			sub.style.left = parseInt(evt.clientX)-10;
			sub.style.width = 350;
		}

		sub.style.visibility = 'visible';

		topcurrSub = elem;
	}
	function tophideSubMenu(elem) {
		if(topcurrSub!=elem) {
			var sub = document.getElementById('topsub' + elem.id.slice(3))
			if(topcurrSub!='')topcurrSub.style.color = "#FFFFFF";
			sub.style.visibility = 'hidden';
			topcurrSub = '';
		}
	}
	function tophighlightCat(elem) {
		elem.style.color = "#934E49";
		tophideCurrSubMenu();
	}
	function toplowlightCat(elem) {
		elem.style.color = "#FFFFFF";
	}
	function tophideCurrSubMenu() {
		if(topcurrSub!='') {
			topcurrSub.style.color = "#FFFFFF";
			var sub = document.getElementById('topsub' + topcurrSub.id.slice(3))
			sub.style.visibility = 'hidden';
			topcurrSub = '';
		}
	}
	function topcheckSubStatus() {
		if(!btopSubHold && topcurrSub!='') {
			document.getElementById('topsub' + topcurrSub.id.slice(3)).style.visibility = 'hidden';
			topcurrSub.style.color = "#FFFFFF";
		}
		setTimeout('topcheckSubStatus()', 1000);
	}