var LANG=new Array();
LANG["cmn_form_empty"]="значение не указано";
LANG["cmn_form_num"]="не цифровое значение";
LANG["cmn_form_incorrect"]="неправильный формат";
LANG["cmn_form_char_max_prefix"]="Пожалуйста введите максимум";
LANG["cmn_form_char_min_prefix"]="Пожалуйста введите минимум";
LANG["cmn_form_char_postfix"]="симв.";
LANG["cmn_form_field"]="Поле";
function dm_window_open(file, width, height){
	window.open(file, "_blank", "width="+width+",height="+height+",titlebar=no,toolbar=no,location=no,status=no,menubar=yes,scrollbars=yes,resizable=yes");
}

function dm_show_el(el){
	document.getElementById(el).style.display='inline';
}

function dm_hide_el(el){
	document.getElementById(el).style.display='none';
}

function dm_write_to_el(el,text){
	document.getElementById(el).innerHTML=text;
}
function add_to_cart(id,module,num){
	if (document.getElementById("module_id"+id)){
		var check=document.getElementById("module_id"+id);
		var td=check.parentNode;
		td.innerHTML='<a href="index.php?page=cart"><img src="theme/default/img/notepad_small.gif" border="0" width="20" height="30" title="Объявление находится в блокноте"/></a>';
	}
	if (document.getElementById("pmodule_id"+id)){
		var pcheck=document.getElementById("pmodule_id"+id);
		var ptd=pcheck.parentNode;
		ptd.innerHTML='<a href="index.php?page=cart"><img src="theme/default/img/notepad_small.gif" border="0" width="20" height="30" title="Объявление находится в блокноте"/></a>';
	}
	if (module=="car"){
		document.getElementById("cart_car").innerHTML='автомобилей: <a href="index.php?page=cart" class="blue_big"><b>'+num+'</b></a>';
	}
	
	
}

var browser;

function dm_detect_browser(){
	var detect = navigator.userAgent.toLowerCase();
	if (detect.indexOf("konqueror") + 1){
		browser="konqueror";
	} else if (detect.indexOf("safari") + 1){
		browser="safari";
	} else if (detect.indexOf("omniweb") + 1){
		browser="omniweb";
	} else if (detect.indexOf("opera") + 1){
		browser="opera";
	} else if (detect.indexOf("webtv") + 1){
		browser="webtv";
	} else if (detect.indexOf("icab") + 1){
		browser="icab";
	} else if (detect.indexOf("msie") + 1){
		browser="msie";
	} else if (!detect.indexOf("compatible") + 1){
		browser="mozilla";
	} else {
		browser="unknown";
	}
}
dm_detect_browser();

dm_validate_form=function(form){
	this.form=form;
	this.error=new Array(0);
	this.validate_empty=validate_empty;
	this.validate_regexp=validate_regexp;
	this.validate_length=validate_length;
	this.validate_int=validate_int;
	this.validate_float=validate_float;
	this.push_error=push_error;
	this.add_fields=add_fields;
	this.clean_field=clean_field;
}
	function validate_empty(el,title){
		if (el.options){
            if (el.options[el.options.selectedIndex].value!="") return true;
            
            this.push_error(el,title,LANG["cmn_form_empty"]);
            return false;
			
		} else if (el.length){
              	for (var z=0;z<el.length;z++){
                        if (el[z].checked) return true;
                }
                this.push_error(el,title,LANG["cmn_form_empty"]);
                return false;
        } else if (el.value==null || !el.value.length>0){
                this.push_error(el,title,LANG["cmn_form_empty"]);
                return false;
        }
        return true;
	}
	
	function validate_int(el,title){
        if (("" + parseInt(el.value)) != el.value){
			this.push_error(el,title,LANG["cmn_form_num"]);
			return false;
        }
		return true;		
	}

	function validate_float(el,title){
        if (("" + parseFloat(el.value)) != el.value){
			this.push_error(el,title,LANG["cmn_form_num"]);
			return false;
        }
		return true;		
	}
	
	
	function validate_regexp(el,name,exp){
		if (!el.value.match(eval(exp))){
			this.push_error(el,name,LANG["cmn_form_incorrect"]);
			return false;
		}
		return true;
	}
	
	function validate_length(el,name,min,max){
        if (el.length){
                for (var z=0;z<el.length;z++){
                        if (el[z].checked) return true;
                }
                this.push_error(el,name,LANG["cmn_form_empty"]);
                return false;
        }
        if (el.value==null || !el.value.length>0){
                this.push_error(el,name,LANG["cmn_form_empty"]);
                return false;
        } else if (min!=null){
        	if (el.value.length<min){
        		this.push_error(el,name,LANG["cmn_form_char_max_prefix"]+min+LANG["cmn_form_char_postfix"]);
                return false;
        	}
        } else if (max!=null){
        	if (el.value.length>max){
        		this.push_error(el,name,LANG["cmn_form_char_min_prefix"]+max+LANG["cmn_form_char_postfix"]);
                return false;
        	}
        }
        return true;
	
	}
	
	function push_error(el,title,message){
		if (el.type!="text" && browser!="mozilla" && el.focus!=null){
			//el.focus();
		}
		this.error[this.error.length]=new Array(el.name,LANG["cmn_form_field"]+' "'+title+'": '+message);
	}
	
	function clean_field(el){
		if (el.value){
			el.value="";
		}
	}
	
	function add_fields(fields){
		for (var i in fields){
			var arr=dm_explode(":",fields[i]);
			switch (arr[1]) { 
				case "null":
					this.validate_empty(this.form.elements[arr[0]],arr[2]);
				break;
				case "int":
					this.validate_int(this.form.elements[arr[0]],arr[2]);
				break;
				case "float":
					this.validate_float(this.form.elements[arr[0]],arr[2]);
				break;	
				case "min":
					this.validate_length(this.form.elements[arr[0]],arr[2],arr[3],null);
				break;				
				case "max":
					this.validate_length(this.form.elements[arr[0]],arr[2],null,arr[3]);
				break;	
				case "clean":
					this.clean_field(this.form.elements[arr[0]]);
				break;
			
			}
		}
	}

function dm_explode(separator,_string){
	var new_string=new String(_string);
	var i=0;
	var arr=new Array();
	while (new_string.indexOf(separator)!=-1){
		var last_string=new_string.substring(0,new_string.indexOf(separator));
		new_string=new_string.substring(new_string.indexOf(separator)+1,new_string.length);
		arr[i]=last_string;
		i++;	
	}
	if (new_string.length>0) {
		arr[i]=new_string;
	}
	
	return arr;
}

function dm_frame_build(url,id) {
	var i_frame = document.getElementById(id);
	if ( i_frame.contentWindow ) {
      	i_frame.contentWindow.location.replace( url );
    } else {
        i_frame.src = url;
    }
}

function dm_car_model(make_id,model_id,selected){
	var model=document.getElementById(model_id);
	model.options.length=null;
	model.options[0] = new Option( 'Загрузка...', '' );
	dm_frame_build( 'call/car_model.php?make_id='+make_id+'&name='+model_id+'&selected='+selected,'frame_car_make');
}

function dm_car_model_selected(model,selected){
	var model=document.getElementById(model);
	var length=model.options.length;
	if (length){
		for (var i=0;i<length;i++){
			if (model.options[i].value==selected){
				model.selectedIndex=i;
				break;
				return true;
			}
		}	
	}
}

function dm_car_model_build(arr,model){
	var model=document.getElementById(model);
	model.options.length=0;
	model.options[0] = new Option( 'Модель', '' );
	for (var i=0;i<arr.length;i++){
		var value=dm_explode('^',arr[i]);
		model.options[i+1] = new Option (value[1], value[0]);
	}
}

function dm_region(country_id,city_id,selected){
	var city=document.getElementById(city_id);
	city.options.length=null;
	city.options[0] = new Option( 'Загрузка...', '' );
	dm_frame_build( 'call/region.php?country_id='+country_id+'&name='+city_id+'&selected='+selected,'frame_region');
}

function dm_region_selected(city,selected){
	var city=document.getElementById(city);
	var length=city.options.length;
	if (length){
		for (var i=0;i<length;i++){
			if (city.options[i].value==selected){
				city.selectedIndex=i;
				break;
				return true;
			}
		}	
	}
}

function dm_region_build(arr,city){
	var city=document.getElementById(city);
	city.options.length=0;
	city.options[0] = new Option( 'Город', '' );
	for (var i=0;i<arr.length;i++){
		var value=dm_explode('^',arr[i]);
		city.options[i+1] = new Option (value[1], value[0]);
	}
}

function dm_symbols_remain(max_symbols,el_form,el_symbol){
	if(el_form.value.length > max_symbols){
		el_form.value = el_form.value.substring(0, max_symbols);	
		return;
	}
	var span_list=document.getElementsByTagName("span");
	for (e=0;e<span_list.length;e++){
		if (span_list[e].getAttribute("name")==el_symbol){
			document.getElementsByTagName("span")[e].innerHTML = max_symbols-el_form.value.length;
		}	
	}
}

function file_type_check(el,btn,types) {
	if (!types) types=ALLOWED_TYPES;
	var fn = document.getElementById(el).value;
	if (fn == ""){
		document.getElementById(btn).disabled = true;
	} else {
		var ext = fn.split(".");
		if (ext.length==1)
		ext = '.noext.';
		else
		ext = "."+ext[ext.length-1].toLowerCase();
		//alert(ext);
		var error=true;
		//alert(ext);
		var arr=dm_explode(";",types);
		for (var i=0;i<arr.length;i++){
			if (arr[i]==ext){
				error=false;
			} 
		} 
		var index=fn.lastIndexOf("\\");
		
		var file_name=fn.slice(index+1,fn.length);
		if (file_name){
			if (file_name.match(/[а-яА-я]+/)) { 
				alert("В названии файла присутствуют недопустимые символы - русские буквы. ");
				error=true;
			}		
		}
		if (error==true) {
			document.getElementById(btn).disabled = true;
		} else {
			document.getElementById(btn).disabled = false;
		}
	}

}

function dm_set_img(img_id,sid){
	var sid=document.getElementById(sid);
	var arr=dm_explode(";",sid.value);
	if (sid.value==""){
		sid.value=img_id;
	} else {
		var error=false;
		for (var i=0;i<arr.length;i++){
			if (arr[i]==img_id){
				error=true;
			} 
		}
		if (error==false){
			sid.value=sid.value+";"+img_id;
		}
	}
	
}

function dm_delete_img(img_id,sid){
	if (window.confirm('Удалить?')){
		var sid=document.getElementById(sid);
		var arr=dm_explode(";",sid.value);
		var value='';
		for (var i=0;i<arr.length;i++){
			if (arr[i]==img_id){
				delete arr[i];
			} else if (arr[i]=='false'){
				delete arr[i];
			} else {
				value+=arr[i]+';';
			}
		}
		sid.value=value.substring(0,value.length-1);
		var img=document.getElementById(img_id);
		img.parentNode.parentNode.removeChild(img.parentNode);
		
		if (img_num>1){
			img_num--;
		}
		document.getElementById('img_count').innerHTML=12-img_num+1;
	}
}

function dm_img_upload(img,thumb,img_id,sid){
	document.getElementById('img_count').innerHTML=12-img_num;
	document.getElementById('indicator').style.display='none';
	document.getElementById('img_url').style.display='none';
	document.getElementById('img').style.display='inline';
	document.getElementById('img_upload').reset();
	document.getElementById('btn_upload').disabled = true;
	
	var table=document.getElementById('img_table');
	var i = document.createElement('img');
	var text=document.createElement('span');
	text.innerHTML="<br><a href=\"javascript:dm_delete_img("+img_id+",'"+sid+"');\">Удалить</a>";
	i.src = thumb;
	i.id=img_id;
	
	var row=table.firstChild.lastChild;
	var cells=row.cells;
	if (img_num<=1 && row.cells[0]) {
		row.removeChild(row.cells[0]);
	}
	
	var length=cells.length;
	if (length>=2){
		var row2=row.cloneNode(false);
		table.firstChild.appendChild(row2);
		row=row2;
		length=0;
	}
	var td=row.insertCell(length);
	td.appendChild(i);
	td.appendChild(text);
	dm_set_img(img_id,sid);
	img_num++;
	document.getElementById('sort').value=img_num*10;
}

function set_check_range(the_form, do_check,el) {
	for (var i = 0; i < document.forms[the_form].elements.length; i++) {
		if (document.forms[the_form].elements[i].type=='checkbox'){
			if (el!=undefined) {
				var arr=document.forms[the_form].elements[i].name.match(/^(.*)\[\d*\]$/i);
				if (arr!=null && arr[1] && arr[1]==el){
					document.forms[the_form].elements[i].checked = do_check;
				}
			} else {
				document.forms[the_form].elements[i].checked = do_check;
			}
		}
	}
	return true;
} 
function displaySelects(mode){
    if (browser=="msie" || browser=="opera"){    
		allInputs=document.getElementsByTagName('select');
        for (var i=0;i < allInputs.length; i++){
                allInputs[i].style.display=mode? 'inline' : 'none';
        }
    }
}


function dm_show_login(){
	var div=document.getElementById('login');
	var height=YAHOO.util.Dom.getViewportHeight();
	YAHOO.util.Dom.setStyle(div, 'display', 'block'); 
	YAHOO.util.Dom.setStyle(div, 'top', Math.ceil(height/2-160)); 
	document.getElementById("main_login").focus();
	displaySelects(false);
	return false;
}

function dm_hide_login(){
	var div=document.getElementById('login');
	YAHOO.util.Dom.setStyle(div, 'display', 'none'); 
	displaySelects(true);
	return false;
}

function preventSelectDisabled(oSelect) {
    var isOptionDisabled = oSelect.options[oSelect.selectedIndex].disabled;
    if(isOptionDisabled) {
        oSelect.selectedIndex = oSelect.defaultSelectedIndex;
        return false;
    }
    else oSelect.defaultSelectedIndex = oSelect.selectedIndex;
    return true;
}