﻿// JScript File

// ========= Return element id =============================
function getEl(sId) {
  return (document.all? document.all(sId): (document.getElementById? document.getElementById(sId): null));
}




function AddToBasket ( sForm )
{
    //alert (document[ sForm ]);
    //document[ sForm ].action = "add_to_basket.aspx"
    document[ sForm ].ArtPrice.value = document[ sForm ].txtTotal.value;
    if (document[ sForm ].ArtUpload.value == 'True')
    {
        sFile = document[ sForm ].file1.value;
        if ((sFile != '')&&( sFile.substring(sFile.length-3, sFile.length) == 'jpg' ))
        {
            
            document[ sForm ].ArtDesc.value = '[' + sFile.substring(sFile.lastIndexOf("\\")+1, sFile.length) + ']';
            document[ sForm ].submit(); 
        }
        else {
            alert( aFormError[ 0 ] );
        }
    }
    else {
        document[ sForm ].submit(); 
    }    
}

function CalculateAttributes ( sForm, sTitle, sValue )
{
    document[ sForm ].txtTotal.value = format_float(sValue);
    document[ sForm ].ArtDesc.value = '('+ sTitle +')';
}

function CalculateCustom1 ( sForm, sValue )
{
   
    nBasicPrice = parseFloat(document[ sForm ].txtBasicPrice.value);
    nSinglePrice = parseFloat(document[ sForm ].txtSinglePrice.value);
    nStartCount = parseFloat(document[ sForm ].txtStartCount.value);            
            
    document[ sForm ].txtTotalPieces.value = nStartCount + parseFloat(sValue);
    document[ sForm ].txtTotal.value = format_float(nBasicPrice + (nSinglePrice * parseFloat(sValue)));
    
    document[ sForm ].ArtDesc.value = document[ sForm ].txtTotalPieces.value;

}


function brojkef(myfield, e) {
  var key, keychar;
  if (window.event) {
    key = window.event.keyCode;
    if (key == ','.charCodeAt(0)) window.event.keyCode = key = '.'.charCodeAt(0);
  } else if (e) {
    key = e.which;
  } else {
    return true;
  };
  keychar = String.fromCharCode(key);
  return ((("0123456789").indexOf(keychar) > -1) || (key==0))
}


function format_float( x ) {
    var ostatak = Math.round( 100 * (x - Math.round( x-0.5 )) );
    while( (ostatak+'').length < 2 ) { ostatak += '0'; }
    var cjelobrojno = Math.round( x-0.5 )+'';
    var broj = '';
    var nCount = 0;
    for( var i=cjelobrojno.length-1; i>=0; i-- ) {
      broj = cjelobrojno.substr( i, 1 ) + broj;
      ++ nCount;
      if( (nCount == 3) && (i>0) ) {
        broj = '.' + broj;
        nCount = 0;
      }
    }
		ostatak	= ( ostatak != '00' ) ? ostatak : '00';
    return broj + ',' + ostatak;
  }
  
  
  function checkOrder()
  {
    var sMissing = "";
    var theForm = document.formBuy;
    if ( theForm.txtSenderIme.value == "" ) sMissing += "\n- " + aFormError[ 1 ];
    if ( theForm.txtSenderPrezime.value == "" ) sMissing += "\n- " + aFormError[ 2 ];
    //if ( theForm.txtSenderEmail.value == "" ) sMissing += "\n- E-mail naručitelja";
        
    var x = theForm.txtSenderEmail.value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	if (!filter.test(x)) {sMissing += "\n- " + aFormError[ 3 ];}
	
	if ( theForm.txtSenderPhone.value == "" ) sMissing += "\n- " + aFormError[ 4 ];
    
    if ( theForm.txtSenderAdress.value == "" ) sMissing += "\n- " + aFormError[ 5 ];
    if ( theForm.txtSenderZIP.value == "" ) sMissing += "\n- " + aFormError[ 6 ];
    if ( theForm.txtSenderCity.value == "" ) sMissing += "\n- " + aFormError[ 7 ];
    if ( theForm.txtSenderCountry.value == "" ) sMissing += "\n- " + aFormError[ 8 ];
    
    if ( theForm.txtRecName.value == "" ) sMissing += "\n- " + aFormError[ 9 ];
    if ( theForm.txtRecAdress.value == "" ) sMissing += "\n- " + aFormError[ 10 ];
    
    if (isDate(theForm.txtShipmentDate.value, "d.M.yyyy"))
    {
       
        if (( compareDates(theForm.txtShipmentDate.value, "d.M.yyyy", theForm.txtAvailableDelivery.value, "d.M.yyyy") == 1) || ( compareDates(theForm.txtShipmentDate.value, "d.M.yyyy", theForm.txtAvailableDeliveryTill.value, "d.M.yyyy") == 0))
        {
            sMissing += "\n-----\n- "+  aFormError[ 11 ] +" "+ theForm.txtShipmentDate.value +".";
        }
        else {
            if ( checkValidDate(theForm.txtShipmentDate.value) == false )
            {
                sMissing += "\n-----\n- "+  aFormError[ 11 ] +" "+ theForm.txtShipmentDate.value +".";
            }
        }
        
    }
    else {
        sMissing += "\n-----\n- " + aFormError[ 12 ];
    }
    
    
    if ( theForm.txtShipmentFrom.value == "" || theForm.txtShipmentTill.value == "" )
    {
        sMissing += "\n- " + aFormError[ 13 ];
    }
    else {
    
		sToday = new Date();
		sNewDate	= sToday.getDate() +'.'+ (sToday.getMonth()+1) +'.'+ sToday.getFullYear();
				
		if ( compareDatesCustom1(theForm.txtShipmentDate.value, sNewDate) == -1 )
		{
		    MinValue = parseInt(theForm.txtAvailableDeliveryTime.value);
		}
		else {
			MinValue = 10;
		}		    
		
		
		// time delivery
		
		if ( !checkExceptionDate() )
        {
            MaxValue = 20;
            TmpFirstValue = parseInt(theForm.txtShipmentFrom.value);
            TmpSecondValue = parseInt(theForm.txtShipmentTill.value);                
            if (!( ((TmpFirstValue >= MinValue)&&(TmpFirstValue <= MaxValue))&&((TmpSecondValue >= TmpFirstValue)&&(TmpSecondValue <= MaxValue)) ) || ( TmpFirstValue == TmpSecondValue ))
            {        
                sMissing += "\n- "+  aFormError[ 14 ] +" "+ TmpFirstValue +" - "+ TmpSecondValue +" "+  aFormError[ 15 ] +". ("+ aFormError[ 17 ] +")";                        
            }    
            
        }         
    }
    
    if ( sMissing != "" ) {
    alert (  aFormError[ 16 ] + '\n' + sMissing +'\n' );
  }
	else {
	  
	  if ( theForm.ddlPayment.value == '1' )
	  {

	    theForm.action = '/order.aspx';
        theForm.target = '_self';	
	    theForm.submit();
	  
	  } else {
	    
	    theForm.action = '/order_virman.aspx';
        theForm.target = '_blank';	
	    theForm.submit();
	    
	  }  
	  	  
	}
  }
  
  function checkPayment()
  {
    var theForm = document.formBuy;
    if ( theForm.ddlPayment.value == '2' )
    {
        alert ( 'Ukoliko plaćate virmanom potrebno je obavezno faksirati nam potvrdu o plaćanju\nNAJKASNIJE 4 SATA PRIJE TRAŽENOG PERIODA ISPORUKE!' );
    }
  }
	
  
  function checkValidDate( dDate )
  {
    var bValid = true;    
  
    var theForm = document.formBuy;
    var sFreeDays = theForm.txtFreeDays.value;
    var aDates = sFreeDays.split(", ");
    for( i=0; i < aDates.length; i++ )
    {
        if ( compareDatesCustom1(dDate, aDates[i]) == -1 )
        {
            bValid = false
        }
    }
 
    return (bValid);
    //alert(aDates.length);
  }
  
  
  
  function printOrder()
  {
    var theForm = document.forms['aspnetForm'];
    theForm.action = '/order_print.aspx';
    theForm.target = '_blank';
    theForm.submit();
  
    /*var winURL = '/order_print.aspx';
     var winIme = 'ImgZoom'
     var winAtributi =
       'width=670'       +
       ',height=550'     +
       ',toolbar=yes'     +
       ',location=no'    +
       ',directories=no' +
       ',status=no'      +
       ',menubar=no'     +
       ',scrollbars=yes'  +
       ',resizable=no'   ;

     var win = eval("window.open(winURL, winIme, winAtributi)");
     win.focus();*/
  }
  
  
  
// ========= Check date exceptions =============================
  
  // dates for exceptions
  var aExceptionDates = new Array ( '14.2', '14.02' );
  
  function CheckValentinovo()
  {
    var theForm = document.formBuy;
    if (isDate(theForm.txtShipmentDate.value, "d.M.yyyy"))
    {
        

        // if date in exception list - chenge time period
        if ( checkExceptionDate() )
        {
            getEl('periodStandard').style.display = 'none';
            getEl('periodValentinovo').style.display = 'block';
            fillTimePeriod()
        }
        else {
            getEl('periodStandard').style.display = 'block';
            getEl('periodValentinovo').style.display = 'none';
        }
    }
  }
  
  
  function checkExceptionDate( sNewDate )
  {
    if ( sNewDate )
    {
        sNewDate                = new Date( sNewDate );
        var selectedDateCheck   = sNewDate.getDate() +'.'+ (sNewDate.getMonth() + 1);
    }
    else {
        var theForm             = document.formBuy;
        var selectedDate        = theForm.txtShipmentDate.value;   
        var selectedDateCheck   = selectedDate.substring(0, selectedDate.lastIndexOf("."));
    }    
    var dateIsException         = false;
                                                      
    // check for date exception
    for ( i = 0; i < aExceptionDates.length; i++ )
    {
        if ( selectedDateCheck == aExceptionDates[ i ] ) 
        {
            dateIsException = true;
        }
    }        
    return ( dateIsException );
  }
  
  
  function fillTimePeriod()
  {
    var sToday                      = new Date();
    var theForm                     = document.formBuy;
    var selectedTime                = theForm.ddlShipmentFromTill.value;
    if ( checkExceptionDate( sToday ) )
    {
        if ( sToday.getHours() > 7 ) // working hours
        {
            //if ( sToday.getHours() < 12  ) theForm.ddlShipmentFromTill.options[0] = null;
            theForm.ddlShipmentFromTill.options[0] = null;
            if ( (sToday.getHours() > 11) && ( sToday.getHours() < 16 ) )
            {
                theForm.ddlShipmentFromTill.options[0] = null;
                theForm.ddlShipmentFromTill.options[0] = null;
            }            
        }    
    }
    
    theForm.txtShipmentFrom.value   = selectedTime.substring(0, selectedTime.indexOf('##'));
    theForm.txtShipmentTill.value   = selectedTime.substring(selectedTime.indexOf('##')+2);
  }