// JavaScript Document

function get( q ) {
	return document.getElementById( q );
}

var lv = 'login lub email';
var pv = 'hasło';

function clearLogin() {
	var l = get( 'public_login' );
	if( l.value == lv ) {
		l.value = '';
	}
}

function fillLogin() {
	var l = get( 'public_login' );
	if( trim( l.value ) == '' ) {
		l.value = lv;
	}
}

function clearPassword() {
	var p = get( 'public_password' );
	if( p.value == pv ) {
		p.value = '';
	}
}

function fillPassword() {
	var p = get( 'public_password' );
	if( trim( p.value ) == '' ) {
		p.value = pv;
	}
}

function try_login() {
	var l = get( 'public_login' );
	var p = get( 'public_password' );	
	if( ( l.value == '' ) || ( p.value == '' ) || ( l.value == lv ) || ( p.value == pv ) ) {
		alert( 'Proszę wypełnić login i hasło' );
	} else {
		var ajax = new myAjax();
		ajax.action = 'publicLogin';
		ajax.post( 'login='+l.value+'&pass='+p.value+'&cn=Client' );
		ajax.onLoad = function() {
			if( this.response == 'ok' ) {
				document.location.reload();
			} else {
				alert( this.response );
			}
		}
	}
}

function try_remind() {
	var e 	= get( 'email' );
	e.value = trim( e.value );	
	if( e.value == '' ) {
		alert( 'Wprowadź swój adres e-mail' );
	} else {
		var ajax	= new myAjax();
		hide( 'div_reminder' );
		ajax.action = 'try_remind';
		ajax.post( 'email=' + e.value, 'reminder_message' );
		ajax.onLoad = function() {
			if( this.response == 'ok' ) {
				document.location.href = "/Haslo-zostalo-wyslane";
			} else {
				show( 'div_reminder' );
			}
		}
	}
}

function togleRegisterFirm() {
	get( 'vat' ).checked = true;
	show( 'vat_form' );
}

function togleOrder( what ) {
	if( document.what != what ) {
		document.what 	= what;
		var ajax 	= new myAjax();
		ajax.action 	= 'togleOrder';
		ajax.post( 'what='+what, 'togler_response' );
	}
}


function registerSave(){
	var ss = "Proszę wypenić następujące pola\n";
	var se = ss;
	var se2 = '';
	
	var reqstr = '|';
	var vat_req = 'name|Pełna nazwa firmy|street|Ulica|house_number|Numer|city|Miasto|postal_code|Kod pocztowy';
	var del_req = 'name|Pełna nazwa firmy|street|Ulica|house_number|Numer|city|Miasto|postal_code|Kod pocztowy|person|Osoba kontaktowa|phone|Numer telefonu|';
	var req = new Array();
	var delivery = get( 'delivery' );
	
	var t = vat_req.split( '|' );
	for( i = 0; i < t.length; i+= 2 ) {
		if( ( t[ i ] != undefined ) && ( t[ i + 1 ] != undefined ) ) {
			req.push( 'vat_'+t[ i ] );
			req.push( t[ i+1 ]+' - adres i dane płatnika' );
		}
	}

	var t = del_req.split( '|' );
	for( i = 0; i < t.length; i+= 2 ) {
		if( ( t[ i ] != undefined ) && ( t[ i + 1 ] != undefined ) ) {			
			req.push( 'delivery_'+t[ i ] );
			req.push( t[ i+1 ]+' - adres i dane odbiorcy' );
		}
	}
	

	for( i = 0; i < req.length; i+= 2 ) {
		var fn = getValue( req[ i ] );
		var ft = req[ i + 1 ];
		if( ( trim( fn ) == '' ) && ( ft != undefined ) ) {
			labelHighlight( req[ i ] );
			se2 += ft+"\n";
		} else {
			labelDehighlight( req[ i ] );
		}
	}

	var vn = trim( getValue( 'vat_nip' ) );
	var pcv = trim( getValue( 'vat_postal_code' ) );
	var pcd = trim( getValue( 'delivery_postal_code' ) );
	
	if( ( vn != '' ) && ( !checkNumber( vn ) ) ) {
		se2 += "Poprawny numer NIP\n";
		labelHighlight( 'vat_nip' );
	}
	if( ( pcv != '' ) && ( !checkNumber( pcv ) ) ) {
		se2 += "Poprawny kod pocztowy - adres płatnika\n";
		labelHighlight( 'vat_postal_code' );
	}

	if( ( pcd != '' ) && ( !checkNumber( pcd ) ) ) {
		se2 += "Poprawny kod pocztowy - adres odbiorcy\n";
		labelHighlight( 'delivery_postal_code' );
	}


	if( se2 != '' ) {
		alert( ss + se2 );
	} else if( !get( 'rights' ).checked ) {
		alert( 'Żeby dokonać zakupów w naszym sklepie musisz zezwolić na przetwarzanie danych osobowych' );
	} else {
		swapHashCode();
		var forma = get( 'register' );
		forma.submit();
	}

}




function gotoRegister() {
	document.location.href = '/rejestracja';
}
