function markachange(){
//  fillcombo(document.frmdodaj.modelid,"http://www.autogielda.mina.pl/combo/getmodele.php?markaid=" + document.frmdodaj.markaid.value);
  fillcombo2(document.frmdodaj.modelid, document.getElementById("modele_marka_" + document.frmdodaj.markaid.value),0,'------------');

  refreshlock1();
}
function modelchange(){
//  fillcombo(document.frmdodaj.typid,"http://www.autogielda.mina.pl/combo/gettypy.php?modelid=" + document.frmdodaj.modelid.value);
  refreshlock2();
}
function markainnachange(){
	refreshlock1();
}
function ismarkainnachange(){
	refreshlock1();
}  
function ismodelinnychange(){
	refreshlock2();
}
function istypinnychange(){
	refreshlock3();
}
function modelinnychange(){
	refreshlock2();
}

function refreshvisible(){
curform = document.frmdodaj;
if (curform.ismarkainna.checked){
  document.getElementById('markainna0').style.display='none';
  document.getElementById('markainna1').style.display='';
}
else{
  document.getElementById('markainna0').style.display='';
  document.getElementById('markainna1').style.display='none';
}

if (document.frmdodaj.ismodelinny.checked){
  document.getElementById('modelinny0').style.display='none';
  document.getElementById('modelinny1').style.display='';
}
else{
  document.getElementById('modelinny0').style.display='';
  document.getElementById('modelinny1').style.display='none';
}

//if (document.frmdodaj.istypinny.checked){
//  document.getElementById('typinny0').style.display='none';
//  document.getElementById('typinny1').style.display='';
//}
//else{
//  document.getElementById('typinny0').style.display='';
//  document.getElementById('typinny1').style.display='none';
//}
}
function initfrmdodaj(modelid,typid, powiatid){
  fillcombo2(document.frmdodaj.modelid, document.getElementById("modele_marka_" + document.frmdodaj.markaid.value) , modelid, '--------------');
//  fillcombo(document.frmdodaj.typid,"http://www.autogielda.mina.pl/combo/gettypy.php?modelid=" + document.frmdodaj.modelid.value + "&default=" + typid);
  ismarkainnachange();
  wojewodztwochange();
//  fillcombo(document.frmdodaj.powiatid,"http://www.autogielda.mina.pl/combo/getpowiaty.php?wojewodztwoid=" + document.frmdodaj.wojewodztwoid.value+ "&default=" + powiatid);
  fillcombo2(document.frmdodaj.powiatid, document.getElementById("powiaty_woj_" + document.frmdodaj.wojewodztwoid.value) , powiatid, '--------------');

}

function refreshlock1(){
curform = document.frmdodaj;
	if (curform.ismarkainna.checked){
  	curform.markaid.value=0;
  	curform.markaid.disabled=true;
  }
  else{
  	curform.markaid.disabled=false;
  }
	if ((!curform.ismarkainna.checked && (curform.markaid.value==0))|| (curform.ismarkainna.checked &&(curform.markainna.value.replace(/ /, '')=='')) ){
    	curform.modelid.value=0;
      curform.modelid.disabled=true;
      curform.modelinny.disabled=true;
      curform.ismodelinny.disabled=true;
  }
  else{
      curform.modelid.disabled=false;
      curform.modelinny.disabled=false;
      curform.ismodelinny.disabled=false;
  }
  refreshlock2();
	refreshvisible();
}

function refreshlock2(){
curform = document.frmdodaj;
	if (curform.ismodelinny.checked || curform.ismarkainna.checked || curform.ismodelinny.disabled){
  	curform.modelid.value=0;
  	curform.modelid.disabled=true;
  }
  else{
  	curform.modelid.disabled=false;
  }
	if ( (curform.modelinny.disabled)||(!curform.ismodelinny.checked && (curform.modelid.value==0))|| (curform.ismodelinny.checked &&(curform.modelinny.value.replace(/ /, '')=='')) ){
//    	curform.typid.value=0;
//      curform.typid.disabled=true;
//      curform.typinny.disabled=true;
//      curform.istypinny.disabled=true;
  }
  else{
//      curform.typid.disabled=false;
//      curform.typinny.disabled=false;
//      curform.istypinny.disabled=false;
  }
//  refreshlock3();
	refreshvisible();
}

function refreshlock3(){
curform = document.frmdodaj;
//	if (curform.istypinny.disabled || curform.istypinny.checked ||curform.ismodelinny.checked || curform.ismarkainna.checked){
//  	curform.typid.value=0;
//  	curform.typid.disabled=true;
//  }
//  else{
//  	curform.typid.disabled=false;
//  }
//	refreshvisible();
}
function iskolorinnychange(){
	refreshvisible2();
}

function refreshvisible2(){
curform = document.frmdodaj;
if (curform.iskolorinny.checked){
	curform.kolorid.disabled=true;
  document.getElementById('kolorinny0').style.display='none';
  document.getElementById('kolorinny1').style.display='';
}
else{
	curform.kolorid.disabled=false;
  document.getElementById('kolorinny0').style.display='';
  document.getElementById('kolorinny1').style.display='none';
}
}


function wojewodztwochange(){
//  fillcombo(document.frmdodaj.powiatid,"http://www.autogielda.mina.pl/combo/getpowiaty.php?wojewodztwoid=" + document.frmdodaj.wojewodztwoid.value);
  fillcombo2(document.frmdodaj.powiatid, document.getElementById("powiaty_woj_" + document.frmdodaj.wojewodztwoid.value) , 0, '--------------');
  if (document.frmdodaj.wojewodztwoid.value==0) {
  	document.frmdodaj.powiatid.disabled=true;
  }
  else{
  	document.frmdodaj.powiatid.disabled=false;  	
  }
}
function isphotochange(){
curform = document.frmdodaj;
if (curform.isphoto.checked){
	curform.photo.disabled=false;
}
else{
	curform.photo.disabled=true;
}
}

function submitform(){
var sErr;
curform = document.frmdodaj;
sErr = 'Proszę podać markę samochodu';
	if (curform.markaid.value==0){
  	if (!curform.ismarkainna.checked) {
    	alert(sErr);
      return
    }
    else if (curform.markainna.value.replace(/ /g, '')==''){
    	alert(sErr);
      return
    }
  }
sErr = 'Proszę podać model samochodu';
	if (curform.modelid.value==0){
  	if (!curform.ismodelinny.checked) {
    	alert(sErr);
      return
    }
    else if (curform.modelinny.value.replace(/ /g, '')==''){
    	alert(sErr);
      return
    }
  }
sErr = 'Proszę podać rocznik samochodu';
	if (curform.rocznik.value==0){
    	alert(sErr);
      return
  }
sErr = 'Proszę podać kolor samochodu';
	if (curform.kolorid.value==0){
  	if (!curform.iskolorinny.checked) {
    	alert(sErr);
      return
    }
    else if (curform.kolorinny.value.replace(/ /g, '')==''){
    	alert(sErr);
      return
    }
  }
sErr = 'Proszę podać przebieg samochodu';
 if (curform.przebieg.value.replace(/ /g, '')==''){
    	alert(sErr);
      return
 }
 if (curform.przebieg.value.match(/^[1-9]+[0-9]*$/)==null){
    	alert(sErr);
      return
 }
sErr = 'Proszę podać cenę samochodu';
 if (curform.cena.value.replace(/ /g, '')==''){
    	alert(sErr);
      return
 }
 if (curform.cena.value.match(/^[1-9]+[0-9]*$/)==null){
    	alert(sErr);
      return
 }
sErr = 'Proszę podać osobę kontaktową';
 if (curform.displayname.value.replace(/ /g, '')==''){
    	alert(sErr);
      return
 }
sErr = 'Proszę podać województwo';
 if (curform.wojewodztwoid.value==0){
    	alert(sErr);
      return;
 }
sErr = 'Proszę podać powiat';
 if (curform.powiatid.value==0){
    	alert(sErr);
      return;
 }

	curform.action='/dodaj_save.php';
	curform.submit();	
}
