/**
*   @version $Id: extranet.js 21 2009-04-30 10:21:11Z paulinad $
*	@package frontend
*	@author piotrj
*/

/**--------------------------------------------------------------------
*	zamowienia/zapytania
*---------------------------------------------------------------------*/

	/**
	*	Opis: wysyla zamowienie
	*/
	function sendOrder( a_text, a_type, a_text2, a_logged, a_order )
	{
		if( a_logged == 1 )
		{
			if( $( "regulamin" ).checked == true )
			{
				ok = 1;
			}
			else
			{
				ok = 0;
			}
		}
		else
		{
			ok = 1;
		}
		if( ok == 1 )
		{
			//if( ( ( a_type == 1 ) || ( ( $( "exname" ).value != "" ) && ( $( "exsurname" ).value != "" ) && ( $( "exmail" ).value != "" ) && ( $( "exphone" ).value != "" ) ) ) && ( $( "captchaCode" ).value != "" ) )
			//{
				$( "ordzamow" ).value = 1;
				if( a_order == 1 )
				{
					$( "ordertype" ).value = 1;
				}
				else
				{
					$( "ordertype" ).value = 0;
				}
				setOrder( 0, 0 );
			/*}
			else
			{
				alert( a_text );
			}*/
		}
		else
		{
			alert( a_text2 );
		}
	}

	/**
	*	Opis: dodaje produkt do koszyka
	*/
	function setOrder( a_id, a_del )
	{
		oInp = $( "kpr"+a_id );
		if( !myIsInt( oInp.value, 1, 10 ) )
		{
			oInp.value = 1;
		}
		if( a_del == 1 )
		{
			todel = a_id;
		}
		else
		{
			todel = -1;
		}
		num = 0;
		name = "ordcode"+num;
		fullcode = "";
		while( l_code = $( name ) )
		{
			if( num != todel )
			{
				ilosc = $( "kpr"+num ).value;
				fullcode += l_code.value+"+"+ilosc+"|";
			}
			num++;
			name = "ordcode"+num;
		}
		$( "setfull" ).value = 1;
		$( "fullorder" ).value = fullcode;
		$( "orderform" ).submit();
	}

/**-------------------------------------------------------------------*/


/**--------------------------------------------------------------------
*	logowanie, formularz rejestracyjny
*---------------------------------------------------------------------*/

	/**
	*	Opis: sprawdza, czy e-mail i haslo do rejestracji nie sa puste
	*/
	function sendPassword( a_txt, a_adr )
	{
		if( $( "exemail" ).value != "" )
		{
			$( 'sendpass' ).value = 1;
			docFormExL = $( "exlogin" );
			docFormExL.action = a_adr;
			docFormExL.submit();
		}
		else
		{
			alert( a_txt );
		}
	}

	/**
	*	Opis: przelacza pomiedzy wyborem polskich wojewodztw, a zmiana kraju
	*/
	function changeCountry( a_id )
	{
		adiv = $( a_id );
		ainp = $( "exkraj" );
		asel = $( "exprovince" );
		if( $( "exinny" ).checked == true )
		{
			adiv.style.color = "#cccccc";
			ainp.style.borderColor = "#7c94a5";
			ainp.disabled = false;
			asel.disabled = true;
			asel.value = 0;
		}
		else
		{
			adiv.style.color = "#666666";
			ainp.value = "";
			ainp.style.borderColor = "#cccccc";
			ainp.disabled = true;
			asel.disabled = false;
		}
	}

	/**
	*	Opis: sprawdza, czy e-mail i haslo do rejestracji nie sa puste
	*	@param		String		a_text		komunikat o koniecznsci wypelnienia poprawnie wszystkich pol
	*	@param		String		a_pass_text	komunikat o niezgodnosci hasel
	*	@param		Integer		a_type		typ formularza
	*	@param		String		a_adr		action formularza, jesli trzeba zmienic
	*	@param		Integer		a_user_type	rodzaj uzytkownika 0-normalny, 1-firma
	*/
	function checkRegistrationForm( a_text, a_pass_text, a_type, a_adr, a_user_type, a_tab_type )
	{
		var ok = 0; // alert wypelnienij poprawnie
		var ok2 = 0; // alert niezgodnosc hasel
		var Obj;
		var ObjPom;
		//sprawdzanie loginu  
		if( a_type == 1 )
		{
			Obj = $( "exclientlogin" );
			ObjPom = $( "div_exclientlogin" );
			Obj.className = '';
			ObjPom.className = '';
			loginOk = 0;
			if( ( a_type == 1 ) && ( Obj.value != "" ) )
			{
				if( !checkLength( Obj.value, 3, 20 ) )
				{
					loginOk = 1;
				}
				else if( !checkText( Obj.value ) )
				{
					loginOk = 1;
				}
			}
			else
			{
				loginOk = 1;
				
			}
			if( loginOk == 1 )
			{
				Obj.className = 'cfFormError';
				ObjPom.className = 'FormFieldInfoError';
				ok = 1;
			}
		}
		
		// sprawdzenie hasla
		if( ( a_type == 4 ) //zmiana hasla
			|| 
			( a_type == 1 ) // nowy user
			|| 
			( 
				( a_type == 2 ) // zmiana usera
				&& 
				( a_tab_type == 0 ) // zakladka z haslami
				&& 
				( // ktores haslo nie jest puste
					( $( "expass" ).value != '' ) 
					|| 
					( $( "expass2" ).value != '' ) 
				) 
			) 
		)
		{
			Obj = $( "expass" );
			var Obj2 = $( "expass2" );
			ObjPom = $( 'div_expass' )
			var ObjPom2 = $( 'div_expass2' );
			Obj.className = '';
			Obj2.className = '';
			ObjPom.className = '';
			ObjPom2.className = '';
			var passOK = 0;
			if( ( Obj.value != "" ) || ( Obj2.value != "" ) )
			{
				if( !checkLength( Obj.value, 6, 50 ) )
				{
					passOK = 2;
				}
				else if( !checkText( Obj.value ) )
				{
					passOK = 2;
				}
				else if( Obj.value != Obj2.value )
				{
					passOK = 1;
				}
			}
			else
			{
				passOK = 2;
			}
			if( passOK > 0 )
			{
				Obj.className = 'cfFormError';
				Obj2.className = 'cfFormError';
				if( passOK > 1 )
				{
					ObjPom.className = 'FormFieldInfoError';
					ObjPom2.className = 'FormFieldInfoError';
					ok = 1;
				}
				else
				{
					ok2 = 1;
				}
			}
			
			/*
			if( ( ok == 0 ) && ( a_type != 2 ) )
			{
				if( $( "expass" ).value.trim() != $( "expass2" ).value.trim() )
				{
					ok = 2;
				}
			}
			*/
		}
		
		// sprawdzenie adresu mailowego
		if( ( a_type == 1 ) || ( ( a_type == 2 ) && ( a_tab_type == 0 ) ) )
		{
			Obj = $( "exmail" );
			Obj.className = '';
			if( Obj.value == "" )
			{
				Obj.className = 'cfFormError';
				ok = 1;
			}
			else if( isEMail( Obj.value ) == 0 )
			{
				Obj.className = 'cfFormError';
				ok = 1;
			}
		}
		
		// sprawdzenie pozostalych pol
		var i = 0;
		var fields = Array();
		fields[i++] = Array( 1, "exregulation", "exregulationOuter" );
		if( a_user_type == 1 )
		{
			fields[i++] = Array( 0, "exstreet" );
			fields[i++] = Array( 0, "expostcode;" )
			fields[i++] = Array( 0, "excity" );
			fields[i++] = Array( 0, "exnip" );
			fields[i++] = Array( 0, "exprovince" );
			fields[i++] = Array( 0, "exfirmname" );
			fields[i++] = Array( 0, "exshortfirmname" );
			fields[i++] = Array( 0, "exnumber" );
		}
		if( a_type != 4 )
		{
			fields[i++] = Array( 0, "captchaCode" );
			//fields[i++] = Array( 0, "exclientlogin" );
			//fields[i++] = Array( 0, "exmail" );
		}
		for( var j = 0; j< fields.length; j++ )
		{
			if( $( fields[j][1] ) )
			{
				Obj = $( fields[j][1] );
				if( fields[j][0] == 1 )
				{
					ObjPom = $( fields[j][2] );
					ObjPom.className = '';
					if( !Obj.checked )
					{
						ObjPom.className = 'cfFormError';
						ok = 1;
						//alert( fields[j][1] );
					}
				}
				else
				{
					Obj.className = '';
					if( Obj.value.trim() == '' )
					{
						Obj.className = 'cfFormError';
						ok = 1;
						//alert( fields[j][1] );
					}
				}
			}
		}
		if( ( ok == 0 ) && ( ok2 == 0 ) )
		{
			docFormExL = $( "exlogin" );
			if( a_type == 4 )
			{
				docFormExL.action = a_adr;
			}
			docFormExL.submit();
		}
		else if( ok != 0 )
		{
			alert( a_text );
		}
		else
		{
			alert( a_pass_text );
		}
		
		/*
		if( ( a_type != 4 ) && $( "exReg" ) )
		{
			if( $( "exregulation" ).checked == true )
			{
				ok = 1;
			}
			else
			{
				ok = 0;
			}
		}
		if( a_user_type == 1 ) // firma
		{
			if( ( $( "exstreet" ).value == "" ) || ( $( "expostcode" ).value == "" ) || ( $( "excity" ).value == "" ) || ( $( "exnip" ).value == "" ) || ( $( "exprovince" ).value == "" ) || ( $( "exfirmname" ).value == "" )|| ( $( "exshortfirmname" ).value == "" ) || ( $( "exnumber" ).value == "" ) )
			{
				ok = 0;			
			}
		}
		if( ( 
				a_type == 4 
			)
			|| 
			( 
				( 
					ok == 1 
				) 
				&& 
				( 
					$( "exclientlogin" ).value != "" 
				) 
				&& 
				( 
					$( "exmail" ).value != "" 
				) 
				&& 
				( 
					( 
						( 
							$( "expass" ).value != "" 
						) 
						&& 
						( 
							$( "expass2" ).value != "" 
						) 
					) 
					|| 
					( 
						a_type == 2 
					) 
				) 
				&& 
				( 
					$( "captchaCode" ).value != "" 
				) 
			) ) 
		{
			if( $( "expass" ).value == $( "expass2" ).value )
			{
				docFormExL = $( "exlogin" );
				if( a_type == 2 || a_type == 4 )
				{
					docFormExL.action = a_adr;
				}
				//docFormExL.submit();
			}
			else
			{
				alert( a_pass_text );
			}
		}
		else
		{
			alert( a_text );
		}
		*/
	}
	
	/**
	 * sprawdza poprawnosc dlugosci tekstu
	 * @author		unknown
	 * @param		sring		txt			tekst do sprawdzenia
	 * @param		integer		minLength	minimalna dozwolona dlugosc tekstu
	 * @param		integer		maxLength	maksymalna dozwolona dlugosc tekstu
	 * @return		boolean		czy tekst mam porawna dlugosc
	 */
	function checkLength( txt, minLength, maxLength )
	{
		t = txt.length;
		if( ( t < minLength ) || ( t > maxLength ) )
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	/**
	 * sprawdza alfanumerycznosc tekstu
	 * @author		unknown
	 * @param		sring		txt		tekst do sprawdzenia
	 * @return		boolean		czy tekst jest alfanumeryczny
	 */
	function checkText( txt )
	{
		re = /^[a-zA-Z0-9]+$/;
		var patt = new RegExp( re );
		if( patt.test( txt ) )
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}

	/**
	*	Opis: sprawdza, czy e-mail i haslo do rejestracji nie sa puste
	*/
	function checkLogin( a_text )
	{
		if( ( $( "exemail" ).value != "" ) && ( $( "exhaslo" ).value != "" ) )
		{
			$( "exlogin" ).submit();
		}
		else
		{
			alert( a_text );
		}
	}
	
	function switchEditTabs( filterType )
	{
		$( "tabType" ).value = filterType;
		$( "reloadTab" ).value = 1;
		$( "exlogin" ).submit();
	}
	
	/**
	*	Opis: przeladowuje formularz w celu dodania pliku - avatar
	*/
	function addAvatar( id, txt )
	{
		var vFile = $( id );
		var ObjPom = $( id+'OuterDiv' );
		ObjPom.className = '';
		if( vFile.value != '' )
		{
			$( "exAddAvatar" ).value = 1;
			$( "exlogin" ).submit();
		}
		else
		{
			ObjPom.className = 'cfFormError';
			alert( txt );
		}
	}

	function removeAvatar()
	{
		$( "delAvatar" ).value = 1;
		$( "exlogin" ).submit();
	}
	
	/**
	*	Opis: usuwanie konta
	*/
	function deleteAccount( a_adr, a_txt )
	{
		var Obj = $( 'exdelAccount' )
		Obj.className = '';
		if( Obj.value.trim() == '' )
		{
			Obj.className = 'cfFormError';
			alert( a_txt );
		}
		else
		{
			var docFormExL = $( "exlogin" );
			docFormExL.action = a_adr;
			docFormExL.submit();
			
		}
	}

/**-------------------------------------------------------------------*/


/**--------------------------------------------------------------------
*	modul projekty
*---------------------------------------------------------------------*/

	/**
	*	Opis: resetuje filtry w module projekty
	*/
	function dropProjectFilters()
	{
		$( "dropPFilters" ).value = 1;
		$( "pWords2" ).value = "";
		$( "pWords" ).value = "";
		$( "projectFilterForm" ).submit();
	}

	/**
	*	Opis: ustawia slowa do wyszukania w module projekty
	*/
	function sendProjectWords( txt )
	{
		words = $( "pWords2" ).value;
		if( words != "" )
		{
			words = erase_space( words, 1 );
			//re = /\+/g;
			//words = words.replace( re, " " );
			$( "pWords" ).value = words;
			//$( "dropPFilters" ).value = 1;
			$( "projectFilterForm" ).submit();
		}
		else
		{
			alert( txt );
		}
	}

/**-------------------------------------------------------------------*/


/**--------------------------------------------------------------------
*	modul aukcje
*---------------------------------------------------------------------*/

	/**
	*	Opis: sbmituje zmiane w ofercie aukcyjnej
	*/
	function auctionFormSubmit( a_txt, a_txt2 )
	{
		formatPrice( "acprice" );
		formatNumber( "acpayment" );
		formatNumber( "acdelivery" );
		acprice = $( "acprice" ).value;
		acpayment = $( "acpayment" ).value;
		acdelivery = $( "acdelivery" ).value;
		if( ( acprice != "0.00" ) && ( acpayment != "0" ) && ( acdelivery != "0" ) )
		{
			if( confirm( a_txt2 ) )
			{
				$( "accheck" ).value = "1";
				$( "auctionForm" ).submit();
			}
		}
		else
		{
			alert( a_txt );
		}
	}

/** ----------------------------------------------------------------------
* 
*-----------------------------------------------------------------------*/

	/**
	*	Opis: ??
	*/
	function checkName( name, minLength, maxLength, txt )
	{
		t = name.length();
		if( ( t < minLength ) || ( t > maxLength ) )
		{
			alert( txt );
			return 0;
		}
		else
		{
			return 1;
		}
	}

/** ----------------------------------------------------------------------
* 	to dla "moje ogloszenia"
*-----------------------------------------------------------------------*/

	/**
	*	Opis: sbmituje zmiane w ofercie aukcyjnej
	*/
	function submitFilterAnForm( filterType )
	{
		$( "myAnFilterType" ).value = filterType;
		$( "filterAnForm" ).submit();
	}

	/**
	*	Opis: sbmituje zmiane w ofercie aukcyjnej
	*/
	function setMyAnnouncementAction( aType, aTarget, txt )
	{
		if( ( txt == '' ) || ( confirm( txt ) ) )
		{
			$( "actionType" ).value = aType;
			$( "actionTarget" ).value = aTarget;
			$( "filterAnForm" ).submit();
		}
	}

	function showHideBox( selectId, boxId, optionName )
	{
		if( $( selectId ) )
		{
			num = $( selectId ).selectedIndex;
			if ( $( selectId ).options[num].text == optionName )
			{
				$( boxId ).style.display = 'none';
			}
			else
			{
				$( boxId ).style.display = 'block';
			}
		}
	}

	function showHideAddress( radioId, boxId, optionName )
	{
		if( $( radioId ) && $( boxId ) )
		{
			if(  $( radioId ).value == 0 )
			{
				$( boxId ).style.display = 'none';
			}
			else
			{
				$( boxId ).style.display = 'block';
			}
		}
	}

	function printOrder( adr )
	{
		if( $( "orderform" ) )
		{
			$( "orderform" ).action = adr;
			$( "orderform" ).target="_blank";
			$( "orderform" ).submit();
		}
	}
	
	/**
	*	Opis: sbmituje zmiane w ofercie aukcyjnej
	*/
	function submitFilterEcForm( filterType )
	{
		$( "myEcFilterType" ).value = filterType;
		$( "filterEcForm" ).submit();
	}

	/**
	*	Opis: sbmituje zmiane w ofercie aukcyjnej
	*/
	function setMyEventAction( aType, aTarget, txt )
	{
		if( ( txt == '' ) || ( confirm( txt ) ) )
		{
			$( "actionType" ).value = aType;
			$( "actionTarget" ).value = aTarget;
			$( "filterEcForm" ).submit();
		}
	}

	/**
	*	Opis: submituje komentarz
	*/
	function setMyEventCategory( vCategory, vAdr )
	{
		$( "ecCategorySet" ).value = vCategory;
		mef = $( "filterEcForm" );
		mef.action = vAdr;
		mef.submit();
	}

	/**
	*	Opis: submituje komentarz
	*/
	function setMyEventTag( vTag, vAdr )
	{
		$( "ecTagsToSearch" ).value = vTag;
		$( "searchOnlyInTags" ).value = 1;
		mef = $( "filterEcForm" );
		mef.action = vAdr;
		mef.submit();
	}

	/**
	*	Opis: submituje komentarz
	*/
	function addEventForUser2( txt, eventId )
	{
		var remind = 0;
		if( confirm( txt ) )
		{
			remind = 1;
		}
		$( "ecAddEventForUser" ).value = eventId;
		$( "ecEventForUserRemind" ).value = remind;
		var calForm = $( "filterEcForm" );
		//calForm.action = '#';
		calForm.submit();
	}

	/**
	*	Opis: submituje komentarz
	*/
	function removeEventFromUser( txt, eventId )
	{
		if( confirm( txt ) )
		{
			$( "ecRemoveEventFromUser" ).value = eventId;
			var calForm = $( "filterEcForm" );
			//calForm.action = '#';
			calForm.submit();
		}
	}


