/**
*   @version $Id$
*/

/**--------------------------------------------------------------------
*	zamowienia/zapytania
*---------------------------------------------------------------------*/

	/**
	*	Opis: wysyla zamowienie
	*/
	function sendOrder( a_text, a_type, a_text2, a_logged, a_order, a_etap )
	{
		sendOrder2( a_text, a_type, a_text2, a_logged, a_order, a_etap, 0 );
	}

	/**
	*	Opis: wysyla zamowienie
	*/
	function sendOrder2( a_text, a_type, a_text2, a_logged, a_order, a_etap, util )
	{
		if( ( ( a_order == 0 ) && ( $( "ordName" ).value != "" ) && ( $( "ordSurname" ).value != "" ) && ( $( "ordEmail" ).value != "" ) && ( $( "ordAnaliticAccount" ).value != "" ) && ( $( "ordRegion" ).value != "" ) && ( $( "ordOperator" ).value != "" ) && ( $( "ordAdditionalInfo" ).value != '' ) ) || ( ( ( a_order == 1 ) || ( a_order == 2 ) ) && ( $( "ordName" ).value != "" ) && ( $( "ordSurname" ).value != "" ) && ( $( "ordEmail" ).value != "" ) && ( $( "ordAnaliticAccount" ).value != "" ) && ( $( "ordRegion" ).value != "" ) && ( $( "ordOperator" ).value != "" ) && ( $( "ordProvince" ).value != "" ) && ( $( "ordCity" ).value != "" ) && ( $( "ordPostCode" ).value != "" ) && ( $( "ordStreet" ).value != "" ) && ( $( "ordBuilding" ).value != "" ) && ( $( "ordAdditionalInfo" ).value != '' ) ) )
		//if( ( ( a_order == 0 ) && ( $( "ordName" ).value != "" ) && ( $( "ordSurname" ).value != "" ) && ( $( "ordEmail" ).value != "" ) && ( $( "ordAnaliticAccount" ).value != "" ) && ( $( "ordRegion" ).value != "" ) && ( $( "ordOperator" ).value != "" ) ) || ( ( a_order == 1 ) && ( $( "ordName" ).value != "" ) && ( $( "ordSurname" ).value != "" ) && ( $( "ordEmail" ).value != "" ) && ( $( "ordAnaliticAccount" ).value != "" ) && ( $( "ordRegion" ).value != "" ) && ( $( "ordOperator" ).value != "" ) && ( $( "ordProvince" ).value != "" ) && ( $( "ordCity" ).value != "" ) && ( $( "ordPostCode" ).value != "" ) && ( $( "ordStreet" ).value != "" ) && ( $( "ordBuilding" ).value != "" ) ) )
		{
			ok = 1;
			//lala = '';
			if( oObjNipChk = $( "receiptType1" ) )
			{
				//lala += 'oObjNipChk exists';
				if( oObjNipChk.checked )
				{
					//lala += ' and is checked;';
					if( ( $( "ordNip" ).value == '' ) || ( $( "ordCompanyName" ).value == '' ) )
					{
						//lala += ' ordNip is empty;';
						ok = 0;
					}
					else if( oObjDiffAddr = $( "diffrentAddress" ) )
					{
						//lala += ' oObjDiffAddr exists';
						if( oObjDiffAddr.checked )
						{
							//lala += ' and is checked;';
							if( ( $( "ordProvince2" ).value == '' ) || ( $( "ordCity2" ).value == '' ) || ( $( "ordPostCode2" ).value == '' ) || ( $( "ordStreet2" ).value == '' ) || ( $( "ordBuilding2" ).value == '' ) || ( $( "ordAdditionalInfo2" ).value == '' ) )
							{
								//lala += ' but some fields are missing;';
								ok = 0;
							}
						}
					}
				}
			}
			//alert( lala );
			if( ok == 1 )
			{
				$( "ordEtap" ).value = a_etap;
				if( a_etap > 1 )
				{
					$( "ordzamow" ).value = 1;
				}
				if( a_order == 1 )
				{
					$( "ordertype" ).value = 1;
				}
				else if( a_order == 2 )
				{
					$( "ordertype" ).value = 2;
				}
				else
				{
					$( "ordertype" ).value = 0;
				}
				setOrder2( 0, 0, util );
			}
			else
			{
				alert( a_text );
			}
		}
		else
		{
			alert( a_text );
		}
	}

	/**
	*	Opis: dodaje produkt do koszyka
	*/
	function setOrder( a_id, a_del )
	{
		setOrder2( a_id, a_del, 0 );
	}

	/**
	*	Opis: dodaje produkt do koszyka
	*/
	function setOrder2( a_id, a_del, util )
	{
		oInp = $( "kpr"+a_id );
		if( !myIsInt( oInp.value, 1, 10 ) || ( oInp.value < 0 ) )
		{
			oInp.value = 1;
		}
		/*if( a_del == 1 )
		{
			todel = a_id;
		}
		else
		{
			todel = -1;
		}*/
		num = 0;
		name = "ordcode"+num;
		fullcode = "";
		ck = "";
		while( l_code = $( name ) )
		{
			//if( num != todel )
			//{
				ok = $( "chk"+num ).checked;
				if( ( a_del == 0 ) || ( ( a_del == 1 ) && !ok ) )
				{
					ilosc = $( "kpr"+num ).value;
					fullcode += l_code.value+"+"+ilosc+"|";
					if( ( a_del == 0 ) && ok )
					{
						ck += ""+num+"|";
					}
				}
			//}
			num++;
			name = "ordcode"+num;
		}
		if( util == 0 )
		{
			$( "setfull" ).value = 1;
			$( "fullorder" ).value = fullcode;
			$( "basketcheck" ).value = ck;
		}
		else
		{
			$( "fullUtility" ).value = fullcode;
		}
		document.forms.orderform.submit();
	}

/**-------------------------------------------------------------------*/


/**--------------------------------------------------------------------
*	logowanie, formularz rejestracyjny
*---------------------------------------------------------------------*/

	/**
	*	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
	*/
	function checkRegistrationForm( a_text, a_pass_text, a_type, a_adr )
	{
		if( ( $( "exname" ).value != "" ) && ( $( "exsurname" ).value != "" ) && ( $( "exmail" ).value != "" ) && ( $( "exstreet" ).value != "" ) && ( $( "expostcode" ).value != "" ) && ( $( "excity" ).value != "" ) && ( $( "exphone" ).value != "" ) && ( ( ( $( "expass" ).value != "" ) && ( $( "expass2" ).value != "" ) ) || ( a_type == 2 ) ) )
		{
			if( $( "expass" ).value == $( "expass2" ).value )
			{
				if( a_type == 2 )
				{
					document.forms.exlogin.action = a_adr;
				}
				document.forms.exlogin.submit();
			}
			else
			{
				alert( a_pass_text );
			}
		}
		else
		{
			alert( a_text );
		}
	}

/**-------------------------------------------------------------------*/

	/**
	*	Opis: ustawia sortowanie w transakcjach
	*/
	function setSortTrans( sortBy, sortType, type )
	{
		$( "actionSortBy"+type+"" ).value = sortBy;
		$( "actionSortType"+type+"" ).value = sortType;
		document.forms.actionForm.submit();
	}

	/**
	*	Opis: ustawia sortowanie w transakcjach
	*/
	function dropActionFilter()
	{
		$( "resetActionFilter" ).value = 1;
		changeActionFilter();
	}

	/**
	*	Opis: ustawia sortowanie w transakcjach
	*/
	function changeActionFilter()
	{
		$( "actionFilterChange" ).value = 1;
		document.forms.actionForm.submit();
	}

	/**
	*	Opis: ustawia sortowanie w transakcjach
	*/
	function sendActionWords()
	{
		oVal = $( "transWords" );
		iVal = oVal.value;
		iVal = iVal.replace( /\s+/g, ' ' );
		iVal = iVal.replace( /(^\s+)|(\s+$)/g, '' );
		oVal.value = iVal;
		changeActionFilter();
	}

/**-------------------------------------------------------------------*/

	/**
	*	Opis: ustawia ktora transakcja ma zmienic stan i status, oraz na jakie wartosci
	*/
	function changeTransactionState( id, status, state, text )
	{
		if( confirm( text ) )
		{
			$( "transId" ).value = id;
			$( "transStatus" ).value = status;
			$( "transState" ).value = state;
			document.forms.actionForm.submit();
		}
	}

	/**
	*	Opis: ustawia pola formularza przed przeslaniem
	*/
	function setTransactionOrder( del )
	{
		num = 0;
		name = "ordcode"+num;
		fullcode = "";
		ck = "";
		while( oCode = $( name ) )
		{
			ok = $( "chk"+num ).checked;
			if( ( del == 0 ) || ( ( del == 1 ) && !ok ) )
			{
				if( ( del == 0 ) && ok )
				{
					ck += ""+num+"|";
				}
			}
			oKpr = $( "kpr"+num );
			if( !myIsInt( oKpr.value, 1, 10 ) || ( oKpr.value < 0 ) )
			{
				oKpr.value = 1;
			}
			ilosc = oKpr.value;
			fullcode += oCode.value+"+"+ilosc+"|";
			num++;
			name = "ordcode"+num;
		}
		$( "orderBuy" ).value = fullcode;
		$( "selTransCheck" ).value = ck;
		delType = 0;
		if( oDel = $( "orderDeliveryCheck" ) )
		{
			if( oDel.checked )
			{
				delType = 1;
			}
		}
		$( "orderDeliveryType" ).value = delType;
	}

	/**
	*	Opis: ustawia pola formularza przed przeslaniem
	*/
	function setTransactionSell( del )
	{
		num = 0;
		name = "chk"+num;
		fullcode = "";
		ck = "";
		while( oChk = $( name ) )
		{
			ok = oChk.checked;
			if( ( del == 0 ) || ( ( del == 1 ) && !ok ) )
			{
				num2 = 0;
				name2 = "ordcode"+num+"a"+num2+"";
				//alert( "--"+name2+"--" );
				newCode = "";
				while( oCode = $( name2 ) )
				{
					newCode = oCode.value;
					if( newCode != '' )
					{
						formatPrice( "price"+num+"a"+num2+"" );
						newCode += "+"+$( "price"+num+"a"+num2+"" ).value;
						oKpr = $( "kpr"+num+"a"+num2+"" );
						if( !myIsInt( oKpr.value, 1, 10 ) || ( oKpr.value < 0 ) )
						{
							oKpr.value = 0;
						}
						ilosc = oKpr.value;
						if( ilosc > 0 )
						{
							newCode += "+"+ilosc;
							newCode += "+"+num;
							newCode += "+"+$( "ordid"+num+"a"+num2+"" ).value;
						}
						else
						{
							newCode = "";
						}
					}
					num2++;
					name2 = "ordcode"+num+"a"+num2+"";
				}
				if( newCode != "" )
				{
					fullcode += newCode+"|";
				}
			}
			num++;
			name = "chk"+num;
		}
		//alert( "--"+fullcode+"--" );
		$( "orderSell" ).value = fullcode;
	}

	/**
	*	Opis: ustawia pola formularza przed przeslaniem
	*/
	function setTransactionSell2()
	{
		num = 0;
		name = "chk"+num;
		fullcode0 = "";
		fullcode1 = "";
		while( oChk = $( name ) )
		{
			num2 = 0;
			for( num2 = 0; num2 <= 1; num2++ )
			{
				newCode = "";
				name2 = "ordcode"+num+"a"+num2+"";
				if( oCode = $( name2 ) )
				{
					newCode = oCode.value;
					formatPrice( "price"+num+"a"+num2+"" );
					newCode += "+"+$( "price"+num+"a"+num2+"" ).value;
					oKpr = $( "kpr"+num+"a"+num2+"" );
					if( !myIsInt( oKpr.value, 1, 10 ) || ( oKpr.value < 0 ) )
					{
						oKpr.value = 0;
					}
					newCode += "+"+oKpr.value;
					newCode += "+"+num;
					newCode += "+"+$( "ordid"+num+"a"+num2+"" ).value;
				}
				if( newCode != "" )
				{
					if( num2 == 0 )
					{
						fullcode0 += newCode+"|";
					}
					else
					{
						fullcode1 += newCode+"|";
					}
				}
			}
			num++;
			name = "chk"+num;
		}
		//alert( fullcode0+"; "+fullcode1+"; " );
		$( "orderSell" ).value = fullcode0;
		$( "orderSell2" ).value = fullcode1;
	}

	/**
	*	Opis: ustawia nowy stan, status i zmiany w transakcji
	*/
	function setNewState( status, state, back, text )
	{
		if( ( text == '' ) || ( confirm( text ) ) )
		{
			$( "newStatus" ).value = status;
			$( "newState" ).value = state;
			$( "backToStart" ).value = back;
			setTransactionOrder( 0 );
			if( ordType != 2 )
			{
				setTransactionSell( 0 );
			}
			else
			{
				setTransactionSell2();
			}
			document.forms.transactionOrderForm.submit();
		}
	}

	/**
	*	Opis: ustawia nowy stan, status i zmiany w transakcji pod warunkiem podania konta analitycznego
	*/
	function setStateWithAnaliticAccount( status, state, txt, back, text )
	{
		if( $( "orderAnaliticAccount" ).value != 0 )
		{
			setNewState( status, state, back, text );
		}
		else
		{
			alert( txt );
		}
	}

	/**
	*	Opis: ustawia nowy stan, status i zmiany w transakcji pod warunkiem podania konta analitycznego
	*/
	function denyTransactionAnalitic( status, state, txt, txt2, txt3 )
	{
		if( $( "orderAnaliticAccount" ).value != 0 )
		{
			denyTransaction( status, state, txt, txt2 );
		}
		else
		{
			alert( txt3 );
		}
	}

	/**
	*	Opis: ustawia nowy stan, status i zmiany w transakcji pod warunkiem podania konta analitycznego
	*/
	function denyTransaction( status, state, txt, txt2 )
	{
		oTab = $( "reasonTab" );
		if( oTab.style.display == 'block' )
		{
			if( $( "orderReason" ).value != '' )
			{
				if( confirm( txt2 ) )
				{
					setNewState( status, state, 1, '' );
				}
			}
			else
			{
				alert( txt );
			}
		}
		else
		{
			oTab.style.display = 'block';
			oTab2 = $( "reasonTab2" );
			oTab2.style.display = 'none';
		}
	}

	/**
	*	Opis: okresla typ wstawiania (0 - z zapytania, 1 - z koszyka )
	*/
	function setInsertFrom( type, txt, del )
	{
		$( "insertFrom" ).value = type;
		setTransactionOrder( 0 );
		if( $( "selTransCheck" ).value != '' )
		{
			if( ordType != 2 )
			{
				setTransactionSell( del );
			}
			else
			{
				setTransactionSell2();
			}
			document.forms.transactionOrderForm.submit();
		}
		else
		{
			alert( txt );
		}
	}

	/**
	*	Opis: przeladowuje formulaz transakcji
	*/
	function submitTransaction( del )
	{
		if( checkPersonalData() == 1 )
		{
			setTransactionOrder( del );
			if( ordType != 2 )
			{
				setTransactionSell( del );
			}
			else
			{
				setTransactionSell2();
			}
			document.forms.transactionOrderForm.submit();
		}
		else
		{
			alert( MUSISZ_WYPELNIC_WSZYSTKIE_POLA );
		}
	}

	/**
	*	Opis: przeladowuje formulaz transakcji do nowego etapu
	*/
	function setNewEtap( etap )
	{
		if( checkPersonalData() == 1 )
		{
			setTransactionOrder( 0 );
			if( ordType != 2 )
			{
				setTransactionSell( 0 );
			}
			else
			{
				setTransactionSell2();
			}
			$( "orderEtap" ).value = etap;
			document.forms.transactionOrderForm.submit();
		}
		else
		{
			alert( MUSISZ_WYPELNIC_WSZYSTKIE_POLA );
		}
	}

	/**
	*	Opis: sprawdza formularz danych personalnych
	*/
	function checkPersonalData()
	{
		var ok = 1;
		if( $( "canEditPersonal" ).value == 1 )
		{
			var fieldNames = Array( 'ordName', 'ordSurname', 'ordEmail', 'ordProvince', 'ordCity', 'ordPostCode', 'ordStreet', 'ordBuilding' );
			var diffrentFrom = Array( '', '', '', 0, '', '', '', '' );
			for( var i = 0; i <= 7; i++ )
			{
				if( $( fieldNames[i] ).value == diffrentFrom[i] )
				{
					ok = 0;
				}
			}
		}
		return ok;
	}

	/**
	*	Opis: sprawdza, czy zamowienie expresowe, czy nie
	*/
	function setOrdExpress()
	{
		if( $( "ordExpressCheck" ).checked )
		{
			$( "ordExpress" ).value = 1;
		}
		else
		{
			$( "ordExpress" ).value = 0;
		}
	}

	/**
	*	Opis: przesyla oferte do zamowienia
	*/
	function moveToOrder( orderId, txt, txt2, isAnalitic, basketSess, adr )
	{
		//if( basketSess == '' )
		//{
			ok = 1;
			if( isAnalitic > 0 )
			{
				if( $( "orderAnaliticAccount" ).value == 0 )
				{
					alert( txt2 );
					ok = 0;
				}
			}
			if( ok == 1 )
			{
				$( "orderIdToMove" ).value = orderId;
				document.forms.transactionOrderForm.action = adr;
				document.forms.transactionOrderForm.submit();
			}
		/*}
		else
		{
			alert( txt );
		}*/
	}

	/**
	*	Opis: zapamietuje konto analityczne
	*/
	function sendAnaliticNumber( text, count )
	{
		iVal = $( "exAnaliticAccountNumber" ).value;
		if( iVal.length == count )
		{
			document.forms.actionForm.submit();
		}
		else
		{
			alert( text );
		}
	}

	/**
	*	Opis: dodaje produkt do koszyka
	*/
	function showHideAdditionalData( id1, id2 )
	{
		if( $( id1 ).checked )
		{
			$( id2 ).style.display = 'block';
		}
		else
		{
			$( id2 ).style.display = 'none';
		}
	}

	/**
	*	Opis: wysyla zamowienie
	*/
	function updateOrderInBase( text, type )
	{
		if( confirm( text ) )
		{
			$( 'serOrder1' ).value = type;
			$( 'transactionOrderForm' ).submit();
		}
	}


