/*
  $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 getElement(id){ 
   //  return (document.getElementById) ? document.getElementById(aID)
    //                                  : document.all[aID];
	
 if (document.getElementById) { // W3C
     return  document.getElementById(id);
   }
   else if (document.layers) { // Netscape 4
     return  document.layers[id];
   }
   else if (document.all) { // IE 4/5
      return document.all[id];
   }
   } 
   

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 changeSRC(img_id,img_src) {
//document.[img_id].src=img_src;
//document.getElementById(img_id).src=img_src;
getElement(img_id).src=img_src;
}

function changeProduct(imgsrc,atid,atname,nam){
	// ändert das Produktbild und stellt im Hidden-Field die neue Auswahl ein
	changeSRC(nam,imgsrc);
	getElement(nam).title=atname;
	getElement(nam).alt=atname;
	document.getElementById("attribsel").value=atid;  // das hidden-field aktualisieren
	//alert(imgsrc + " - "+atid+" - "+ atname + " - "+ nam);
}

function changeResizeLink(imgsrc,atname)
{
	document.getElementById("resizelink").href=imgsrc;  // den vergr?erungslink aktualisieren
	document.getElementById("resizelink").title=atname;  // den vergr?erungslink aktualisieren
}

function changeHref(detailLink)
{
	//document.getElementById("detailLink").style.display = "none";
	document.getElementById("detailLink1").href=detailLink;
	document.getElementById("detailLink2").href=detailLink;
	//document.getElementById("avail").innerHTML=av;
	//alert(price);
}

function changePrice(price,av){
	
	document.getElementById("priceh1").innerHTML=price;  // das hidden-field aktualisieren
	document.getElementById("avail").innerHTML=av;
	//alert(price);
	}

// aktiviert eine DIV oder SPAN und deaktiviert alle anderen DIVs die mit den gleichen Zeichen als ID beginnen
// z.Bsp. showDIV("data","data2","div") aktiviert die DIV mit der ID data2 und deaktiviert data1, dataxyz usw.
// geht auch mit SPAN, INPUT, etc.!
function ShowOne(dname,dactive,htmltag)
{
	var divs = document.getElementsByTagName(htmltag); // dov, span,...
	for (var i = 0; i < divs.length; i++) 
	{
		var div = divs[i];
		if (div.id.indexOf(dname) == 0 && div.style) {
		div.style.display = 'none';
		}
	}
	getElement(dactive).style.display="inline";
}

 function checkBrowserName(name){  
   var agent = navigator.userAgent.toLowerCase();  
   if (agent.indexOf(name.toLowerCase())>-1) {  
     return true;  
   }  
   return false;  
 }  


//beide funktionen von http://techpatterns.com/downloads/javascript_cookies.php
function Set_Cookie( name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

//zeigt eine Hilfsebene oder nicht, setzt dabei einen minimize cookie mit dem selben namen, status inline oder none
function ShowInfo(dactive,changeit)
{
	//wenn changeit nicht gesetzt ist, dann initalisiere die Ebenen
	if(changeit=="")
	{
		//setze die ebene auf den im cookie abgespeicherten status
		if(Get_Cookie(dactive)) getElement(dactive).style.display=Get_Cookie(dactive);
		//else getElement(dactive).style.display="";
		//alert(dactive+" wurde gesetzt auf:" + getElement(dactive).style.display);
	}
	else
	{
		if(getElement(dactive).style.display=="inline") getElement(dactive).style.display="none";	
		else getElement(dactive).style.display="inline";
		Set_Cookie(dactive, getElement(dactive).style.display, 90, '/', '', '' ); //speichere den neuen status fE 90 Tage	
	}
	//setze den modifikatorbutton (dactive+mod), z.bsp. tdinfo1mod
	//wenn es eine SPAN ist
	//if(getElement(dactive).style.display=="inline") getElement(dactive+"mod").innerHTML="(-)";
	//else getElement(dactive+"mod").innerHTML="(+)";
	//wenn es ein bild ist
	if(getElement(dactive).style.display=="inline") getElement(dactive+"mod").src="images/minimize_7px.png";
	else getElement(dactive+"mod").src="images/full_7px.png";
	
}


var neuesFenster;
function Popup(nam,url,width,height,lft,tp,scroll)
{
	neuesFenster=window.open(url,nam,"width="+width+", height="+height+", left="+lft+", top="+tp+", location=no, menubar=no, statusbar=yes, toolbar=no, scrollbars="+scroll);
	neuesFenster.focus();
}


function centeredPopup(nam,url,width,height,scroll)
{
	leftVal = (screen.width-width) / 2;
	topVal = ((screen.height-height) / 2)-(height);
	if(topVal<0) topVal=1;
	//alert("screen width="+screen.width+" screen height="+screen.height+" width="+width+", height="+height+", left="+leftVal+", top="+topVal);
	Popup(nam,url,width,height,leftVal,topVal,scroll);
	//neuesFenster=window.open(url,nam,"width="+width+", height="+height+", left="+leftVal+", top="+topVal+", location=no, menubar=no, statusbar=yes, toolbar=no, scrollbars="+scroll);
	//neuesFenster.focus();
	//alert("Name"+neuesFenster.name);
	
}