﻿///<reference path="JQuery.Intellisense.js">
function leftColheight() 
{
    var rightColheight = document.getElementById('rightCol').offsetHeight;
    var rightColheight2 = rightColheight - 180;
    var leftColheight = document.getElementById('leftCol').offsetHeight;
    //var leftColheight2 = leftColheight - 220;

    //alert(rightColheight + '/' + leftColheight);

    if (rightColheight > leftColheight) {
        $('.indexLeftBorder').css('height', rightColheight2 + 'px');
        //alert('right' + rightColheight);
    } else {
        $('.indexLeftBorder').css('height', rightColheight2 + 'px');
        //alert('left' + leftColheight);
    }
    leftColheightTimeout = setTimeout("leftColheight()", 50);
}

var currentMenu = '';
function showFormZayavka(toShowDivID, toHideDivID) 
{
	document.getElementById(toShowDivID).style.display = 'block';
	document.getElementById(toHideDivID).style.display = 'none';
}
function closeThisForm(eID, eID2) 
{
	if (document.getElementById(eID)) {
		document.getElementById(eID).style.display = 'none';
	}
	if (document.getElementById(eID2)) {
		document.getElementById(eID2).style.display = 'block';
	}
}
function showAdvSearch() 
{
	document.getElementById('advancedSearch').style.display = 'block';
}
function hideAdvSearch() 
{
	document.getElementById('advancedSearch').style.display = 'none';
}
function showFormZapros(bID, bID2, eID)
 {
	if (document.getElementById(bID).style.display == 'block') {
		document.getElementById(bID).style.display = 'none';
		document.getElementById(bID2).style.display = 'none';
		document.getElementById(eID).style.borderBottom = '1px solid #c7dcea';
	} else {
		document.getElementById(bID).style.display = 'block';
		document.getElementById(bID2).style.display = 'none';
		document.getElementById(eID).style.borderBottom = '1px solid #fff';
	}
}
function showFormZaprosThanks(bID, bID2) 
{
	document.getElementById(bID).style.display = 'none';
	document.getElementById(bID2).style.display = 'block';
}
function closeT2Div(bID, eID) 
{
	document.getElementById('additionalDivContentThanks2').style.display = 'none';	
	document.getElementById(eID).style.borderBottom = '1px solid #c7dcea';
}
function showForm(bID, bID2, eID, eID2, thanksDiv) {
	if (!thanksDiv) { thanksDiv = 'additionalDivContentThanks'; }
	if (document.getElementById(thanksDiv).style.display == 'block') {
		document.getElementById(thanksDiv).style.display = 'none';
	}
	if (document.getElementById(eID).style.display == 'block') {
		document.getElementById(eID).style.display = 'none';
		document.getElementById(bID).style.borderBottom = '1px solid #c7dcea';
	} else {
		//if (document.getElementById(eID2).style.display == 'block') {
			document.getElementById(eID2).style.display = 'none';
			document.getElementById(bID2).style.borderBottom = '1px solid #c7dcea';
		//}
		document.getElementById(eID).style.display = 'block';
		document.getElementById(bID).style.borderBottom = '1px solid #ffffff';
	}
}

function showHideForm(bID, eID) 
{
//    alert(document.getElementById(eID).style.display);
	if (document.getElementById(eID).style.display == 'block') 
	{
		document.getElementById(eID).style.display = 'none';
		document.getElementById(bID).style.borderBottom = '1px solid #c7dcea';
	}
	else
	{
		document.getElementById(eID).style.display = 'block';
		document.getElementById(bID).style.borderBottom = '1px solid #ffffff';
	}
}

function showTDiv(firstDiv, secondDiv) 
{
//	document.getElementById('additionalDivContentThanks').style.display = 'block';
	document.getElementById(firstDiv).style.borderBottom = '1px solid #c7dcea';
	document.getElementById(secondDiv).style.borderBottom = '1px solid #c7dcea';
}
function closeTDiv(firstDiv, secondDiv, thanksDiv) 
{
	if (!thanksDiv) { thanksDiv = 'additionalDivContentThanks'; }
	document.getElementById(thanksDiv).style.display = 'none';	
	document.getElementById(firstDiv).style.borderBottom = '1px solid #c7dcea';
	document.getElementById(secondDiv).style.borderBottom = '1px solid #c7dcea';
}
function getAjax() 
{
	var bodyID = $("body").attr('id');
	$(".linksList li span").click(function() {
		if (bodyID == 'home') bodyID = 'news';
		//else if (bodyID == 'service') bodyID = 'news-service';
		else bodyID = 'news-service';
		
		var html = $.ajax({
		 url: "inc/block-ajax-" + bodyID + ".php?param=" + this.className,
		 async: false
		}).responseText;
		document.getElementById("newsAjaxBlock").innerHTML = html;
		getAjax();
	});
}
var liS = Array('photos p0', 'photos p1', 'photos p2', 'photos p3', 'photos p4', 'photos p5');
var divS = Array('mainContent-0', 'mainContent-1', 'mainContent-2', 'mainContent-3', 'mainContent-4', 'mainContent-5');

function hideAllDivs(currentClassName) 
{
	for (y = 0; y < liS.length; y++) {
		if (liS[y] !== currentClassName) {
			document.getElementById(divS[y]).style.display = 'none';
		}
	}
}
function validate_email(field) 
{
	field=trim(field); 
	var emailFilter=/^.+@.+\..{2,3}$/;
	var illegalChars= /[\!\"\Ј\$\%\^\&\*\+\=\{\}\~\#\?\(\)\<\>\,\;\:\\\/\"\[\]\ ]/;
	
	if ((!(emailFilter.test(field))) || (field.match(illegalChars))) {
		return 0;
	} else {
		return 1;
	}
}

function trim(str) 
{
    if (str == null)
        return "";
	str = this != window? this : str;
	return str.replace(/^\s+|\s+$/g,"");
}

function validate_non_empty_string(str)
{
    str = trim(str);
    return str != "";
}


function showComments(id,url,f)
{
    $(id + " .comments").load(url, null, function() { f();  $(id).show(); });    
}
function hideComments(id)
{
    $(id).hide();
}

function validateCommentForm(f)
{
    return (
        validate_non_empty_string(f.Name.value) &&
        validate_email(f.Email.value) &&
        validate_non_empty_string(f.Text.value)
        );
}

function validateCVForm(f)
{
    return (
        validate_non_empty_string(f.fio.value) &&
        validate_non_empty_string(f.contacts.value) &&
        validate_non_empty_string(f.file.value)
        );
}

function commentFormSubmitHandler(form,  showComments, captchaUrl)
{
    if (validateCommentForm(form))
    {
        return formSubmitAjax(form,function(){},
            function()
            {
                 $(form.parentNode.parentNode).hide();                     
                 form.reset()
                 showComments();
                 formGetNewCaptchaAjax(form, captchaUrl);
            });
    }
    else
    {
        alert("Пожалуйста, заполните поля отмеченные (*)");
        return false;
    }
}


function menuAnimation() {
    var opened = $("ul.services li.opened");
    if ((opened.length > 0) && (opened[0] != this.parentNode)) {
        opened.removeClass("opened").children("ul").slideUp("normal");
    }
    $(this.parentNode).addClass("opened").children("ul").slideDown("normal");
}

function setupMenuAnimation() {
    $("ul.services > li > span").click(menuAnimation);
}

function SetupAboutCompany()
{
    var aboutCompanyMenu = $("ul.aboutList li");
	var lastShowedDivID = aboutCompanyMenu.length >0 ? aboutCompanyMenu[0].id.substr(1):"";
	$("div.description[id^='about']").hide();
	$("#"+lastShowedDivID).show();
	$("ul.aboutList li").click(function() {
		$("#"+lastShowedDivID).hide();
		lastShowedDivID = this.id.substr(1);
		$("#"+lastShowedDivID).show();
		$("ul.aboutList li").removeClass();
		
        if (this.offsetHeight <= 18) {
			    this.className = 'current';
		    } else {
			    this.className = 'current current2';
		    }
    
	});
}

function formGetNewCaptchaAjax(f, captchaUrl) {
    $(f).find("div.captchaBlock").load(captchaUrl);
}

function formSubmitAjax(f, onRequest, onRequestComplete)
{
     var action = f.action;
     var serializedForm = $(f).serialize()+"&ajax=1";
     $.post(action, serializedForm, function()
     {
        onRequestComplete(f);
     });
     onRequest(f);
     return false;
}
function validateSubscribeForm(f)
{
    var checkedArray = jQuery.map($("div#subscribe div#checkboxes input"), function(o){return o.checked;});
    var anyChecked = jQuery.inArray(true,checkedArray) > -1;
    var email = document.getElementById("Subscribe.Email");
    return (validate_email(email.value) == 1) && anyChecked;
}
function showHideThanks()
{
    $("#subscribe").toggle();$("#thanks").toggle();
}

function subscribeFormSubmitHandler()
{
    if(validateSubscribeForm(this))
    {
        var f = this;
        return formSubmitAjax(this,function(){},function(){showHideThanks(); f.reset()});
    }
    else
    {
        alert("Введите правильный email и установите хотя бы один флажок");
        return false;
    }
}

/*Как весь код будет сделан засунуть в scripts.js*/
function validateSendLinkForm(f) {
    return (validate_non_empty_string(f.Name.value) && validate_email(f.Email.value))
}

function sendLinkFormSubmitHandler(f, ctrl, captchaUrl) {
    if (validateSendLinkForm(f)) {
        return formSubmitAjax(f,
        function() { formGetNewCaptchaAjax(f, captchaUrl); /*Здесь код выполняемый при отправке*/ },
        function() { $(ctrl).show(); setTimeout(" $('#" + ctrl.id + "').hide();", 3000); f.reset()/*Здесь код выполняемый при получении*/ }
        );
    }
    else {
        alert("Пожалуйста, заполните поля отмеченные (*)");
        return false;
    }
}

$(document).ready(function() {
    if ($("#scheme")) {
        if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("MSIE 6.0") > -1) {
            $("#close_scheme_link").css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../images/scheme_white_bg.png', sizingMethod='scale', enabled=true)");
        }
    }
    if ($(".additionalLinks")) {
        if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("MSIE 6.0") > -1) {
            $(".additionalLinks").css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../images/gradient2_2__.png', sizingMethod='scale', enabled=true)");
        }
    }
//    try {
//        $(".leftTabs > ul").tabs();
//    } catch (ex) { }
//    //$(".topTabs > ul").tabs();
    setupMenuAnimation();

    if (document.getElementById('photos')) {
        var ff = document.getElementById('photos').getElementsByTagName('li');
        for (x = 0; x < ff.length; x++) {
            ff[x].onclick = function() {
                document.getElementById(divS[this.className.substr(8, 1)]).style.display = 'block';
                hideAllDivs(this.className);
            }
        }
    }
});
