/*
  $Id: general.js 1739 2007-12-20 00:52:16Z hpdl $

  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;
}

function creatContainerDiv(id,url,title,admin)
{
	// get body
	var objBody = document.getElementsByTagName("body").item(0);

	// if specific container doesn't already exist
	if(!document.getElementById(id+'Container'))
	{

		// create container
		var dvContainer=document.createElement('div');
		dvContainer.style.display='none';
		dvContainer.setAttribute('id',id+"Container");
		dvContainer.setAttribute('rel',"Container");
		objBody.appendChild(dvContainer);
		Element.hide(id+'Container');
		dvContainer=$(id+'Container');
		dvContainer.style.position="absolute";
		dvContainer.style.zIndex=1;
		objBody.appendChild(dvContainer);
	}

	// get page and screen sizes
	var arrPageSize=getPageSize();
	var screenHeight=(document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight);


	// setup the innerHTML for the pop-up
	Element.setInnerHTML(id+'Container',"<table width=100% align=left border=0 cellspacing=0 cellpadding=0 style='background-color: transparent;'><tr><td colspan=3 height=5 bgcolor='#000000'></td></tr><tr><td width=5 bgcolor='#000000'></td><td><table width=100% align=center border=0 cellspacing=0 cellpadding=3px bgcolor=white><tr><td align=left width=70% style='padding-left:15px'><span id='"+id+"Title' class='popupTitle'></span></td><td align=right width=30%><img id='idImgTopClose"+id+"' src='"+WEB_ROOT+"images/closelabel.gif' style='cursor:pointer;' onClick='closeContainerDiv(\""+id+"\")'>&nbsp;</td></tr><tr><td width=95% colspan=2 height='100%' align='center' bgcolor='white'><div id='"+id+"' class='popupDesc'></div></td></tr><tr><td width='100%' bgcolor='white' colspan='2' align='right'><img id='idImgBottomClose"+id+"' src='"+WEB_ROOT+"images/closelabel.gif' style='cursor:pointer;' onClick='closeContainerDiv(\""+id+"\")' >&nbsp;</td></tr></table></td><td width=5 bgcolor='#000000'></td></tr><tr><td colspan=3 height=5 bgcolor='#000000'></td></tr></table>");

	// set the left offset and height for the container
	$(id+'Container').style.left=400+'px';
	Element.setHeight(id+'Container', 300);

	Element.setWidth(id+'Container', 500);

	// get page scroll height and set offsets accordingly for container and id div
	var arrPageScroll=getPageScroll();
	Element.setTop(id+'Container',arrPageScroll[1]+150);
	//Element.setTop(id+'Container',50);
	Element.setHeight(id, 300);

	Element.setWidth(id, 460);

	// set id div's overflow
	$(id).style.overflow='auto';

	// if title is test
	if (!(/^\s*$/.test(title) || /^\s*undefined\s*$/.test(title)))
	{
		// reset it to supplied value
		$(id+'Title').innerHTML=title;
	}

	// hide/show various elements
	hideSelectBoxes();
	Element.show(id+'Container');

	// loop through
	for (var x=1;x<6;x++)
	{
		// and setup timeouts
		setTimeout("if($('idImgTopClose"+id+"')) { $('idImgTopClose"+id+"').src='"+WEB_ROOT+"images/closelabel.gif'; } if($('idImgBottomClose"+id+"')) { $('idImgBottomClose"+id+"').src='"+WEB_ROOT+"images/closelabel.gif'; } ",x*1000);
	}

	// if the url is not a test/blank url
	if (!(/^\s*$/).test(url) && url!='undefined')
	{
		// pull the page
		AjaxRequest.get({
			'url'				:url,
			'onLoading'			:function(req){ $(id).innerHTML="";},
			'onSuccess'			:function(req){
									$(id).innerHTML=req.responseText;
								},
			'onError'			:function(req){ closeContainerDiv(id); }
		})
	}
}

function creatContainerDivBig(id,url,title,admin)
{
	// get body
	var objBody = document.getElementsByTagName("body").item(0);

	// if specific container doesn't already exist
	if(!document.getElementById(id+'Container'))
	{

		// create container
		var dvContainer=document.createElement('div');
		dvContainer.style.display='none';
		dvContainer.setAttribute('id',id+"Container");
		dvContainer.setAttribute('rel',"Container");
		objBody.appendChild(dvContainer);
		Element.hide(id+'Container');
		dvContainer=$(id+'Container');
		dvContainer.style.position="absolute";
		dvContainer.style.zIndex=1;
		objBody.appendChild(dvContainer);
	}

	// get page and screen sizes
	var arrPageSize=getPageSize();
	var screenHeight=(document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight);


	// setup the innerHTML for the pop-up
	Element.setInnerHTML(id+'Container',"<table width=100% align=left border=0 cellspacing=0 cellpadding=0 style='background-color: transparent;'><tr><td colspan=3 height=5 bgcolor='#000000'></td></tr><tr><td width=5 bgcolor='#000000'></td><td><table width=100% align=center border=0 cellspacing=0 cellpadding=3px bgcolor=white><tr><td align=left width=70% style='padding-left:15px'><span id='"+id+"Title' class='popupTitle'></span></td><td align=right width=30%><img id='idImgTopClose"+id+"' src='"+WEB_ROOT+"images/closelabel.gif' style='cursor:pointer;' onClick='closeContainerDiv(\""+id+"\")'>&nbsp;</td></tr><tr><td width=95% colspan=2 height='100%' align='center' bgcolor='white'><div id='"+id+"' class='popupDesc'></div></td></tr><tr><td width='100%' bgcolor='white' colspan='2' align='right'><img id='idImgBottomClose"+id+"' src='"+WEB_ROOT+"images/closelabel.gif' style='cursor:pointer;' onClick='closeContainerDiv(\""+id+"\")' >&nbsp;</td></tr></table></td><td width=5 bgcolor='#000000'></td></tr><tr><td colspan=3 height=5 bgcolor='#000000'></td></tr></table>");

	// set the left offset and height for the container
	var screenWidth=(document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth);
	var leftGap = parseInt((parseInt(screenWidth) - 725)/2);
	$(id+'Container').style.left=leftGap+'px';
	Element.setHeight(id+'Container', 550);

	Element.setWidth(id+'Container', 725);

	// get page scroll height and set offsets accordingly for container and id div
	var arrPageScroll=getPageScroll();
	var TopPos = 220;
	if(arrPageScroll[1] > 120)
		TopPos = arrPageScroll[1] + 100;
	Element.setTop(id+'Container',TopPos);
	//Element.setTop(id+'Container',50);
	Element.setHeight(id, 520);

	Element.setWidth(id, 700);

	// set id div's overflow
	$(id).style.overflow='auto';

	// if title is test
	if (!(/^\s*$/.test(title) || /^\s*undefined\s*$/.test(title)))
	{
		// reset it to supplied value
		$(id+'Title').innerHTML=title;
	}

	// hide/show various elements
	hideSelectBoxes();
	Element.show(id+'Container');

	// loop through
	for (var x=1;x<6;x++)
	{
		// and setup timeouts
		setTimeout("if($('idImgTopClose"+id+"')) { $('idImgTopClose"+id+"').src='"+WEB_ROOT+"images/closelabel.gif'; } if($('idImgBottomClose"+id+"')) { $('idImgBottomClose"+id+"').src='"+WEB_ROOT+"images/closelabel.gif'; } ",x*1000);
	}

	// if the url is not a test/blank url
	if (!(/^\s*$/).test(url) && url!='undefined')
	{
		// pull the page
		AjaxRequest.get({
			'url'				:url,
			'onLoading'			:function(req){ $(id).innerHTML="";},
			'onSuccess'			:function(req){
									$(id).innerHTML=req.responseText;
								},
			'onError'			:function(req){ closeContainerDiv(id); }
		})
	}
}

function closeContainerDiv(id)
{
	// if the div doesn't exist
	if (!($(id)))
	{
		// just fail gracefully
		return;
	}

	// do final close-out
	doDivFinalClosing(id);
}

function doDivFinalClosing(id)
{
	if(!(/^\s*$/.test(id) || /^\s*undefined\s*$/.test(id)))
	{
		Element.hide(id+'Container');
	}
	var arr=document.getElementsByTagName("Div");
	for(var i=arr.length-1;i>=0;i--)
	{
		if(arr[i].getAttribute("rel")=="Container")
		{
			if(arr[i].style.display!='none')
			{
				hideSelectBoxes();
				break;
			}
			else
			{
				showSelectBoxes();
			}
		}
		else
		{
			showSelectBoxes();
		}
	}
	if(!(/^\s*$/.test(id) || /^\s*undefined\s*$/.test(id)))
	{
		$(id+'Container').parentNode.removeChild($(id+'Container'));
	}
}