function preSend(){
    var pForm = document.getElementById('form_add_pic');
    var pDiv  = document.getElementById('in_pic');
	var house_id = document.getElementById('house_id');
	var id = "-1";
	if (house_id) {
	       id = house_id.value;	
		}

    removeChildNodes(pForm);
    copyTemplate(pForm, pDiv);
    sendPic(pForm,id);
}

function creatTD(url_p,photo_id){

    var TD = document.createElement("TD");
    var IMG = document.createElement("IMG");
    var A = document.createElement("A");
    var BR = document.createElement("BR");
    var del = document.createTextNode("delete");
	
    TD.setAttributeNode(creatAttr("class","center f85")); 
    //IMG.setAttributeNode(creatAttr("src","photos/pic7.gif")); 
    IMG.setAttributeNode(creatAttr("src",url_p)); 
	//IMG.setAttributeNode(creatAttr("photo_id",""+photo_id+""));
	IMG.photo_id = photo_id
    IMG.setAttributeNode(creatAttr("width","121"));
    IMG.setAttributeNode(creatAttr("height","87"));
	IMG.setAttributeNode(creatAttr("onclick","main_photo('"+url_p+"',"+photo_id+");return false;"));
    A.setAttributeNode(creatAttr("href",""));
    A.setAttributeNode(creatAttr("onclick","deletePic('"+url_p+"',"+photo_id+");return false;"));
    A.appendChild(del);
	
    TD.appendChild(IMG);
    TD.appendChild(BR);
	
	var span = document.createElement("SPAN");
	var tpl = document.getElementById('span_descr_tpl');
	copyTemplate(span, tpl);
	span.childNodes[1].name = "photo_desc_" + photo_id;
	span.childNodes[1].value = global_photo_descr;
	span.style.color = "#000";
	TD.appendChild(span);
	
	TD.appendChild(document.createElement("BR"));
    TD.appendChild(A);
    
	
	
    return TD;
}

function creatAttr(name,value){
   var attr = document.createAttribute(name);
   attr.nodeValue = value;
   return attr; 
}

function sendPic(dForm,house_id){
   //var dForm = document.getElementById('form_add_pic');
   doLoad("/add_pic.html",{form : dForm, house_id: house_id},"addPicteForm('',req.responseJS,req.responseText)");

}


function parent(obj,name){
	 var par = obj.parentNode;
	 if (par){
	    if (par.nodeName == name){
			par = parent(obj,name)
			}
	 }
	 return par;
}
	
function deletePic(urls,id){
	ajaxData("/delete_pic.html",{photo:urls,id:id},"deletepicteForm('"+urls+"',req.responseJS,req.responseText)");
}

function deletepicteForm(data,hash,text){
    if (text=="error"){
		if(hash.alert){
        	alert(hash.alert);
    	}
	}
    var pDiv  = document.getElementById('in_pic2');
    var m_tr = my_getElementByName(pDiv,"TR");
    for (var i=0; i < m_tr.length-1; i++){
        var m_td = my_getElementByName(m_tr[i],"TD");
	for (var ii=0; ii < m_td.length; ii++){
	    var m_img = my_getElementByName(m_td[ii],"IMG"); 
	    for (var j=0; j < m_img.length; j++){
	        var m_atr = m_img[j].attributes;
		for (var jj=0; jj < m_atr.length; jj++){
	            var test = m_atr[jj];
		    var as = 0;
		    if (m_atr[jj].nodeName=="src"){
		        if (m_atr[jj].nodeValue==data){
			    removeChildNodes(m_td[ii]);
			    m_tr[i].removeChild(m_td[ii]);
				delete_main_photo(data);
			    return;
			   }
		      }
	        }
	    } 
	}
    }

}

function delete_main_photo(url){
	//alert(document.getElementById("img_main_photo").src);
	//alert(url);
	var main_photo_url = document.getElementById("img_main_photo").src ;
	if ( main_photo_url.indexOf(url) != -1) {
	         var pDiv  = document.getElementById('in_pic');
		    var imgs = my_getElementByName(pDiv,"IMG");
			for (var i=0; i<imgs.length; i++){
				main_photo(imgs[i].src, imgs[i].photo_id);
				return;
	}
	main_photo("","")
	document.getElementById("main_photo").style.display = "none"
		
	}
	
}


function doLoad(url,data,function_on_complite){
    var req = new JsHttpRequest();
    // Code automatically called on load finishing.
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            eval(function_on_complite);
        }
    }
    //req.open("POST", url, true);
    req.loader = 'SCRIPT';
    req.open("GET", url, true);
    req.send(data);
}


function addPhotoInTable(obj,url_pic){
    var children = obj.childNodes;
    for(var i=0; i<children.length; i++){
        if (children[i].nodeType == 1){
            if (children[i].tagName == "TR"){
                var children_tr = children[i].childNodes;
                var numb_td = 0;
                for(var ii=0; ii<children.length; ii++){
                    if (children[ii].nodeType == 1){
                        if (children[ii].tagName == "TD"){
                            numb_td++;
                        }
                    }
                    if (numb_td==4)
                        brack;
                }        
            }
        }
    }
}

function my_getElementById(node,id){
}
function my_getElementByName(node,name){
    var m_obj = new Array();
	var ind = 0;
	//alert(node.tagName);
    if (node.nodeType != 1 )
	    return [];
	
	var children = node.childNodes;
    for(var i=0; i < children.length; i++){
        if (children[i].nodeType == 1){ 
		    if (children[i].tagName == name){
		 	m_obj[ind] = children[i];
            ind++;
			}
			var m_obj_new = my_getElementByName(children[i],name);
			for (var j=0; j < m_obj_new.length; j++){
			    m_obj[ind] = m_obj_new[j];
                ind++;
		 	}
	    }
    }
	return m_obj;	
}

function edit_house(id){
    document.getElementById("house_id").value = id;	
    document.getElementById("form_edit_house").submit();
}

// --------------------------------------------------
//                 Main photo
// --------------------------------------------------
function main_photo(url,photo_id){

   var div_main_photo  = document.getElementById('main_photo');
   div_main_photo.style.display = "";
   document.getElementById("img_main_photo").src = url;
   document.getElementById("input_main_photo").value = url;
   document.getElementById("input_main_photo_id").value = photo_id
}

// -------------------------------------------------------------
//  show menu from resources
//
function showMenuResources(prop){

    if(prop == "wanted"){
        showResource('rescontent','template_wanted','0');
        var a_link = document.getElementById('wanted');
        //var na_link = document.getElementById('appraiser');
        a_link.setAttribute("class", "current");
        a_link.className = "current";
        //na_link.setAttribute("class", "none");
        //na_link.className = "none";
    }
	
    //if(prop == "appraiser"){
    //   showResource('rescontent','template_appraiser','0');
    //    var a_link = document.getElementById('appraiser');
	//	var na_link = document.getElementById('wanted');
    //    a_link.setAttribute("class", "current");
    //    a_link.className = "current";
    //    na_link.setAttribute("class", "none");
    //    na_link.className = "none";
    //}

}

function showResource(div1,div2,flag){
	 var _div1 = document.getElementById(div1);
	 var _div2 = document.getElementById(div2);
	 removeChildNodes(_div1);
     copyTemplate(_div1, _div2);
}

function checkMark(mark,id){
	doLoad("/user_area_ajax_mark_house.html",{id:id},"editMark('"+ mark+"','"+id+"',req.responseJS,req.responseText)");
	
}
function editMark(mark,id,hash,mes){
 if (mes == "error"){
	 alert(hash.alert);
	 return;
	 }	
 var a_mark = document.getElementById("mark_"+id+"");
  var a__mark = document.getElementById("mark__"+id+"");
 if(mark=='sold'){
    a_mark.onclick = new Function("checkMark('sell','"+id+"');return false;");   
    a__mark.onclick = new Function("checkMark('sell','"+id+"');return false;");   
	removeChildNodes(a_mark);
	removeChildNodes(a__mark);
	var st = document.createElement("STRONG");
    var t = document.createTextNode("Mark as Sold");
	st.appendChild(t);
	a_mark.appendChild(st);
	copyTemplate(a__mark,a_mark);
 }	
 if(mark=='sell'){
	a_mark.onclick = new Function("return false;")
	a__mark.onclick = new Function("return false;")
	removeChildNodes(a_mark);
	removeChildNodes(a__mark);
	var st = document.createElement("STRONG");
    var t = document.createTextNode("Sold");
	st.appendChild(t);
	a_mark.appendChild(st);
	copyTemplate(a__mark,a_mark);
 }
}



function SetCookie(name, value, expires, path, domain, secure)
{
 expires = "01-dec-2020  01:01:01"
 var now = new Date();
 now.setTime(now.getTime() + 14 * 24 * 60 * 60 * 1000);
 expires = now;
 document.cookie = name + "=" + escape(value) +
                ((expires) ? "; expires=" + expires : "") +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "");
}

function GetCookieValueByName(name)
{
 var beg = document.cookie.indexOf(name+"=");
 if(beg==-1)
  return false;
  var end = document.cookie.indexOf(";", beg + name.length);
   if(end==-1)
     end = document.cookie.length;
     return unescape(document.cookie.substring(beg + name.length + 1, end));
}

function addFavorite(id){
   m = GetCookieValueByName('fav');
   
   if (m==false){
      SetCookie('fav', id,'/','/')
  }	  
   else{
     SetCookie('fav', m+'-'+id,'/','/')   
   }
   fav_img = document.getElementById('fav_'+id)
   if (fav_img) {
        fav_img.src = "http://static.fizber.com/images/icons/rem2.gif"
        fav_img.onclick = new Function("remFavorite("+id+");return false;")
   }
   fav_img = document.getElementById('fav_'+id+"_1")
   if (fav_img) {
       fav_img.src = "http://static.fizber.com/images/icons/rem2.gif"
       fav_img.onclick = new Function("remFavorite("+id+");return false;")
   }
   link = document.getElementById('afv_' + +id);
   if(link){
   		link.innerHTML = "Remove from favourites";
   		link.onclick = new Function("remFavorite("+id+");return false;")
   }
   setNumberFavorites();
}

function remFavorite(id){
   //alert("remFavorite")
   v = GetCookieValueByName('fav');
   m = ""
   if (v!=false){ m = (v+"").split('-'); }	 
   else return;  
   cnt = m.length;
   f = ""
   for (var i=0; i<cnt; i++){
       if(id!=m[i]){
	      if (f!="") {f =f +"-" + m[i] }
		  else {f =  m[i]}  	        
	   }
   }
   SetCookie('fav', f,'/','/');
   fav_img = document.getElementById('fav_'+id);
   if (fav_img) {
       fav_img.src = "http://static.fizber.com/images/icons/f5.gif"
       fav_img.onclick = new Function("addFavorite("+id+");return false;")
   }
   fav_img = document.getElementById('fav_'+id+"_1");
   if (fav_img){
       fav_img.src = "http://static.fizber.com/images/icons/f5.gif"
       fav_img.onclick = new Function("addFavorite("+id+");return false;")
   }
   link = document.getElementById('afv_' + +id);
   if(link){
   		link.innerHTML = "Add to favourites";
   		link.onclick = new Function("addFavorite("+id+");return false;")
   }
   setNumberFavorites();
}
	
function addFavorite2(id){
   m = GetCookieValueByName('fav');
   if (m==false){
      SetCookie('fav', id,'/','/')
   }	  
   else{
     SetCookie('fav', m+'-'+id,'/','/')   
   }
   fav_a = document.getElementById('fav_'+id)
   fav_a.className='remfav'
   fav_a.innerHTML = "Remove to Favorites"
   fav_a.onclick = new Function("remFavorite2("+id+");return false;")
   setNumberFavorites();
}


function remFavorite2(id){
   v = GetCookieValueByName('fav');
   m = ""
   if (v!=false){ m = (v+"").split('-'); }	 
   else return;  
   cnt = m.length;
   f = ""
   for (var i=0; i<cnt; i++){
       if(id!=m[i]){
	      if (f!="") {f =f +"-" + m[i] }
		  else {f =  m[i]}  	        
	   }
   }
   SetCookie('fav', f,'/','/');
   fav_r = document.getElementById('fav_'+id);
   fav_r.className = "addfav"
   fav_r.innerHTML = "Add to Favourites"
   fav_r.onclick = new Function("addFavorite2("+id+");return false;")
   setNumberFavorites()
}

function setNumberFavorites(){
   v = GetCookieValueByName('fav');
   m = ""
   cnt = 0
   if (v!=false){
       m = (v+"").split('-');
       cnt = m.length;
   }
   else cnt = 0;
   //alert (cnt+": cnt");
   if (cnt == 0){
       document.getElementById('menu_favorites').innerHTML = "You Have no Favorite Homes";
	   document.getElementById('menu_favorites').onclick = new Function("show_favorites_div(); return false;")
	   divf = document.getElementById('view_favorites')
	   if (divf){
		  divf.style.display="none";   
	   }
	   
   }
   else {
	   document.getElementById('menu_favorites').innerHTML = "Favourites " + cnt;
	   document.getElementById('menu_favorites').onclick = ""
	   divf = document.getElementById('view_favorites')
	   if (divf){
		  divf.style.display="";   
	   }
    }
}						