document.observe("dom:loaded", function() {


  // Working with efilterorders form
  if ($('efilterorders')) {

    // Determine Radio button values
    var radioValue = radio_button_value('efilterorders', 'emailtype');
    var radioValue2 = radio_button_value('efilterorders', 'noemail');

    // toggle screen and update value accordingly
    efilter_toggle(radioValue);
    efilter_updateprice(radioValue2);
  
    // on click
    $$('input.efilterorders_noemail').each( function(currentElement){

	  currentElement.observe('click', function(currentElement){
	    efilter_updateprice($(this).value);
	});
    });

    $$('input.efilterorders_emailtype').each( function(currentElement){
	  currentElement.observe('click', function(currentElement){
	    efilter_toggle($(this).value);
	});
    });

    $('efilterorders').observe('submit', function(event){
      Event.stop(event);

      if(form_validation("efilterorders")!="0") {
	alert(form_validation("efilterorders"));
	exit;
      }

      var radioValue = radio_button_value('efilterorders', 'emailtype');
      var nomail = radio_button_value('efilterorders', 'noemail');
      
      if(radioValue=="") {var emailtype="Hosted elsewhere but I'm interested in hosting with Impala";};
      if(radioValue=="1") {var emailtype="Hosted elsewhere";};
      if(radioValue=="2") {var emailtype="Hosted with Impala / Worthers";};

      new Ajax.Request('/data/impala/scripts/ajax_impala_scripts.php', {
	method:'get',
	parameters: {
	  action: 'submit',
	  product: 'efilter', 
	  company_name: $('efilterorders_companyname').value,
	  contact_name: $('efilterorders_contactname').value,
	  email: $('efilterorders_email').value,
	  phone_number: $('efilterorders_phonenumber').value,
	  address1: $('efilterorders_address1').value,
	  address2: $('efilterorders_address1').value,
	  city: $('efilterorders_city').value,
	  postcode: $('efilterorders_postcode').value,
	  product_item: nomail,
	  already_registered: emailtype
	},
	onSuccess: function(transport){
	  window.location = "/pages/thankyou-efilter";
	},
	onFailure: function(){ 
	  alert("Something went wrong...");
	}
      });
    });

  }


  // Working with zimbraorders form
  if ($('zimbraorders')) {
    
    // input box validation for numeric values
    $('zimbraorders_totalusers').observe('blur', function(event) {
      var iresult = (zimbra_validation($('zimbraorders_totalusers'), $('zimbraorders_totaloutlook')));      
      if(iresult=="1") {
	alert("Please use a numerical value for Total Number of users");
	$('zimbraorders_totalusers').value="0";
	$('zimbraorders_totalusers').focus();
      }
      if(iresult=="2") {
	alert("You cannot have more Outlook Connectors than total number of mailboxes");
	$('zimbraorders_totalusers').value = $('zimbraorders_totaloutlook').value;
	$('zimbraorders_totalusers').focus();
      }
    });


    $('zimbraorders_totaloutlook').observe('blur', function(event) {
      if (!/^-?\d+$/.test($('zimbraorders_totaloutlook').value) && $('zimbraorders_totaloutlook').value!='') {
    	  var iresult = (zimbra_validation($('zimbraorders_totaloutlook'), $('zimbraorders_totaloutlook')));      
		  alert("Please use a numerical value for Outlook Connector");	
		  $('zimbraorders_totaloutlook').value = "0";
	
      }
      if (parseInt($('zimbraorders_totalusers').value) < parseInt($('zimbraorders_totaloutlook').value)) {
		alert("You cannot have more Outlook Connectors than total number of mailboxes");
		$('zimbraorders_totaloutlook').value = $('zimbraorders_totalusers').value;
      }
    });

    $('zimbraorders_totalmobile').observe('blur', function(event) {
      if (!/^-?\d+$/.test($('zimbraorders_totalmobile').value) && $('zimbraorders_totaloutlook').value!='') {
		alert("Please use a numerical value for Mobile Access ");
      }
     if (parseInt($('zimbraorders_totalusers').value) < parseInt($('zimbraorders_totalmobile').value)) {   
		alert("You cannot have more Mobile Connections than total number of mailboxes");
		$('zimbraorders_totalmobile').value = $('zimbraorders_totalusers').value;

      }

    });


    // get text for outlook connector and mobile access
    new Ajax.Updater('outlook_connector' , '/data/impala/scripts/ajax_impala_scripts.php', { 
	method: 'get',
	parameters: {span_text: 'outlookconnector'} 
    });

    $('outlook_connector').style.display = 'none';

    new Ajax.Updater('mobile_access' , '/data/impala/scripts/ajax_impala_scripts.php', { 
	method: 'get',
	parameters: {span_text: 'mobileaccess'} 
    });

    $('mobile_access').style.display = 'none';

    // create zero value
    $('zimbraorders_totalusers').value = "0";
    $('zimbraorders_totaloutlook').value = "0";
    $('zimbraorders_totalmobile').value = "0";


    $('outlookconnector').observe('mouseover', function(event) {
      Event.stop(event);
	$('outlook_connector').style.display = "block";
    });

    $('outlookconnector').observe('mouseout', function(event) {
      Event.stop(event);
	$('outlook_connector').style.display = "none";
    });

    $('outlookconnector').observe('click', function(event) {
      Event.stop(event);
    });

    $('mobileaccess').observe('mouseover', function(event) {
      Event.stop(event);
	$('mobile_access').style.display = "block";
    });

    $('mobileaccess').observe('mouseout', function(event) {
      Event.stop(event);
	$('mobile_access').style.display = "none";
    });

    $('mobileaccess').observe('click', function(event) {
      Event.stop(event);
    });

    // determine type of payment option
    if($('zimbraorders_payment_option_0').checked!=0) {
      var payment_options = "monthly";		
    } else {
      var payment_options = "yearly";
    }
    zimbra_updateprice(payment_options);
    
    $('zimbraorders_diskspace').observe('change', function(currentElement) {

      // determine type of payment option
      if($('zimbraorders_payment_option_0').checked!=0) {
	var payment_options = "monthly";		
      } else {
	var payment_options = "yearly";
      }
      zimbra_updateprice(payment_options);

    });

    // use ajax to calculate totals (data pulled from db)
    $$('input').each( function(currentElement){
	currentElement.observe('change', function(currentElement){

	      // determine type of payment option
	      if($('zimbraorders_payment_option_0').checked!=0) {
		var payment_options = "monthly";		
	      } else {
		var payment_options = "yearly";
	      }
	      zimbra_updateprice(payment_options);
	});

	currentElement.observe('click', function(currentElement){

	      // determine type of payment option
	      if($('zimbraorders_payment_option_0').checked!=0) {
		var payment_options = "monthly";		
	      } else {
		var payment_options = "yearly";
	      }
	      zimbra_updateprice(payment_options);
	});

    });

    // submit form via ajax
    $('zimbraorders').observe('submit', function(event){
      Event.stop(event);

      if (!/^-?\d+$/.test($('zimbraorders_totalusers').value)) {

	alert("Please use a numerical value for Total Number of users");
	exit;

      }

      if (!/^-?\d+$/.test($('zimbraorders_totaloutlook').value) && $('zimbraorders_totaloutlook').value!='') {

	alert("Please use a numerical value for Outlook Connector");
	exit;

      }

      if (!/^-?\d+$/.test($('zimbraorders_totalmobile').value) && $('zimbraorders_totaloutlook').value!='') {

	alert("Please use a numerical value for Mobile Access ");
	exit;

      }

      // determine type of payment option
      if($('zimbraorders_payment_option_0').checked!=0) {
	var payment_options = "monthly";		
      } else {
	var payment_options = "yearly";
      }

      if(form_validation("zimbraorders")!="0") {
	alert(form_validation("zimbraorders"));
	exit;
      }

      new Ajax.Request('/data/impala/scripts/ajax_impala_scripts.php', {
	method:'get',
	parameters: {
	  action: 'submit',
	  product: 'zimbra', 
	  company_name: $('zimbraorders_companyname').value,
	  contact_name: $('zimbraorders_contactname').value,
	  email: $('zimbraorders_email').value,
	  phone_number: $('zimbraorders_phonenumber').value,
	  address1: $('zimbraorders_address1').value,
	  address2: $('zimbraorders_address1').value,
	  city: $('zimbraorders_city').value,
	  postcode: $('zimbraorders_postcode').value,
	  total_users: $('zimbraorders_totalusers').value,
	  total_outlook: $('zimbraorders_totaloutlook').value,
	  total_mobile: $('zimbraorders_totalmobile').value,
	  domain_address: $('zimbraorders_domainaddress').value,
	  already_registered: $('zimbraorders_alreadyregistered').checked,
	  disk_space: $('zimbraorders_diskspace').value,
	  spam_filter: $('zimbraorders_spamfilter').checked,
	  payment_options: payment_options
	},
	onSuccess: function(transport){
	  window.location = "/pages/thankyou-zimbra";
	},
	onFailure: function(){ 
	  alert("Something went wrong...");
	}
      });

    });
  }

  function zimbra_updateprice(payment_options) {

    if (payment_options=="yearly") {
      $("zimbra_time_period").update("annum");
    } else {
      $("zimbra_time_period").update("month");
    }

    new Ajax.Updater('cost' , '/data/impala/scripts/ajax_impala_scripts.php', { 
	method: 'get',
	parameters: {
		    action: 'getprice',
		    product: 'zimbra', 
		    total_users: $('zimbraorders_totalusers').value,
		    total_outlook: $('zimbraorders_totaloutlook').value,
		    total_mobile: $('zimbraorders_totalmobile').value,
		    domain_address: $('zimbraorders_domainaddress').value,
		    already_registered: $('zimbraorders_alreadyregistered').checked,
		    disk_space: $('zimbraorders_diskspace').value,
		    spam_filter: $('zimbraorders_spamfilter').checked,
		    payment_options: payment_options
		    } 
    });
  }

  function zimbra_validation(z_element, z_element2) {

    if (!/^-?\d+$/.test(z_element.value)) {
      return "1";
    }
    
    if (parseInt(z_element.value) < parseInt(z_element2.value)) {
      return "2";
    }
    return "0";   
  }

  function form_validation(form_name) {
     if(form_name=="zimbraorders") {
      if (parseInt($('zimbraorders_totalusers').value)<=0) {
		$('zimbraorders_totalusers').focus();
		return "You must order Zimbra for at least one user";
	  }
      if (parseInt($('zimbraorders_totaloutlook').value)<0) {
		$('zimbraorders_totaloutlook').focus();
		return "Number of Outlook connector users cannot be negative";
	  }
      if (parseInt($('zimbraorders_totalmobile').value)<0) {
		$('zimbraorders_totalmobile').focus();
		return "Number of Mobile access users cannot be negative";
	  }
     }

     if($(form_name + "_domainaddress").value=="") {
		$(form_name + "_domainaddress").focus();
		return "Please include a domain address";
	 }
     if($(form_name + "_contactname").value=="") {
		$(form_name + "_contactname").focus();
		return "Please include a contact name";
	 }
     if($(form_name + "_email").value=="") {
		$(form_name + "_email").focus();
		return "Please include an email address";
	 }
     if(!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test($(form_name + "_email").value)) {
		$(form_name + "_email").focus();
		return "Please enter a valid email address";
	 }

     if($(form_name + "_phonenumber").value=="") {
		$(form_name + "_phonenumber").focus();
		return "Please include a phone number";
	 }
     if($(form_name + "_address1").value=="") {
		$(form_name + "_address1").focus();
		return "Please include address line 1";
	 }
     if($(form_name + "_city").value=="") {
		$(form_name + "_city").focus();
		return "Please include a city";
	 }
     if($(form_name + "_postcode").value=="") {
		$(form_name + "_postcode").focus();
		return "Please include a postcode";
	 }
     if(!$(form_name + "_tcagree").checked) {
		$(form_name + "_tcagree").focus();
		return "You must agree to our terms and conditions before ordering this package";
	 }

     return "0";
  }

  function efilter_updateprice(product_item_value) {

      new Ajax.Updater('cost' , '/data/impala/scripts/ajax_impala_scripts.php', { 
	  method: 'get',
	  parameters: {action: 'getprice', product: 'efilter', product_item: product_item_value} 
      });
    }

  function efilter_toggle(togglevalue) {
    var radioGrp = document['forms']['efilterorders']['noemail']; 
    for(i=0; i < radioGrp.length; i++){ 
	if (radioGrp[i].checked == true) { 
	    var radioValue2 = radioGrp[i].value; 
	} 
    }

      if(togglevalue!="2"){
	  $('email_hosted').update("eFilter is charged at a fixed rate when your email is hosted elsewhere");
	  $('efilter_fixedrate').hide();
	  efilter_updateprice(2);
      } else {
	  $('email_hosted').update("No of email addresses on server (all addresses must be protected)");
	  $('efilter_fixedrate').show();
	  efilter_updateprice(radioValue2); 
     }
  }

function radio_button_value(formid, buttonname) {

    var radioGrp = document['forms'][formid][buttonname]; 
    for(i=0; i < radioGrp.length; i++){ 
	if (radioGrp[i].checked == true) { 
	    var radioValue = radioGrp[i].value; 
	} 
    }
  return radioValue;
}

});
 
