﻿var constHeaderHeight = 138;
var constFooterHeight = 32;
var constHistoryHeight = 245;
var constSideCartSummation = 40;
var constSideCartHeight = 245;
var constBodyHeight =
    constHistoryHeight +
    constSideCartSummation +
    constSideCartHeight;

function onLoad()
{
    onResize();
    
    window.onresize = onResize;
}

function onResize()
{
    if(null != (eSideMenuMiddle = $get('divSideMenuMiddle')))
    {
        if(eSideMenuMiddle.offsetHeight < 312)
        {
            eSideMenuMiddle.style.height = '312px';
        }
    }

    if(null != (eBody = $get('divBody')))
    {
        if(eBody.offsetHeight < 510)
        {
            eBody.style.height = '510px';
        }
    }

    if(null != (eSideCart = $get('divSideCart')) && null != (eBody = $get('divBody')))
    {
        eSideCart.style.height = eBody.offsetHeight - constHistoryHeight - constSideCartSummation + 10 + 10 + 'px';
    }
    
  
//    var clientHeight = Math.max(
//        (jsfGetClientHeight() - constHeaderHeight - constFooterHeight),
//        (jsfGetDocumentHeight() - constHeaderHeight - constFooterHeight));
//        
//    if(null != (e = $get('divSideCart')))
//    {
//        // stratch the 'divSideCart' div iff client height is higher than it's minimal size
//        if(( clientHeight - constHistoryHeight - constSideCartSummation ) > constSideCartHeight )
//        {
////            e.style.height = clientHeight - constHistoryHeight - constSideCartSummation + 'px';
//        }
//    }
    
    // stratch 'divSideCart', 'divSideMenu' divs iff client height is higher than themes minimal size
//    if( clientHeight > constBodyHeight )
//    {
//        if(null != (e = $get('divSideMenu')))
//            e.style.height = clientHeight + 'px';

//        if(null != (e = $get('divBody')))
//            e.style.height = clientHeight + 'px';
//    }
}

function jsfGetClientWidth()
{
    return document.compatMode == 'CSS1Compat' && !window.opera ?
        document.documentElement.clientWidth :
        document.body.clientWidth;
}

function jsfGetClientHeight()
{
    return document.compatMode == 'CSS1Compat' && !window.opera ?
        document.documentElement.clientHeight :
        document.body.clientHeight;
}

function jsfGetDocumentWidth()
{
    return (document.all) ?
        document.body.offsetWidth : (document.layers) ?
        document.body.document.width : 0;
}

function jsfGetDocumentHeight()
{
//    return document.documentElement.scrollHeight;
    return (document.all) ?
        document.body.offsetHeight : (document.layers) ?
        document.body.document.height : 0;
}

function jsfSH(id) // Show Hide
{
	if(null != (e = $get(id)))
	    e.style.display = (e.style.display == 'none') ? '':'none' ;
}

function jsfSC(id, className) // Set Class
{
    if(null != (es = document.getElementsByName('aSM')))
        for(i = 0; i < es.length; i++)
            es[i].className = es[i].className.replace('C','');
    
	if(null != (e = $get(id)))
	    e.className = className;
}

function jsfNum(e)
{
    if(!e) {
        //if the browser did not pass the event 
        //information to the function, 
        //we will have to obtain it from the 
        //event register
        if (window.event) { //Internet Explorer
            e = window.event;
        }
        else { //total failure, we have no way of referencing the event
            return false;
        }
    }
    
    if(e.shiftKey) { //for digits
        return false;
    }
    if (typeof(e.keyCode) == 'number') { //DOM
      e = e.keyCode;
    } else if (typeof(e.which) == 'number') { //NS 4 compatible
      e = e.which;
    } else if (typeof(e.charCode) == 'number') { //also NS 6+, Mozilla 0.9+
      e = e.charCode;
    } else { //total failure, we have no way of obtaining the key code
      return false;
    }    

    if (e != 0) {
        return (
            (e <= 105 && e >= 96) ||
            (e <= 57 && e >= 48) ||
            (e == 109) ||
            (e == 8) ||
            (e == 9) ||
            (e == 27) ||
            (e == 46)) ? true : false;
    }
    
    return false;
}
// ------------------ region PROPERTIES ------------------
function jsfSetColorOption(idProduct, idGroup, iOption, nProperties) // Properties - Set Color Option ComboBox 
{
    idSelect = 'selectColor@' + idProduct + '@' + idGroup;
    
	if(null != (e = $get(idSelect)))
	    e.selectedIndex = iOption;

    idRect = iOption;

    jsfSetColorRect(idProduct, idGroup, iOption, nProperties);
}
function jsfSetColorRect(idProduct, idGroup, iOption, nProperties) // Properties - Set Color Rectangle
{
    idDivBase = 'divColor@'+ idProduct + '@' + idGroup + '@';
    
    for(i = 0; i <= nProperties; i++)
    	if(null != (e = $get(idDivBase + i)))
    	    e.className = 'divColor';

	if(null != (e = $get(idDivBase + iOption)))
        e.className = 'divColorSelected';
}
function jsfSetCartColorOption(iCartRow, idProduct, idGroup, iOption, nProperties) // Properties - Set Color Option ComboBox 
{
    idSelect = 'selectColor@' + iCartRow + '@' + idProduct + '@' + idGroup;
    
	if(null != (e = $get(idSelect)))
	    e.selectedIndex = iOption;

    idRect = iOption;

    jsfSetCartColorRect(iCartRow, idProduct, idGroup, iOption, nProperties);
}
function jsfSetCartColorRect(iCartRow, idProduct, idGroup, iOption, nProperties) // Properties - Set Color Rectangle
{
    idDivBase = 'divColor@'+ iCartRow + '@' + idProduct + '@' + idGroup + '@';
    
    for(i = 0; i <= nProperties; i++)
    	if(null != (e = $get(idDivBase + i)))
    	    e.className = 'divColor';

	if(null != (e = $get(idDivBase + iOption)))
        e.className = 'divColorSelected';
}
// ------------------ endregion PROPERTIES ------------------
function jsfTrim(s)
{
  s = this != window ? this : s;

  return s.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
function jsfPassportError(idNum)
{
	while ( idNum.length < 9 )
		idNum = "0" + idNum ;

	idNum1 = idNum.substr(0,1) * 1;
	idNum2 = idNum.substr(1,1) * 2;
	idNum3 = idNum.substr(2,1) * 1;
	idNum4 = idNum.substr(3,1) * 2;
	idNum5 = idNum.substr(4,1) * 1;
	idNum6 = idNum.substr(5,1) * 2;
	idNum7 = idNum.substr(6,1) * 1;
	idNum8 = idNum.substr(7,1) * 2;
	idNum9 = idNum.substr(8,1) * 1;

	if( idNum1 > 9 ) idNum1 = (idNum1%10) + 1;
	if( idNum2 > 9 ) idNum2 = (idNum2%10) + 1;
	if( idNum3 > 9 ) idNum3 = (idNum3%10) + 1;
	if( idNum4 > 9 ) idNum4 = (idNum4%10) + 1;
	if( idNum5 > 9 ) idNum5 = (idNum5%10) + 1;
	if( idNum6 > 9 ) idNum6 = (idNum6%10) + 1;
	if( idNum7 > 9 ) idNum7 = (idNum7%10) + 1;
	if( idNum8 > 9 ) idNum8 = (idNum8%10) + 1;
	if( idNum9 > 9 ) idNum9 = (idNum9%10) + 1;

	var sumVal = idNum1 + idNum2 + idNum3 + idNum4 + idNum5 + idNum6 + idNum7 + idNum8 + idNum9;
	sumVal = sumVal % 10;

	return ( sumVal > 0 ) ? true:false ;
}
function jsfCreditCardError(cardNumber)
{
    var cardTotal = 0;
    var dnum = 0;
    var test = 0;

    if (cardNumber.length >= 13 )
    {
        for (i = cardNumber.length; i >= 1 ;  i--)
        {
            test = test + 1;
            
            num = cardNumber.charAt(i - 1);
            
            if ((test % 2) != 0)
            {
                cardTotal = cardTotal + parseInt(num);
            }
            else
            {
                dnum = parseInt(num) * 2;
                
                if (dnum >= 10)
                {
                    cardTotal = cardTotal + 1 + dnum - 10;
                }
                else
                {
                    cardTotal = cardTotal + dnum;
                }
            }
        }
        
        return ((cardTotal % 10) != 0) ? true : false;
    }

    return true;
}
function jsfDateError( strDD, strMM, strYYYY, required )
{
	var intDD = strDD ;
	var intMM = strMM ;
	var intYYYY = strYYYY ;
	
	var intCounter = 0 ;
	
	if( intDD != 0 ) intCounter++ ;
	if( intMM != 0 ) intCounter++ ;
	if( intYYYY != 0 ) intCounter++ ;
	
	if(( required == true ) && ( intCounter == 0 )) return true ;
	else if( intCounter == 0 ) return false ;
	else if( intCounter < 3 ) return true ;
	
	var intDDs = new Array( 0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ) ;
	
	intDDs[2] = (( intYYYY % 4 ) == 0 ) ? 29:28 ;
	
	return ( intDD > intDDs[intMM] ) ? true:false ;
}

function jsfPIWC(path, wndWidth, wndHeight) // Popup Image Window Centered
{
    var wndLeft = ( screen.width - wndWidth ) / 2;
    var wndTop = ( screen.height - wndHeight ) / 2;
    win = window.open("CmsPopupImg.aspx?path=" + path, "", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no," +
	    "resizable=no,copyhistory=no,width=" + wndWidth +",height=" + wndHeight + ",top=" + wndTop + ",left=" + wndLeft, true  );
    win.focus();
}
function jsfPWC(url, width, height) // Popup Window Centered
{
    var left = ( screen.width - width ) / 2;
    var top = ( screen.height - height ) / 2;
    win = window.open(url, "", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=" + width +",height=" + height + ",top=" + top + ",left=" + left, true);
    win.focus();
}

function jsfBgE(e)
{
    e.style.backgroundColor = '#faa';
}
function jsfBgC(a)
{
    for(i=0; i < a.length; i++)
        if(null != (e = $get(a[i])))
            e.style.backgroundColor = '#fff';
}

