﻿var constHeaderHeight = 50;
var constFooterHeight = 50;
var constHistoryHeight = 350;
// if you do not need a separator between 'divHistory' and 'divSideCart'
// set 'constSeparatorHistoryCartHeight = 0' and
// add 'display: none;' to 'trSeparatorHistoryCart' class in 'Store.css'
// subtract '10px' form 'height: 710px;' in 'divBody' and 'divSideMenu' clesses in css
var constSeparatorHistoryCartHeight = 10;
var constSideCartSummation = 30;
var constSideCartHeight = 350;
var constBodyHeight =
    constHistoryHeight +
    constSeparatorHistoryCartHeight +
    constSideCartSummation +
    constSideCartHeight;

function jsfOnLoad()
{
    jsfOnResize();
    
    window.onresize = jsfOnResize;
}

function jsfOnResize()
{
    var clientHeight = Math.max(
        (jsfGetClientHeight() - constHeaderHeight - constFooterHeight),
        (jsfGetDocumentHeight() - constHeaderHeight - constFooterHeight));
        
    if(null != (e = document.getElementById('divSideCart')))
    {
        // stratch the 'divSideCart' div iff client height is higher than it's minimal size
        if(( clientHeight - constHistoryHeight - constSeparatorHistoryCartHeight - constSideCartSummation ) > constSideCartHeight )
        {
            e.style.height = clientHeight - constHistoryHeight - constSeparatorHistoryCartHeight - constSideCartSummation + 'px';
        }
    }
    
    // stratch 'divSideCart', 'divSideMenu' divs iff client height is higher than themes minimal size
    if( clientHeight > constBodyHeight )
    {
        if(null != (e = document.getElementById('divSideMenu')))
            e.style.height = clientHeight + 'px';

        if(null != (e = document.getElementById('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 = document.getElementById(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 = document.getElementById(id)))
	    e.className = className;
}

function jsfNum(e)
{
    key = (document.layers) ? e.which:window.event.keyCode;
	
	if (window.event.shiftKey) return false;        
    
    if (key != 0)
    {
        return ((key <= 105 && key >= 96) || (key <=57 && key >= 48) || (key == 109) 
			|| (key == 8) || (key == 9) || (key ==27) || (key == 46)) ? true:false;
	}
}
// ------------------ region PROPERTIES ------------------
function jsfSetColorOption(idProduct, idGroup, iOption, nProperties) // Properties - Set Color Option ComboBox 
{
    idSelect = 'selectColor@' + idProduct + '@' + idGroup;
    
	if(null != (e = document.getElementById(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 = 1; i <= nProperties; i++)
    	if(null != (e = document.getElementById(idDivBase + i)))
    	    e.className = 'divColor';

	if(null != (e = document.getElementById(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 = document.getElementById(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 = document.getElementById(idDivBase + i)))
    	    e.className = 'divColor';

	if(null != (e = document.getElementById(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 ;
}


