function LoadEmptyDiscount() {
	var discount_info = new Array() ;
	discount_info[ 0 ] = -1 ;
	discount_info[ 1 ] = 0 ;
	discount_info[ 2 ] = 0 ;
	discount_info[ 3 ] = 0 ;
	discount_info[ 4 ] = 0 ;
	discount_info[ 5 ] = null ;
	discount_info[ 6 ] = '' ;
	return( discount_info ) ;
}

function LoadNewCustomerDiscount() {
	var discount_info = new Array() ;
	discount_info[ 0 ] = 0 ;
	discount_info[ 1 ] = GetInteger( 'NCD_Discount', 0 ) ;
	discount_info[ 2 ] = GetInteger( 'NCD_DiscountFreight', 0 ) ;
	discount_info[ 3 ] = GetInteger( 'NCD_MaxProducts', -1 ) ;
	discount_info[ 4 ] = GetInteger( 'NCD_MaxDeliveries', -1 ) ;
	discount_info[ 5 ] = GetNamedElement( 'NCD_Expiration' ).value ;
	discount_info[ 6 ] = '' ;
	return( discount_info ) ;
}

function LoadCodeDiscount() {
	var discount_info = new Array() ;
	discount_info[ 0 ] = 1 ;
	discount_info[ 1 ] = GetInteger( 'DC_Discount', 0 ) ;
	discount_info[ 2 ] = GetInteger( 'DC_DiscountFreight', 0 ) ;
	discount_info[ 3 ] = GetInteger( 'DC_MaxProducts', -1 ) ;
	discount_info[ 4 ] = GetInteger( 'DC_MaxDeliveries', -1 ) ;
	discount_info[ 5 ] = GetNamedElement( 'DC_Expiration' ).value ;
	discount_info[ 6 ] = GetNamedElement( 'DC_Code' ).value ;
	return( discount_info ) ;
}

function ClearCodeDiscount() {
	GetNamedElement( 'DC_Discount' ).value = 0 ;
	GetNamedElement( 'DC_DiscountFreight' ).value = 0 ;
	GetNamedElement( 'DC_MaxProducts' ).value = -1 ;
	GetNamedElement( 'DC_MaxDeliveries' ).value = -1 ;
	GetNamedElement( 'DC_Expiration' ).value = '' ;
	GetNamedElement( 'DC_Code' ).value = '' ;
}

function LoadCompanyDiscount() {
	var discount_info = new Array() ;
	discount_info[ 0 ] = 2 ;
	discount_info[ 1 ] = GetInteger( 'CD_Discount', 0 ) ;
	discount_info[ 2 ] = GetInteger( 'CD_DiscountFreight', 0 ) ;
	discount_info[ 3 ] = -1 ;
	discount_info[ 4 ] = -1 ;
	discount_info[ 5 ] = '' ;
	discount_info[ 6 ] = '' ;
	return( discount_info ) ;
}

function LoadDiscountInfo() {
	var discount_info = LoadEmptyDiscount() ;
	var ndc = LoadNewCustomerDiscount() ;
	if( ( ndc[ 1 ] > discount_info[ 1 ]
		|| ( ndc[ 1 ] == discount_info[ 1 ] && ndc[ 2 ] >= discount_info[ 2 ] ) )
		&& ndc[ 3 ] != 0
		&& ndc[ 4 ] != 0 ) {
		discount_info = ndc ;
	}
	var dc = LoadCodeDiscount() ;
	if( ( dc[ 1 ] > discount_info[ 1 ]
		|| ( dc[ 1 ] == discount_info[ 1 ] && dc[ 2 ] >= discount_info[ 2 ] ) )
		&& dc[ 3 ] != 0
		&& dc[ 4 ] != 0 ) {
		discount_info = dc ;
	}
	var cd = LoadCompanyDiscount() ;
	if( ( cd[ 1 ] > discount_info[ 1 ]
		|| ( cd[ 1 ] == discount_info[ 1 ] && cd[ 2 ] >= discount_info[ 2 ] ) ) ) {
		discount_info = cd ;
	}
	return( discount_info ) ;
}

function LoadDiscountInfoByCode( discode ) {
	var result = -2 ;
	window.status = "Check discount code..." ;
	var discount = 0.0 ;
	var discount_freight = 0.0 ;
	var max_products = 0 ;
	var deliveries = 0 ;
	var expiration = '' ;
	var xmlhttp = null ;
	if( window.XMLHttpRequest ) {
		xmlhttp = new XMLHttpRequest() ;
	} else if( window.ActiveXObject ) {
		xmlhttp = new ActiveXObject( 'Microsoft.XMLHTTP' ) ;
	}
	if( xmlhttp != null ) {
		xmlhttp.open( 'GET', 'CheckDiscount.aspx?discode='+discode+'&rnd'+Math.random(), false ) ;
		xmlhttp.send( '' ) ;
		if( xmlhttp.status == 200 ) {
			var n = xmlhttp.responseXML.documentElement ;
			var d  = n.getAttribute( 'discount' ) ;
			if( d == 'inuse' ) {
				result = 0 ;
			} else if( d == 'bad' ) {
				result = -1 ;
			} else if( d == 'expired' ) {
				result = -2 ;
			} else {
				discount = d ;
				discount_freight = n.getAttribute( 'freight_discount' ) ;
				max_products = n.getAttribute( 'max_products' ) ;
				deliveries = n.getAttribute( 'deliveries' ) ;
				expiration = n.getAttribute( 'expiration' ) ;
				result = 1 ;
			}
			window.status = 'Done.' ;
		} else {
			window.status = 'Unable to check discount code.' ;
		}
		GetNamedElement( 'DC_Discount' ).value = discount ;
		GetNamedElement( 'DC_DiscountFreight' ).value = discount_freight ;
		GetNamedElement( 'DC_MaxProducts' ).value = max_products ;
		GetNamedElement( 'DC_MaxDeliveries' ).value = deliveries ;
		GetNamedElement( 'DC_Expiration' ).value = expiration ;
		GetNamedElement( 'DC_Code' ).value = discode ;
	} else {
		window.status = 'Your browser does not support this feature.' ;
	}
	return( result ) ;
}

function SBB_Discount_Update() {
	var di = LoadDiscountInfo() ;
	var discount = di[ 1 ] ;
	var discount_freight = di[ 2 ] ;
	var max_products = di[ 3 ] ;
	var deliveries = di[ 4 ] ;
	var expiration = di[ 5 ] ;
	
	if (document.getElementById( 'SBB_DiscountBox_Title' ) != null) {
		if( di[ 0 ] == 1 ) {
			document.getElementById( 'SBB_DiscountBox_Title' ).innerHTML = document.getElementById( 'SBB_DiscountBox_Msg_4' ).innerHTML ;
		} else if( di[ 0 ] == 2 ) {
			document.getElementById( 'SBB_DiscountBox_Title' ).innerHTML = document.getElementById( 'SBB_DiscountBox_Msg_5' ).innerHTML ;
		} else {
			document.getElementById( 'SBB_DiscountBox_Title' ).innerHTML = document.getElementById( 'SBB_DiscountBox_Msg_3' ).innerHTML ;
		}
	}

	if (document.getElementById( 'SBB_DiscountBox_Visible' ) == null) {
		return;
	}

	if( discount > 0 || discount_freight > 0 ) {
		document.getElementById( 'SBB_DiscountBox_Visible' ).style.display = 'inline' ;
		document.getElementById( 'DiscountBox_Discount' ).innerHTML = discount + "%" ;
		document.getElementById( 'DiscountBox_Freight' ).innerHTML = discount_freight + "%" ;
		document.getElementById( 'DiscountBox_MaxProducts' ).innerHTML = max_products ;
		document.getElementById( 'DiscountBox_ExpirationDate' ).innerHTML = expiration ;
		if( max_products > 0 ) {
			document.getElementById( 'DiscountBox_MaxProductsTR' ).style.display = 'inline' ;
		} else {
			document.getElementById( 'DiscountBox_MaxProductsTR' ).style.display = 'none' ;
		}
		document.getElementById( 'DiscountBox_NumberOfDeliveries' ).innerHTML = deliveries ;
		if( deliveries > 0 ) {
			document.getElementById( 'DiscountBox_MaxDeliveriesTR' ).style.display = 'inline' ;
		} else {
			document.getElementById( 'DiscountBox_MaxDeliveriesTR' ).style.display = 'none' ;
		}
		if( expiration != '' ) {
			document.getElementById( 'DiscountBox_ExpirationTR' ).style.display = 'inline' ;
		} else {
			document.getElementById( 'DiscountBox_ExpirationTR' ).style.display = 'none' ;
		}
	} else {
		document.getElementById( 'SBB_DiscountBox_Visible' ).style.display = 'none' ;
	}
}

function GetFreightRate( amount, count ) {
	var result = 0 ;
	var fd = GetNamedElement( 'FD_Rate' ) ;
	if( fd != null ) {
		var els =  fd.value.split( ';' ) ;
		for( var i = 0 ; i < els.length ; i++ ) {
			var v = els[ i ].split( ':' ) ;
			if( v.length == 2 ) {
				// v[ 0 ] = amount
				// v[ 1 ] = discount
				if( v[ 0 ] >= amount ) {
					// found
					result = parseInt( v[ 1 ] ) ;
					break ;
				}
			}
		}
	}
	return( result ) ;
}
