function toggle(id) {
	if ( (document.getElementById(id).style.display == 'block') ||
		 (document.getElementById(id).style.display == '') ) {
		document.getElementById(id).style.display = 'none';
	}
	else {
		document.getElementById(id).style.display = '';
	}
}

function toggle_block(id) {
	if ( (document.getElementById(id).style.display == 'block') ||
		 (document.getElementById(id).style.display == '') ) {
		document.getElementById(id).style.display = 'none';
	}
	else {
		document.getElementById(id).style.display = 'block';
	}
}

function show(id) {
	document.getElementById(id).style.display = '';
}

function show_block(id) {
	document.getElementById(id).style.display = 'block';
}

function hide(id) {
	document.getElementById(id).style.display = 'none';
}


function absPosition(obj) {
	var x = y = 0;
	while(obj) {
		x += obj.offsetLeft;
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return {x:x, y:y};
}

function clear_form(form_id)
{	el = document.getElementById(form_id);
	for (i = 0; i < el.elements.length; i++) {		if (el.elements[i].name.indexOf("SEND") != -1) {
			el.elements[i].value = "";
			el.elements[i].selectedIndex = 0;
		}	}}



function check_email(value) {
	reg = /^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i;
	if (!value.match(reg)) return false;
	return true;
}

function check_phone(value)
{
	reg = /^(\+\s*\d)?(\s*\d+)(\s*\-*\s*\d+)+$/;
	if (!value.match(reg)) return false;
	return true;
}

function rand(min, max)
{
	return Math.floor(Math.random() * (max - min + 1)) + min;
}


function get_bn(name)
{
	var list = document.getElementsByName(name);
	return list[0];
}

function get_bn_el(name, el)
{
	for (i = 0; i < el.elements.length; i++) {
		if (el.elements[i].name == name)
			return el.elements[i];
	}
	return null;
}

function html_decode(s){   // Convert all applicable characters from HTML entities
	var obj = document.getElementById('tmp');
	obj.innerHTML = s;
    return obj.innerHTML;
}

function check_form_call_me()
{
	el = document.getElementById("form_call_me");
	if (get_bn_el("SEND[name]", el).value.replace(/\s+/,'').length == 0) {
		alert(contacts_message_name);
		get_bn_el("SEND[name]", el).focus();
		return false;
	}
	if (!check_phone(get_bn_el("SEND[phone]", el).value.replace(/\s+/,''))) {
		alert(contacts_message_phone);
		get_bn_el("SEND[phone]", el).focus();
		return false;
	}
	if (get_bn_el("SEND[message]", el).value.replace(/\s+/,'').length == 0) {
		alert(contacts_message_message);
		get_bn_el("SEND[message]", el).focus();
		return false;
	}

	return true;
}

function send_call_me()
{
    var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            //alert(req.responseText);
            //document.getElementById('comment_debug').innerHTML = req.responseText;
            var response = req.responseJS;

            hide('phone_wait');
            switch (response) {
            	case '0':
            		$('#phone_p_result').attr('class', 'contacts_ok');
            		$('#phone_p_result').html(result_ok.replace(/--br--/g, '<br />'));
            		clear_form('form_call_me');
            		break;
            	case '1':
            		$('#phone_p_result').attr('class', 'contacts_error');
            		$('#phone_p_result').html(result_not_filled.replace(/--br--/g, '<br />'));
            		break;
            	default:
            		$('#phone_p_result').attr('class', 'contacts_error');
            		$('#phone_p_result').html(result_not_send.replace(/--br--/g, '<br />'));

            }
            show_block('phone_result');
            //handle(form_id, response);
        	//DeleteBlockDiv();
        }
    }
	//CreateBlockDiv();
	hide('phone_result');
	show_block('phone_wait');
	//ShowWait();
	//show(form_id + "_wait");
	//alert(form_id + "_wait");
    req.open(null, '/loader.php', true);
    req.send( { 'param' : 'call_me', 'lang' : fe_lang, 'form' : document.getElementById('form_call_me')} );

	return true;
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function clear_form_subscribe()
{
	el = document.getElementById("form_subscribe");
	get_bn_el("SEND[email]", el).value = subscribe_your_email;
	r = get_bn_el("SEND[status]", el);
	setCheckedValue(r, '1');
}


function send_subscribe()
{
    var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            //alert(req.responseText);
            //alert(req.responseJS);
            //document.getElementById('comment_debug').innerHTML = req.responseText;
            var response = req.responseJS;

            hide('subscribe_wait');
            switch (response) {
            	case '1':
            		$('#subscribe_p_result').html(subscribe_result_subscribe);
            		break;
            	default:
            		$('#subscribe_p_result').html(subscribe_result_unsubscribe);
            		break;
            }
       		clear_form_subscribe();
            show_block('subscribe_result');
            //handle(form_id, response);
        	//DeleteBlockDiv();
        }
    }
	//CreateBlockDiv();
	hide('subscribe_result');
	show_block('subscribe_wait');
	//ShowWait();
	//show(form_id + "_wait");
	//alert(form_id + "_wait");
    req.open(null, '/loader.php', true);
    req.send( { 'param' : 'subscribe', 'form' : document.getElementById('form_subscribe')} );

	return true;
}



function check_form()
{
	el = document.getElementById("form_contacts");
	if (get_bn_el("SEND[name]", el).value.replace(/\s+/,'').length == 0) {
		alert(contacts_message_name);
		get_bn_el("SEND[name]", el).focus();
		return false;
	}
	if (!check_email(get_bn_el("SEND[email]", el).value.replace(/\s+/,''))) {
		alert(contacts_message_email);
		get_bn_el("SEND[email]", el).focus();
		return false;
	}
	if (get_bn_el("SEND[message]", el).value.replace(/\s+/,'').length == 0) {
		alert(contacts_message_message);
		get_bn_el("SEND[message]", el).focus();
		return false;
	}

	if (get_bn("SEND[captcha]").value.length == 0) {
		alert(contacts_message_captcha);
		get_bn("SEND[captcha]").focus();
		return false;
	}


	return true;
}


function check_cat_form()
{
	el = document.getElementById("form_contacts");
	if (get_bn_el("SEND[name]", el).value.replace(/\s+/,'').length == 0) {
		alert(contacts_message_name);
		get_bn_el("SEND[name]", el).focus();
		return false;
	}
	if (get_bn_el("SEND[schet]", el).value.replace(/\s+/,'') == 'email' && !check_email(get_bn_el("SEND[email]", el).value.replace(/\s+/,''))) {
		alert(contacts_message_email);
		get_bn_el("SEND[email]", el).focus();
		return false;
	}

	if (get_bn_el("SEND[schet]", el).value.replace(/\s+/,'') == 'fax' && get_bn_el("SEND[fax]", el).value.replace(/\s+/,'').length == 0) {
		alert(contacts_message_fax);
		get_bn_el("SEND[fax]", el).focus();
		return false;
	}


	if (get_bn_el("SEND[company]", el).value.replace(/\s+/,'').length == 0) {
		alert(contacts_message_company);
		get_bn_el("SEND[company]", el).focus();
		return false;
	}
	if (get_bn_el("SEND[unn]", el).value.replace(/\s+/,'').length == 0) {
		alert(contacts_message_unn);
		get_bn_el("SEND[unn]", el).focus();
		return false;
	}
	if (get_bn_el("SEND[req]", el).value.replace(/\s+/,'').length == 0) {
		alert(contacts_message_req);
		get_bn_el("SEND[req]", el).focus();
		return false;
	}


	if (get_bn_el("SEND[zakaz]", el).value.replace(/\s+/,'').length == 0) {
		alert(contacts_message_zakaz);
		get_bn_el("SEND[zakaz]", el).focus();
		return false;
	}


	if (get_bn("SEND[captcha]").value.length == 0) {
		alert(contacts_message_captcha);
		get_bn("SEND[captcha]").focus();
		return false;
	}


	return true;
}



function check_form_subscribe()
{
	el = document.getElementById("form_subscribe");
	if (!check_email(get_bn_el("SEND[email]", el).value.replace(/\s+/,''))) {
		alert(contacts_message_email);
		get_bn_el("SEND[email]", el).focus();
		return false;
	}

	return true;
}


function check_form1()
{
	if (get_bn("SEND[name]").value.replace(/\s+/,'').length == 0) {
		alert();
		get_bn("SEND[name]").focus();
		return false;
	}
	if (get_bn("SEND[city]").value.replace(/\s+/,'').length == 0) {
		alert("Bitte geben Sie den PLZ, Stadt");
		get_bn("SEND[city]").focus();
		return false;
	}
	if (!check_phone(get_bn("SEND[phone]").value.replace(/\s+/,''))) {
		alert(html_decode("Bitte geben Sie eine g&#252;ltige Telefonnummer"));
		get_bn("SEND[phone]").focus();
		return false;
	}
	if (!check_email(get_bn("SEND[email1]").value.replace(/\s+/,''))) {
		alert(html_decode("Bitte geben Sie eine g&#252;ltige E-mail bestatigen"));
		get_bn("SEND[email1]").focus();
		return false;
	}
	if (get_bn("SEND[message]").value.replace(/\s+/,'').length == 0) {
		alert("Bitte geben Sie eine Nachricht");
		get_bn("SEND[message]").focus();
		return false;
	}
	if (get_bn("SEND[captcha]").value.length == 0) {
		alert("Bitte geben Sie den Code Zeichen");
		get_bn("SEND[captcha]").focus();
		return false;
	}


	return true;
}



function fancy_titleFormat(title, currentArray, currentIndex, currentOpts) {
	if (!title) title = '';
	var _title = '', _desc = '';
	var i = title.indexOf("~#~");
	if (i != -1) {
		_title = title.substring(0, i);
		_desc = title.substring(i + 3, title.length);
	}
	else
		_title = title;
	return (currentArray.length > 1 ? '<div style="text-align: left; font-size: 10px; color: #777;">' +  (currentIndex + 1) + ' / ' + currentArray.length + '</div>' : '') +
	'<div style="text-align: center;"><b>' + _title + '</b></div>' +
	'<div style="text-align: justify; font-size: 12px;">' + _desc + '</div>';
}




