﻿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 jsfCalcQuickBuyOffset()
{
    if(null!=(eQuickBuyByCN = $get('divQuickBuyByCN')))
    {
//        alert(
//            'getScrollHeight:'+getScrollHeight()+'\n'+
//            'jsfGetClientHeight:'+jsfGetClientHeight()+'\n'+
//            'eQuickBuyByCN.clientHeight:'+eQuickBuyByCN.clientHeight);

        eQuickBuyByCN.style.top = 
            (getScrollHeight() + jsfGetClientHeight()/2 - eQuickBuyByCN.clientHeight/2) +
            'px';
            
//        alert(
//            'getScrollWidth:'+getScrollWidth()+'\n'+
//            'jsfGetClientWidth:'+jsfGetClientWidth()+'\n'+
//            'eQuickBuyByCN.clientWidth:'+eQuickBuyByCN.clientWidth);
            
        eQuickBuyByCN.style.left = 
            (getScrollWidth() + jsfGetClientWidth()/2 - eQuickBuyByCN.clientWidth/2) +
            'px';
    }
}

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 getScrollWidth()
{
   var w = window.pageXOffset ||
           document.body.scrollLeft ||
           document.documentElement.scrollLeft;
           
   return w ? w : 0;
} 

function getScrollHeight()
{
   var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
           
   return h ? h : 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)
{
    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 COLOR ------------------
function jsfSetColorOption(idProduct, idGroup, iOption, nProperties) // Properties - Set Color Option ComboBox 
{
    idSelect = 'selectColor@' + idProduct + '@' + idGroup;
    
	if(null != (e = document.getElementById(idSelect)))
	{
	    e.selectedIndex = iOption;
	    $get('spanColor').innerHTML = e.options[e.selectedIndex].text;
    }

    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 COLOR------------------
// ------------------ region PROPERTIES SIZE ------------------
function jsfSetSizeOption(idProduct, idGroup, iOption, nProperties) // Properties - Set Size Option ComboBox 
{
    idSelect = 'selectSize@' + idProduct + '@' + idGroup;
    
	if(null != (e = document.getElementById(idSelect)))
	{
	    e.selectedIndex = iOption;
	    $get('spanSize').innerHTML = e.options[e.selectedIndex].text;
    }

    idRect = iOption;

    jsfSetSizeRect(idProduct, idGroup, iOption, nProperties);
}
function jsfSetSizeRect(idProduct, idGroup, iOption, nProperties) // Properties - Set Size Rectangle
{
    idDivBase = 'divSize@'+ idProduct + '@' + idGroup + '@';
    
    for(i = 1; i <= nProperties; i++)
    	if(null != (e = document.getElementById(idDivBase + i)))
    	    e.className = 'divSize';

	if(null != (e = document.getElementById(idDivBase + iOption)))
        e.className = 'divSizeSelected';
}
// ------------------ endregion PROPERTIES SIZE ------------------
// ------------------ region PROPERTIES Quick Buy COLOR ------------------
function jsfSetQuickBuyColorOption(idProduct, idGroup, iOption, nProperties) // Properties - Set Color Option ComboBox 
{
   
    idSelect = 'selectQuickBuyColor@' + idProduct + '@' + idGroup;
    
	if(null != (e = document.getElementById(idSelect)))
	{
	    e.selectedIndex = iOption;
	    $get('spanColorQB').innerHTML = e.options[e.selectedIndex].text;
    }

    idRect = iOption;

    jsfSetQuickBuyColorRect(idProduct, idGroup, iOption, nProperties);
}
function jsfSetQuickBuyColorRect(idProduct, idGroup, iOption, nProperties) // Properties - Set Color Rectangle
{
   
    idDivBase = 'divQuickBuyColor@'+ 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';       
    }
}
// ------------------ endregion PROPERTIES COLOR ------------------
// ------------------ region PROPERTIES QUICK BUY SIZE ------------------
function jsfSetQuickBuySizeOption(idProduct, idGroup, iOption, nProperties) // Properties - Set Size Option ComboBox 
{
    idSelect = 'selectQuickBuySize@' + idProduct + '@' + idGroup;
    
	if(null != (e = document.getElementById(idSelect)))
	{
	    e.selectedIndex = iOption;
	    $get('spanSizeQB').innerHTML = e.options[e.selectedIndex].text;
    }

    idRect = iOption;

    jsfSetQuickBuySizeRect(idProduct, idGroup, iOption, nProperties);
}
function jsfSetQuickBuySizeRect(idProduct, idGroup, iOption, nProperties) // Properties - Set Size Rectangle
{
    idDivBase = 'divQuickBuySize@'+ idProduct + '@' + idGroup + '@';
    
    for(i = 1; i <= nProperties; i++)
    	if(null != (e = document.getElementById(idDivBase + i)))
    	    e.className = 'divSize';

	if(null != (e = document.getElementById(idDivBase + iOption)))
        e.className = 'divSizeSelected';
}
// ------------------ endregion PROPERTIES SIZESIZE ------------------
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 ;
}
// ------------------- QUICK BUY ERRORS ---------------------
function jsfQuickBuyErrors(idColor, idSize)
{
    var msg = '';
    
    if(msg == '') {
        if((null != (e = $get(idColor))) && (e.selectedIndex == 0)) {
            msg = '.בחר צבע'; }}

    if(msg == '') {
        if((null != (e = $get(idSize))) && (e.selectedIndex == 0)) {
            msg = '.בחר מידה'; }}
    
    if(msg == '')
    {
        jsfH('divQuickBuyByCN');
        jsfH('divlight2');
        
        return true; 
    }

    alert(msg);
    
    return false;
}
// ------------------- QUICK BUY ERRORS ---------------------

//---------------------TRADING------------------

function jsfTrade()
{    
    var a;    
    var last;
    var middle;
    var first;
    var toLast;
    if (null != (e1 = $get('divSideCartSummation')))
    {
        a=e1.innerHTML;
        last=a.lastIndexOf("₪");        
        first=a.lastIndexOf("|");
        a=a.substring(first+13,last-12);
        if (a=="-29.90")
        {
            if (null != (e2 = $get('trcredit')))
                e2.style.display='none';
        }
    }
}		
//--------------------TRADING-------------------