<!-- Progress Bar -->
<?php echo $this->progressBar;?>

<!-- Cart Summary -->
<?php echo $this->cartSummary;?>

<script type="text/javascript" src="javascript/jquery.validate.min.js"></script>
<script type="text/javascript" src="javascript/jquery.passwordstrength.js"></script>

<!-- Main Page -->
<script type="text/javascript">

function toggleUsers(type) {
    if(type == 'existing') {
        document.getElementById('newUserFields').style.display = 'none';
        document.getElementById('newUserFields').style.visibility = 'hidden';
        
        <?php if ( count($this->paymentmethods) == 0 ) { ?>
        document.getElementById('paymentInformation').style.display = 'block';
        document.getElementById('paymentInformation').style.visibility = 'visible';
        <?php } else { ?>
        document.getElementById('paymentInformation').style.display = 'none';
        document.getElementById('paymentInformation').style.visibility = 'hidden';
        <?php } ?>
        document.getElementById('existingUserFields').style.display = 'block';
        document.getElementById('existingUserFields').style.visibility = 'visible';
        
        document.getElementById('userSelectionExisting').checked = true;
        document.getElementById('userSelectionNew').checked = false;
        
        allowSubmit = '1';
        
    } else {
        document.getElementById('newUserFields').style.display = 'block';
        document.getElementById('newUserFields').style.visibility = 'visible';
        
        document.getElementById('paymentInformation').style.display = 'block';
        document.getElementById('paymentInformation').style.visibility = 'visible';
        
        document.getElementById('existingUserFields').style.display = 'none';
        document.getElementById('existingUserFields').style.visibility = 'hidden';
        
        document.getElementById('userSelectionExisting').checked = false;
        document.getElementById('userSelectionNew').checked = true;
        
        allowSubmit = '0';
    }

    return false;
}

var selectedGateway = '<?php echo @$this->defaultGateway;?>';
function toggleGateway(gatewayName) {

    var el = document.getElementById(selectedGateway+'-extraFields');
    if(el) {
        el.style.display = 'none';
        el.style.visibility = 'hidden';
    }
    var el = document.getElementById(gatewayName+'-extraFields');
    if(el) {
        el.style.display = 'block';
        el.style.visibility = 'visible';
    }
    selectedGateway = gatewayName;
    $('#payment_desc').html(paymentDescs[gatewayName]);
}

function agreeTC() {
    var el = document.getElementById('submitButton');
    if(el.disabled) {
        el.disabled = false;
    } else {
        el.disabled = true;
    }
}

// Phone Digit Validation

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}
function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

$.validator.addMethod("phone", function(phone_number, element) {
    var phoneNumberDelimiters = "- ext.";
    var validWorldPhoneChars = phoneNumberDelimiters + "+";
    var minDigitsInIPhoneNumber = 10;
    s=stripCharsInBag(phone_number,validWorldPhoneChars);
    return this.optional(element) || isInteger(s) && s.length >= minDigitsInIPhoneNumber;
}, "<?php echo $this->lang("Please enter a valid phone number in the format +#.##########");?>");

// Field Validation
$(document).ready(function(){
    $("#submitForm").validate({
       errorClass: "invalid",
       errorElement: "span",
       rules: {
        password: {
            required: true
            <?php if(isset($this->enforcePassword)){ ?>,
            minlength: <?php echo $this->minPassLength;?>
            <?php } ?>
        },
        password_again: {
            required: true,
            equalTo: "#password"
        },
        recaptcha_response_field: {
            required: true
        }
      },
      messages: {
        password_again: {
                equalTo: "<?php echo $this->lang("Passwords do not match");?>."
        }
     }
    });

    $('#password').keyup(function(){$('#result-password').html(passwordStrength($('#password').val()))})

});

var RecaptchaOptions = {
    theme : 'clean'
 };

var allowSubmit = '<?php echo $this->loggedIn; ?>';
function doSubmitForm() {
    if(allowSubmit == '1') {
        jQuery('#submitForm').unbind('submit').submit();
    } else {
        jQuery('#submitForm').submit();
    }
    return false;
}
var vatVAR = null;
// Function to update the tax information
function updateTax() {
    getTax();
    
    // Get the country and state information to pass to the view
    var selectedState = document.getElementById(stateVAR).value;
    var selectedCountry = document.getElementById(countryVAR).options[document.getElementById(countryVAR).selectedIndex].value;
    if(document.getElementById(vatVAR)) {
        var selectedVATNumber = document.getElementById(vatVAR).value;
    }
    
    var received = function (response) {
        $('#taxInformation').html(response.responseText);
    }
    Ext.Ajax.request({
       url: 'order.php?step=updateTax',
       success: received,
       failure: received,
       params: { state: selectedState, country: selectedCountry, vatNumber: selectedVATNumber }
    });
}


function confirmCartCancel() {

    Ext.Msg.show({
       title: "<?php echo $this->lang('Comfirm Cancel Order');?>",
       msg: "<?php echo $this->lang('Are you sure that you wish to cancel this order?');?>",
       buttons: Ext.Msg.YESNO,
       fn: processConfirmCancel,
       icon: Ext.MessageBox.QUESTION
    });
}
// Callback function for ExtJS delete popup
function processConfirmCancel(btn) {

    // Check if we can remove the item
    if(btn == 'yes') {

        // redirect
        window.location = '<?php echo $this->cancelOrderURL;?>';
	}
}
</script>

<style type="text/css">
span.invalid { color: red; padding-left: 10px;}
</style>

<div style="margin-right:220px; margin-bottom:20px;">
<h3><?php echo $this->lang("Your Customer Information");?></h3>
<p class="pagedesc"><?php echo $this->lang("Please enter your profile and billing details below to continue.");?></p>


<form name="customerdata" action="order.php?step=process" method="POST" id="submitForm">

<div id="loggedInBox" style="border:1px solid #DBE6EC; margin:20px 0; padding:10px;-moz-border-radius: 5px;-webkit-border-radius: 5px; <?php if(!$this->loggedIn) { ?>visibility:hidden; display:none;<?php } ?>">
<strong><?php echo $this->lang("Logged in as");?> <span id="user-firstName"><?php echo @$this->gUserFirstName; ?></span> <span id="user-lastName"><?php echo @$this->gUserLastName; ?></span></strong>
<br><?php echo $this->lang("If this is not you");?> <a href="index.php?sessionHash=<?php echo $this->gHash;?>&fuse=newedge&action=Logout&redirTo=order.php?step=4" ><?php echo $this->lang("click here to logout!");?></a>

</div>


<div id="loggedOutBox" style="border:1px solid #DBE6EC; margin:20px 0; padding:10px;-moz-border-radius: 5px;-webkit-border-radius: 5px; <?php if($this->loggedIn) { ?>visibility:hidden; display:none;<?php } ?>">

    <?php echo $this->lang("I am a");?>&nbsp;&nbsp;
    <input id="userSelectionNew" type="radio" name="userSelection" value="new" checked onclick="toggleUsers('new');"> <?php echo $this->lang("New Customer");?>&nbsp;&nbsp;
    <input id="userSelectionExisting" type="radio" name="userSelection" value="existing" onclick="toggleUsers('existing');"> <?php echo $this->lang("Existing Customer");?>

</div>


<?php if(@$this->fieldError) { ?>
    <div style='border:1px solid #900; background:#ffeded; color:#900; font-weight:bold; margin-bottom:20px; padding:10px;-moz-border-radius: 5px;-webkit-border-radius: 5px;'><?php echo $this->fieldError;?></div>
<?php } ?>
    
    <div id="existingUserFields" style="visibility:hidden; display:none;">
        <table class="ts2" cellspacing="0">
            <tr>
              <td class="label" valign="top"><?php echo $this->lang("E-mail Address");?>:</td>
              <td><input type="text" size="40" name="existingEmail"></td>
            </tr>
            <tr>
              <td class="label" valign="top"><?php echo $this->lang("Password");?>:</td>
              <td><input type="password" name="existingPassword"></td>
            </tr>
        </table>
    </div>
    
<?php if ( count($this->customFields) > 0) { ?>
    <div id="newUserFields" <?php if($this->loggedIn) { ?>style="visibility:hidden; display:none;"<?php } ?>>
        <table class="ts2" cellspacing="0">

            <!-- loop through the custom fields -->
            <?php foreach ($this->customFields as $customField) { ?>
                <tr>
                  <td class="label" valign="top"><?php echo $customField['rowLabel']; ?>:</td>
                  <td>

                      <?php if(@$customField['fieldType'] == 'text') { ?>

                      <input type="text" name='CT_<?php echo @$customField['fieldId'];?>' id='CT_<?php echo @$customField['fieldId'];?>' <?php if(@$customField['fieldSize']){ echo 'size="'.$customField['fieldSize'].'"'; } else { echo'size="25"'; } ?> value="<?php echo htmlentities(@$customField['value']); ?>"  <?php if(@$customField['isRequired']) {echo 'class="required"'; } ?> />

                      <?php } ?>
                      <?php if(@$customField['fieldType'] == 'email') { ?>

                        <input type="text" name='CT_<?php echo @$customField['fieldId'];?>' id='CT_<?php echo @$customField['fieldId'];?>' size="40" value="<?php echo @$customField['value']; ?>"  class="required email" />

                      <?php } ?>
                      <?php if(@$customField['fieldType'] == 'phone') { ?>

                        <input type="text" name='CT_<?php echo @$customField['fieldId'];?>' id='CT_<?php echo @$customField['fieldId'];?>' <?php if(@$customField['fieldSize']){ echo 'size="'.$customField['fieldSize'].'"'; } else { echo'size="25"'; } ?> value="<?php echo htmlentities(@$customField['value']); ?>" class="phone <?php if(@$customField['isRequired']) {echo 'required phone'; } ?>" />

                      <?php } ?>
                      <?php if(@$customField['fieldType'] == 'language') { ?>

                        <select name='CT_<?php echo $customField['fieldId'];?>' id="CT_<?php echo $customField['fieldId'];?>">
                            <?php foreach ($customField['languages'] as $language) { ?>
                                <option value="<?php echo $language['typeLanguageOptionValue']; ?>" <?php echo $language['typeLanguageOptionSelected']; ?> ><?php echo $language['typeLanguageOptionLabel']; ?></option>
                            <?php } // end languages foreach ?>
                        </select>

                      <?php } ?>
                      <?php if(@$customField['fieldType'] == 'yesno') { ?>

                        <select name='CT_<?php echo $customField['fieldId'];?>' id="CT_<?php echo $customField['fieldId'];?>">
                            <option value="1" <?php echo $customField['yesSelected']; ?> ><?php echo $this->lang("Yes"); ?>&nbsp;</option>
                            <option value="0" <?php echo $customField['noSelected']; ?> ><?php echo $this->lang("No"); ?>&nbsp;</option>
                        </select>

                      <?php } ?>
                      <?php if(@$customField['fieldType'] == 'organization') { ?>

                        <input type="text" name='CT_<?php echo @$customField['fieldId'];?>' id='CT_<?php echo @$customField['fieldId'];?>' <?php if(@$customField['fieldSize']){ echo 'size="'.$customField['fieldSize'].'"'; } else { echo'size="25"'; } ?> value="<?php echo htmlentities(@$customField['value']); ?>"  <?php if(@$customField['isRequired']) {echo 'class="required"'; } ?> />&nbsp; <input type="checkbox" name="isOrganization" <?php if ($customField['checkedIsOrganization']) { echo "'checked=checked;'"; }?> />

                      <?php } ?>
                      <?php if(@$customField['fieldType'] == 'date') { ?>

                        <span style="display: inline-block">
                            <input type="hidden" name='CTT_<?php echo $customField['fieldId'];?>' id="CTT_<?php echo $customField['fieldId'];?>" value="<?php echo $customField['typeDateConstant'];?>" />
                            <input type="text" name='CT_<?php echo $customField['fieldId'];?>' id="CT_<?php echo $customField['fieldId'];?>" value="<?php echo htmlentities($customField['value']); ?>" />
                            <script type="text/javascript">
                                datefields.push("CT_<?php echo $customField['fieldId'];?>");
                            </script>
                        </span>

                      <?php } ?>
                      <?php if(@$customField['fieldType'] == 'textarea') { ?>

                         <textarea name='CT_<?php echo $customField['fieldId'];?>' id="CT_<?php echo $customField['fieldId'];?>" style="display: inline-block; width: 300px; vertical-align: top;" rows ="10" cols='30'></textarea>

                      <?php } ?>
                      <?php if(@$customField['fieldType'] == 'dropdown') { ?>

                         <select name='CT_<?php echo $customField['fieldId'];?>' id="CT_<?php echo $customField['fieldId'];?>">
                            <?php foreach ($customField['dropdownoptions'] as $dropdown) { ?>
                                <option value="<?php echo $dropdown['typeDropdownOptionValue']; ?>" <?php echo $dropdown['typeDropdownOptionSelected']; ?> ><?php echo $dropdown['typeDropdownOptionLabel']; ?></option>
                            <?php } //end forloop dropdown ?>
                        </select>

                      <?php } ?>
                      <?php if(@$customField['fieldType'] == 'country') { ?>

                          <script type="text/javascript">
                              countryVAR = 'CT_<?php echo $customField['fieldId'];?>';
                          </script>
                          <select name='CT_<?php echo $customField['fieldId'];?>' id="CT_<?php echo $customField['fieldId'];?>" onChange="updateTax();">
                              <?php echo $customField['countries']; ?>
                          </select>

                      <?php } ?>

                      <?php if(@$customField['fieldType'] == 'state') { ?>
                          <script type="text/javascript">
                              var stateVAR = 'CT_<?php echo $customField['fieldId'];?>';
                          </script>
                          <input type="text" name='CT_<?php echo @$customField['fieldId'];?>' id='CT_<?php echo @$customField['fieldId'];?>' <?php if(@$customField['fieldSize']){ echo 'size="'.$customField['fieldSize'].'"'; } else { echo'size="25"'; } ?> value="<?php echo htmlentities(@$customField['value']); ?>"  <?php if(@$customField['isRequired']) {echo 'class="required"'; } ?> onBlur="updateTax();" />
                      <?php } //isState ?>

                      <?php if(@$customField['fieldType'] == 'vat') { ?>

                          <script type="text/javascript">
                              var vatVAR = 'CT_<?php echo $customField['fieldId'];?>';
                          </script>
                          <input type="text" name='CT_<?php echo @$customField['fieldId'];?>' id='CT_<?php echo @$customField['fieldId'];?>' <?php if(@$customField['fieldSize']){ echo 'size="'.$customField['fieldSize'].'"'; } else { echo'size="25"'; } ?> value="<?php echo htmlentities(@$customField['value']); ?>"  <?php if(@$customField['isRequired']) {echo 'class="required"'; } ?> onBlur="updateTax();" />
                          <span id="vat_country"></span>
                          <span id="vat_valid" style="display:none"><?php echo $this->lang("Valid VAT Number"); ?></span>
                          <span id="vat_invalid" style="display:none"><?php echo $this->lang("Invalid VAT Number"); ?> <a href="javascript:updateTax();"><?php echo $this->lang("Retry"); ?></a></span>
                          <span id="vat_error" style="display:none"><?php echo $this->lang("Unable to validate at the moment."); ?> <a href="javascript:updateTax();"><?php echo $this->lang("Retry"); ?></a></span>

                      <?php } ?>
                      <?php if(@$customField['fieldType'] == 'password') { ?>

                         <input type="password" name='<?php echo @$customField['fieldId'];?>' id='<?php echo @$customField['fieldId'];?>' <?php if(@$customField['fieldSize']){ echo 'size="'.$customField['fieldSize'].'"'; } else { echo'size="25"'; } ?>  class="<?php echo $customField['validationClass'];?>" />
                         <div style="color:green; padding-top: 2px; padding-left: 2px;" id='result-<?php echo @$customField['fieldId'];?>'></div>
                      <?php } ?>

                      <?php if(@$customField['fieldDescription']) { ?>
                        <br />
                        <span style="font-size:11px; color:#666;"><?php echo @$customField['fieldDescription'];?></span>
                      <?php } ?>

                  </td>

                </tr>
            <?php } //end customField ?>
            <?php if(@$this->showCaptcha) { ?>
                <tr>
                  <td class="label"><?php echo $this->lang('Are you Human?');?></td>
                  <td><?php echo recaptcha_get_html($this->publickey); ?></td>
                </tr>
            <?php } ?>
        </table>
    </div>
<?php } ?>


<h3><?php echo $this->lang("Payment Information");?></h3>
<div id="taxInformation">
    <?php if(@$this->tax['taxRequired']) { ?>
        <br>
        <?php echo $this->lang('Sub Total');?>: <?php echo @$this->tax['subTotal'];?> <br>
        <?php if(@$this->tax['taxAmount']) {
            echo @$this->tax['taxName'];?>: <?php echo @$this->tax['taxAmount'];?> <br>
        <?php } ?>
        <?php if(@$this->tax['tax2Amount']) {
            echo @$this->tax['tax2Name'];?>: <?php echo @$this->tax['tax2Amount'];?> <br>
        <?php } ?>
        <?php if(@$this->tax['taxCoupons']) { ?>
            <?php echo @$this->tax['couponDiscounts'];?> <br>
        <?php } ?>
        <br>
        <strong><?php echo $this->lang('Total to pay');?>: <?php echo @$this->tax['totalPay'];?></strong>
    <?php } ?>
</div>

<div id="paymentInformation" <?php if($this->loggedIn) { ?>style="visibility:hidden; display:none;"<?php } ?>>
<div style="border:2px solid #ddd; background:#f6f6f6; padding:15px; margin:20px 0;-moz-border-radius: 5px;-webkit-border-radius: 5px;">
    <?php if(count($this->paymentmethods) != 0) { ?>
        <?php echo $this->lang("Pay using");?>: <select name="paymentMethod" id="paymentMethod" onchange="toggleGateway(this.options[this.selectedIndex].value);">
            <?php foreach($this->paymentmethods AS $tmpKey => $key) { ?>
                <option id="<?php echo $key['paymentTypeOptionValue'];?>" value="<?php echo $key['paymentTypeOptionValue'];?>" <?php if($key['paymentTypeOptionSelected']) { $descKey = $tmpKey?> selected="selected" <?php } ?>><?php echo $key['paymentTypeOptionLabel'];?></option>
            <?php } ?>
        </select>
        <?php if ( isset($descKey) ) { ?>
        <div id="payment_desc" style="font-size:11px; color:#666;"><?php echo $this->paymentmethods[$descKey]['description']; ?></div>
        <?php } ?>
    <?php } else { ?>
        <strong><?php echo $this->lang('There are no Payment Processors configured!');?></strong>
    <?php } ?>
</div>


<!-- Handle the extra fields for payment processors -->
<?php foreach($this->paymentmethods AS $key) { ?>
    <?php if(@is_array($key['extraFields'])) { ?>
        <input type="hidden" name="<?php echo $key['paymentTypeOptionValue']."_autopayment"?>" value="<?php echo $key['autoPayment'];?>"></input>
        <div id="<?php echo $key['paymentTypeOptionValue'];?>-extraFields" <?php if(!$key['paymentTypeOptionSelected']) {?> style="visibility:hidden; display:none;" <?php } ?>>
            <table class="ts2" cellspacing="0">
             <?php foreach($key['extraFields'] AS $field) { ?>
                <tr>
                  <td class="label"><?php echo $field['fieldTitle'];?></td>
                  <td>
                      <?php if($field['fieldType'] == 'yesno') { ?>

                          <select name="<?php echo $field['fieldName'];?>" <?php if(@$field['fieldDisabled']) {?> disabled <?php } ?>>
                              <option value="1" <?php if($field['fieldValue'] == 1) { ?>selected="selected"<?php } ?>><?php echo $this->lang('Yes');?>&nbsp;</option>
                              <option value="0" <?php if($field['fieldValue'] == 0) { ?>selected="selected"<?php } ?>><?php echo $this->lang('No');?>&nbsp;</option>
                          </select>

                      <?php } elseif($field['fieldType'] == 'text') { ?>

                        <input name="<?php echo $field['fieldName'];?>" type="text" size="<?php echo @$field['fieldSize'];?>" value="<?php echo @$field['fieldValue'];?>" <?php if(@$field['fieldDisabled']) {?> disabled <?php } ?> />
                      
                      <?php } elseif($field['fieldType'] == 'dropdown') { ?>

                          <select name="<?php echo $field['fieldName'];?>" <?php if(@$field['fieldDisabled']) {?> disabled <?php } ?>>
                              <?php foreach($field['fieldValue'] AS $value) { ?>
                                <option value="<?php echo $value['value'];?>" ><?php echo $value['text'];?>&nbsp;</option>
                              <?php } ?>
                          </select>

                      <?php } elseif($field['fieldType'] == 'weaccept') { ?>

                        <?php foreach($field['fieldValue'] AS $weAccept) { ?>
                            <img src="<?php echo $weAccept['img'];?>" id="<?php echo $weAccept['id'];?>" alt="<?php echo $weAccept['alt'];?>">
                        <?php } ?>
         
                      <?php } ?>
                    
                      <?php if(@$field['fieldDescription']) { ?>
                        <br />
                        <span style="font-size:11px; color:#666;"><?php echo $field['fieldDescription'];?></span>
                      <?php } ?>
                  </td>
                </tr>
             <?php } ?>
            </table>
        </div>

    <?php } ?>
<?php } ?>

</div>

<p style="text-align: center">
    <?php if(@$this->termsConditions) { ?>
        <input type="checkbox" onclick="agreeTC();" name="agree" value="1" style="position:relative; left:2px;">
        <?php echo $this->lang('I Agree to the'); ?>
        <?php if($this->termsConditions == '-1') { ?>
            <a href="<?php echo @$this->termsConditionsUrl;?>" target="_blank"><?php echo $this->lang('Terms and Conditions');?></a>
        <?php } else { ?>
            <a onclick="javascript:GB_show('Terms & Conditions','terms',550,700);" href="#"><?php echo $this->lang('Terms and Conditions');?></a>
        <?php } ?>
        <br><br>
    <?php } ?>
<?php if(count($this->paymentmethods) != 0) { ?>        
    <input class="button" onclick="return doSubmitForm();" id="submitButton" type="submit" value="<?php echo $this->lang('Finish');?>" <?php if(@$this->termsConditions) { ?>disabled<?php } ?> />
<?php } ?>
</p>
<div id="terms" style="display:none"><?php echo @$this->termsConditionsText;?></div>
<div class="clear"></div>

    <script type="text/javascript">
        var mainForm = document.forms['customerdata'];
        updateTax();
        <?php if(@$this->validatingExisting) { ?> toggleUsers('existing'); <?php } ?>

        var paymentDescs = new Array();   
        <?php foreach($this->paymentmethods as $key) { ?>
        paymentDescs['<?php echo $key['paymentTypeOptionValue']; ?>'] = '<?php echo $key['description']; ?>';
        <?php } ?>

        <?php if ( count($this->paymentmethods) == 1 && @$this->defaultGateway == '' ) { ?>
        toggleGateway('<?php echo $this->paymentmethods[0]['paymentTypeOptionValue']; ?>');
        <?php }  ?>
    </script>
</form>

