// custom checkout form for fusion2
// overwrites default iem checkout form

iem.output.displayCheckout = function( flds, currPage, frmAction ) {
    var htm = '';
   
    var req = new Array();

    // fusion2 never allows delivery details, so always skip page 2
    if( flds[ 'nextPage' ][ 'value' ] == 2 ) {
        flds[ 'nextPage' ][ 'value' ] = 3;
    }

    // print out all hidden fields
    for( var fld in flds ) {
        if( flds[ fld ][ 'type' ] == 'HIDDEN' ) {
            htm += '<input type="hidden" name="' + fld + '" value="' + flds[ fld ][ 'value' ] + '" />\n';    
        }
    }
    switch( currPage ) {
        case 1 : {
            
            htm += '<h1>Step 1: Your details</h1>';                
            req = new Array( 'First_Name', 'Surname', 'Address_1', 'Town', 'County', 'Postcode', 'Country', 'EMail', 'Telephone' );
            break;   
        }
        case 3 : {
            htm += '<h1>Step 2: Card details</h1>';
            req = new Array( 'Card_Name', 'Card_Type', 'Card_Number', 'Card_End' );
            break;   
        }
    }
    
    htm += '<table>\n\n';
    
    // print out input fields
    for( var fld in flds ) {
        if( flds[ fld ][ 'type' ] != 'HIDDEN' ) {
            
            var reqMark = '';
            
            for( var r = 0; r < req.length; r++ ) {
                
                if( req[ r ] == fld ) {
                    reqMark = '*';
                }
            }
            
            var fldText = fld.split( '_' ).join( ' ' );
            var fldName = 'fld' + flds[ fld ][ 'idx' ] + '_1';
            
            htm += '<tr><th>' + fldText + ' ' + reqMark + '</th><td>';
            
            switch( fld ) {
                case 'Mail_List' : {
                    htm += '<input type="checkbox" style="border: 0" name="' + fldName + '" title="' + fldText + '" checked="checked" />';                                   
                    break;
                }
                case 'Title' : {
                    htm += '<select name="' + fldName + '" title="' + fldText + '">' +
                        '<option>Mr</option>' + 
                        '<option>Mrs</option>' + 
                        '<option>Ms</option>' + 
                        '<option>Miss</option>' + 
                    '</select>';  
                    break; 
                }
                case 'Country' :
                case 'Delivery_Country' : {
                    htm += 'United Kingdom';  
                    htm += '<input type="hidden" name="' + fldName + '" title="' + fldText + '" value="United Kingdom" />';
                    break; 
                }
                case 'Card_Number' : {
                    htm += '<input type="text" class="text" name="' + fldName + '" title="' + fldText + '" value="" onchange="this.value=this.value.replace( /[^0-9]*/g, \'\' );" />';
                    break;   
                }
                case 'Card_Type' : {

                    var cardTypes = new Array( 'Mastercard', 'Visa', 'Electron', 'Switch/Maestro', 'Solo', 'HSBC' );

                    htm += '<select name="' + fldName + '" title="' + fldText + '">';
                    htm += '<option value=""></option>';   
                    for( var i = 0; i < cardTypes.length; i++ ) {
                        htm += '<option value="' + cardTypes[ i ] + '">' + cardTypes[ i ] + '</option>';   
                    }
                    htm += '</select>';
                    break;   
                }
                case 'Card_Start' :
                case 'Card_End' : {

                    // generate month drop down
                    var months = new Array( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
                    htm += '<select name="' + fldName + '_mm" title="' + fldText + '" onchange="this.form[ \'' + fldName + '\' ].value=this.form[ \'' + fldName + '_mm\' ][ this.form[ \'' + fldName + '_mm\' ].selectedIndex ].value + \'/\' + this.form[ \'' + fldName + '_yyyy\' ][ this.form[ \'' + fldName + '_yyyy\' ].selectedIndex ].value">';
                    htm += '<option value=""></option>';   
                    for( var i = 0; i < 12; i++ ) {
                        htm += '<option value="' + ( i + 1 ) + '">' + months[ i ] + '</option>';   
                    }
                    htm += '</select>';
                    
                    // generate year drop down                                            
                    var currDate = new Date();
                    var currYear = currDate.getFullYear();
                    var maxAge = 5;
                    
                    htm += '<select name="' + fldName + '_yyyy" title="' + fldText + '" onchange="this.form[ \'' + fldName + '\' ].value=this.form[ \'' + fldName + '_mm\' ][ this.form[ \'' + fldName + '_mm\' ].selectedIndex ].value + \'/\' + this.form[ \'' + fldName + '_yyyy\' ][ this.form[ \'' + fldName + '_yyyy\' ].selectedIndex ].value">';
                    htm += '<option value=""></option>';   
                    if( fld == 'Card_Start' ) {
                        for( var i = currYear; i > currYear - maxAge; i-- ) {
                            htm += '<option value="' + i + '">' + i + '</option>';   
                        }
                    } else {
                        for( var i = currYear; i < currYear + maxAge; i++ ) {
                            htm += '<option value="' + i + '">' + i + '</option>';   
                        }
                    }
                    htm += '</select>';                        
                    htm += '<input type="hidden" name="' + fldName + '" title="' + fldText + '">';
                    break; 
                }
                default : {
                    switch( flds[ fld ][ 'type' ] ) {
                        case 'LARGE_TEXT' : { 
                            htm += '<textarea name="' + fldName + '" title="' + fldText + '" value="' + flds[ fld ][ 'value' ] + '" class="text"></textarea>';
                            break;
                        }
                        case 'BOOL' : {
                            htm += '<input type="checkbox" style="border: 0" name="' + fldName + '" title="' + fldText + '" />';                                
                            break;
                        }
                        default: {
                            htm += '<input type="text" name="' + fldName + '" title="' + fldText + '" value="' + flds[ fld ][ 'value' ] + '" class="text" />';
                            break;
                        }
                    }
                    break;    
                }
            }
            htm += '</td></tr>\n';
        }
    }
    //if( currPage == 1 ) {
    //    htm += '<tr><th colspan="2"><p><input type="checkbox" style="border: 0" onclick="this.form.nextPage.value = ( this.checked ? 3 : 2 );" /> Use this address for delivery</p></th></tr>';
    //}                
   
    //htm += '<tr><th colspan="2"><input type="submit" value="Continue" /><br /><br /></th></tr>';
    
    htm += '<tr><th colspan="2"><input type="image" class="image" src="/fusion2/_res/69.gif" border="0" alt="Continue" /><br /><br /></th></tr>';

    // confirm address details
    if( currPage == 3 ) {
        htm += '<tr><td colspan="2"><strong>Your details</strong></td></tr>\n';
        for( var fld in flds ) {
            if( flds[ fld ][ 'type' ] == 'HIDDEN' ) {
                if( fld != 'cartId' && fld != 'location' && fld != 'func_1' && fld != 'nextPage' ) {
                    htm += '<tr><td colspan="2">' + flds[ fld ][ 'value' ] + '</td></tr>\n';
                    //htm += '<tr><th>' + fld.split( '_' ).join( ' ' ) + '</th><td>' + flds[ fld ][ 'value' ] + '</td></tr>\n';
                }
            }
        }
        htm += '<tr><th>&nbsp;</th><td>&nbsp;</td></tr>';
    }
    htm += '</table>\n\n';
    
    var reqCmd = '';
    for( var i = 0; i < req.length; i++ ) {
        if( flds[ req[ i ] ] ) {
            reqCmd += 'this.fld' + flds[ req[ i ] ][ 'idx' ] + '_1';
        } else {
            reqCmd += 'this.' + req[ i ];
        }
        if( i < req.length - 1 ) {
            reqCmd += ', ';   
        }
    }

    htm = '<form name="frmCheckout" method="post" action="' + frmAction + '" onsubmit="return iem.output.validateForm(' + reqCmd + ');">' + htm + '</form>\n\n';

    document.write( htm );
}


iem.output.displayCheckoutWin = function( checkoutUrl ) {

    if( iem.output.windowTimer ) {
        clearInterval( iem.output.windowTimer );
        iem.output.checkoutWin.close();
        iem.output.checkoutWin = null;
    }
    var x = ( screen.width  - 450 ) / 2;
    var y = ( screen.height - 600 ) / 2;
    iem.output.checkoutWin = top.open( checkoutUrl, 'checkoutWin', 'width=450,height=600,status=yes,scrollbars=yes,resizable=yes,left=' + x + ',top=' + y );
    iem.output.windowTimer = setInterval( function() { iem.output.windowMonitor( iem.output.checkoutWin ); }, 500 );
}


iem.output.displayCartList = function( cartRows ) {

    var htm = '';
    var rowCount = 0;

    for( var lineID in cartRows ) {

        rowCount++;

        var row = cartRows[ lineID ];

        htm += '<form name="frm' + lineID + '" onsubmit="if(this.productQty.value>0){iem.CartUpdate( \'[LineID] = [' + lineID + ']\', { \'fld' + lineID + '_' + row[ 'Qty' ][ 'index' ] + '\' : this.productQty.value } );iem.CartList( \'[Completed] = [-1]\' );iem.CartSubmit( window.location );}else{alert(\'Please enter a quantity\');}return false;">' +
            '<tr>' +
            //' <td rowspan="2">' +
            //( row[ 'Image_Thumbnail' ][ 'value' ] ? ' <img src="' + row[ 'Image_Thumbnail' ][ 'value' ] + '" border="0" alt="' + row[ 'Product_Name' ][ 'value' ] + '" />' : '&nbsp;' ) +
            //' </td>' +
            ' <td colspan="4">';
        //htm += '    <h3>' + row[ 'Product_Name' ][ 'value' ] + ' ' + row[ 'Product_Code' ][ 'value' ] + '</h3>';
        
        for( var i = 1; i <= 5; i++ ) {
            htm += ( row[ 'Option_' + i ][ 'value' ] != 0 ? row[ 'Option_' + i ][ 'value' ].replace( '|', ': ' ) + ' ' : '' );
        }
        
        if( row[ 'Instructions' ][ 'value' ] ) {
            htm += '<br />Date of visit: ' + row[ 'Instructions' ][ 'value' ];
        }
        
        htm += ' </td>' +
            '</tr>' +
            '<tr>' +
            ' <td><input type="text" name="productQty" value="' + row[ 'Qty' ][ 'value' ] + '" size="2" maxlength="3" /></td>' +
            ' <td class="price">@ ' + row[ 'Base_Price' ][ 'value' ] + '</td>' +
            ' <td class="price"><strong>' + row[ 'Sub_Total' ][ 'value' ] + '</strong></td>' +
            //' <td class="price"><input type="submit" value="Update" />&nbsp;' +
            ' <td class="price"><input type="image" class="image" border="0" src="/_res/73.gif" alt="Update" />&nbsp;' +
            //'<input type="button" value="Remove" onclick="iem.CartDelete( \'[LineID] = [' + lineID + ']\' );iem.CartList( \'[Completed] = [-1]\' );iem.CartSubmit();return false;" />' + 
            '<a href="#" onclick="iem.CartDelete( \'[LineID] = [' + lineID + ']\' );iem.CartList( \'[Completed] = [-1]\' );iem.CartSubmit();return false;"><img src="/_res/71.gif" alt="Remove" border="0" /></a>' + 
            '</td>' +
            '</tr>' +
            '<tr><td colspan="4"><hr /></td></tr>' +
        '</form>';
    }
    if( ! rowCount ) {
        htm += '<tr><td colspan="4">Sorry, your shopping basket is currently empty.<br /><br /></td></tr>';
    }
    document.write( htm );
}


iem.output.displayCartTotal = function( totals ) {
    
    var htm = '';
    
    var lookup = {
        'Product_Total' : 'Subtotal',   
        //'Shipping_Total' : 'Shipping',
        'Grand_Total' : 'TOTAL (inc VAT)'
    }
    
    for( var total in totals ) {
        if( lookup[ total ] ) {
            htm += '<tr>' +
                '<td colspan="3"><strong>' + ( lookup[ total ] ? lookup[ total ] : total ) +  '</strong></td>' + 
                '<td class="price">' + totals[ total ] + '</td>' + 
                '<td>&nbsp;</td>' + 
            '</tr>';
        }
    }
    
    document.write( htm );
}    

    iem.output.displayCartPreview = function( cartRows ) {

        var htm = '';
        
        for( var lineID in cartRows ) {

            var row = cartRows[ lineID ];        
        
            htm += '<h2>' + row[ 'Product_Code' ][ 'value' ] + '</h2>' + 
            '<form name="frm' + lineID + '" onsubmit="iem.CartUpdate( \'[LineID] = [' + lineID + ']\', { \'fld' + lineID + '_' + row[ 'Completed' ][ 'index' ] + '\' : \'-1\', \'fld' + lineID + '_' + row[ 'Qty' ][ 'index' ] + '\' : this.productQty.value, \'fld' + lineID + '_' + row[ 'Instructions' ][ 'index' ] + '\' :  ( typeof( this.productMessage ) == \'object\' ? this.productMessage.value : \'\' ) } );iem.CartList( \'[Completed] = [-1]\' );iem.CartSubmit();return false;">' +
            '<table>';
            
            for( var i = 1; i <= 5; i++ ) {
                if( row[ 'Option_' + i ][ 'options' ] ) {
            
                    htm += '<tr>';
                    htm += '<th>' + row[ 'Option_' + i ][ 'optName' ] + '</th>';
                    htm += '<td>';
                    
                    var optCount = 0;
                    for( var idx in row[ 'Option_' + i ][ 'options' ] ) optCount++;
                    
                    if( optCount > 1 ) {
                        htm += '<select name="Option_' + i + '" onchange="iem.CartUpdate( \'[Product_ID] = [' + row[ 'Product_ID' ][ 'value' ] + '] and [Completed] = [0]\', { \'fld' + lineID + '_' + row[ 'Option_' + i ][ 'index' ] + '\' : this[ this.selectedIndex ].value } );iem.CartList( \'[Product_ID] = [' + row[ 'Product_ID' ][ 'value' ] + '] and [Completed] = [0]\' );iem.CartSubmit( window.location );">';
                		for( var idx in row[ 'Option_' + i ][ 'options' ] ) {
                        	htm += '<option value="' + idx + '"' + ( idx == row[ 'Option_' + i ][ 'value' ] ? ' selected="selected"' : '' ) + '>' + row[ 'Option_' + i ][ 'options' ][ idx ][ 0 ] + '</option>';
                		}
                        htm += '</select>';
                    } else {
                		for( var idx in row[ 'Option_' + i ][ 'options' ] ) {
                        	htm += row[ 'Option_' + i ][ 'options' ][ idx ][ 0 ];
                		}
                    }
                    
                    htm += '</td>';
                    htm += '</tr>';
                }
            }
            
            htm += '<tr>' + 
                '<th>Quantity</th>' + 
                '<td><input type="text" name="productQty" value="' + row[ 'Qty' ][ 'value' ] + '" size="2" maxlength="3" class="qty" title="Enter Quantity here..." /> @ ' + row[ 'Base_Price' ][ 'value' ] + '</td>' +
                '</tr>' +
                '<tr>' +
                '<th>Date of Visit</th>' +
                '<td><input type="text" name="productMessage" value="' + ( row[ 'Option_1' ][ 'value' ] == 3 ? '31/12/2008' : '' ) + '" /> (dd/mm/yyyy)</td>' +
                '</tr>' +
                '</table>' +
                '<div style="padding: 5px;"><input type="image" class="image" src="/_res/78.gif" alt="Add to basket" border="0" /></div>' +
            '</form>';
        }
        document.write( htm );
    }   
    
iem.output.windowTimer = null;

    iem.output.windowMonitor = function( win ) {
        if( win.closed ) {
            iem.win.location.reload();
            clearInterval( iem.output.windowTimer );
        }
    }
    
