var IE=!!document.all;

function el(elemID){
	var x =document.getElementById(elemID); 
	if (!x) {
		return {
			Name: "null"
		};
	}
	return x;
};

function mailto(user,server){
	window.open('mailto:'+user+'@'+server,'mailto');
	return false;
}

function check_email(email) {
	 return ((trim(email)).search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) ? true : false;
}

function email_is_present(email) {
	 return (email.search(/\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+/) != -1) ? true : false;
}

function check_login(login) {
	 return ((trim(login)).search(/^[-_A-Za-z0-9]+$/) != -1) ? true : false;
}

function trim(str){
	str = str.replace(/[\s ]+$/,'').replace(/^[\s ]+/,'');
	return str;
}

function _max(ta,id,m) {

	n=m-trim(ta.value).length;
	if (n<0){
		ta.value = ta.value.substr(0,m);
		eval('len_'+id).innerHTML=0;
	}
	else {
		eval('len_'+id).innerHTML=n;
	}
}

function open_image(img_id,w,h,img_url){
	w = w+300; //	300
	sc = screen.height - 130;
	h = h+40; h = (sc<h)?sc:h;
	_top = (sc-h)/2;
	_top = (_top)?_top:20;
	window.open('/pages/photo_show.php?'+(img_id?('id='+img_id):('url='+img_url)),'new','width='+w + ', height=' + h + ', left=' + ((screen.width-w)/2) + ', top=' + _top + ', scrollbars=1' ).focus();
	return false;
}

function open_page(path, iw, ih){
	
	w = iw==''?Math.round(screen.width*0.8):iw;
	//w = w>1000?1000:w;
	h = ih==''?Math.round(screen.height*0.8):ih;
	//h = h>700?700:h;
	l = Math.round((screen.width-w)/2);
	t = Math.round((screen.height-h)/2);
	
	open(path,'new','width='+w+', height='+h+', left='+l+', top='+t+', scrollbars=0').focus();
	return false;
}


function show(obj){
	d='';
	for (i in obj) {
		if (i!='outerHTML' && i!='innerHTML') d += i + ' - ' + obj[i] + '; ';
	}
	alert(d);
}

function delete_ask(){
	return (confirm('Вы уверены в том, что хотите удалить позицию? Эту операцию невозможно будет отменить.'))?true:false;
}

var r_menu = new Array();
function menu(n){
	if (r_menu[n]==undefined) {
		r_menu[n] = eval('menu_div_'+n).innerHTML;
		eval('menu_div_'+n).innerHTML='';
		return false;
	} else if(r_menu[n]!='') {
		eval('menu_div_'+n).innerHTML = r_menu[n];
		r_menu[n] = '';
		return false;
	}
	else return true;
}
standart_form=0;



function check_form(t,reg,captcha){
	error_massage = '';
	
	if (reg) {
		if (trim(t.name.value)=='')	error_massage += "ваше имя, ";
		if (trim(t.login.value)=='')	error_massage += "логин, ";
		if (!check_email(t.email.value)) error_massage += "e-mail, ";
		if (trim(t.pass_1.value)=='') error_massage += "пароль, ";
		else if (trim(t.pass_1.value)!=trim(t.pass_2.value)) error_massage += "пароли не совпадают, ";
	} else {
		if (trim(t.name.value)=='')	error_massage += "ваше имя, ";
		if (!check_email(t.email.value)) error_massage += "e-mail, ";
	}

	if (standart_form) {
		if (trim(t.info.value)=='')	error_massage += "письмо, ";
	} else {
		for(i in need_feel){
			
			if ((t.elements['data['+i+']']).value!=undefined && trim(t.elements['data['+i+']'].value)==''){
				error_massage += need_feel[i] + ", ";
			}
		}
	}
	
	if (captcha){
		if (t.protect_code.value!=undefined && (t.protect_code.value=='' || t.protect_code.value.length<5) ){
			error_massage += "защита от спама, ";
		}
	}
	
	if (error_massage=='') return true;
	else {
		alert('Пожалуйста заполните поля: \n' + error_massage.substr(0,error_massage.length-2)+'.');
		return false;
	}
}


function choose_color(input_name){
	palette_window = window.open('/_admin/shop/palette.php?input_name='+input_name,'palette','toolbars=no, width=890, height=420');
}


function check_color(t){
	check_r = parseInt(t.value.substr(0,2), 16);
	check_g = parseInt(t.value.substr(2,2), 16);
	check_b = parseInt(t.value.substr(4,2), 16);
	
	if (isNaN(check_r)) check_r = 0;
	if (isNaN(check_g)) check_g = 0;
	if (isNaN(check_b)) check_b = 0;
	
	r = dec_to_hex(check_r, '',1);
	g = dec_to_hex(check_g, '',1);
	b = dec_to_hex(check_b, '',1);
	
	t.value = r+''+g+''+b;
	t.style.backgroundColor = '#'+r+''+g+''+b;
	t.style.color = 'rgb('+(255-check_r)+','+(255-check_g)+','+(255-check_b)+')';

}


function dec_to_hex(num_dec, num_hex,for_html){
	dec_to_hex_sumb = new Array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
	ocnovanie = 16;
	while(num_dec>0) {
		num_hex = dec_to_hex_sumb[num_dec%ocnovanie] + '' + num_hex;
		num_dec = Math.floor(num_dec/ocnovanie);
	}
	if (for_html) {
		if (num_hex=='') return '00';
		if ((num_hex+'').length==1)  return '0'+num_hex;
		else return num_hex;
	}
	else return num_hex?num_hex:0;
}

function getCookie(Name) {
	var search = Name + "=";
	if (document.cookie.length > 0) {	// if there are any cookies
		offset = document.cookie.indexOf(search);
		if (offset != -1) {		// if cookie exists
			offset += search.length;	// set index of beginning of value
			end = document.cookie.indexOf(";", offset);	// set index of end of cookie value
			if (end == -1)
				end = document.cookie.length;
			return unescape(document.cookie.substring(offset, end));
		}
	}
}

function setCookie(name, value, expire, path) {
	
	if (expire) {
		expires = new Date();
		expires.setTime (new Date().getTime() + 3600*24*expire);
	}
	
	tmp = name + "=" + escape(value);
	if (expires!=null)	tmp+="; expires=" + expires.toGMTString();
//	if (domain!=null && trim(domain)!='')	tmp+="; domain=" + escape(domain);
	if (path!=null && trim(path)!='')		tmp+="; path=" + escape(path);
//	if (secure==1)							tmp+="; secure";
	document.cookie = tmp;
}

function e_box_secure(arr){
	arr = arr.split(';');
	for (i in arr) document.write(String.fromCharCode(arr[i]/3));
}

function omo(t){
		 if (t.src.substr(-12)=='pic_home.gif') t.src='/style/img/pic_home_on.gif';
	else if (t.src.substr(-11)=='pic_map.gif')  t.src='/style/img/pic_map_on.gif';
	else if (t.src.substr(-12)=='pic_mail.gif') t.src='/style/img/pic_mail_on.gif';
}
function omu(t){
		 if (t.src.substr(-15)=='pic_home_on.gif') t.src='/style/img/pic_home.gif';
	else if (t.src.substr(-14)=='pic_map_on.gif')  t.src='/style/img/pic_map.gif';
	else if (t.src.substr(-15)=='pic_mail_on.gif') t.src='/style/img/pic_mail.gif';
}

function getElementPosition(elemId){
    var elem = el(elemId);
	
    var w = elem.offsetWidth;
    var h = elem.offsetHeight;
	
    var l = 0;
    var t = 0;
	
    while (elem)
    {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }

    return {"left":l, "top":t, "width": w, "height":h};
}

function set_prev_width(){	img_prev_pos = getElementPosition('img_prev_div'); el('img_prev_id').style.width = (img_prev_pos['width']*1)+'px'; }

var table_row = document.all?'block':'table-row';

