function replace_schar(str){
	str.replace(String.fromCharCode(47),' **slash** ');
	str.replace(String.fromCharCode(92),' **backslash** ');
	str.replace(String.fromCharCode(39),' **apicesing** ');
	str.replace(String.fromCharCode(34),' **dapice** ');
	str.replace(String.fromCharCode(133),"&agrave;");
	str.replace(String.fromCharCode(138),"&egrave;");
	str.replace(String.fromCharCode(130),"&eacute;");
	str.replace(String.fromCharCode(141),"&igrave;");
	str.replace(String.fromCharCode(149),"&ograve;");
	str.replace(String.fromCharCode(151),"&ugrave;");
	return;
}

function conta_chr(limit){
  var lenMax=limit;
  var strlength=document.view_edit.descrizione.value.length;
  /*total = eval("600");*/
  total = limit;
  char = eval(document.view_edit.descrizione.value.length);
  left = eval(total - char);
  if (left <= "-1"){
    var dif = eval(char - limit);
    var value = document.view_edit.descrizione.value.substr(0,char-dif);
    document.view_edit.descrizione.value = value;
    var left = "0";
  }
  document.view_edit.chr_counter.value=left;
}

function conta(limit){
  var lenMax=limit;
  var strlength=document.form_reg_re.descrizione.value.length;
  /*total = eval("600");*/
  total = limit;
  char = eval(document.form_reg_re.descrizione.value.length);
  left = eval(total - char);
  if (left <= "-1"){
    var dif = eval(char - limit);
    var value = document.form_reg_re.descrizione.value.substr(0,char-dif);
    document.form_reg_re.descrizione.value = value;
    var left = "0";
  }
  document.form_reg_re.counter.value=left;
}

function conta_prop_des(limit){
  var lenMax=limit;

  var strlength=document.frm_prop_data.pro_desc.value.length;
  /*total = eval("600");*/
  total = limit;
  char = eval(document.frm_prop_data.pro_desc.value.length);
  left = eval(total - char);
  if (left <= "-1"){
    var dif = eval(char - limit);
    var value = document.frm_prop_data.pro_desc.value.substr(0,char-dif);
    document.frm_prop_data.pro_desc.value = value;
    var left = "0";
  }
  document.frm_prop_data.counter.value=left;
}

function checkNewRE(frm){

	if ( (frm.nome.value=='') || (frm.descrizione.value=='') || (frm.indirizzo.value=='') || (frm.mail.value=='') || (frm.telefono.value=='')  || (frm.username.value=='') || (frm.password.value=='') || (frm.website.value=='') || (frm.nation.value=='') || (frm.city.value==''))
	{
		alert ('Data missing, please compiled all the fields');
		return;
	}

	var ok=true;
	var a=frm.mail;
	var lng=a.value;
	var pe=a.value.indexOf('.');
	var ss=a.value.indexOf(' ');
	var lch=a.value.length-1;
	var atr=a.value.indexOf('@');
	var att=atr+1;
	var sq=a.value.substring(0,atr); 
	var sw=a.value.substring(atr,pe); 
	var se=a.value.substring(pe,lch);
	if((atr<1) || (pe==att) || (pe==lch) || (pe<1)  || (ss != -1) || (sq.length<=2) || (sw.length<=3) || (se.length<=1) || (lng=='')) {
		alert('Wrong email address!');
		return;
	}

	if (frm.descrizione.value.length>600){
		alert ('Description too long, you have typed: '+frm.descrizione.value.length+' chars (the limit is 1500)');
		return;
	} else {
		replace_schar(frm.descrizione.value);

	}

	if (frm.password.value.length<4){
		alert ('Your password is too small: you have to insert minimun 4 chars');
		return;
	}
	
	if ((isNaN(frm.telefono.value)) || (frm.telefono.value.length<=8)){
		alert ('Error, you have typed a wrong phone number');
		return;
	}

    frm.submit();
}

function checkRqsDel(frm){
  if (frm.request.value==""){
    alert('You have to compile the form with the motivations of your request.. Thanks a lot!');
    return;
  } else {
    frm.submit();
  }
}

function checkNewProposal(frm){
  if (frm.title.value==""){
    alert('Data missing, please compile all fields with *');
    return;
  }

  if (frm.price.value==""){
    alert('Data missing, please compile all fields with *');
    return;
  }

  if (frm.pro_desc.value==""){
    alert('Data missing, please compile all fields with *');
    return;
  }

  if (frm.sqm.value==""){
    alert('Data missing, please compile all fields with *');
    return;
  }

  if (frm.city.value==""){
    alert('Data missing, please compile all fields with *');
    return;
  }

  if (isNaN(frm.price.value)){
    alert('Error: you have typed a wrong price value, you can type only numerical value');
    return;
  }

  if (isNaN(frm.sqm.value)){
    alert('Error: you have typed a wrong SQM value, you can type only numerical value');
    return;
  }

  frm.submit();
}

function erase_conf(id_re,id_pro) {
	var answer = confirm("Are you sure you want to erase this property?")
	if (answer){
		window.location = "erase_pro.php?id_re="+id_re+"&id_pro="+id_pro;
	}
	else{
        return;
	}
}

function set_th(param,id_re,id_pro) {
    if (param == 'none') {
      alert("You can't set this photo as thumb!");
    } else {
	    window.location = "./uploads/set_th.php?thumb_n="+param+"&id_re="+id_re+"&id_pro="+id_pro;
    }
}

function erase_photo(param,id_re,id_pro) {
    if (param == 'none') {
      alert("You can't erase nothing.. There's not photos here!");
    } else {
      var answer = confirm("Are you sure you want all photos uploaded?")
      if (answer) {
	    window.location = "./uploads/delete_all_photo.php?id_re="+id_re+"&id_pro="+id_pro;
      }
    }
}

function ytpp(str) {
  var top = 'top='+ Math.floor((screen.height-200)/2)+',';
  var left = 'left='+ Math.floor((screen.width-400)/2)+',';
  searchWin = window.open(str,'esempio','scrollbars=no,resizable=no,width=400,height=200,'+top+left+'status=no,location=no,toolbar=no');
}

function check_cdw(cdw,form) {
  if (cdw==0) {
    alert("You can't upload this photo, you can upload max 10 photos per property!");
    return;
  } else {
    form.submit();
  }
}

function viewImages(num, id_re, id_pro) {
    var newImg = new Image();

    for (i=1; i<=nun; i++) {
        imgSrc = 'http://www.venice-cera.it/web/wp-content/themes/arthemia-premium/REPS/uploads/'+id_re+'/'+id_pro+'/'+i+'.jpg';
        newImg.src = imgSrc;
        var height = newImg.height;
    	var width = newImg.width;
        var new_height = (height*530)/width;
    	var margin_top = (318 - new_height)/2;
        if (width>=height) {
            document.getElementById("photo_container").innerHTML = "<IMG src='"+imgSrc+"' class='img_player' style='margin:0px;padding:0px;vertical-align:middle;width:530px;margin-top:"+margin_top+"px'>";
        } else {
    	    document.getElementById("photo_container").innerHTML = "<IMG src='"+imgSrc+"' class='img_player' style='margin:0px;padding:0px;vertical-align:middle;height:318px;'>";
	    }
    }
}
