<!-- 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">
// Field Validation
$(document).ready(function(){

    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;?>";
            if(cartItem != ''){
                $.ajax({
                    url: "index.php?fuse=admin&action=deleteCartItem&cartItem="+cartItem+"&bundleCartItem="+cartItem,
                    context: document.body,
                    success: function(){
                        window.location=selectedLink;
                    }
                });
            }else{
                window.location=selectedLink;
            }
        }
    }
});
</script>

<script type="text/javascript">

// Callback function for ExtJS delete popup
function searchDomain(name, tld, searchType, animate) {

    // Don't do anything if we're submitting yourdomain or his translation
    if(name == '<?php echo $this->lang("yourdomain");?>' || name == '') {
        $('#' + searchType + 'Required').show();
        return false;
    }

    // Handle the searching animation
    if(animate) {
    	$('#domainSearchResults').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
        $('#domainSearchResults').html(response.responseText)
    }
    // Query the domain
    Ext.Ajax.request({
       url: 'order.php?step=search',
       success: received,
       failure: received,
       params: { name: name, tld: tld, productGroup:<?php echo $this->productGroup;?>, product:<?php echo $this->product['id'];?>, bundling:<?php echo $this->bundling;?> <?php if ( isset($this->hosting_product) ) { ?>,hosting_product: <?php echo $this->hosting_product; } ?>, searchType: searchType }
    });
}

// Callback function for ExtJS delete popup
function searchSubDomain(name, tld, animate, continueButton) {
    if(name == '') {
        doSubmitForm();
        return false;
    }

    // Handle the searching animation
    if(animate) {
        $('#domainSearchResults').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
        $('#domainSearchResults').html(response.responseText)
        if(continueButton && submitSubDomainOnRegister(false)){
            doSubmitForm();
        }
    }
    // Query the subdomain
    Ext.Ajax.request({
       url: 'order.php?step=subsearch',
       success: received,
       failure: received,
       params: { name: name, tld: tld, showContinueButton: 1}
    });
}

// Function to trigger the search of a domain
function triggerSearch(name, tld) {

    // Update the form fields
    document.getElementById('registerName').value = name;
    document.getElementById('registerTld').value=tld;

    // Do the search
    searchDomain(name, tld, 'register', true);
}

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

$(document).ready( function() {

	$("#registerTld, #transferTld, #registerName").bind("change",function(){
		$('#domainSearchResults').html('');
	});

	$("#registerName, #transferName, #subDomain").bind("keyup input paste",function(){
		$('#domainSearchResults').html('');
	});



	// Detect a change of radio button
	$("input[name$='domainAction']").click(function(){

	 	// Clear any residual results in the div below
	 	$('#domainSearchResults').html('')

 		// Get the radio value
  		var radio_value = $(this).val();

 		// Handle the work
  		if(radio_value=='register') {
    		$("#domainRegister").show();
     		$("#domainTransfer").hide();
     		$("#domainSubdomain").hide();
     		$("#domainManage").hide();
  		} else if(radio_value=='transfer') {
    		$("#domainRegister").hide();
     		$("#domainTransfer").show();
     		$("#domainSubdomain").hide();
     		$("#domainManage").hide();
   		} else if(radio_value=='subdomain') {
    		$("#domainRegister").hide();
     		$("#domainTransfer").hide();
     		$("#domainSubdomain").show();
     		$("#domainManage").hide();
   		} else if(radio_value=='manage') {
   			searchDomain('1','1', 'selfmanage', false);
    		$("#domainRegister").hide();
     		$("#domainTransfer").hide();
     		$("#domainSubdomain").hide();
     		$("#domainManage").show();
   		}
  	});

 	// Hide the other options initially
 	$("#domainManage").hide();
	$("#domainTransfer").hide();
	$("#domainSubdomain").hide();

	// Add some keyup events to trigger search functions
	$("#registerName").keyup(function(event){ if(event.keyCode == 13){ $("#buttonRegisterCheck").click(); } });
	$("#transferName").keyup(function(event){ if(event.keyCode == 13){ $("#buttonTransferCheck").click(); } });

    jQuery.validator.addMethod("domain", function(value, element) {
        return this.optional(element) || /^([-0-9A-Z]+\.)+([0-9A-Z]){2,4}$/i.test(value);
    }, '<?php echo $this->lang("Invalid domain name."); ?>');
	jQuery.validator.addMethod("domain2", function(value, element) {
        return this.optional(element) || /^([-0-9A-Z]+)$/i.test(value);
    }, '<?php echo $this->lang("Invalid domain name."); ?>');
    var validator = $("#domainForm").validate({
        rules: {
            manageName: {
            	domain: true,
            	required: true
            },
            registerName: {
            	required: true,
            	domain2: true
            },
            transferName: {
            	required: true,
            	domain2: true
            },
            subDomain: "required"
        },
		errorPlacement: function(error, element) {
		     error.appendTo( element.parent("td") );
	    }
    });

});


function validateForm() {
	$('#domainSearchResults').html('');
    return jQuery('#domainForm').valid();
}

function doSubmitForm() {
    jQuery('#domainForm').submit();
    return false;
}
</script>

<!-- Main Page -->
<div style="margin-right:220px; margin-bottom:20px;">
<?php echo $this->lang("Bundled Product");?>
<h3><?php echo $this->lang("Configure Product");?>: <?php echo $this->product['name'];?></h3>
<p class="pagedesc"><?php echo $this->product['description'];?></p>

<script type="text/javascript">
function checkAvailabilityRegister()
{
    searchDomain(document.getElementById('registerName').value,document.getElementById('registerTld').options[document.getElementById('registerTld').selectedIndex].text, 'register', true);
}

function checkAvailabilityTransfer()
{
    searchDomain(document.getElementById('transferName').value,document.getElementById('transferTld').options[document.getElementById('transferTld').selectedIndex].text, 'transfer', true);
}

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);
}

// 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);
    }
}
</script>
<form action="order.php?step=4" method="POST" name="domainForm" id="domainForm">
    <input type="hidden" name="product" value="<?php echo $this->product['id'];?>">
    <input type="hidden" name="formId" value="domainReg">

<table class="ts2" cellspacing="0">
    <?php if ( !$this->disallowRegister ) { ?>
    <tr>
        <td><input name="domainAction" type="radio" value="register" checked /> <?php echo $this->lang("Register a new domain name");?></td>
    </tr>
    <tr id="domainRegister">
        <td style="background:#E9F2F7;">www. <input placeholder="your domain" id="registerName" name="registerName" type="text" style="width:280px;margin-bottom: 0px;" /> .

            <select name="registerTld" id="registerTld">
                <?php if(isset($this->tld) && is_array($this->tld)) { ?>
                	<?php foreach($this->tld AS $key) { ?>
                		<option name=""><?php echo $key['name'];?></option>
                	<?php } ?>
                <?php } ?>
            </select>
            &nbsp; <a class="button small white" style="margin-bottom:0px;cursor:pointer;" id="buttonRegisterCheck" onclick="if (validateForm()) checkAvailabilityRegister();"> <?php echo $this->lang('Check Availability'); ?> &raquo;</a>
        </td>
    </tr>
 <?php } ?>
 <?php if($this->allowTransfer) { ?>
    <tr>
        <td><input name="domainAction" type="radio" value="transfer" /> <?php echo $this->lang("Transfer an existing domain name");?></td>
    </tr>
    <tr id="domainTransfer">
        <td style="background:#E9F2F7;">www. <input placeholder="your domain" id="transferName" name="transferName" type="text" style="width:280px;margin-bottom: 0px;" /> .

            <select name="transferTld" id="transferTld">
                <?php if(isset($this->tld) && is_array($this->tld)) { ?>
                	<?php foreach($this->tld AS $key) { ?>
                		<option name=""><?php echo $key['name'];?></option>
                	<?php } ?>
                <?php } ?>
            </select>
            &nbsp; <a class="button small white" style="margin-bottom:0px;cursor:pointer;" id="buttonTransferCheck" onclick="if (validateForm()) checkAvailabilityTransfer();"> <?php echo $this->lang('Check Availability'); ?> &raquo;</a>
        </td>
    </tr>
<?php } ?>

<?php if(isset($this->subDomains)) { ?>
    <tr>
        <td><input name="domainAction" type="radio" value="subdomain" /> <?php echo $this->lang("Use a free Subdomain");?></td>
    </tr>
    <tr id="domainSubdomain">
        <td style="background:#E9F2F7;">www. <input placeholder="subdomain" id="subDomain" name="subDomain" type="text" size="50" style="margin-bottom: 0px;" /> .

            <select name="subDomainTld" id="subDomainTld">
                <?php foreach($this->subDomains AS $key) {?>
                <option name=""><?php echo $key['name'];?></option>
                <?php }?>
            </select>
            &nbsp;<a class="button small white" style="margin-bottom:0px;cursor:pointer;" id="buttonSubDomainCheck" onclick="if (validateForm()) checkAvailabilitySubDomain();"> <?php echo $this->lang('Check Availability'); ?> &raquo;</a>
        </td>
    </tr>
<?php } ?>

<?php if(isset($this->bundling) && $this->bundling == 1 && $this->allowSelfManage) {?>
    <tr>
        <td><input name="domainAction" type="radio" value="manage" /> <?php echo $this->lang("Manage a domain myself");?></td>
    </tr>
    <tr id="domainManage">
        <td style="background:#E9F2F7;">www. <input placeholder="your domain" id="manageName" name="manageName" type="text" size="60" style="margin-bottom: 0px;" />
        <a class="button small white" style="cursor:pointer;margin-bottom:0px;" onclick="if (validateForm())  doSubmitForm();"> <?php echo $this->lang('Continue'); ?> &raquo;</a>
        </td>
    </tr>
<?php } ?>
</table>
    <div id="domainSearchResults"></div>
</form>
</div>

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

<?php if(@$this->triggerSearch) { ?>
<script type="text/javascript">
    triggerSearch('<?php echo $this->triggerDomain;?>', '<?php echo $this->triggerTld;?>');
</script>
<?php } ?>