function newAgency(root){
	name=prompt("Unesite naziv nove agencije","");
	if(name!="" && name!="null" && name!=null){
		document.location=root+"?view=users&newAgency="+name;
	}
}

function addRealestate(text){
	name="Novi oglas";
	if(name=="" || name=="null" || name==null){
		name="code";
	}
	else{
		document.location="admin.php?view=realestates&newRealestate="+name;
	}
}

function duplicateRealestate(id){
	document.location="admin.php?view=realestates&duplicateRealestate="+id;
}

function deleteRealestate(){
	selObj = document.getElementById("realestateIdSelect");
	selIndex = selObj.selectedIndex;
	id=selObj.options[selIndex].value;
	document.location="admin.php?view=realestates&deleteId="+id;
}

function deleteImage(baseUrl,imageId,realestateId){
	document.location=baseUrl+"?view=realestates&deleteImageId="+imageId+"&selectedTab=tab_images&selectedRealestateId="+realestateId;
}

function deletePageImage(imageId,realestateId, selectedTab){
	document.location="admin.php?view=webPageOnPortal&deleteImageId="+imageId+"&selectedRealestateId="+realestateId+"&selectedTab="+selectedTab;
}

function deleteNewsImage(imageId,newsId, selectedTab){
	document.location="admin.php?view=news&deleteImageId="+imageId+"&news_id="+newsId+"&selectedTab="+selectedTab;
}

function deleteProjectImage(imageId,project_id, selectedTab){
	document.location="admin.php?view=projects&deleteImageId="+imageId+"&project_id="+project_id+"&selectedTab="+selectedTab;
}

function addRegion(baseUrl,promptText){
	name=prompt(promptText,"");
	if(name!="" && name!="null" && name!=null){
		document.location=baseUrl+"?view=cities&newRegion="+name;
	}
}

function editRegion(baseUrl,selectedRegionId,regionName,regionOrderId,promptText1,promptText2){
	if(newName=prompt(promptText1,regionName)){
		if(newOrderId=prompt(promptText2,regionOrderId)){
			document.location=baseUrl+"?view=cities&action=editRegion&selectedRegionId="+selectedRegionId+"&newName="+newName+"&newOrderId="+newOrderId;
		}
	}
}

function deleteRegion(baseUrl,confirmText){
	if(confirm(confirmText+"?")){
		selObj = document.getElementById("selectedRegionId");
		selIndex = selObj.selectedIndex;
		id=selObj.options[selIndex].value;
		document.location=baseUrl+"?view=cities&deleteRegionId="+id;
	}
}


function addIsland(baseUrl,selectedRegionId,promptText){
	name=prompt(promptText,"");
	if(name!="" && name!="null" && name!=null){
		document.location=baseUrl+"?view=cities&selectedRegionId="+selectedRegionId+"&newIsland="+name;
	}
}

function editIsland(baseUrl,selectedRegionId,selectedIslandId,islandName,islandOrderId,promptText1,promptText2,promptText3){
	if(newName=prompt(promptText1,islandName)){
		if(newOrderId=prompt(promptText2,islandOrderId)){
			oldRegionId=selectedRegionId;
			if(newRegionId=prompt(promptText3,selectedRegionId)){
				document.location=baseUrl+"?view=cities&action=editIsland&oldRegionId="+oldRegionId+"&selectedRegionId="+newRegionId+"&selectedIslandId="+selectedIslandId+"&newName="+newName+"&newOrderId="+newOrderId+"&newRegionId="+newRegionId;
			}
		}
	}
}

function deleteIsland(baseUrl,selectedRegionId, confirmText){
	if(confirm(confirmText+"?")){
		selObj = document.getElementById("selectedIslandId");
		selIndex = selObj.selectedIndex;
		id=selObj.options[selIndex].value;
		document.location=baseUrl+"?view=cities&selectedRegionId="+selectedRegionId+"&deleteIslandId="+id;
	}
}

function addCity(baseUrl,selectedRegionId,selectedIslandId,promptText){
	name=prompt(promptText,"");
	if(name!="" && name!="null" && name!=null){
		document.location=baseUrl+"?view=cities&selectedRegionId="+selectedRegionId+"&selectedIslandId="+selectedIslandId+"&newCity="+name;
	}
}

function editCity(baseUrl,selectedRegionId,selectedIslandId,selectedCityId,cityName,cityOrderId,promptText1,promptText2,promptText3){
	alert("This change will not be applyed on island parent! That change should be made manualy.");
	if(newName=prompt(promptText1,cityName)){
		if(newOrderId=prompt(promptText2,cityOrderId)){
			oldRegionId=selectedRegionId;
			if(newRegionId=prompt(promptText3,selectedRegionId)){
				document.location=baseUrl+"?view=cities&action=editCity&selectedIslandId="+selectedIslandId+"&oldRegionId="+oldRegionId+"&selectedRegionId="+newRegionId+"&selectedCityId="+selectedCityId+"&newName="+newName+"&newOrderId="+newOrderId+"&newRegionId="+newRegionId;
			}
		}
	}
}

function deleteCity(baseUrl,selectedRegionId,selectedIslandId,confirmText){
	if(confirm(confirmText+"?")){
		selObj = document.getElementById("selectedCityId");
		selIndex = selObj.selectedIndex;
		id=selObj.options[selIndex].value;
		document.location=baseUrl+"?view=cities&selectedRegionId="+selectedRegionId+"&selectedIslandId="+selectedIslandId+"&deleteCityId="+id;
	}
}

function addCityArea(baseUrl, selectedRegionId,selectedIslandId,selectedCityId,promptText){
	name=prompt(promptText,"");
	if(name!="" && name!="null" && name!=null){
		document.location=baseUrl+"?view=cities&newCityArea="+name+"&selectedCityId="+selectedCityId+"&selectedRegionId="+selectedRegionId+"&selectedIslandId="+selectedIslandId;
	}
}

function editCityArea(baseUrl, selectedRegionId,selectedIslandId,selectedCityId,selectedCityAreaId,cityAreaName,cityAreaOrderId,promptText1,promptText2,promptText3){
	if(newName=prompt(promptText1,cityAreaName)){
		if(newOrderId=prompt(promptText2,cityAreaOrderId)){
			if(newCityId=prompt(promptText3,selectedCityId)){
				document.location=baseUrl+"?view=cities&action=editCityArea&selectedRegionId="+selectedRegionId+"&selectedIslandId="+selectedIslandId+"&selectedCityId="+newCityId+"&selectedCityAreaId="+selectedCityAreaId+"&newName="+newName+"&newOrderId="+newOrderId+"&newCityId="+newCityId;
			}
		}
	}
}

function deleteCityArea(baseUrl, selectedRegionId,selectedIslandId,selectedCityId,confirmText){
	if(confirm(confirmText+"?")){
		selObj = document.getElementById("selectedCityAreaId");
		selIndex = selObj.selectedIndex;
		id=selObj.options[selIndex].value;
		document.location=baseUrl+"?view=cities&selectedCityId="+selectedCityId+"&deleteCityAreaId="+id+"&selectedRegionId="+selectedRegionId+"&selectedIslandId="+selectedIslandId;
	}
}

function deleteGuestbookEntry(id){
	document.location="admin.php?view=guestbook&deleteGuestbookId="+id;
}

function newRealestateOnHome(){
	document.location="admin.php?view=realestatesOnHome&action=newAd";
}

function deleteAd(id){
	document.location="admin.php?view=realestatesOnHomeAdmin&action=delete&selectedAdId="+id;
}

function activateAd(id,ad_type){
	document.location="admin.php?view=realestatesOnHome&action=activate&selectedAdId="+id+"&type="+ad_type;
}

function setValidTill(date,dateFormated){
	document.getElementById("valid_till").value=date;
	if(document.getElementById("valid_till_span").innerHTML){
		document.getElementById("valid_till_span").innerHTML=dateFormated;
	}
	else{
		document.getElementById("valid_till_span").value=dateFormated;
	}
}

function checkPrice(alertText){
	adType=document.getElementById("adType").options[document.getElementById("adType").selectedIndex].value;
	price=document.getElementById("price").value;
	if(adType==1){
		if(price<5000){
			if(confirm(alertText)){
				return true;
			}
			else{
				return false;
			}
		}
	}
	return true;
}

function updateCredits(newCreditsValue){
	document.getElementById("headerCredits").innerHTML=newCreditsValue;
}

function checkLength(element,maxLength){
	currentLength = element.value.length;
	charsLeft = maxLength-currentLength;
	infoElement=document.getElementById(element.id+"_info");
	infoElement.innerHTML=charsLeft;
	color="green";
	if(charsLeft<0){
		color="red";
	}
	if(infoElement.style.color!=color){
		infoElement.style.color=color;
	}
}

 function showTab(id){
  	//alert(1);
  	for(i=0;i<tabIdArray.length;i++){
  		state="none";
  		current = tabIdArray[i];
  		if(document.getElementById(current+"_link")){
  			document.getElementById(current+"_link").className="";
  		}
  		if(current==id){
  			state="block";
  			document.getElementById("selectedTab").value=current;
  			if(document.getElementById(current+"_link")){
  				document.getElementById(current+"_link").className="selected";
  			}
  		}
  		document.getElementById(current).style.display=state;
  	}
	if(document.getElementById("selectedTab"))document.getElementById("selectedTab").value=id;
	for(i=1;i<11;i++){
		if(document.getElementById("selectedTab"+i))document.getElementById("selectedTab"+i).value=id;
	}
	//alert(document.getElementById("selectedTab").value);
  }

function addNewPeriod(projectId,errorMesage){
	selectedTab=document.getElementById("selectedTab").value;
	project_period_start_date=document.getElementById("project_period_start_date").value;
	project_period_end_date=document.getElementById("project_period_end_date").value;
	if(project_period_start_date!="" && project_period_end_date!=""){
		document.location="admin.php?view=projects&action=newPeriod&project_id="+projectId+"&selectedTab="+selectedTab+"&project_period_start_date="+project_period_start_date+"&project_period_end_date="+project_period_end_date;
	}
	else{
		alert(errorMesage);
	}
}

function adminLogout(){
	document.forms["admin_logout_form"].submit();
}

function activateProjectPeriod(projectId,id){
	selectedTab=document.getElementById("selectedTab").value;
	document.location="admin.php?view=projects&action=activatePeriod&project_id="+projectId+"&periodId="+id+"&selectedTab="+selectedTab;//+"&project_period_start_date="+project_period_start_date+"&project_period_end_date="+project_period_end_date;
}


function addTag(tag,lang){
	if(tag && tag!=""){
		document.getElementById("tags_"+lang).value=tag;
		document.forms["forma"].submit();
	}
}

function removeTag(baseUrl, tagId,realestateId){
	selectedTab=document.getElementById("selectedTab").value;
	document.location=baseUrl+"?view=realestates&selectedTab="+selectedTab+"&selectedRealestateId="+realestateId+"&deleteTagId="+tagId;
}

function generateTags(baseUrl, realestateId){
	selectedTab=document.getElementById("selectedTab").value;
	document.location=baseUrl+"?view=realestates&selectedTab="+selectedTab+"&selectedRealestateId="+realestateId+"&autoGenerateTags=true";
}

function activateAllAds(){
	for(i=0;i<2000;i++){
		if(document.getElementById("activate_"+i)==null)break;		
		document.getElementById("activate_"+i).checked = document.getElementById("activateAll").checked;
	}
}

function deleteAllAds(){
	for(i=0;i<2000;i++){
		if(document.getElementById("delete_"+i)==null)break;		
		document.getElementById("delete_"+i).checked = document.getElementById("deleteAll").checked;
	}
}


var imageNumber = 1;
function createInputs(n){
	removeInputs(imageNumber);
	imageNumber = n;
	for(i=1;i<parseInt(imageNumber)+1;i++){
		newInput = document.createElement('input');
		newInput.setAttribute("id","fileInput"+i);
		newInput.setAttribute("name","fileInput"+i);
		newInput.setAttribute("type","file");
		newInput.setAttribute("size","30");
		newInput.setAttribute("style","display:block;margin-bottom:5px;");
		document.getElementById("fileInputHolder"+i).appendChild(newInput);
	}
	
	document.getElementById("imageSave").style.display=n>0?"block":"none";
}

function removeInputs(){
	//alert(document.getElementById("fileInput1")!=null);
	if(document.getElementById("fileInput1")!=null){
		for(i=1;i<parseInt(imageNumber)+1;i++){
			document.getElementById("fileInputHolder"+i).removeChild(document.getElementById("fileInput"+i));
		}
	}
}




