
$(document).ready(

   function() 
   {
      changePaymentDetails();      
   }
   
);

function PriceStep()
{
    this.gb = '';
    this.price = '';
}


function calculateCompoundPrice(gb, priceStepArray)
{   
   var totalPrice = 0;
   
   for(var i=0; i<priceStepArray.length   ;++i)
   {
         if( i == 0 )
            currentGb = priceStepArray[i].gb;
         else
            currentGb = priceStepArray[i].gb - priceStepArray[i-1].gb;
         
         currentPrice = priceStepArray[i].price;
         
         totalPrice += currentGb * currentPrice;
         
         if(  gb <= priceStepArray[i].gb )
            break;
   } 
   
   return totalPrice;
}


/* FORM SUBMISSION */
function submitForm()
{	
   //$('#numberOfComputers').val(id-1);
   
   
    var pass = true;
   for(var i=1; i<=10; ++i)
   {
      if (($("#storageSizeTxt" + i).val() != "" ) || (i == 1))
      {
       		if( $("#computerNameTxt" + i).val() == "" ) {
     			pass = false;
			}
      }
   }
   
   
   
   for(var i=1; i<=10; ++i)
   {
      if( $("#versionDropDown" + i).length > 0 )
      {
         $("#versionDropDown" + i).attr("disabled", false); 
      }
   }
   
   if (pass == false) { 	
   		alert("Please enter a Backup Account Name for each computer");
   }
   
  return pass;
}


/* STORAGE SPACE */
function addPrice(id)
{
   var storageSize = $('#storageSizeTxt' + id).val();   
   var monthlyPriceDiv = 'monthlyPriceTxt' + id;


   if( isNaN(storageSize) )
   {
      $('#' + monthlyPriceDiv).val('');
      alert('Invalid Storage Size');
   }
   else
   {      
      calculateAndDisplayPrice(storageSize, monthlyPriceDiv);
   }
   
   calculateAmounts();
}


function calculateAndDisplayPrice(storageSize, monthlyPriceDiv)
{
   var price = 0;
   price = parseFloat(calculateCompoundPrice(storageSize,priceStepsArray));  
   $('#' + monthlyPriceDiv).val('$' + price.toFixed(2));
}


function versionChanged(id)
{
   var storageSize = $('#storageSizeTxt' + id).val();
   var version = $('#versionDropDown' + id).val();
   var monthlyPriceDiv = 'monthlyPriceTxt' + id;
   
   if( isNaN(storageSize) || storageSize.length == 0)
      $('#' + monthlyPriceDiv).val('');
   else
      calculateAndDisplayPrice(storageSize, version, monthlyPriceDiv);

   displayExtraOptions(id);   
   calculateAmounts();      
}



function typeChanged(id)
{
   var type = $('#typeDropDown'+id).val();
   var versionDropDown = $('#versionDropDown'+id);

   if( type == 'server' )
   {
      versionDropDown.attr("selectedIndex",1);
      versionDropDown.attr("disabled", true); 
      versionChanged(id);
   }
   else
   {
      versionDropDown.attr("disabled", false); 
   }
}






function addComputer()
{
   // Validate number of computers
   if( id > COMPUTERLIMIT )
   {
      $('#addCompDiv').html('To order more than 10 plans please contact us on 1300 661 105');
      return;
   } 
   
   
   
   var newRow = '';
   
   newRow += '<tr id="row' + id + '">';
   newRow += '<td height="35" valign="middle">';
   newRow += '<input name="computerNameTxt' + id + '" type="text" id="computerNameTxt' + id + '" size="23" maxlength="255" />';
   newRow += '</td>';
   
   newRow += '<td height="18" align="left" valign="middle">';
   newRow += '<select name="typeDropDown' + id + '" id="typeDropDown' + id + '" onchange="typeChanged( \'' + id + '\')">';
   newRow += '<option value="desktop" selected="selected" >Desktop</option>';
   newRow += '<option value="laptop" >Laptop</option>';
   newRow += '<option value="server" >Server</option>';
   newRow += '</select></td>';
   
   newRow += '<td align="left" valign="middle">';
   newRow += '<select name="versionDropDown' + id + '" id="versionDropDown' + id + '" onchange="versionChanged( \'' + id + '\' )">';
   newRow += '<option value="standard" selected="selected" >Standard</option>';
   newRow += '<option value="advanced" >Advanced</option>';
   newRow += '</select>';
   newRow += '</td>';
   
   newRow += '<td>';
   newRow += '<input name="storageSizeTxt' + id + '" type="text" id="storageSizeTxt' + id + '" size="3" maxlength="20" onchange="addPrice( \'' + id + '\' )"/> ';
   newRow += '<input type="hidden" name="licensePriceTxt' + id + '" id="licensePriceTxt' + id + '" >';
   newRow += '</td>';
   newRow += '<td>&nbsp;GB</td>';
   
   newRow += '<td width="100">';
   newRow += '<div id="currentPriceDiv' + id + '"></div>';
   newRow += '</td>';
   
   newRow += '<td><div align="right">';
   newRow += '<input id="monthlyPriceTxt' + id + '" name="monthlyPriceTxt' + id + '" size="10" maxlength="12" readonly="readonly" style="text-align:right" />';   
   newRow += '</div></td>';
   
   newRow += '<td id="removeButtonTD' + id + '"><input type="button" name="remove" value="Remove" class="button" onclick="removeComputer(\'' + id + '\')" /></td>';
   newRow += '</tr>';
   
   newRow += '<tr id="advancedOptionsTr' + id + '"><td colspan="7"><div id="advancedOptionsDiv' + id + '"></div></td><td></td></tr>';
   newRow += '<tr id="rowHR' + id + '"><td colspan="7"><hr /></td><td></td></tr>';
   

   $('#computerRowsTable').append(newRow);
    $('#numberOfComputers').val(id);
   ++id;
  
   calculateAmounts();
   refreshRemoveButtons();
}

function refreshRemoveButtons()
{
   for(var i=1; i<id-1; ++i)
   {
      $('#removeButtonTD' + i).html('');      
   }
   
   var currentID = id-1;   
   $('#removeButtonTD' + currentID).html('&nbsp;<input type="button" name="remove" value="Remove" class="button" onclick="removeComputer(\'' + currentID + '\')" />');   
}


function removeComputer(currentID)
{
   $('#row' + currentID).remove();
   $('#rowHR' + currentID).remove();
   $('#advancedOptionsTr' + currentID).remove();
    $('#numberOfComputers').val(currentID-1);
   --id;
  

   refreshRemoveButtons();
   calculateAmounts();
   
   if( id == COMPUTERLIMIT )
      $('#addCompDiv').html('<a href="javascript:addComputer();">&nbsp;remove</a>');
   
}






function displayExtraOptions(id)
{
   var advancedOptionsDiv = $('#advancedOptionsDiv'+id);
   var version = $('#versionDropDown'+id).val();
   

   var output = '';

   if( version == 'advanced' )
   {   
      output += '<table border="0">';
      output += '<tr><td valign="top">';
      
      output += '<table cellpadding="0" cellspacing="2" border="0">';
      output += '<tr>';
      output += '<td valign="top"><input type="checkbox" name="osbHardDriveCheckBox' + id + '" id="osbHardDriveCheckBox' + id + '" /></td>';
      output += '<td>OBS USB Hard drive $' +PRICEOSBHARDDRIVE + '+GST + $' + DEPOSITOSBHARDRIVE + ' Deposit (Deposit refunded on return)</td>';
      output += '</tr>';
      output += '<tr><td></td></tr>';      
      output += '<tr>';
      output += '<td valign="top"><input type="checkbox" name="customerHardDriveCheckBox' + id + '" id="customerHardDriveCheckBox' + id + '" /></td>';
      output += '<td>Customer/Reseller USB Hard drive $' + PRICECUSTOMERHARDDRIVE + '+GST </td>';
      output += '</tr>';
      output += '</table>';  
   
      output += '</td><td>';   
      output += '<table cellpadding="0" cellspacing="0" border="0">';
	  output += '<tr>';
      output += '<td align="right" width="532">Continuous data protection';
      output += '   <input type="checkbox" name="protectionCheckBox' + id + '" id="protectionCheckBox' + id + '" onClick="extraClicked(this.checked,PRICEPROTECTION,\'protectionTotalTxt' + id + '\')" /></td><td colspan="2"><input name="protectionTotalTxt' + id + '" type="text" id="protectionTotalTxt' + id + '" style="text-align:right" value="$0.00" size="10" readonly="readonly" /></td><td>&nbsp;</td>';
      output += '</tr>';
      output += '<tr>';
      output += '<td align="right" width="532">Oracle Database Server';
      output += '   <input type="checkbox" name="oracleCheckBox' + id + '" id="oracleCheckBox' + id + '" onClick="extraClicked(this.checked,PRICEORACLE,\'oracleTotalTxt' + id + '\')" /></td><td colspan="2"><input name="oracleTotalTxt' + id + '" type="text" id="oracleTotalTxt' + id + '" style="text-align:right" value="$0.00" size="10" readonly="readonly" /></td><td>&nbsp;</td>';
      output += '</tr>';
      output += '<tr>';
      output += '<td align="right">SQL / mySQL Module';
      output += '   <input type="checkbox" name="sqlCheckBox' + id + '" id="sqlCheckBox' + id + '" onClick="extraClicked(this.checked,PRICESQL,\'sqlTotalTxt' + id + '\')" /></td><td colspan="2"><input name="sqlTotalTxt' + id + '" type="text" id="sqlTotalTxt' + id + '" style="text-align:right" value="$0.00" size="10" readonly="readonly" /></td><td>&nbsp;</td>';
      output += '</tr>';
      output += '<tr>';
      output += '<td align="right">Lotus Domino / Notes';
      output += '   <input type="checkbox" name="lotusCheckBox' + id + '" id="lotusCheckBox' + id + '" onClick="extraClicked(this.checked,PRICELOTUS,\'lotusTotalTxt' + id + '\')" /></td>';
      output += '<td colspan="2"><input name="lotusTotalTxt' + id + '" type="text" id="lotusTotalTxt' + id + '" style="text-align:right" value="$0.00" size="10" readonly="readonly" /></td><td>&nbsp;</td>';
      output += '</tr>';
      output += '<tr>';
      output += '<td align="right">Microsoft Exchange Mailboxes 1-450';
      output += '   <input name="exchangeTxt' + id + '" type="text" id="exchangeTxt' + id + '" value="0" size="2" style="text-align:right" onChange="mailboxesChanged(this.value,\'exchangeTotalTxt' + id + '\')" /></td><td colspan="2"><input name="exchangeTotalTxt' + id + '" type="text" id="exchangeTotalTxt' + id + '" style="text-align:right" value="$0.00" size="10" readonly="readonly" /></td><td>&nbsp;</td>';
      output += '</tr>';
      output += '</table>';      
      output += '</td></tr>';   
      output += '</table>';      
                    
      advancedOptionsDiv.html(output);      
   }
   else if( version == 'standard' )
   {
      advancedOptionsDiv.html('');
   }
}

function extraClicked(checked, amount, textField)
{
   if( checked )
   {
      var formattedAmount = parseFloat(amount);      
      $('#'+textField).val('$' + formattedAmount.toFixed(2) );
   }
   else
      $('#'+textField).val('$0.00');
      
   calculateAmounts();
}

function mailboxesChanged(numberOfMailboxes,textField)
{
   var amount;
   if( isNaN(numberOfMailboxes) || parseFloat(numberOfMailboxes) < 0 ||  numberOfMailboxes.length == 0)
   {
      amount = 0;
      numberOfMailboxes = 0;
   }
   else if( parseFloat(numberOfMailboxes) > MAILBOXLIMIT )
   {
      amount = MAILBOXLIMIT;
      numberOfMailboxes = MAILBOXLIMIT;
   }
   else
      amount = parseFloat(numberOfMailboxes) * parseFloat(PRICEMAILBOX);
   
   amount = '$' + amount.toFixed(2);
   $('#'+textField).val(amount);
   
   calculateAmounts();
}





function calculateAmounts()
{
   var currentAmount = 0;
   var currentVersion = '';
   
   var totalAmount = 0;
   var totalLicenseFees = 0;
   var totalTaxAmount = 0;
   var totalGrossAmount = 0;
   var totalDiscount = 0;
   
   for(var i=1; i<=id; ++i)
   {
      if( $("#monthlyPriceTxt" + i).length > 0 )
      {
         currentAmount = $("#monthlyPriceTxt" + i).val();
         currentAmount = currentAmount.substring(1);
         
         if( !isNaN(currentAmount) && currentAmount.length > 0 )
         {
            totalAmount += parseFloat(currentAmount);
         }      
      }  
      
      // Check extras
      if( $("#oracleCheckBox" + i).length > 0  && $("#oracleCheckBox" + i).attr("checked") == true  )
         totalAmount += parseFloat(PRICEORACLE);
      if( $("#sqlCheckBox" + i).length > 0  && $("#sqlCheckBox" + i).attr("checked") == true  )
         totalAmount += parseFloat(PRICESQL);
      if( $("#lotusCheckBox" + i).length > 0  && $("#lotusCheckBox" + i).attr("checked") == true  )
         totalAmount += parseFloat(PRICELOTUS);
      if( $("#exchangeTxt" + i).length > 0 )
         totalAmount += parseFloat(PRICEMAILBOX)*parseFloat($("#exchangeTxt" + i).val());   
	  if( $("#protectionCheckBox" + i).length > 0  && $("#protectionCheckBox" + i).attr("checked") == true  )
         totalAmount += parseFloat(PRICEPROTECTION);

         
         
      if(  $("#versionDropDown" + i).length > 0 )
      {      
	  	
         currentVersion = $("#versionDropDown" + i).val();
		 
         if( currentVersion == 'standard' )
         {
            $("#licensePriceTxt" + i).val(standardLicensePrice);

			totalLicenseFees += parseFloat(standardLicensePrice);
         }
         else if( currentVersion == 'advanced' ) {
            $("#licensePriceTxt" + i).val(advancedLicensePrice);
			totalLicenseFees += parseFloat(advancedLicensePrice);
		 }
            
						

      }    
	  //alert($("#versionDropDown" + i).val()+" "+standardLicensePrice);
   }
   
   
      
   totalTaxAmount = (totalAmount+totalLicenseFees) * 0.1;
   totalTaxAmount = totalTaxAmount.toFixed(2);
   
   totalGrossAmount = parseFloat(totalAmount) + parseFloat(totalTaxAmount) + parseFloat(totalLicenseFees);
   
   if( DISCOUNT > 0 )
      totalDiscount = totalGrossAmount * (DISCOUNT/100);
   else
      totalDiscount = 0;
   
   //totalGrossAmount = totalGrossAmount - totalDiscount;
   totalGrossAmount = totalGrossAmount.toFixed(2);
   
   
   
   totalLicenseFees = totalLicenseFees.toFixed(2);
  // totalDiscount = totalDiscount.toFixed(2);
   totalAmount = totalAmount.toFixed(2);
   
   $('#gstTotal').html(totalTaxAmount);
   $('#subTotal').html(totalAmount);
   $('#licenseTotal').html(totalLicenseFees);
  // $('#discountTotal').html(totalDiscount);
   $('#total').val('$' + totalGrossAmount);
   
   $('#subTotalTxt').val(totalAmount);
   $('#gstTotalTxt').val(totalTaxAmount);
   $('#licenseTotalTxt').val(totalLicenseFees);   
   $('#discountTotalTxt').val(totalDiscount);
}



function resetMonthlyPrices()
{
   for(var i=1; i<=id; ++i)
   {
      if( $("#backupStorageDropDown" + i).length > 0 )
      {
         currentAmount = $("#backupStorageDropDown" + i).val();
         if( isNaN(currentAmount) )
            $("#monthlyPriceTxt" + i).val('');
            
      }
   }
}


























function validateComputers()
{   
   var valid = true;
   var error = '';

   for(var i=1; i<=id && valid; ++i)
   {
      if( $('#computerNameTxt' + i).length > 0 )
      {
         if( $('#computerNameTxt' + i).val().length == 0 )
         {
            error += "\n- computer name";
            $('#computerNameTxt' + i).css('background-color',HIGHLIGHTCOLOUR);
            valid = false;
         }
         else
            $('#computerNameTxt' + i).css('background-color',DEFAULTCOLOUR);
         
         if( $("#backupStorageDropDown" + i).attr("selectedIndex") == 0 )
         {
            error += "\n- backup storage";
            $('#backupStorageDropDown' + i).css('background-color',HIGHLIGHTCOLOUR);
            valid = false;
         }
         else
            $('#backupStorageDropDown' + i).css('background-color',DEFAULTCOLOUR);
      }
   }
   
   return error;
}















/* YOUR DETAILS */
function validateDetails()
{
   var error = '';

   if( $('#firstNameTxt').val().length == 0 )
   {
      $('#firstNameTxt').css('background-color',HIGHLIGHTCOLOUR);
      error += "\n- first name";
   }
   else
      $('#firstNameTxt').css('background-color',DEFAULTCOLOUR);
   
   
   if( $('#lastNameTxt').val().length == 0 )
   {
      $('#lastNameTxt').css('background-color',HIGHLIGHTCOLOUR);
      error += "\n- last name";
   }
   else
      $('#lastNameTxt').css('background-color',DEFAULTCOLOUR);
    
   if( $('#emailTxt').val().length == 0 )
   {
      $('#emailTxt').css('background-color',HIGHLIGHTCOLOUR);
      error += "\n- e-mail address"; 
   }
   else
      $('#emailTxt').css('background-color',DEFAULTCOLOUR);
   
   var emailRegex = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
   if( !emailRegex.test( $('#emailTxt').val() ) )
   {
      $('#emailTxt').css('background-color',HIGHLIGHTCOLOUR);
      error += "\n- e-mail address";
   }
   else
      $('#emailTxt').css('background-color',DEFAULTCOLOUR);

   
   if( $('#areacodeTxt').val().length == 0 || isNaN($('#areacodeTxt').val()) )
   {
      $('#areacodeTxt').css('background-color',HIGHLIGHTCOLOUR);
      error += "\n- area code"; 
   }
   else
      $('#areacodeTxt').css('background-color',DEFAULTCOLOUR);
   
   if( $('#phoneTxt').val().length == 0 )
   {
      $('#phoneTxt').css('background-color',HIGHLIGHTCOLOUR);
      error += "\n- phone number";
   }
   else
      $('#phoneTxt').css('background-color',DEFAULTCOLOUR);
   
   var phoneRegex = /^[0-9 -]+$/;
   if( !phoneRegex.test( $('#phoneTxt').val() ) )
   {
      $('#phoneTxt').css('background-color',HIGHLIGHTCOLOUR);
      error += "\n- phone number";
   }
   else
      $('#phoneTxt').css('background-color',DEFAULTCOLOUR);
      
   if( !phoneRegex.test( $('#mobileTxt').val() ) )
   {
      $('#mobileTxt').css('background-color',HIGHLIGHTCOLOUR);
      error += "\n- mobile number";
   }
   else
      $('#mobileTxt').css('background-color',DEFAULTCOLOUR);
   
   if( $('#postcodeTxt').val().length == 0 || isNaN($('#postcodeTxt').val()) )
   {
      $('#postcodeTxt').css('background-color',HIGHLIGHTCOLOUR);
      error += "\n- post code";
   }     
   else
      $('#postcodeTxt').css('background-color',DEFAULTCOLOUR);
   
         
   return error;
}































/* PAYMENT DETAILS */
function showPopup(mother, type) {
		if (!$("#popupBox").length) {
			var mother = "#"+mother;
			var htmlString = '';
			
			switch (type) {
				case "type":
					htmlString ="<div id='popupBox'>You can only choose advanced plans for server type</div>";
					break;
				case "cvc":
					htmlString = "<div id='popupBox' style='margin:-280px 0 0 0'><p class='subheader'>What is a CVC number?</p><p>The CVC or Card Verification Code is a 3 digit number that is used by credit card agencies as an added security precaution when handling internet transactions. It is the last 3 digits of the number found on the back of your credit card underneath the magnetic strip.</p><p align='center'><img src='images/cvc.gif'><p></div>";
					break;
			}
			$(mother).append(htmlString);
		}
}

function removePopup(mother) {
	$("#popupBox").remove();
}


function displayPaymentDetailsValidation()
{
   $("#paymentDetailsValidationDiv p strong").html( validatePaymentDetails() );
}



function changePaymentDetails()
{
   var selectedElement = $('#paymentMethodDropDown').val();

   // Hide all the divs
   $('#creditCardPaymentDiv').hide();
   $('#phonePaymentDiv').hide();
   $('#ipAddressDiv').hide();
   
   
   
   // Show only the relevant div
   if( selectedElement == 'credit_card' )
   {
      $('#creditCardPaymentDiv').show();
      $('#ipAddressDiv').show();
   }
   else if( selectedElement == 'phone' )
   {
      $('#phonePaymentDiv').show();
   }
}


