/*
  $Id: general.js,v 1.1.1.1 2004/03/04 23:40:52 ccwjr 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;
}

// show pop-up image
// added by Yann _ 20090114 _ task 28081
function $_(o) {
	return (typeof o=='string') ? o=document.getElementById(o) : o;
}
function inputclean(_this, defval){
    if(_this.value == defval){ _this.value = '';}
    else if(_this.value == ''){ _this.value = defval;}
    return;
}
function truebody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

var tw = 0;
var th = 0;
var ttitle = '';
var cur = '';
function getinfoobj(){
	if (document.getElementById){ return $_("info").style;}
	else if (document.all){ return document.all.info.style;}
}
function hideinfo(){
	getinfoobj().visibility="hidden";
        document.onmousemove="";
        cur.title= ttitle;
        $_('tinfo').innerHTML = '';
        getinfoobj().left=-1000;
	getinfoobj().top=-1000;
}
function showinfo(tinfo){
        if(navigator.userAgent.toLowerCase().indexOf('opera') == -1){
                $_('tinfo').innerHTML = tinfo.title;
                cur = tinfo;
                ttitle  = tinfo.title;
                if(!ttitle || ttitle==' ') return;
                tinfo.title='';

                document.onmousemove=inf_followmouse;

                if ( !document.all ){
                  th = $_('info').offsetHeight;
                  tw = $_('info').offsetWidth;
                }else if( document.all ){
                  th = document.all['info'].offsetHeight;
                  tw = document.all['info'].offsetWidth;
                }
		getinfoobj().visibility="visible";
	}
}
function inf_followmouse(e){
	if(navigator.userAgent.toLowerCase().indexOf('opera') == -1){
                var xcoord=10;
		var ycoord=10;
                if (typeof e != "undefined"){
			xcoord+=e.pageX;
			ycoord+=e.pageY;
		}
		else if (typeof window.event !="undefined"){
			xcoord+=truebody().scrollLeft+event.clientX;
			ycoord+=truebody().scrollTop+event.clientY;
		}
                ycoord = ycoord-th-10;

                var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15;
		var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight);

                if (ycoord-truebody().scrollTop<th) ycoord=ycoord+th+10;//23

		getinfoobj().left=xcoord+"px";
		getinfoobj().top=ycoord+"px";
	}
}


var ttw= 0;
var tth= 0;

function getthumbobj(){
	if (document.getElementById){ return $_("thumb").style;}
	else if (document.all){ return document.all.thumb.style;}
}

function hideproduct(){
	getthumbobj().display="none";
    getthumbobj().visibility="hidden";
    document.onmousemove="";
    $_('ttimg').src='../../images/blank.gif';
	$_('ttdesc').innerHTML = '';

	getthumbobj().left=-1000;
	getthumbobj().top=-1000;
	window.playslide = true;
}

function showproduct(tt, _src){
	window.playslide = false;
	if(navigator.userAgent.toLowerCase().indexOf('opera') == -1){
		getthumbobj().display="block" ;
		
		
		if(_src=='undefined' || !_src) _src = $_(tt).src;
		$_('ttimg').src= _src;//tt.href
		$_('ttdesc').innerHTML = "<br />" + $_(tt).alt;//tt.title;
		
		document.onmousemove = th_followmouse;
		//getthumbobj().width=ttw+"px";
		
		tth = 0;
		//if ( !document.all ){ tth = $_('thumb').offsetHeight;}
		//else if( document.all ){ tth = document.all['thumb'].offsetHeight;}
		
		getthumbobj().visibility="visible";
	}
}

function th_followmouse(e){
	if(navigator.userAgent.toLowerCase().indexOf('opera') == -1){
		var xcoord = 20;
		var ycoord = 0;
		var topScreenBorder = truebody().scrollTop;
		var MSIE = navigator.userAgent.toLowerCase().indexOf('msie');
		var NETS = navigator.userAgent.toLowerCase().indexOf('netscape');
		var acturalClientY = MSIE>0 ? event.clientY : e.clientY;

        if (typeof e != "undefined"){
			xcoord+=e.pageX;
			ycoord+=e.pageY;
		}
		else if (typeof window.event !="undefined"){
			xcoord+=truebody().scrollLeft+event.clientX;
			ycoord+=truebody().scrollTop+event.clientY;
		}

        var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth;
		var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight);

		// adjust the position of the pop up image
        if (xcoord + $_("thumb").scrollWidth > docwidth){ xcoord=xcoord-$_("thumb").scrollWidth-30;}

		if (truebody().scrollTop < 180) topScreenBorder = 180;  // avoid the flash banner

		if ($_("thumb").scrollHeight > window.innerHeight) {  // for the over full screen image
			if ((acturalClientY - truebody().scrollTop) > (window.innerHeight / 2) ) {
				ycoord = topScreenBorder + window.innerHeight - $_("thumb").scrollHeight;
			} else {
				ycoord = topScreenBorder;
			}
		} else if ($_("thumb").scrollHeight > 250) {  // for the big image but not the full screen
			if ((acturalClientY - truebody().scrollTop) > (window.innerHeight / 2) ) {
				ycoord = topScreenBorder + window.innerHeight - $_("thumb").scrollHeight;
			} else {
				ycoord = topScreenBorder;
			}
		}

/*
        if ( $_("thumb").scrollHeight > docheight / 3 ) {  // separate the long pic and short pic
	        if(ycoord - truebody().scrollTop > $_("thumb").scrollHeight / 3 * 2){  ycoord = ycoord - $_("thumb").scrollHeight / 3 ;} // move up-side if the gap is too large        
	        if(ycoord - truebody().scrollTop < $_("thumb").scrollHeight / 6 ){  ycoord = ycoord + $_("thumb").scrollHeight / 6 ;} // move down-side if the gap is too small
	    } else {
	    	if(ycoord - truebody().scrollTop > $_("thumb").scrollHeight / 5 * 2){  ycoord = ycoord - $_("thumb").scrollHeight / 5 ;} // move up-side if the gap is too large        
	        if(ycoord - truebody().scrollTop < $_("thumb").scrollHeight / 3 ){  ycoord = ycoord + $_("thumb").scrollHeight / 3 ;} // move down-side if the gap is too small
	    }
*/

        //if (ycoord-truebody().scrollTop > truebody().clientHeight){ ycoord = ycoord - tth - 23;}//23

		getthumbobj().left=xcoord+"px";
		getthumbobj().top=ycoord+"px";
	}
}
