
//BROWSER DETECTION
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var ie4 = document.all;
 
function DetectBrowser(){ //Detectarea browserului folosit
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=(navigator.userAgent.indexOf("Opera")>-1 && document.getElementById)?1:0
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	return this
}
var bw=new DetectBrowser();

//FIELDS
function imgField(field, img){
	if(img == "over") {
		border_color 	= "#3574A1";
		border_style 	= "dashed";
		bgcolor 		= "#F6F6F6";
	}else {
		border_color 	= "#cccccc";
		border_style 	= "solid";
		bgcolor 		= "#FFFFFF";
	}
	//field.style.borderColor 	= border_color;
	//field.style.borderStyle 	= border_style;
	field.style.backgroundColor = bgcolor;
}

function imgField2(field, img){
	
	if(field.type == 'textarea'){
		if(img == "over") field.className='textarea1_over';
		else field.className='textarea1';
	}else if(field.type == 'text' || field.type == 'password' || field.type == 'file'){
		if(img == "over") field.className='field1_over';
		else field.className='field1';
	}else{
		//bgcolor
		if(img == "over") bgcolor = "#FDFDFD";
					 else bgcolor = "#F9F9F9";
		field.style.backgroundColor = bgcolor;
		
		//border color
		if(img == "over") border_color = "#575656";
					 else border_color = "#999999";
		field.style.borderColor = border_color;
	}		
}

function imgfield2(field, img){
	
	if(field.type == 'textarea'){
		if(img == "over") field.className='textarea1_over';
		else field.className='textarea1';
	}else if(field.type == 'text' || field.type == 'password' || field.type == 'file'){
		if(img == "over") field.className='field1_over';
		else field.className='field1';
	}else{
		//bgcolor
		if(img == "over") bgcolor = "#FDFDFD";
					 else bgcolor = "#F9F9F9";
		field.style.backgroundColor = bgcolor;
		
		//border color
		if(img == "over") border_color = "#575656";
					 else border_color = "#999999";
		field.style.borderColor = border_color;
	}		
}

function imgSettings(field, img){
	field = $('#' + field);
	field.toggleClass("about_field_background2");
}

function imgSettingsTextarea (field, img){
 
field = $('#' + field);
field.toggleClass("about_textarea_background2");
} 


//BUTS
function imgBut(but, img){
	but.src = img;
}


//Reset Form
function resetForm(formName){
	if(ns6) form = eval("document.getElementById('"+formName+"')");
	else if(ns4) form = eval("document."+formName);
	else form = eval("document.all."+formName);
	form.reset();
}



function focus2(field){
	if(field.value == 0) field.value = '';
}	


function blur2(field){
	if(field.value == '') field.value = '0';
}	


function fieldValue(field_name){
	if(ns6) field = eval("document.getElementById('"+field_name+"')");
	else if(ns4) field = eval("document."+field_name);
	else field = eval("document.all."+field_name);
	return field.value;
}

function setFieldValue(field_name, val){
	if(ns6) field = eval("document.getElementById('"+field_name+"')");
	else if(ns4) field = eval("document."+field_name);
	else field = eval("document.all."+field_name);
	return field.value;
	field.value = val;
}

function writeIn(field_name, val){
	if(ns6) field = eval("document.getElementById('"+field_name+"')");
	else if(ns4) field = eval("document."+field_name);
	else field = eval("document.all."+field_name);
	field.innerHTML = val;
}



function formatNR(nr, dec)
{
str = "" + Math.round(eval(nr) * Math.pow(10,dec));
while(str.length < dec)
	str = "0" + str;
decidx = str.length - dec;
tmp = str.substring(0,decidx);
if(tmp == '')
	tmp = '0';
if(dec > 0)
	tmp = tmp + '.' + str.substring(decidx, str.length);
return(tmp);
}



function getkey(e)
{
if (window.event)
   return window.event.keyCode;
else if (e)
   return e.which;
else
   return null;
}


function goodchars(e, goods)
{
var key, keychar;
key = getkey(e);
if (key == null) return true;

// get character
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();
goods = goods.toLowerCase();

// check goodkeys
if (goods.indexOf(keychar) != -1)
	return true;

// control keys
if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
   return true;

// else return false
return false;
}



function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function
  

//Email Validation
function emailValid(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function formSubmit(form, act){
	document.forms[form].action = act;
	document.forms[form].submit();
}





function fieldOb(field_name){
	if(ns6) field = eval("document.getElementById('"+field_name+"')");
	else if(ns4) field = eval("document."+field_name);
	else field = eval("document.all."+field_name);
	return field;
}

function changeRowColor(row, color){
	rOb = fieldOb(row);
	rOb.style.backgroundColor = color;  
}






//Validates a field
function ValidateField(elem, name){
	if((elem.value == ''))	{ // ||(elem.value == 0)
    		alert('Warning: Not all mandatory fields (*) have been filled!');
    		elem.focus();
   	 		return(false);
   	}
	else if(name.indexOf('email') != -1){
			 	if(!emailValid(elem.value)){
					alert('Invalid E-mail address !');
					elem.focus();
   	 				return(false);
				}
	}
	return(true);
}

//Just Validates a field without (alert & focus)
function JustValidateField(elem){
	if((elem.value == '')||(elem.value == 0))	return(false);
	else if(name.indexOf('email') != -1){
			 	if(!emailValid(elem.value)){
   	 				return(false);
				}
	}
	return(true);
}


//Validate form for Mandatory fields to be filled properly
function Validate(form, fields){
    mandatory_fields = fields.split(',');
	for(i=0; i<mandatory_fields.length; i++){
		
		if(mandatory_fields[i].indexOf('|')){
			//groupped fields (at least one of them must be filled/selected)
			mandatory_group_fields = mandatory_fields[i].split('|');
			valid = 0;
			for(j=0; j<mandatory_group_fields.length; j++){
				elem = eval('form.' + mandatory_group_fields[j]);
				if(ValidateField(elem, mandatory_group_fields[j]) ) valid = 1;
				else return(false);
			}
			if(!valid){
				//no fields filled
				elem = eval('form.' + mandatory_group_fields[0]);
				if(!ValidateField(elem, mandatory_group_fields[0])) return(false);
			}
			
		}else{
			//single field
			elem = eval('form.' + mandatory_fields[i]);
			if(!ValidateField(elem, mandatory_fields[i])) return(false);
		}
		
	}//for
					
	return(true);
}


//Login Form Check
function checkLogin(form){
   	if(form.login_user.value == '')	{
    		alert('Insert Username');
    		form.login_user.focus();
   	 	return(false);
   	}
   	if(form.login_pass.value == '')	{
    		alert('Insert Password');
    		form.login_pass.focus();
   	 	return(false);
   	}
	return(true);
}

//Search Form Check
function checkSearch(form){
   /*
   if((form.price_search.value == '') && (form.bedrooms_search.value == '') && (form.bathrooms_search.value == '') )	{
    		alert('Please specify at least one search criterea!');
    		form.price_search.focus();
   	 	return(false);
   	}
	*/
	return(true);
}


function checkFormPass(form){
   	if(form.old_pass.value == '')	{
    		alert('Enter current password!');
    		form.old_pass.focus();
   	 	return(false);
   	}
	
   	if(form.new_pass_1.value == '')	{
    		alert('Enter new password!');
    		form.new_pass_1.focus();
   	 	return(false);
   	}
	
   	if(form.new_pass_2.value == '')	{
    		alert('Reenter new password!');
    		form.new_pass_2.focus();
   	 	return(false);
   	}
	
   	if(form.new_pass_1.value != form.new_pass_2.value)	{
    		alert('Error! You have reentered a different new password!');
    		form.new_pass_1.focus();
   	 	return(false);
   	}

	return(true);
}

var allowed_file_types = '';
var err_msg = 'Warning: File type not allowed...';

function ValidateFile(form, file_var){
	fis = eval('form.' + file_var);
	fis = fis.value.toLowerCase();
	fis_parts = fis.split('.');
	if(fis_parts.length == 1) ext = '';
						 else ext = fis_parts[fis_parts.length - 1];

	if(fis == ''){
		alert('Warning: No file specified...');
		return false;
	}else if(ext == ''){
		alert(err_msg);
		return false;
	}else{
		valid = false;
		allowed_file_types2 = allowed_file_types.toLowerCase();
		allowed_file_types2 = allowed_file_types2.split(',');
		for(i=0; i<allowed_file_types2.length; i++){
			if(ext == allowed_file_types2[i]) valid = true;
		}
		if(!valid) alert(err_msg);
		return valid;
	}

	return true;
}


function ValidateSubmitListing(form){
	if(!Validate(form, 'date_start_day,date_start_month,date_start_year,time_start_hour,time_start_min,time_end_hour,time_end_min,address,city,province,zip,price,mls,mls_link,supr,type_of_home,bedrooms,bathrooms')) return false;

	if(form.picture.value != '') {
		allowed_file_types = 'jpg,gif';
		err_msg = 'Warning: Picture file type not allowed...';
		if(!ValidateFile(form, 'picture')) return false;
	}
	
	if(form.spec.value != '') {
		allowed_file_types = 'doc,rtf,pdf,txt,htm';
		err_msg = 'Warning: Spec sheet file type not allowed...';
		if(!ValidateFile(form, 'spec')) return false;
	}
	
	return true;
}



function ValidateProfile(form){
	if(!Validate(form, 'user,pass1,pass2,fname,lname,email')) return false;
		
	if(form.photo.value != '') {
		allowed_file_types = 'jpg,gif';
		err_msg = 'Warning: Photo file type not allowed...';
		if(!ValidateFile(form, 'photo')) return false;
	}
	
	return true;
}

function GotoPage(pag, GET){
	if(ns6) form = eval("document.getElementById('formSearch')");
	else if(ns4) form = eval("document.formSearch");
	else form = eval("document.all.formSearch");
	form.action = PHP_SELF + "?"+GET+"&pag="+pag;
	form.submit();
}

function ShowUrlField(id){
	var arr = new Array();
	arr[0] = '';
	arr[1] = 'image_table';
	arr[2] = 'flash_table';
	arr[3] = 'embed_table';
	
	show = eval("document.getElementById('" + arr[id] + "').style");
	
	for(i = 1; i < 4; i++){
		hide = eval("document.getElementById('" + arr[i] + "').style");
		hide.display = 'none';
	}
	
	show.display = 'block';

}

function ShowRestImage(){
	document.getElementById("image_section").style.visibility = 'visible';
	document.getElementById("text_section").style.visibility = 'hidden';
	document.getElementById("text_section").style.position = 'absolute';
}

function ShowRestText(){
	document.getElementById("image_section").style.visibility = 'hidden';
	document.getElementById("text_section").style.visibility = 'visible';
	document.getElementById("text_section").style.position = '';
}

function CreateTagsFromTitle(value, form){
	var vector = value.split(' ');
	
	bad_tags = new Array(',','.',':',';','_','-','',' ','HE','SHE','OR','THE','AND','OF','AS','IS','ARE','WE','ME','YOU','TO','A','IN','IT','THAT','WAS','FOR','ON','WITH','I','HIS','THEY','BE','AT','ONE','HAVE','THIS','FROM','HAD','BY','BUT','WHAT','SOME','CAN','WERE','THERE','WHEN','UP','USE','YOUR','HOW','SAID','AN','EACH','WHICH','DO','THEIR','TIME','IF','WILL','WAY','ABOUT','MANY','THEN','THEM','WRITE','WOULD','LIKE','SO','THESE','HER','LONG','MAKE','THING','SEE','HIM','TWO','HAS','LOOK','AM');
	
	vector2 = new Array();
	j = 0;
	
	tag_flag = 0;
	
	for(i = 0; i < vector.length; i++){
		for(k=0; k< bad_tags.length; k++){
			if(vector[i].toUpperCase() == bad_tags[k]){
				tag_flag = 1;
				
			}
		}
		if(tag_flag == 0){
			vector2[j] = vector[i];
			j++;
			
		}
		tag_flag = 0;
			
	}
	
	
	
	var string = vector2.join(',');
	
	if(form == null){
	
		if(document.frmRamble.tags.value != null && document.frmRamble.tags.value != '' && document.frmRamble.tags.value != ' ')
			document.frmRamble.tags.value += ',' + string;
		else
			document.frmRamble.tags.value += string;
			
		newval = document.frmRamble.tags.value;
		arr = newval.split(",");
		arr = unique(arr);
		document.frmRamble.tags.value = arr.join(",");	
	}
	else{
		frm = eval("document." + form + ".tags");
		if(frm.value != null && frm.value != '' && frm.value != ' ')
			frm.value += ',' + string;
		else
			frm.value += string;	
			
		newval = frm.value;
		arr = newval.split(",");
		arr = unique(arr);
		frm.value = arr.join(",");		
	}			
}

/**
 * Removes duplicates in the array 'a'
 * @author Johan Känngård, http://dev.kanngard.net
 */
function unique(a) {
	tmp = new Array(0);
	for(i=0;i<a.length;i++){
		if(!contains(tmp, a[i])){
			tmp.length+=1;
			tmp[tmp.length-1]=a[i];
		}
	}
	return tmp;
}

/**
 * Returns true if 's' is contained in the array 'a'
 * @author Johan Känngård, http://dev.kanngard.net
 */
function contains(a, e) {
	for(j=0;j<a.length;j++)if(a[j].toUpperCase() == e.toUpperCase())return true;
	return false;
}

 
function imgIncomingShoutsTextarea (field, img){
 
field = $('#' + field);
field.toggleClass("incoming_shouts_textarea2");
} 

function OpenRow (field){
	ob = fieldOb(field);
	if(ob != null)
		if(ob.className == 'ascuns') ob.className = '';
		else if(ob.className != 'ascuns') ob.className = 'ascuns';
} 

function SelectBoxes(type, ids){
	arr_ids = ids.split(',');
	for(i = 0; i < arr_ids.length; i++){
		x = MM_findObj('select_' + arr_ids[i]);
		if (x) if (type=='all') x.checked = true; else x.checked = false;
	}
}

function DeleteMultiple(what) {
	document.forms['frm'].action = what; 
	document.forms['frm'].submit();	
}


function OpenAdDetailTopLinks(field_open, field_close) {
	obo = document.getElementById(field_open);
	if(obo.className == '') obo.className = 'ascuns';
	else obo.className = '';
	obc = document.getElementById(field_close);
	obc.className = 'ascuns';
}

function HideVideocatalogCombo(valoare) {
	ob = document.getElementById('vide_catalog_combo_place');
	if(valoare == 0) ob.className = 'ascuns';
	else ob.className = '';
}

function HideWebLinkInVideo(valoare) {
	ob = document.getElementById('web_link_form');
	if(valoare == 0) ob.className = 'ascuns';
	else ob.className = '';
}

function BannersDivHeight(){
	if(ie4){
		ob = document.getElementById('banners');
		if(ob != null){
			divh  = ob.offsetHeight;
			ob.style.height = parseInt(divh) + 20 + 'px';
		}
	}
}

function WriteInSearchField(from, to){
	ob_from = document.getElementById(from);
	ob_to = document.getElementById(to);
	if(ob_from != null && ob_to != null){
		ob_to.value = ob_from.value;
	}
}

function LoadFLVPlayer(){
	ob1 = fieldOb('player_no_captions');
	if(ob1 != null) FLVPlayerNoCaptions('player_no_captions');
	ob2 = fieldOb('player');
	if(ob2 != null) FLVPlayerCaptions('player');
}

function LoadFLVPlayerForBanners(name, captions){
	if(captions != 1) FLVPlayerNoCaptions(name);
	else FLVPlayerCaptions(name);
}