var sDocType = "";
var sManuf = "";
var sProdLine = "";
var sProdModel = "";


function SmbtContact()
{
 var iErr = false;
 if (ChkFld_str("nam"))   { alert("Input your name."); iErr = true; return false; }
 if (ChkFld_email("eml")) { alert("Input your correct e-mail."); iErr = true; return false; }
 if (ChkFld_str("msg"))   { alert("Input your message."); iErr = true; return false; }
 
 document.getElementById("form1").submit();
}


function SmbtBuy()
{
 var iErr = false; 
 if (ChkFld_str("nam"))   { alert("Input your name."); iErr = true; return false; }
 if (ChkFld_email("eml")) { alert("Input your correct e-mail."); iErr = true; return false; }
 
 document.getElementById("form1").submit();
}


// клик по документам
function DocTypeClick(iId)
{
 if (sDocType) // .length()
   {
   	document.getElementById(sDocType).className = "";
   }
 
 var sId = "dt"+iId;
 document.getElementById(sId).className = "sel";
 sDocType = sId;

 document.getElementById("idDocType").value = iId;
 document.getElementById("idManuf").value = 0;
 document.getElementById("idProdLine").value = 0;
 document.getElementById("idProdModel").value = 0;
 
 sManuf     = ""; document.getElementById("Manuf").innerHTML = "";
 sProdLine  = ""; document.getElementById("prodLine").innerHTML = "";
 sProdModel = ""; document.getElementById("prodModel").innerHTML = "";
 
 doLoad("aj/ajSchem.php?act=manf", "ajForm", "Manuf", true, 4); // "AuthResult"

}


// клик по производителю
function ManufClick(iId)
{
 if (sManuf) // .length()
   {
   	document.getElementById(sManuf).className = "";
   }
 
 var sId = "manuf"+iId;
 document.getElementById(sId).className = "sel";
 sManuf = sId;

 //document.getElementById("idDocType").value = iId;
 document.getElementById("idManuf").value = iId;
 document.getElementById("idProdLine").value = 0;
 document.getElementById("idProdModel").value = 0;

 sProdLine  = ""; document.getElementById("prodLine").innerHTML = "";
 sProdModel = ""; document.getElementById("prodModel").innerHTML = "";
 
 doLoad("aj/ajSchem.php?act=line", "ajForm", "prodLine", true, 4);
}


// клик по линейки продукции
function ProdLineClick(iId, sStr)
{
 if (sProdLine) // .length()
   {
   	document.getElementById(sProdLine).className = "";
   }
 
 var sId = "pl"+iId;
 document.getElementById(sId).className = "sel";
 sProdLine = sId;

 //document.getElementById("idDocType").value = iId;
 //document.getElementById("idManuf").value = iId;
 document.getElementById("idProdLine").value = sStr;
 document.getElementById("idProdModel").value = 0;

 sProdModel = ""; document.getElementById("prodModel").innerHTML = "";
 
 doLoad("aj/ajSchem.php?act=modl", "ajForm", "prodModel", true, 4);
}


// клик по модели продукции
function ModelLineClick(iId)
{
 if (sProdModel) // .length()
   {
   	document.getElementById(sProdModel).className = "";
   }
 
 var sId = "modl"+iId;
 document.getElementById(sId).className = "sel";
 sProdModel = sId;

 //document.getElementById("idDocType").value = iId;
 //document.getElementById("idManuf").value = iId;
 //document.getElementById("idProdLine").value = sStr;
 document.getElementById("idProdModel").value = iId;
 
 doLoad("aj/ajSchem.php?act=schm", "ajForm", "foundData", true, 4, "initLytebox");
}



function JSRedirect(sLocation)
{
 window.location.href = sLocation;
}


function DelBy(Ask, Locat)
{
 var is_confirmed = confirm(Ask);
 if (is_confirmed)
   {
    JSRedirect(Locat);
   }
}


//-----------------------------------------------------------------------------


// выбрать в <select> заданный элемент по id
function SelectInCBById(sCB, sId)
{
 var o = document.getElementById(sCB);
 if (!o) return false;

 for (var i=0; i < o.options.length; i++)
    {
     if (o.options[i].value == sId) 
       {
        o.selectedIndex = i;
       	break;
       }
    }
}

// проверить строковое поле
function ChkFld_str(IdName)
{
 var Obj = document.getElementById(IdName);
 Obj.value = trim(Obj.value);
 if (Obj.value == '') { Obj.className = 'FldErr'; Obj.focus(); return 1; }
 else                 { Obj.className = 'InpFld'; }
 return 0;
}

// проверить поле файла
function ChkFld_file(IdName)
{
 var Obj = document.getElementById(IdName);
 if (Obj.value == '') { Obj.className = 'FldErr'; Obj.focus(); return -1; }
 else { Obj.className = 'InpFld'; }
 return 0;
}

// проверить цивровое поле
function ChkFld_num(IdName, minval, maxval)
{
 var Obj = document.getElementById(IdName);
 Obj.value = trim(Obj.value);
 if (Obj.value < minval || Obj.value > maxval) 
      { Obj.className = 'FldErr'; Obj.focus(); return -1; }
 else { Obj.className = 'InpFld'; }
 return 0;
}

// проверить телефон
function ChkFld_tel(IdName, IsCanBeEmpty)
{
 var $isErr;
 var Obj = document.getElementById(IdName);
 Obj.value = trim(Obj.value);
 if (Obj.value == '' && IsCanBeEmpty) { Obj.className = 'InpFld'; return 0; }

 $isErr = false;
 for (i=0; i<Obj.value.length; i++)
    {
     if ((Obj.value[i] >= '0' && Obj.value[i] <= '9') || Obj.value[i] == '+') { ; }
     else { $isErr = true; }
    }

 if (Obj.value == '') { $isErr = true; }
 if ($isErr) { Obj.className = 'FldErr'; Obj.focus(); return -1; }
 else        { Obj.className = 'InpFld'; }
 return 0;
}


// проверить е-майл
function ChkFld_email(IdName)
{
 var Obj = document.getElementById(IdName);
 var value = trim(Obj.value);

 // reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
 // if (!value.match(reg)) { Obj.className = 'FldErr'; Obj.focus(); return -1; }
 // else { Obj.className = 'InpFld'; }
 // if ( !(/\w+@\w+\.[a-z]{2,4}/.test(value)) )

 if ( !(/\w+@\w+\.[A-Za-z0-9_]{2,4}/.test(value)) ) 
      { Obj.className = 'FldErr'; Obj.focus(); return -1; }
 else { Obj.className = 'InpFld'; }

 return 0;
}

// проверить строковое поле
function ChkFld_id(IdName, ObjName)
{
 var Obj    = document.getElementById(IdName);
 var FldObj = document.getElementById(ObjName);
 if (Obj.value <= 0)
   { 
    FldObj.className = 'FldErr'; 
    FldObj.focus(); return -1; 
   }
 else { FldObj.className = 'InpFld'; }
 return 0;
}

function ChkFld_ch(IdName, CheckValue, ErrObjName)
{
 var Obj    = document.getElementById(IdName);
 var ObjErr = document.getElementById(ErrObjName);
 if (Obj.checked != CheckValue) 
   {
    ObjErr.className = 'FldErr'; Obj.focus(); return -1;
   }
 else { ObjErr.className = ''; }
 return 0;
}

function ChkFld_date(IdNamePre)
{
 var ObjD = document.getElementById((IdNamePre+"d"));
 var ObjM = document.getElementById((IdNamePre+"m"));
 var ObjY = document.getElementById((IdNamePre+"y"));
 var isErr = false;
 var M = ObjM.options[ObjM.selectedIndex].value;
 var D = ObjD.value;

 var DT = new Date(ObjY.value, ObjM.options[ObjM.selectedIndex].value-1, ObjD.value);
 if (isNaN(DT) || ObjY.value < 1900)
   {
    isErr = true;
   }

      if (M < 1 || M > 12) isErr = true;
 else if (D < 1 || D > 31) isErr = true;

 if (isErr)
   {
    ObjD.className = 'FldErr'; 
    ObjM.className = 'FldErr'; 
    ObjY.className = 'FldErr'; 
    ObjD.focus();
    return -1;
   }
 else
   {
    ObjD.className = 'InpFld';
    ObjM.className = 'InpFld';
    ObjY.className = 'InpFld';
   }
 return 0;
}

// проверить корректность ввода пароля
function ChkFld_pswd(IdName1, IdName2)
{
 var Obj1 = document.getElementById(IdName1);
 var Obj2 = document.getElementById(IdName2);

 if ((Obj1.value != Obj2.value) || Obj1.value == "" || Obj2.value == "" || Obj1.value.length < 6)
   { 
    Obj1.className = 'FldErr'; 
    Obj2.className = 'FldErr'; 
    Obj1.focus(); 
    return -1; 
   }
 else { Obj1.className = 'InpFld'; Obj2.className = 'InpFld'; }
 return 0;
}

//-----------------------------------------------------------------------------
//
function NewsScrollUp()
{
 var Obj = document.getElementById('ScrollContent');
 Obj.scrollTop -= 60;

 var Obj2 = document.getElementById('ScrollPos');
 var St = (Obj.scrollTop/60)+1;
 Obj2.innerHTML = St+' - '+(St+3)+' / 10';
}
//-----------------------------------------------------------------------------
function NewsScrollDown()
{
 var Obj = document.getElementById('ScrollContent');
 Obj.scrollTop += 60;

 var Obj2 = document.getElementById('ScrollPos');
 var St = (Obj.scrollTop/60)+1;
 Obj2.innerHTML = St+' - '+(St+3)+' / 10';
}
//-----------------------------------------------------------------------------
//
function TabChange(ToTab, FromTab)
{
 var Obj1 = document.getElementById(ToTab);
 var Obj2 = document.getElementById(FromTab);

 Obj1.className="NoLink Selected";
 Obj2.className="NoLink";
}
//-----------------------------------------------------------------------------
function open_window(link) 
{
 var win = "width=700, height=450, menubar=no, location=no, resizable=no, scrollbars=yes, left=150, top=100";
 newWin = window.open(link,'newWin',win);
 newWin.focus();
}

//-----------------------------------------------------------------------------

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function ExpClp(id, state_visible)
{
 var set_state = "block";
 if (state_visible == true) { set_state = "block"; }
 else                       { set_state = "none"; }

 obj  = document.getElementById(id);
 if (obj) { obj.style.display = set_state; }
}

function FlipFlop(id)
{
 obj  = document.getElementById(id);
 if (obj.style.display == "none" ||
     obj.style.display == "") { obj.style.display = "block"; }
 else                         { obj.style.display = "none"; }
}

function ShowBlk(id)
{
 document.getElementById('grid').style.display = 'block';
 var set_state = "block";
 obj  = document.getElementById(id);
 if (obj) { obj.style.display = set_state; }
}

function HideBlk(id)
{
 var set_state = "none";
 obj  = document.getElementById(id);
 if (obj) { obj.style.display = set_state; }
 document.getElementById('grid').style.display = 'none';
}

function SetVal(id, val)
{
 obj  = document.getElementById(id);
 if (obj) { obj.value = val; }
}

function SetContent(id, val)
{
 obj  = document.getElementById(id);
 if (obj) { obj.innerHTML = val; }
}



function showGifter(e)/*{{{*/
{
	var D		= e.getElementsByTagName( 'DIV' )[0];
	D.style.display = 'block';
	if( !e.__gift )
	{
		e.__gift	= 1;
        var Img	= D.getElementsByTagName('IMG');
		for( var i = 0; Img[i]; i++ )
		{
			if( !Img[i].src )
			{
				Img[i].src = Img[i].getAttribute('scr');
			}
		}
		Img = null;
	}
	D	= null;
}/*}}}*/
function hideGifter(el)/*{{{*/
{
    var childs = el.getElementsByTagName('div');
    childs[0].style.display = 'none';
}/*}}}*/


function ReInitLB()
{
 $(document).ready(function(){ $(".lightbox").lightbox(); });
}









function ExpandCollapce(id, state_visible)
{
 var set_state = "block";
 if (state_visible == true) { set_state = "block"; }
 else                       { set_state = "none"; }
 obj  = document.getElementById(id);
 if (obj) { obj.style.display = set_state; }
}

function ExpandCollapceFilpFlop(id)
{
 obj  = document.getElementById(id);
 if (obj.style.display == "none") { obj.style.display = "block"; }
 else                             { obj.style.display = "none"; }
}

function ShowRip(id)
{
 document.getElementById('grid').style.display = 'block';
 var set_state = "block";
 obj  = document.getElementById(id);
 if (obj) { obj.style.display = set_state; }
}

function HideRip(id)
{
 var set_state = "none";
 obj  = document.getElementById(id);
 if (obj) { obj.style.display = set_state; }
 document.getElementById('grid').style.display = 'none';
}

function SetVal(id, val)
{
 obj  = document.getElementById(id);
 if (obj) { obj.value = val; }
}

function SetContent(id, val)
{
 obj  = document.getElementById(id);
 if (obj) { obj.innerHTML = val; }
}







/*

//------------------------
function popupInit()
{
 document.write('<div id="PUOverlay"></div>');
 document.write('<div id="PUBlock"><div class="In" id="popupContent"></div></div>');
}

// открыть окно просмотра изображения
function popupDiv(iIdPhoto, isAlbum)
{
 popupOpen();
 if (isAlbum) sAlbum = "&albm=1";
 else         sAlbum = "";
 doLoad("ajax/ajCtrlPhoto.php?act=view&idu=&idp="+iIdPhoto+sAlbum, "BrowseShort", "popupContent", "", true, 4, "");
 
 // <div id="overlay" style="width: 100%; height: 1100px; opacity: 0.25; display: block;"/>
}

// открыть окно оплаты
function popupPay(sAct, iIdUser)
{
 popupOpen();
 doLoad("ajax/ajPay.php?act="+sAct+"&idtouser="+iIdUser, "BrowseShort", "popupContent", "", true, 4, ""); 
}

function popupOpen()
{
 var oObj = document.getElementById("PUOverlay"); 

 document.getElementById("swf1").style.display = "none";
 
 addHandler(window,   'DOMMouseScroll', wheel); // Gecko
 addHandler(window,   'mousewheel', wheel); // Opera
 addHandler(document, 'mousewheel', wheel); // IE

 // TODO: блокировать, скролл по ужержанию колеса как в однокласниках
 // 1. aj, загрузить фото с элементами голосования и прочего

 //addHandler(window,   'DOMMouseDown', mouseTracker);
 //addHandler(window,   'mousedown',    mouseTracker);
 //addHandler(document, 'mousedown',    mouseTracker);
 
 oObj.style.width   = "100%";
 oObj.style.height  = document.body.clientHeight+"px";
 oObj.style.display = "block";
 oObj.style.opacity = "0.25";
 oObj.style.filter  = "progid:DXImageTransform.Microsoft.Alpha(opacity=25);";

 var oObj = document.getElementById("PUBlock");
 oObj.style.display = "block";

 var WinWorkH = get_wh() ? get_wh() : document.body.clientHeight;
 // alert(WinWorkH + ' ' + get_wh() + ' ' + document.body.clientHeight);
 if (WinWorkH < 600)
   { // Высота меньше минимальной допустимой
    oObj.style.width = "720px";
    oObj.style.height   = WinWorkH+"px";
    oObj.style.paddingRight = "20px";
    oObj.style.overflow = "scroll";
    oObj.style.overflowX = "hidden";
    oObj.style.marginTop = "-"+(WinWorkH/2)+"px";
   }
 else
   {
    oObj.style.width = "720px";
    oObj.style.height = "600px";
    oObj.style.paddingRight = "0px";
    oObj.style.overflow = "hidden";
   }

 window.scrollTo(0, 0);
}

function popupClose()
{
 removeHandler(window,   'DOMMouseScroll', wheel); // Gecko
 removeHandler(window,   'mousewheel', wheel); // Opera
 removeHandler(document, 'mousewheel', wheel); // IE

 document.getElementById("PUOverlay").style.display = "none";
 document.getElementById("PUBlock").style.display = "none";
 document.getElementById("swf1").style.display = "block";
}

 // Функция для добавления обработчика событий
function addHandler(object, event, handler, useCapture) 
{
 if (object.addEventListener) { object.addEventListener(event, handler, useCapture ? useCapture : false); }
 else if (object.attachEvent) { object.attachEvent('on' + event, handler); } 
 else                         { alert("Add handler is not supported"); }
}

function removeHandler(object, event, handler)
{
 if (typeof object.removeEventListener != 'undefined') object.removeEventListener(event, handler, false);
 else if (typeof object.detachEvent != 'undefined')    object.detachEvent('on' + event, handler);
 else                                                  throw "Incompatible browser";
}

// function mouseTracker(e)
//{
// e = e || window.Event || window.event;
// if (e && ((e.button == 3 || e.button == 2) || (e.which == 3 || e.which == 2)))
//   {
//    if (e.preventDefault) e.preventDefault();
//    if (e.stopPropagation)  e.stopPropagation();
//    e.returnValue = false;
    //alert("Left clicks only...");
//    return false;
//   }
//} 

// Обработчик события
function wheel(event) 
{
 var delta; // Направление скролла
 // -1 - скролл вниз
 // 1  - скролл вверх
 event = event || window.event;
 // Opera и IE работают со свойством wheelDelta
 if (event.wheelDelta) 
   {
    delta = event.wheelDelta / 120;
    // В Опере значение wheelDelta такое же, но с противоположным знаком
    if (window.opera) delta = -delta;
    // В реализации Gecko получим свойство detail
   } 
 else if (event.detail) 
   {
    delta = -event.detail / 3;
   }
 // Запрещаем обработку события браузером по умолчанию
 if (event.preventDefault)  event.preventDefault();
 event.returnValue = false;
 return delta;
}

function get_ww()
{
 var frameWidth = 0;
 if (self.innerWidth)
    frameWidth = self.innerWidth;
 else if (document.documentElement && document.documentElement.clientWidth)
    frameWidth = document.documentElement.clientWidth;
 else if (document.body) 
    frameWidth = document.body.clientWidth;
 return frameWidth;
}
function get_wh()
{
 var frameHeight = 0;
 if (self.innerHeight) 
    frameHeight = self.innerHeight;
 else if (document.documentElement && document.documentElement.clientHeight) 
    frameHeight = document.documentElement.clientHeight;
 else if (document.body)
    frameHeight = document.body.clientHeight;
 return frameHeight;
} */