window.onload = function(){
	calculateFillingHeight();
    externalLinks();
};
window.onresize = function(){
	calculateFillingHeight();
};

function getEl(name) {
	return document.getElementById(name);	
}

function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i < anchors.length; i++) {
        if (anchors[i].getAttribute("href") && anchors[i].getAttribute("rel") == "external") {
            anchors[i].target = "_blank";
        }
    }
}

function calculateFillingHeight(){
    windowSize = getWindowSize();
	var leftNews = getEl('newsDivLeft');
	var leftMinHeight = findPos(leftNews)[1];
	var leftNewsHeight = leftNews.clientHeight;
	var leftFullHeight = leftMinHeight + leftNewsHeight;
	var contentDiv = getEl('contentDiv');	
	var centerMinHeight = findPos(contentDiv)[1];	
	var centerHeight = contentDiv.clientHeight;
	var centerFullHeight = centerMinHeight + centerHeight;	
	var rightNews = getEl('newsDivRight');
	var rightMinHeight = findPos(rightNews)[1];
	var rightNewsHeight = rightNews.clientHeight;
	var rightFullHeight = rightMinHeight + rightNewsHeight;	
	var	maxPos = Math.max(Math.max(Math.max(windowSize[1],leftFullHeight),centerFullHeight),rightFullHeight);
	setPageBottomPosition(maxPos);
}

function setPageBottomPosition(top){
   //document.body.style.height = top + "px"   
   var footer = getEl('footerColumn');   
   footer.style.top = top+'px';
   footer.style.visibility = 'visible';
   footer.style.display = 'block';   
}

function check_form( form )
{
    
    errors = '';
	if( ! form.name.value )	{
		errors += 'Имя не должно быть пустым!<br />';
	} 
    
    var filter=/^[a-zA-ZА-Яа-я0-9\ ]{5,100}$/    
    if( !filter.test(form.name.value) )    {
        errors += 'Имя содержит запрещенные символы!<br />';
    }     

	if( ! form.mail.value )	{
        errors += 'Email не должен быть пустым!<br />';        
	}

	var filter=/^.+@.+\..{2,3}$/
  	if( !filter.test(form.mail.value) ) {
        errors += 'Некорректный адрес email!<br />';                
  	}

  	if( ! form.text.value ) {
        errors += 'Пожалуйста, заполните поле сообщения!<br />';                
	}

    if (errors=='') {
        return true;   
    } else {
        getEl('formErrors').innerHTML = errors;
        return false;
    }
}

function check_form_search( form )
{
    
    errors = '';
   /* if( form.searchString.value.length<6 )    {
        errors += 'Строка не должна быть пустой! Длина строки не менее 5 символов!';
    } */
    if (errors=='') {
        var filter=/^[a-zA-ZА-Яа-я0-9\ \-]{0,50}$/    
        if( !filter.test(form.searchString.value) )    {
            errors += 'Имя содержит запрещенные символы!';
        }     
    }    

    if (errors=='') {
        return true;   
    } else {
        //document.getElementById('formErrors').innerHTML = errors;
        alert(errors);
        return false;
    }
}

function show_3D_model() {
    _left = 0;
    _top = 0;
    features = "status=no,scrollbars=no,resizable=no,width=280,height=464,top=" + _top + ",left=" + _left;
    w = window.open('','',features);
    w.document.write("<html><head><title>Крипто Смартфон - 3D Модель</title></head><body style='margin: 0;'><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' width='280' height='454' id='rotation_cts' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='movie' value='/pics/flash/rotation_ancort.swf' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><param name='nomenu' value='true' /><embed src='/pics/flash/rotation_ancort.swf' quality='high' bgcolor='#ffffff' width='280' height='454' name='rotation_cts' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></object></body></html>");
}

function getWindowSize(){
    var myWidth = 0, myHeight = 0;
    if (typeof(window.innerWidth) == "number") {
        myWidth = window.innerWidth - 15;
        myHeight = window.innerHeight - 8
    }
    else {
        if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
            myWidth = document.documentElement.clientWidth - 10;
            myHeight = document.documentElement.clientHeight - 15
        }
        else {
            if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
                myWidth = document.body.clientWidth;
                myHeight = document.body.clientHeight
            }
        }
    }
    var windowSize = new Array(2);
    windowSize[0] = myWidth;
    windowSize[1] = myHeight;
    return windowSize
}

function findPos(obj){
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop
        }
        while (obj = obj.offsetParent)
    }
    var tmp = new Array(2);
    tmp[0] = curleft;
    tmp[1] = curtop;
    return tmp
}