var myAdvert = null;

function isNum(num)
{
	var validChars = "0123456789.";
	var c;
  	for(var i=0; i<num.length; ++i){
		c = num.charAt(i);
		if(validChars.indexOf(c) == -1) 
	  		return false;
	}
 	return true;
}

function isPhone(phone){
	var validChars = "0123456789-/";
	var c;
	if (phone.length < 7 || phone.length > 15)
		return false;
	for(var i=0; i<phone.length; i++){
		c = phone.charAt(i);
		if (validChars.indexOf(c) == -1)
			return false;
	}
	return true;
}

function onNextBtnClick(){
	if (myAdvert.validateAdvert())
		document.forms[0].submit();
}

function onBrandElChange(){
	myAdvert.getTypes();
}

function onNoPriceChange(){
	myAdvert.switchPriceControls(this);
}

function onDemandPriceChange(){
	myAdvert.switchPriceControls(this);
}

function Advert( baseUrl, options, brandNameId, brandId, typeId, yearId, priceId, phoneId, nextId, noPriceId, 
				onDemandPriceId, currencyId, kmId, powerId, photoId, otherId) {
   this.baseUrl    	= baseUrl;
   this.options    	= options;
   this.ajaxUpdate 	= null;
   this.brandNameEl	= document.getElementById(brandNameId);
   this.brandEl		= document.getElementById(brandId);
   this.typeEl		= document.getElementById(typeId);
   this.yearEl		= document.getElementById(yearId);
   this.priceEl		= document.getElementById(priceId);
   this.phoneEl		= document.getElementById(phoneId);
   this.nextEl		= document.getElementById(nextId);
   this.noPriceEl	= document.getElementById(noPriceId);
   this.onDemandPriceEl = document.getElementById(onDemandPriceId);
   this.currencyEl	= document.getElementById(currencyId);
   this.kmEl		= document.getElementById(kmId);
   this.powerEl		= document.getElementById(powerId);
   this.photoEl		= document.getElementById(photoId);
   this.otherEl		= document.getElementById(otherId);
}

Advert.prototype = {   

	xmlResponse_getCarBrands: function(request) {
		var brands = request.responseXML.getElementsByTagName('brand');
		var opt;
		
		this.brandEl.options.length = 0;
		this.brandEl.options.add(new Option("", ""), 0);
		for(var i=0; i < brands.length; i++){
			opt = new Option();
			opt.value = brands[i].getAttribute('id');
			opt.text = brands[i].getAttribute('name');
			this.brandEl.options.add(opt, this.brandEl.options.length);
		}
	},
	
	xmlResponse_getCarTypes :function(request) {
		var types = request.responseXML.getElementsByTagName('type');
		var opt;
		
		this.typeEl.options.length = 0;
		this.typeEl.options.add(new Option("", ""), 0);
		for(var i=0; i < types.length; i++){
			opt = new Option();
			opt.value = types[i].getAttribute('name');
			opt.text = types[i].getAttribute('name');
			this.typeEl.options.add(opt, this.typeEl.options.length);
		}
	},
	
	switchPriceControls: function(el){
		if (el.checked==true){
			this.priceEl.disabled = "disabled";
			this.priceEl.value = "";
			this.currencyEl.disabled = "disabled";
			if (el == this.noPriceEl)
				this.onDemandPriceEl.disabled = "disabled";
			else
				this.noPriceEl.disabled = "disabled";
		} else {
			this.priceEl.disabled = "";
			this.onDemandPriceEl.disabled = "";
			this.noPriceEl.disabled = "";
			this.currencyEl.disabled = "";
		}
	},

	validateAdvert: function(){
		var errorMsg = "";
		if (this.brandEl.options != null && this.brandEl[this.brandEl.selectedIndex].value == '')
			errorMsg += "\n\t- odabir marke vozila je obavezan";
		else if (this.brandEl.options == null && this.brandEl.value == '')
			errorMsg += "\n\t- marka vozila je obavezna";
		if (this.typeEl.options != null && this.typeEl[this.typeEl.selectedIndex].value == '')
			errorMsg += "\n\t- odabir tipa vozila je obavezan";
		else if (this.typeEl.options == null && this.typeEl.value == '')
			errorMsg += "\n\t- tipa vozila je obavezan";
		if (this.yearEl.value == "")
			errorMsg += "\n\t- godina proizvodnje je obavezna";
		else if (!isNum(this.yearEl.value) || this.yearEl.value.valueOf()>2100)
			errorMsg += "\n\t- godina mora biti broj od najvie 4 znamenke";
		if (this.noPriceEl.checked == false && this.onDemandPriceEl.checked == false){			
			if (this.priceEl.value == "")
				errorMsg += "\n\t- cijena je obavezna";
			else if (!isNum(this.priceEl.value))
				errorMsg += "\n\t- cijena mora biti broj";
		}
		if (this.phoneEl.value == "")
			errorMsg += "\n\t- broj telefona je obavezan";
		else if (!isPhone(this.phoneEl.value))
			errorMsg += "\n\t- broj telefona nije valjan";
		if (!isNum(this.powerEl.value))
			errorMsg += "\n\t- snaga mora biti broj";
		if (!isNum(this.kmEl.value))
			errorMsg += "\n\t- kilometraža mora biti broj";
		if (this.photoEl.value == '')
			errorMsg += "\n\t- slika je obavezna";
		if (this.otherEl.value.length > 50)
			errorMsg += "\n\t- za dodatne informacije dopušteno maksimalno 50 znakova";
		
		if (errorMsg != ""){
			errorMsg = "Oglas nije ispravno zadan. Molimo vas ispravite sljedeća polja:" + errorMsg;	
			alert(errorMsg);
			return false;
		} else 
			return true;
	},
	
   	handleError: function(request) {
	    alert("Oops! Server error.  Please try again later.");
  	},
  
	getBrands: function(vtype) {
		this.ajaxUpdate = this.xmlResponse_getCarBrands;
		var ajaxHelper = new net.ContentLoader( this, this.baseUrl, "POST", [] );
     	ajaxHelper.sendRequest.apply( ajaxHelper, new Array("op=getBrands", "vtype=" + vtype) );
	},
	
	getTypes: function(){
		var brand = this.brandEl[this.brandEl.selectedIndex].value;
		if (brand == "")
			return;
		this.brandNameEl.value = this.brandEl[this.brandEl.selectedIndex].text;
		if (this.typeEl.options == null)
			return;
		this.ajaxUpdate = this.xmlResponse_getCarTypes;
		var ajaxHelper = new net.ContentLoader( this, this.baseUrl, "POST", [] );
     	ajaxHelper.sendRequest.apply( ajaxHelper, new Array("op=getTypes", "brand=" + brand) );
	},

	updateStatus: function() {
		window.status = "Last Updated: " + new Date();
	}
}


