/*
  $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;
      }
    }
  } else return;

  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;
}

// Custom Code - LAi -- begin
/**
 * function that pops up a small window for displaying short texts
 */
var win = null;
function lai_new_window(wizpage, wizname, w, h, scroll, extras){
		LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
		TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
		//settings = 'height=' + h + ',width=' + w + ',top= 100,' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',resizable, menubar, status'
		settings = 'height=' + h + ',width=' + w + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',resizable' + extras;
		win = window.open(wizpage, wizname, settings);
		if (win.blur) win.focus();
}


/////////////////////////
var x;
var y;
var offx = 10;
var offy = 5;

function initXY(newX, newY) {

	if (newX != '') offx = newX;
	if (newY != '') offy = newY;
}

function XY(e) {
	if (e.pageX || e.pageY) {
		x = e.pageX;
		y = e.pageY;
	}	else if (e.clientX || e.clientY) {
		x = e.clientX + document.body.scrollLeft;
		y = e.clientY + document.body.scrollTop;
	}
}

function setXY(e, id) {
	o = document.getElementById(id);
	
	var left = 0;
	var top = 0;
	var width = 0;
	var height = 0;
	var parentNode = null;
	var offsetParent = null;
	
	offsetParent = o.offsetParent;
	var originalObject = o;
	var el = o;
	while (el.parentNode!=null) {
		el = el.parentNode;
		if (el.offsetParent==null) {
		}
		else {
			var considerScroll = true;
			if (window.opera) {
				if (el==originalObject.parentNode || el.nodeName=="TR") {
					considerScroll = false;
				}
			}
			if (considerScroll) {
				if (el.scrollTop && el.scrollTop>0) {
					top -= el.scrollTop;
				}
				if (el.scrollLeft && el.scrollLeft>0) {
					left -= el.scrollLeft;
				}
			}
		}
		if (el == offsetParent) {
			left += o.offsetLeft;
			if (el.clientLeft && el.nodeName!="TABLE") {
				left += el.clientLeft;
			}
			top += o.offsetTop;
			if (el.clientTop && el.nodeName!="TABLE") {
				top += el.clientTop;
			}
			o = el;
			if (o.offsetParent==null) {
				if (o.offsetLeft) {
					left += o.offsetLeft;
				}
				if (o.offsetTop) {
					top += o.offsetTop;
				}
			}
			offsetParent = o.offsetParent;
		}
	}
	top += originalObject.offsetHeight;
	
	x = left;
	y = top;
}

function ShowMessage(text, id, newWidth) {
	
  left_pos = x + offx + 'px';
  top_pos = y + offy + 'px';
  div = document.createElement('div');
  div.setAttribute("id", id);
  div.style.top = top_pos;
  div.style.left = left_pos;
  div.style.position = "absolute";
	
	
	div.style.backgroundColor = "FFFFCC";
	div.style.borderStyle = "solid";
	div.style.borderWidth = 2;
	div.style.borderColor = "000000";
	div.style.zIndex = 200;
	div.style.width = 200;
	if (newWidth != '') div.style.width = newWidth;
	div.style.height = "auto";
	div.style.padding = 5;


  div.innerHTML = '<font face="arial" size="2" color="#000000">'+text+'</font>';
  document.body.appendChild(div);
}

function Kill(id) {
	
	document.body.removeChild(document.getElementById(id));
}
   
// Custom Code - LAi -- end
