//var SITE_URL = 'http://adovaleo.testservis.rkaczmarek.devel.point-group.pl/';
var SITE_URL = 'http://promocja.valeo.pl/valeo/';
var anr = 0; // aktualny element

function displayImage(nr){
	
	// ukrycie aktualnego elementu
	var ael = document.getElementById('image_big'+anr);
	ael.style.display = 'none';
	
	// ustawinie nowego aktualnego elementu
	anr = nr;
	
	// wyswietlenie wskazanego elementu
	var el = document.getElementById('image_big'+nr);
	el.style.display = 'block';
}

function sprRozmiar(){
	
	var form = document.getElementById('potwierdz');
	
	//-> tylko jesli istnieje pole select nastepuje sprawdzanie rozmiaru
	if (Trim(form.js_wycieraczki.value) == 1) {
					
			var message = ''
			if (Trim(form.wycieraczki_rozmiar_id.value) == "") {
				 message += 'Wybierz rozmiar wycieraczki \n\n';
			}
			
			if (message != '') {
					alert ("Wystąpiły następujące błędy:\n\n" + message);
					return false;
			} else {
					return true;
			}
	}else{
			
			return true;
	}
}

function validateCustomerEditForm(form){

	var message = ''
	if (Trim(form.imie.value) == "") {
		 message += 'Podaj imie\n\n';
	}
	
	if (Trim(form.nazwisko.value) == "") {
		 message += 'Podaj nazwisko\n\n';
	}
	
	if (Trim(form.email.value) == "") {
		message += 'Podaj e-mail\n\n';
	} else {
		if (!check_email(form.email.value)){
			message += 'Podaj poprawny e-mail\n\n';
		}
	}
	if(form.zalogowany.value == 1){
		if (Trim(form.haslo.value) == "") {
			 message += 'Podaj hasło\n\n';
		}
		
		if (Trim(form.haslo_p.value) == "") {
			 message += 'Potwierdz hasło\n\n';
		}
	
	
		if (Trim(form.haslo.value) != Trim(form.haslo_p.value)) {
			 message += 'Hasło i potwierdzenie się nie zgadzają\n\n';
		}
	}
	
	if ((Trim(form.telefon_komorkowy.value) == "") && (Trim(form.telefon_stacjonarny.value) == ""))
	{	
		message += 'Podaj przynajmniej jeden telefon kontaktowy\n\n';
	} 
	if (Trim(form.telefon_komorkowy.value) != "")
	{
		if (!checkMobilePhone(form.telefon_komorkowy.value)){
			message += 'Podaj poprany nr. telefonu komórkowego\n\n';
		}
	}
	
	//-> nie jest wymagany, jesli jednak klient poda to musi byc poprawny
	if (Trim(form.telefon_stacjonarny.value) != "") {
		if (!checkPhone(form.telefon_stacjonarny.value)){
			message += 'Podaj poprany nr. telefonu stacjonarnego\n\n';
		}
	}	
	
	if (Trim(form.nazwa_firmy.value) == "") {
		 message += 'Podaj nazwę firmy\n\n';
	}
	
	if (Trim(form.nip_f.value) == "") {
		message += 'Podaj NIP\n\n';
	} else {
		if (!checkNIP(form.nip_f.value)){
			message += 'Podaj poprany NIP\n\n';
		}
	}
	
	if (Trim(form.wojewodztwo_id_f.value) == "") {
		 message += 'Wybierz województwo\n\n';
	}
	
	if (Trim(form.miasto_f.value) == "") {
		 message += 'Podaj miejscowość\n\n';
	}
	
	if (Trim(form.poczta_f.value) == "") {
		 message += 'Podaj pocztę\n\n';
	}
	
	if (checkZIP(Trim(form.kod_pocztowy_f.value)) == "") {
		 message += 'Podaj poprawny kod pocztowy\n\n';
	}
	
	if (Trim(form.ulica_f.value) == "") {
		 message += 'Podaj nazwę ulicy\n\n';
	}else{
		//if (!checkUlica(form.ulica_f.value)){
		//	message += 'Podaj poprawną nazwę ulicy\n\n';
		//}
	}
	
	if (Trim(form.nr_domu.value) == "") {
		 message += 'Podaj numer domu\n\n';
	}else{
		if (!checkNrDomu(form.nr_domu.value)){
			message += 'Podaj poprawny numer domu\n\n';
		}
	}
	
	//-> nie jest wymagany, jesli jednak klient poda to musi byc poprawny
	if (Trim(form.telefon_f.value) == "") {
			message += 'Podaj nr. telefonu do biura\n\n';
		}else{
			if (!checkPhone(form.telefon_f.value)){
			message += 'Podaj poprany nr. telefonu do biura\n\n';
		}
	}
	
	//-> tylko przy rejestracji
	if(form.zalogowany.value == 1){
		if(form.nazwa.checked != true){
			message += 'Konieczne jest potwierdzenie wyrażenia\nzgody na przetwarzanie danych osobowych\n\n';
		}
	}
	
	if (message != '') {
		
			alert ("Wystąpiły następujące błędy:\n\n" + message);
			return false;
	
	} else {
			
			if(document.getElementById('zalogowany').value != 2){
					getInfoEmail(form.email.value,form);
					
					//-> metody zwiazane z ajaxem maja pewne opuznienie ... dlatego domyslnie jest wysylana informacja
					//-> by niewysylac formularza.
					return false;
			}else{
					return true;
			}
	}
}

//-> funkcje walidacji formularzy

function LTrim(str) { 
	for (var k=0; k<str.length && str.charAt(k)<=" " ; k++) ;
		return str.substring(k,str.length);
	}
	
function RTrim(str) {
	for (var j=str.length-1; j>=0 && str.charAt(j)<=" " ; j--) ;
		return str.substring(0,j+1);
	}
       
function Trim(str) {
	return LTrim(RTrim(str));
} 

function check_email(email) {
    invalid = " /:,;";
    if(email == "") { return false }
    for(i=0; i < invalid.length; i++) {
        bad = invalid.charAt(i);
        if (email.indexOf (bad, 0) != -1) { return false }
    }
    at = email.indexOf("@", 0);
    if( (at == -1) || (at == 0) ) { return false }
    if(email.indexOf("@", at+1) != -1) { return false }
    period = email.indexOf(".", at);
    if( (period == -1) || (period == at+1) ) { return false }
    if(period+3 > email.length == -1) { return false }
    return true;
}

function checkMobilePhone(phone){
	text = Trim(phone);
	var lenght = 10;
	
	var re=/^0\d{9}/;
	
	if ( (text.length != lenght) || (re.test(text) === false) ){
		return false;
	} else {
		return true;
	}
}

function checkPhone(phone){
	text = Trim(phone);
	var lenght = 11;
	
	var re=/^0\d{2}-\d{7}/;
	
	if ( (text.length != lenght) || (re.test(text) === false) ){
		return false;
	} else {
		return true;
	}
}

function checkNIP(text){
	text = Trim(text);
	
	if ( !text.match( /^[0-9]{3}-[0-9]{2}-[0-9]{2}-[0-9]{3}$/ )
	&& !text.match( /^[0-9]{3}-[0-9]{3}-[0-9]{2}-[0-9]{2}$/ )
	&& !text.match( /^[0-9]{10}$/ ) )
	return false;
	
	var my_nums = text.replace(/-/g,'');
	var valid_nums = "657234567";
	var sum=0;
	for (var temp=8;temp>=0;temp--)
	sum += (parseInt(valid_nums.charAt(temp)) * parseInt(my_nums.charAt(temp)));
	if ( (sum % 11) == 10 ? false : ((sum % 11) == parseInt(my_nums.charAt(9))) )
	return true;
	else
	return false;
}

function checkZIP(text){
	text = Trim(text);
	var lenght = 6;
	
	var re=/\d{2}-\d{3}/;
	
	if ( (text.length != lenght) || (re.test(text) === false) ){
		return false;
	} else {
		return true;
	}
}

function checkUlica(text){
	text = Trim(text);
	
	var re=/^ul\. ?\w/;
	
	if ((re.test(text) === false) ){
		return false;
	} else {
		return true;
	}
}

function checkNrDomu(text){
	text = Trim(text);
	
	var re=/\d/;
	
	if ((re.test(text) === false) ){
		return false;
	} else {
		return true;
	}
}

function clearForm(AForm)
{
    for (i = 0; i < AForm.elements.length; i++) {
        if (AForm.elements[i].type == "radio") {
            AForm.elements[i].checked = false;
        } else if (AForm.elements[i].type == "checkbox") {
            AForm.elements[i].checked = false;
        } else if (AForm.elements[i].type == "text") {
            AForm.elements[i].value = '';
        } else if (AForm.elements[i].type == "textarea") {
            AForm.elements[i].value = '';
        } else if (AForm.elements[i].type == "select-one") {
            AForm.elements[i].selectedIndex = 0;
        } else if (AForm.elements[i].type == "select-multiple") {
            AForm.elements[i].selectedIndex = -1;
        }
    }
    return true;
}
