var isIE = document.all?true:false;
var isNS = document.layers?true:false;

function onlyDigits(e, decReq) {
	var key = (isIE) ? window.event.keyCode : e.which;
	var obj = (isIE) ? event.srcElement : e.target;
	var isNum = (key > 47 && key < 58) ? true:false;
	var dotOK = (key==46 && decReq=='decOK' && (obj.value.indexOf(".")<0 || obj.value.length==0)) ? true:false;
	window.event.keyCode = (!isNum && !dotOK && isIE) ? 0:key;
	e.which = (!isNum && !dotOK && isNS) ? 0:key;
	return (isNum || dotOK);
}

function onlyTime(e) {
	var key = (isIE) ? window.event.keyCode : e.which;
	var obj = (isIE) ? event.srcElement : e.target;
	var isNum = ( (key > 47 && key < 58) || (key == 58) ) ? true:false;
	window.event.keyCode = (!isNum && isIE) ? 0:key;
	e.which = (!isNum && isNS) ? 0:key;
	return (isNum);
}

function onlyDate(e) {
	var key = (isIE) ? window.event.keyCode : e.which;
	var obj = (isIE) ? event.srcElement : e.target;
	var isNum = ( (key > 47 && key < 58) || (key == 45) ) ? true:false;
	window.event.keyCode = (!isNum && isIE) ? 0:key;
	e.which = (!isNum && isNS) ? 0:key;
	return (isNum);
}

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 check_query(query) {
    invalid = "<>_%/:,;";
    if(query == "") { return false }
    for(i=0; i < invalid.length; i++) {
        bad = invalid.charAt(i);
        if (query.indexOf (bad, 0) != -1) { return false }
    }
    return true;
}

function isemail(e){return /^[\w\.-]{2,}@[\w\.-]+\.[a-z]{2,5}$/i.test(e)}

function checkLogin(text){
	text = Trim(text);
	var min_lenght = 3;
	var re=/^[A-Za-z_][\w-.]+[A-Za-z0-9]$/i;
	
	if (text.length < min_lenght){
		return 'Podana nazwa użytkownika (login) jest za krótka.\nMinimalna, dopuszczalna ilość znaków to : ' + min_lenght;
	} else {
		if (re.test(text) === false){
			return 'Podana nazwa użytkownika (login) jest błędna.\nLogin nie powinien zawierać znaków specjalnych, polskich znaków diakrytycznych ("ogonków"), spacji, a na pierwszym miejscu nie powinna znajdować się cyfra';
		}
	}
	return '';
}

function checkFormOpinie(form){
	var message = ''
	if (form.comment_text.value == "") {
		message += 'Podaj proszę treść swojej opinii\n\n';
	}
	if (message != '') {
		alert ("Przy wysyłaniu formularza wystąpiły następujące błędy:\n\n" + message);
		return false;
	} else {
		return true;
	}
}

function checkNIP(text){
	text = Trim(text);
	var max_lenght = 13;
	var min_lenght = 10;
	
	var re=/^[0-9][\w-.]+[-0-9]$/i;
	
	if ( (text.length < min_lenght) || (text.length > max_lenght) || (re.test(text) === false) ){
		return false;
	} else {
		return true;
	}
}

function checkZIP(text){
	text = Trim(text);
	var min_lenght = 5;
	
	var re=/^[0-9][\w-.]+[-0-9]$/i;
	
	if ( (text.length < min_lenght) || (re.test(text) === false) ){
		return false;
	} else {
		return true;
	}
}

function validateEmailForm(form){
	var message = ''
	
	if (form.email.value == ""){
		message += 'Musisz podać swój adres e-mail\n';
	} else {
		if (!check_email(form.email.value)) {
			message += 'Podany adres e-mail jest niepoprawny\n';
		}
	}
	
	if (message != '') {
		alert ("Przy wysyłaniu formularza wystąpiły następujące błędy:\n\n" + message);
		return false;
	} else {
		return true;
	}
}


function validateFrmGifts(form){
	var message = '';

	if (message != '') {
		alert ("Przy wysyłaniu formularza wystąpiły następujące błędy:\n\n" + message);
		return false;
	} else {
		return true;
	}
}

function makeColors2(form, itemid, abt){
	document.forms[form].elements['color_id['+itemid+']'].options.length = 0;		
	id = selectedValue(document.forms[form].elements['rozmiar_id['+itemid+']']);
	index = 0;
	for (i=0; i<colors[itemid].length; i++){
		if (colors[itemid][i][0] == id){
			document.forms[form].elements['color_id['+itemid+']'].options[index] = new Option(colors[itemid][i][2], colors[itemid][i][1]);
			index++;
		}
	}
}

function makeColors(form, itemid, abt){
	form.elements['color_id['+itemid+']'].options.length = 0;		
	id = selectedValue(form.elements['rozmiar_id['+itemid+']']);
	index = 0;
	if (id == ''){
		form.elements['color_id['+itemid+']'].options[0] = new Option('wybierz rozmiar', '');
		if ( (abt == 10) || (abt == 18) || (abt == 19) ){
			form.elements['ilosc['+itemid+']'].value = 1;
		}
	} else {
		for (i=0; i<colors[itemid].length; i++){
			if (colors[itemid][i][0] == id){
				form.elements['color_id['+itemid+']'].options[index] = new Option(colors[itemid][i][2], colors[itemid][i][1]);
				index++;
			}
		}
	}
	checkQuantity(form, itemid, abt);
}

function checkQuantity(f, itemid, abt){

	var rozmiar_id;
	var color_id;

	if ( (abt == 10) || (abt == 18) || (abt == 19) ){

		rozmiar_id = selectedValue(f.elements['rozmiar_id['+itemid+']']);
		color_id = selectedValue(f.elements['color_id['+itemid+']']);
		if ( (rozmiar_id > 0) && (color_id > 0) ){
			max_quantity = quantity[itemid][rozmiar_id][color_id];
			if (!(max_quantity > 0)){
				insertCode('info['+itemid+']', '<font color="#CC6633"><b>W tym momencie nie ma produktu w takim zestawieniu</b></font>');
			} else {
				insertCode('info['+itemid+']', '');
			}

			if (f.elements['ilosc['+itemid+']'].value > max_quantity){
				alert("Wbrana przez Ciebie ilość jest niedostępna\nSpróbuj wybrać mniejszą ilość");
				f.elements['ilosc['+itemid+']'].value = 1;
				f.elements['ilosc['+itemid+']'].focus();
				return false;
			} else {
				return true;
			}
			
		} else {
			if (f.elements['ilosc['+itemid+']'].value > 0){
				alert("Najpierw wybierz rozmiar i kolor");
				f.elements['ilosc['+itemid+']'].value = 1;
				return false;			
			} else {
				return true;
			}
		}
	} else {
		return true;
	}
}

function insertCode(layerName, strCode){
	var nn4 = (document.layers)? true:false;
	var ie4 = (document.all)? true:false;
	var nn6 = (document.getElementById && !document.all) ? true:false;

	if (nn6) {
		document.getElementById(layerName).innerHTML = strCode;
	} else 
	if (ie4) {
		document.all[layerName].innerHTML = strCode;
	} else
	if (nn4) {
		document.layers.layerName.document.open();
		document.layers.layerName.document.write(strCode);
		document.layers.layerName.document.close();
	}
	return true;
}

function validatePromocjaForm(form){
	var message = ''
	if (form.code.value == ""){
		message += 'Podaj swój kod\n';
	}
	
	if (form.code.value.length != "12"){
		message += 'Unikatowy kod dostępu musi mieć 12 znaków\n';
	}
	
	if (message != '') {
		alert ("Przy wysyłaniu formularza wystąpiły następujące błędy:\n\n" + message);
		return false;
	} else {
		return true;
	}	
}

function validateCartForm(form, itemid, abt){
	var message = ''
	rozmiar = selectedValue(form.elements['rozmiar_id['+itemid+']']);
	color = selectedValue(form.elements['color_id['+itemid+']']);
	ilosc = form.elements['ilosc['+itemid+']'].value;
	
	if (!(rozmiar > 0)){
		message += 'Musisz wybrać rozmiar\n';
	}
	
	if (!(color > 0)){
		message += 'Musisz wybrać kolor\n';
	}
	
	if (!(ilosc > 0)){
		message += 'Musisz podać ilość\n';
	} else if ( ( (abt == '10') || (abt == '18') || (abt == '19') )  && (ilosc > quantity[itemid][rozmiar][color]) ) {
			message += 'Wbrana przez Ciebie ilość jest niedostępna\nSpróbuj wybrać mniejszą ilość';
	}
	
	if (message != '') {
		alert ("Przy wysyłaniu formularza wystąpiły następujące błędy:\n\n" + message);
		return false;
	} else {
		return true;
	}
}

//validacja przy kliknięciu 'Zamow', czy zaznaczono jakas forme platnosci
function akcja_zamowienie(odnosnik){
	if('<?=$forma_zaplaty?>'=='-1'){
		alert('Wybierz formę płatności.');	
	}else{
		document.location.href = odnosnik;
	}
}

//onChange dla input radio name=forma_zaplaty
function sprawdz_forma_zaplaty(ob){
	if(ob.value==4){
		document.kosz.osobiscie.value='3';
	}
	document.kosz.submit();
}

/* stare */

function validateRegisterForm(form){
	var message = ''
	
	if (form.email.value == ""){
		message += 'Musisz podać swój adres e-mail\n';
	} else {
		if (!check_email(form.email.value)) {
			message += 'Podany adres e-mail jest niepoprawny\n';
		}
	}
	
	if (form.firstname.value == ""){
		message += 'Zapomniałeś podać swoje imię\n';
	}
	
	if (form.lastname.value == ""){
		message += 'Zapomniałeś podać swoje nazwisko\n';
	}
	
	if (form.address.value == ""){
		message += 'Podaj swój adres\n';
	}
	
	if (form.city.value == ""){
		message += 'Podaj nazwę miasta\n';
	}
	
	if (form.postalcode.value == ""){
		message += 'Podaj swój kod pocztowy\n';
	}
	
	if (form.phone.value == ""){
		message += 'Podaj swój nr telefonu\n';
	}
	
	if (selectedValue(form.region) == "") {
		message += 'Wybierz województwo\n';
	}
	
	if (!form.no_signed.checked){
		message += 'Musisz wyrazić zgodę na wystawianie faktur bez podpisu przez firmę RosaStyl\n';
	}
	
	if (form.receiver_firstname){
		if (form.receiver_firstname.value == ""){
			message += 'Podaj imię adresata prezentu\n';
		}
		
		if (form.receiver_lastname.value == ""){
			message += 'Podaj nazwisko adresata prezentu\n';
		}
		
		if (form.receiver_city.value == ""){
			message += 'Podaj miejscowość adresata prezentu\n';
		}
		
		if (form.receiver_postalcode.value == ""){
			message += 'Podaj kod pocztowy adresata prezentu\n';
		}
		
		if (form.receiver_address.value == ""){
			message += 'Podaj adres adresata prezentu\n';
		}
	}		
	
	
	if (message != '') {
		alert ("Przy wysyłaniu formularza wystąpiły następujące błędy:\n\n" + message);
		return false;
	} else {
		return true;
	}
}

function validateOrderForm(form){
  var message = ''

  if (Trim(form.imie_pp.value) == "") {
    message += 'Podaj imię odbiorcy prezentu\n';
  }
  
  if (Trim(form.nazwisko_pp.value) == "") {
    message += 'Podaj nazwisko odbiorcy prezentu\n';
  }
  
  if (Trim(form.miasto_pp.value) == "") {
    message += 'Podaj miasto do którego mamy wysłać prezent\n';
  }
  
  if (Trim(form.kod_pp.value) == "") {
    message += 'Podaj kod pocztowy odbiorcy prezentu\n';
  }
  
  if (Trim(form.adres_pp.value) == "") {
    message += 'Podaj adres odbiorcy prezentu\n';
  }
  
  if (message != '') {
    alert ("Przy wysyłaniu formularza wystąpiły następujące błędy:\n\n" + message);
    return false;
  } else {
    return true;
  }
  
}

function validateLoginForm(form){
	var message = ''
	
	if (form.login.value == "") {
		message += 'Aby się zalogować podaj swój login (e-mail)\n';
	} else {
		if ( isemail(form.login.value) == false ){
			message += 'Podany adres e-mail jest niepoprawny\n';
		}
	}
	
	if (form.haslo.value == "") {
		message += 'Aby się zalogować musisz podać hasło\n';
	}
	
	if (message != '') {
		alert ("Przy wysyłaniu formularza wystąpiły następujące błędy:\n\n" + message);
		return false;
	} else {
		return true;
	}
}

function validateSendToForm(form){
	var message = ''
	
	if (form.email_sender.value == "") {
		message += 'Nie podano adresu e-mail nadawcy\n';
	} else {
		if ( isemail(form.email_sender.value) == false ){
			message += 'Adres e-mail nadawcy jest niepoprawny\n';
		}
	}
	
	if (form.email_receiver.value == "") {
		message += 'Nie podano adresu e-mail odbiorcy\n';
	} else {
		if ( isemail(form.email_receiver.value) == false ){
			message += 'Adres e-mail odbiorcy jest niepoprawny\n';
		}
	}
	
	if (message != '') {
		alert ("Przy wysyłaniu wiadomości pojawiły się błędy:\n\n" + message);
		return false;
	} else {
		return true;
	}
}
