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

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

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

<!-- Main Page -->
<script type="text/javascript">
function checkAvailabilitySubDomain(){
    var tld = false;
    if(document.getElementById('subDomainTld') != undefined){
        tld = document.getElementById('subDomainTld').options[document.getElementById('subDomainTld').selectedIndex].value;
    }
    searchSubDomain(document.getElementById('subDomain').value,tld,true,false);
}

// Callback function for ExtJS delete popup
function searchSubDomain(name, tld, animate, continueButton) {
    if(name == '') {
        $('#submitForm').submit();
        return false;
    }

    // Handle the searching animation
    if(animate) {
    	$('#subDomainSearchResults').html('<table class="ts2" border="0" cellspacing="0"> <tr> <td colspan="6" style="font-size:16px; font-weight:bold; color:#090;" id="textAvailable"><?php echo $this->lang('Searching');?><span id="dots">.</span></td> </tr> </table>');
    	animateSearch();
    }

    // Callback for the ajax request
    var received = function (response) {
        // Populate the html
        $('#subDomainSearchResults').html(response.responseText)
        if(continueButton && submitSubDomainOnRegister(false)){
            $('#submitForm').submit();
        }
    }
    // Query the subdomain
    Ext.Ajax.request({
       url: 'order.php?step=subsearch',
       success: received,
       failure: received,
       params: { name: name, tld: tld}
    });
}

// Function to animiate the searching
function animateSearch() {
    var dotspan = document.getElementById('dots');
    window.setInterval(function(){
        if(dotspan.innerHTML == '....'){
           dotspan.innerHTML = '.';
        } else {
           dotspan.innerHTML += '.';
        }
     }, 1000);
}

// Function to manage what to do when submiting the subdomain
function submitSubDomain(){
    if(document.getElementById('subDomain') == undefined || document.getElementById('subDomain').value == ''){
        return true;
    }else if(typeof submitSubDomainOnRegister != 'function') {
        var tld = false;
        if(document.getElementById('subDomainTld') != undefined){
            tld = document.getElementById('subDomainTld').options[document.getElementById('subDomainTld').selectedIndex].value;
        }
        searchSubDomain(document.getElementById('subDomain').value,tld,true,true);
        return false;
    }else{
        return submitSubDomainOnRegister(true);
    }
}


// Field Validation
$(document).ready(function(){
    $("#submitForm").validate({
       errorClass: "invalid",
       errorElement: "span"
    });

    var selectedLink;
    $(function() {
        $('a:not(.button)').click(function(e){
        	selectedLink = this;
        	if ($(this).hasClass('showcancelalert')) {
        		Ext.MessageBox.confirm('Confirm', 'Are you sure you want to cancel this item?', confirmDeleteItemFromCart);
	        	return false;
        	} else if ($(this).hasClass('nopopupalert').hasClass('openinnewtab')) {
                        return true;
                } else if ($(this).hasClass('nopopupalert')) {
        		confirmDeleteItemFromCart("yes");
        	} else {
	          	Ext.MessageBox.confirm('Confirm', 'Leaving now will cancel this item.  Are you sure you want to leave?', confirmDeleteItemFromCart);
	        	return false;
        	}
        });
    });

    function confirmDeleteItemFromCart(btn, text)
    {
        //lets remove the cartitem via ajax
        if (btn=="yes") {
            // THIS CODE ONLY CLEAN THE CURRENT PRODUCT FROM THE CART
            var cartItem = "<?php echo $this->cartItemId;?>";
            $.ajax({
                url: "index.php?fuse=admin&action=deleteCartItem&cartItem="+cartItem+"&bundleCartItem="+cartItem,
                context: document.body,
                success: function(){
                    window.location=selectedLink;
                }
            });
        }
    }
});
</script>

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

<div style="margin-right:220px; margin-bottom:20px;">

<?php
	if(isset($_POST["domainName"])) {
	$domainUrl = "&amp;domainName=" . $_POST["domainName"];
	if(isset($_POST["tld"])) $domainUrl .= "&amp;tld=" . $_POST["tld"];
	} else {
		$domainUrl = "";
	}
?>

<form action="<?php echo $this->nextUrl.$domainUrl;?>" method="POST" name="submitForm" id="submitForm">
<input type="hidden" name="formId" value="configureProduct">
<input type="hidden" name="product" value="<?php echo $this->product['id'];?>">

<h3><?php echo $this->lang("Configure Product");?>: <?php echo $this->product['name'];?></h3>
<p class="pagedesc">
    <?php if ( count($this->customFields) > 0) { ?>
        <?php if ( count($this->packageAddons) > 0 ) { ?>
            <?php echo $this->lang("Please fill in the product fields below and check through our available add-ons. (* indicates a required field)");?>
        <?php } else { ?>
            <?php echo $this->lang("Please fill in the product fields below. (* indicates a required field)");?>
        <?php } ?>
    <?php } else { ?>
        <?php if ( count($this->packageAddons) > 0 ) { ?>
            <?php echo $this->lang("Please check through our available add-ons.");?>
        <?php } ?>
    <?php } ?>
</p>


<?php if ( count($this->customFields) > 0) { ?>
<table class="ts2" cellspacing="0">
	<?php foreach ( $this->customFields as $field ) { ?>
	<tr>

		<td class="label"><?php if($field['required']) { echo '*'; } ?> <?php echo $this->lang($field['name']);?>:</td>
		<td>
			<?php if ( $field['type'] == typeTEXTFIELD ) { ?>
				<input value="<?php echo $field['savedValue']; ?>" class="<?php if($field['required']) { echo 'required'; } ?>" <?php if(isset($field['isDomain'])) { echo 'id="subDomain"'; } ?> name="PCT_<?php echo $field['id']; ?><?php if(isset($field['isDomain'])) { echo "_1"; } ?>" type="text" onkeypress="return noenter(event);"  />

                <?php if(isset($field['isDomain'])) { ?>
                    <?php if(isset($this->subDomains)) { ?>
                        . <select class="dropdown" id="subDomainTld" name="subDomainTld">
                            <option value="0"><?php echo $this->lang('Choose a Subdomain');?></option>
                            <?php foreach($this->subDomains AS $key) { ?>
                                <?php if ( strlen($key) > 0 ) { ?>
                                <option value="<?php echo $key; ?>"><?php echo $key; ?></option>
                                <?php } ?>
                            <?php } ?>
                        </select>
                    <?php } ?>
                    <a class="button small white" style="margin-bottom:0px;cursor:pointer;" id="buttonSubDomainCheck" onclick="checkAvailabilitySubDomain();"> <?php echo $this->lang('Check Availability'); ?> &raquo;</a>
                    <div id="subDomainSearchResults"></div>
                <?php } ?>

            <?php } else if ( $field['type'] == typeCOUNTRY ) { ?>
                    <select class="dropdown" name="PCT_<?php echo $field['id']; ?>">
                    <?php foreach ( $field['select_options'] as $option ) { ?>
                        <option value="<?php echo $option['value']; ?>" <?php echo ($option['value'] == $this->DefaultCountry)? 'selected="selected"' : ''; ?> ><?php echo $option['label']; ?></option>
                    <?php } ?>
                    </select>
			<?php } else if ( $field['type'] == typeYESNO ) { ?>
				<select class="dropdown" name="PCT_<?php echo $field['id']; ?>">
					<option value="1" <?php echo $field['selectedyes']; ?>><?php echo $this->lang('Yes'); ?></option>
					<option value="0" <?php echo $field['selectedno']; ?>><?php echo $this->lang('No'); ?></option>
				</select>
			<?php } else if ( $field['type'] == typeTEXTAREA ) { ?>
				<textarea name="PCT_<?php echo $field['id']; ?>" class="<?php echo $field['class']; ?>" style="display: inline-block; width: 300px; vertical-align: top;" cols="40" rows="6"><?php echo $field['savedValue']; ?></textarea>
			<?php } else if ( $field['type'] == TYPEDATE ) { ?>
				<span style="display: inline-block">
					<input class="<?php echo $field['class']; ?>" type="text" name="PCT_<?php echo $field['id']; ?>" id="PCT_<?php echo $field['id']; ?>" size="10" value="<?php echo $field['savedValue']; ?>" onkeypress="return noenter(event);" readonly=true>
					<script type="text/javascript">
                        $(document).ready(function(){
                            datefields.push("PCT_<?php echo $field['id']; ?>");
                        });
					</script>
				</span>
			<?php } else if ( $field['type'] == typeDROPDOWN ) { ?>
				<?php if ( isset($field['select_options']) ) { ?>
				<select name="PCT_<?php echo $field['id']; ?>" class="dropdown">
				<?php foreach ( $field['select_options'] as $option ) { ?>
					<option size="30" value="<?php echo $option['value']; ?>"<?php echo $option['selected']; ?>><?php echo $option['label']; ?></option>
				<?php } ?>
				</select>
				<?php } ?>
			<?php } ?>
			<?php if ( strlen(@$field['desc']) >0 ) { ?>
			<br />
			<span style="font-size:11px; color:#666;"><?php echo $this->lang($field['desc']);?></span>
			<?php } ?>
		</td>
	</tr>
	<?php } ?>
</table>
<?php } ?>

<?php if ( count($this->packageAddons) > 0 ) { ?>

<h3><?php echo $this->lang("Product Add-ons");?></h3>

<table class="ts2" cellspacing="0" style="margin-top:20px;">
	<?php foreach ( $this->packageAddons as $addon ) { ?>
	<tr>
    	<td class="label"><?php echo $this->lang($addon['name']);?>:</td>
        <td>
			<?php if ( @$addon['radio_buttons'] == '0' ) { ?>
			<select name="addonSelect_<?php echo $addon['id']; ?>" class="dropdown">
				<?php foreach ( $addon['prices'] as $price ) { ?>
				<option value="addon_<?php echo $addon['id']; ?>_<?php echo $price['price_id']; ?>_<?php echo $price['recurringprice_cyle']; ?>" <?php echo $price['price_selected']; ?>><?php echo $price['price']; ?></option>
				<?php } ?>
			</select>
			<?php } else { ?>
				<?php foreach ( $addon['prices'] as $price ) { ?>
			<span>
				<input type="radio" name="addonSelect_<?php echo $addon['id']; ?>" class="dropdown" id="addon_<?php echo $addon['id']; ?>_<?php echo $price['price_id']; ?>_<?php echo $price['recurringprice_cyle']; ?>" value="addon_<?php echo $addon['id']; ?>_<?php echo $price['price_id']; ?>_<?php echo $price['recurringprice_cyle']; ?>" style="background:none; border:0px;" <?php echo $price['price_selected']; ?>/>
				<?php echo $price['price']; ?>
			</span>
			<br/>
				<?php } ?>
			<?php } ?>
			<?php if ( strlen($addon['desc']) > 0 ) { ?>
			<br />
			<span style="font-size:11px; color:#666;"><?php echo $this->lang($addon['desc']);?></span>
			<?php } ?>
		</td>
    </tr>
	<?php } ?>
</table>
<?php } ?>

<a class="button small white" style="cursor:pointer;" onclick="if(submitSubDomain()){$('#submitForm').submit();}"> <?php echo $this->lang('Continue'); ?> &raquo;</a>
</form>

</div>

<div class="clear"></div>