/**
 * Class FormElement
 *
 * Encapsulates a form input field and provides a method to validate the field?s
 * value against a regular expression.
 */
function FormElement( id, name, validationExpression )
{
	var _id = id;
	var _name = name;
	var _validationExpression = validationExpression;
	
	this.getId = function()
	{
		return _id;
	}
	
	this.getName = function()
	{
		return _name;
	}
	
	this.getValidationExpression = function()
	{
		return _validationExpression;
	}
	
	this.isValid = function()
	{
		var value = document.getElementById( _id ).value;
		
		return (value.search( _validationExpression ) > -1);
	}
}

/**
 * Class FormValidator
 *
 * Validates the values of a set of given form fields using regular expressions.
 */
function FormValidator()
{
	var _formElements = new Array();
	
	/**
	 * Adds an element to this validator?s list of elements
	 */
	this.addElement = function( id, name, validationExpression )
	{
		_formElements[_formElements.length] = new FormElement( id, name, validationExpression );
	}
	
	/**
	 * Validates all elements in this validator?s list of elements against their
	 * validation expressions.
	 *
	 * @return An array of names of invalid elements or an empty array if
	 * all elements are valid.
	 */
	this.validate = function()
	{
		var formErrors = new Array();
		
		for( var i = 0; i < _formElements.length; i++ )
		{
			var element = _formElements[i];
			
			if( !element.isValid() )
			{
				formErrors[formErrors.length] = element.getName();
			}
		}
		
		return formErrors;
	}
}

/**
* toggleDescription() - switches between the short- and Longdescription of a product
* and the Image for switching between both
*/
function toggleDescription()
    	{
        	if (document.getElementById('productShortDescription').style.display =='none')
        	{
            	document.getElementById('productShortDescription').style.display = '';
            	document.getElementById('productLongDescription').style.display = 'none';
            	document.getElementById('button_more_information_image').src = '../view/images/global/webbilder/wa_plus_black.gif';
        	}
        	else 	
        	{
            	document.getElementById('productShortDescription').style.display ='none';
        		document.getElementById('productLongDescription').style.display = '';
            	document.getElementById('button_more_information_image').src = '../view/images/global/webbilder/wa_minus_black.gif';        	
            }
    	}
/**
* number_format() - formats a float to a string
* laenge - lenght after seperator
* sep - symbol for seperator
* th_sep - symbol for seperating thousands
*/    	
function /*out: String*/ number_format( /* in: float   */ number,
                                        /* in: integer */ laenge,
                                        /* in: String  */ sep,
                                        /* in: String  */ th_sep ) {

  number = Math.round( number * Math.pow(10, laenge) ) / Math.pow(10, laenge);
  str_number = number+"";
  arr_int = str_number.split(".");
  if(!arr_int[0]) arr_int[0] = "0";
  if(!arr_int[1]) arr_int[1] = "";
  if(arr_int[1].length < laenge){
    nachkomma = arr_int[1];
    for(i=arr_int[1].length+1; i <= laenge; i++){  nachkomma += "0";  }
    arr_int[1] = nachkomma;
  }
  if(th_sep != "" && arr_int[0].length > 3){
    Begriff = arr_int[0];
    arr_int[0] = "";
    for(j = 3; j < Begriff.length ; j+=3){
      Extrakt = Begriff.slice(Begriff.length - j, Begriff.length - j + 3);
      arr_int[0] = th_sep + Extrakt +  arr_int[0] + "";
    }
    str_first = Begriff.substr(0, (Begriff.length % 3 == 0)?3:(Begriff.length % 3));
    arr_int[0] = str_first + arr_int[0];
  }
  return arr_int[0]+sep+arr_int[1];
}    	

//Funktionsbeschreibung:
//	Diese Funktion pr?ft nach jeder Eingabe im Quantity Field ob die Eingabe OK ist
//	Sollte die Eingabe nicht OK sein, so wird sie zur?ckgesetzt und der User informiert
//	Sollte die Eingabe OK sein, wird confirmInputPressed() aufgerufen die restlichen Felder
//	zur Validierung freigibt...
function checkInputQty() {

try {	
   		//variablen anlegen f?r die zuweisung...
   		var UserInputQty = '';
    	UserInputQty = document.getElementById("productQuantity").value;	
   		//pr?fen ob der Wert der aus dem Feld Quantity ausgelesen wird, auch ein INT ist
   		//definitiv nicht Negativ und kein sonstiger wert/kram...
   		var intCheckVal = parseInt( UserInputQty, 10 );
   		if( isNaN (intCheckVal) )
   		{
   			return 1;
   		}
   		else
   		{
    		return intCheckVal;
    	}	
    	
   	} catch( e ) {
   	//ggf. Err abfangen und true returnen als wenn nie was war!
   	return 1;
  	}
}

/**
* openPopup() - opens a new Browser window 
*/
function openPopup(url,titel,w,h,scroll)
{
	// ?bergebene Prameter pr?fen
	if (!url) return ;
	if (!titel) titel = "Ansicht";
	if (!w) w = 375;
	if (!h) h = 530;
	if (!scroll) scroll = 0;
	
	// Mitte des Bildschrims ermitteln	
	var left = (screen.width)?(screen.width-w)/2:100;
	var top = (screen.height)?(screen.height-h)/2:100;
	var options = "";
	
	// ?bergro?e Popups auf die maximale Bildschirmgr??e setzen und Schrollbars einblenden
	if (left<1)
	{
		left = 0 ;
		w = (screen.width)?(screen.width):w;
		scroll = 1 ;
	}
	if (top<1)
	{
		top = 0 ;
		h = (screen.height)?(screen.height-56):h; // -56 f?r Taskleiste
		scroll = 1 ;
	}
	
	// Optionen zusammenbauen
	options = "width="+w+",height="+h ;
	options += ",top="+top+",left="+left ;
	options += ",scrollbars="+scroll ;
	options += ",location=0,menubar=0,personalbar=0,resizable=1,toolbar=0,status=0" ;
	
	// Anzeigen
	popup = window.open(url,titel,options);
	popup.focus();
}

/**
*	checkScrollbar( elementId ) - checks if there is a 
*   scrollbar for the element with the given elementId for the given 
*	direction.
*
*	@param string elementId - the Id for the element to check
*
*	@return	0 - for no Scrollbar
*	@return 1 - for horizontal Scrollbar existing
*	@return 2 - for vertical Scrollbar existing
*	@return 3 - for both Scrollbars existing
**/
function checkScrollbar( elementId )
{
	var scrollbars = 0;
	if ( document.getElementById( elementId ).scrollHeight > document.getElementById( elementId ).clientHeight )
	{
		scrollbars = scrollbars + 1;

	}
	if ( document.getElementById( elementId ).scrollWidth > document.getElementById( elementId ).clientWidth )
	{
		scrollbars = scrollbars + 2;
	}
	
	return scrollbars;
}

/*********************************************************/
/* FUNCTION:	checkkeyCode(xObjEvent)
/* PARAMETER:	1. -> xObjEvent = the shooted event
/* ACTION:	checks if the event is one we need to 
/*		redirect (arrow down) and if the current 
/*		focus points to a related input field object
/* RETURNS:	nothing			
/*********************************************************/

function checkkeyCode(xObjEvent)
{
	// var to hold the keyCode received from browser switch . . .
	var currentKeyCode = '';
	
	// Mozilla, Safari, Opera, Netscape etc . . . :)
	if(xObjEvent) 
	{
		currentKeyCode = xObjEvent.which;		
		currentCtrlStatus = xObjEvent.ctrlKey;		
	}
			
	// IE :(
	if(window.event)
	{
		currentKeyCode = window.event.keyCode;
		currentCtrlStatus = window.event.ctrlKey;
	}
	if ( (currentKeyCode > 36 && currentKeyCode < 41) || (currentKeyCode > 47 && currentKeyCode < 58) || currentKeyCode==8 || currentKeyCode==13 || currentKeyCode==0 || (currentCtrlStatus == true && currentKeyCode==116))
	{
		return true;
	} 
	else 
	{ 
		return false;
	}
}
						
/*********************************************************/
/* FUNCTION:	setBolInputFieldFocusStat(xObjField, xBool)
/* PARAMETER:	1. -> xObjField = input field object
/*		2. -> xBool (true || false) 
/* ACTION:	set the marker for the given input field 
/*		object 
/*		TO
/*		xBool('s)-Value
/* RETURNS:	nothing			
/*********************************************************/
function setBolInputFieldFocusStat(xObjField,xBool)
{
	ajax_bol_field_focus_matrix[xObjField.id] = xBool;
	if ( xBool )
	{		
		document.onkeypress = checkkeyCode;
	}
	else
	{
		document.onkeypress = "";
	}
} 

function setBolInputFieldFocusStatTrue()
			{
				setBolInputFieldFocusStat(this, true);
			}
			
			function setBolInputFieldFocusStatFalse()
			{
				setBolInputFieldFocusStat(this, false);
			}
			
/**
 * funtion to get Objects elements and attributes
 * @param mixed id of the Object
 * @return list of elements with attributes 
 */
function listObjectElements( id )
{
	for (var element in document.getElementById(id)) 
	{ 
    document.write(element + "=" + document.getElementById(id)[element] + "<br>"); 
	}
} 

function countLines( id ) 
{
	
	cols = document.getElementById( id ).cols;
    var newLines = 0;
    var deletedLines = 0;
    while ( checkScrollbar( id ) == 1 )
	{
		document.getElementById( id ).rows = newLines ;
		newLines++;
	}
	/*
	while ( checkScrollbar( id ) != 1 )
	{
		document.getElementById( id ).rows = newLines ;
		newLines--;
		deletedLines++;	
	}
	*/
	document.getElementById( id ).rows = ( newLines - 1 ); //(deletedLines - 1));
	
    return true;
} 

function getFlashPath() {

	    if (document.all) {
	        //ie
	        return document.preloader;
	    }
	    else {
	        //safari
	        var ffilm = document.getElementById("preloader");
	        try {
	            flashfilm.SetVariable("tst", "1");
	        } catch (Exception) {
	            //firefox
	            ffilm = document.embeds["preloader"];
	        }
	        return ffilm;
	    }

	}


function flipToPage(page, section, level, url, sid) 
{

	try
	{
		sectionLevelString = 'sectionLevel'+level;
		levelLength = document.getElementsByName(sectionLevelString);
		
		if( levelLength.length > 0 )
		{
			for( i=0; i< levelLength.length; i++)
			{
				/*klappt alle Sections auf und zu*/
				if( document.getElementsByName('sectionLevel'+level)[i].id == 'sectionEntry'+section )
				{
					if( document.getElementById('sectionEntry'+section).style.display == 'none')
					{
						document.getElementById('sectionEntry'+section).style.display='inline';
					}
					else
					{	
						document.getElementById('sectionEntry'+section).style.display='none';
					}
				}
				else
				{	
						document.getElementsByName('sectionLevel'+level)[i].style.display='none';
				}
				
				/*klappt jede Section einzeln auf und zu*/
				/*
				if( document.getElementById('sectionEntry'+section).style.display == 'none')
				{
					document.getElementById('sectionEntry'+section).style.display='inline';		
				}
				else
				{	
					document.getElementById('sectionEntry'+section).style.display='none';
				}
				*/		
			}	
		}
		else
		{
			var x = document.getElementsByTagName("div");
	  
			  for( var i = 0; i < x.length; i++ )
			  {
			  	if( x[i].name == sectionLevelString )
			  	{
			  		//document.getElementById( x[i].id ).innerHTML = x[i].id;
			  		if( x[i].id == 'sectionEntry'+section )
					{
						if( document.getElementById('sectionEntry'+section).style.display == 'none')
						{
							document.getElementById('sectionEntry'+section).style.display='block';
						}
						else
						{	
							document.getElementById('sectionEntry'+section).style.display='none';
						}
					}
					else
					{	
							x[i].style.display='none';
					}
			  	}
			  }	
		}	  
	}
	catch( e )
	{
		//alert(e.message);
	}	

	var flashfilm = getFlashPath();

	if (flashfilm && page != 0)
	{
		try
		{
			flashfilm.SetVariable("jscommand", "jump_to_id" + "|" + page);
		}
		catch( e )
		{
		}
	}
	else 
	{
		if( page == 0)
		{
			page = '';
		}
	    window.location.href = url + "controller/product.php?action=showCatalog&pageId=" + page + "&section=" + section + "&" + sid;
	}    
}


	function cookieActive() 
	{
	   var start = new Date();
	
	   document.cookie = "testCookie=test";
	
	   var actualTime = new Date();
	   var duration = actualTime.getTime()-start.getTime();
	
	   if (duration > 500) 
	   {
	      return 2;
	   }
	   else 
	   {
	      var activeCookie = document.cookie;
	      if (activeCookie && activeCookie.indexOf("testCookie") > -1) 
	      {
	      	return 1;
	      }	
	      else 
	      {
	      	return 0;
	      }	
	   }
	}
	
	function readCookie(name) 
	{
	   var activeCookie = document.cookie;
	
	   var posName = activeCookie.indexOf("; " + name + "=");
	   if (posName == -1) 
	   {
	      if (activeCookie.indexOf(name + "=") == 0) 
	      {
	      	posName = 0;
	      }
	      else
	      {
	       	return null;
	      }	
	   }
	
	   var valueStart = activeCookie.indexOf("=", posName)+1;
	   var valueEnd = activeCookie.indexOf(";", posName+1);
	   if (valueEnd == -1) 
	   {
	   		valueEnd = activeCookie.length;
	   }	
	
	   var actualValue = activeCookie.substring(valueStart, valueEnd);
	   return unescape(actualValue);
	}

	function fixedGMTString(fixDate)
	{
	   var onceUpon=new Date(1970,0,1,12);
	   if (onceUpon.toGMTString().indexOf("02")>0) 
	   {
	      fixDate.setTime(fixDate.getTime()-1000*60*60*24);
	   }
	   return fixDate.toGMTString();
	}
	
	function writeCookie(name,actualValue,expiry,pfad,dom,secure) 
	{
	   actualActiveCookie = name + "=" + escape(actualValue);
	   if (expiry)
	   {
	      actualActiveCookie += ";expires=" + fixedGMTString(expiry);
	   }   
	   if (pfad)
	   {
	   		actualActiveCookie += ";path=" + path;
	   }		
	   if (dom) 
	   {
		   	actualActiveCookie += ";domain=" + dom;
		}   	
	   if (secure)
	   {
	   	 actualActiveCookie += ";secure";
	   } 
	   document.cookie = actualActiveCookie;
	}

	function hideTab( id )
	{
		var actualTime = new Date();
   		var expiry = new Date(actualTime.getTime() + 1000*60*60*24*30);	
		if( document.getElementById( id ).style.display == "none" )
		{
			document.getElementById( id ).style.display = "block";
   			writeCookie(id, "block", expiry);
		}
		else
		{
			document.getElementById( id ).style.display = "none";
			writeCookie(id, "none", expiry);
		}	
	}
	
	function reloadStatus( id )
	{
		if (cookieActive()) 
		{
   			var idCookie = readCookie(id);
   			
 			if( idCookie != null )
			{
				document.getElementById( id ).style.display = idCookie;
			}
			// if it is the navigation display it even it is not set
			else if( id == ('rnpMenu') )
			{
				document.getElementById( id ).style.display = "block";
			}
			else
			{
				document.getElementById( id ).style.display = "none";
			}	
		}	
	}
