YAHOO.namespace("ycplant.container");

function init() {
	
	var divForm = document.getElementById('dialog1');
	divForm.style.display= "block";

	// Define various event handlers for Dialog
	var handleSubmit = function() {
		this.submit();
	};
	var handleCancel = function() {
		this.cancel();
	};
	var handleSuccess = function(o) {
	    var response = o.responseText;
		response = response.split("<!")[0];
			
		if(response.indexOf('YES') != -1){
				  
		  YAHOO.ycplant.container.dialog1.show();
		  var divThankYou = document.getElementById("thank_you");
		  divThankYou.style.display = "block";
		  
		  var divFormBody = document.getElementById("form_body");
		  divFormBody.style.display = "none";
		  
		  
		}else{
		  YAHOO.ycplant.container.dialog1.show();
		  var errorDiv = document.getElementById("error");
		  errorDiv.innerHTML = "Sorry, couldn't send your enquiry, please try again.";
		}
	};
	var handleFailure = function(o) {
		alert("Submission failed: " + o.status);
	};

	// Instantiate the Dialog
	YAHOO.ycplant.container.dialog1 = new YAHOO.widget.Dialog("dialog1", 
							{ width : "30em",
							  fixedcenter : true,
							  visible : false, 
							  constraintoviewport : true
							});

	// Validate the entries in the form to require that both first and last name are entered
	YAHOO.ycplant.container.dialog1.validate = function() {
		var data = this.getData();
		
		
		
		 var alerttxt ="";
		 var errors = false;
		 
		 if(data.firstname == ""){
		        alerttxt += "Please enter your first name<br/>";
                errors =  true;
		}
		
		if(data.surname == ""){
		        alerttxt += "Please enter your surname<br/>";
                errors =  true;
		}
		
		if(data.phone == ""){
		        alerttxt += "Please enter your phone number<br/>";
                errors =  true;
                
		}else{
			if(data.phone.length < 10){
				
				alerttxt += "Valid telephone number is required (minimum of 10 digits)<br/>";
                errors =  true;
			}
			if (data.phone != parseInt(data.phone, 10))
			{
  				alerttxt += "Numeric and no white spaces required in contact telephone number<br/>";
                errors =  true;
			}
		}
			
		
		if(errors){
			    var errorDiv = document.getElementById("error");
		        errorDiv.innerHTML = alerttxt;
		        return false;
		}else{
		    return true;
		
		}
	};

	// Wire up the success and failure handlers
	YAHOO.ycplant.container.dialog1.callback = { success: handleSuccess,
						     failure: handleFailure };
	
	// Render the Dialog
	YAHOO.ycplant.container.dialog1.render();
	YAHOO.util.Event.addListener("enquire", "click", YAHOO.ycplant.container.dialog1.show, YAHOO.ycplant.container.dialog1, true);
	
}

function init1(){
	
	// call back form ------------------------------------------------------------------------------------------------
	
	var divForm = document.getElementById('dialog2');
	divForm.style.display= "block";

	// Define various event handlers for Dialog
	var handleSubmit = function() {
		this.submit();
	};
	var handleCancel = function() {
		this.cancel();
	};
	var handleSuccess = function(o) {
	    var response = o.responseText;
		response = response.split("<!")[0];
		
	
		if(response != "0"){
		 // document.location.reload();
		  YAHOO.ycplant.container.dialog2.show();
		  var divThankYou = document.getElementById("thank_you");
		  divThankYou.style.display = "block";
		  
		   var divLogOrderNumber = document.getElementById("log_order_number");
		  divLogOrderNumber.style.display = "block";
		  // divLogOrderNumber.innerHTML = "<img height='1' width='1' src='http://switch.atdmt.com/action/brmdfs_CallMeBackThankyou_3/v3/ato."+ response +"/atm1.1/atm2.1'/>";
		
		    elementImg = document.createElement("img");
			elementImg.setAttribute("src","switch.atdmt.com/action/brmdfs_CallMeBackThankyou_3/v3/ato."+ response +"/atm1.1/atm2.1");
			elementImg.setAttribute("height", "1");
			elementImg.setAttribute("width", "1");
			divLogOrderNumber.appendChild(elementImg);
		  
		   var divFormBody = document.getElementById("form_body");
		  divFormBody.style.display = "none";
		  
		}else{
		  YAHOO.ycplant.container.dialog2.show();
		  var callBackErrorDiv = document.getElementById("callback_error");
		 callBackErrorDiv.innerHTML = "Sorry could not send your enquiry, please try again";
		}
	};
	var handleFailure = function(o) {
		alert("Submission failed: " + o.status);
	};

	// Instantiate the Dialog
	YAHOO.ycplant.container.dialog2 = new YAHOO.widget.Dialog("dialog2", 
							{ width : "30em",
							  fixedcenter : true,
							  visible : false, 
							  constraintoviewport : true
							});

	// Validate the entries in the form to require that both first and last name are entered
	YAHOO.ycplant.container.dialog2.validate = function() {
		var data = this.getData();
		
		
		 var alerttxt ="";
		 var errors = false;
		
		if(data.name == ""){
		        alerttxt += "Please enter your first name<br/>";
                errors =  true;
		}
		
		if(data.surname == ""){
		        alerttxt += "Please enter your surname<br/>";
                errors =  true;
		}
			
		if(data.choice == null){
		        alerttxt += "Please tick one of the checkboxes <br/>";
                errors =  true;
		}
		
	
		if(data.phone == ""){
		        alerttxt += "Please enter your contact telephone number<br/>";
                errors =  true;
                
		}else{
			if(data.phone.length < 10){
				
				alerttxt += "Valid telephone number is required (minimum of 10 digits)<br/>";
                errors =  true;
			}
			if (data.phone != parseInt(data.phone, 10))
			{
  				alerttxt += "Numeric and no white spaces required in contact telephone number<br/>";
                errors =  true;
			}
		}
		
		if(!data.email == ""){
		    var pattern = /^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/;
     	    if (!pattern.test(data.email))
             {
				alerttxt += "Valid email address is required<br/>";
                errors =  true;
             }
		}
			
		if(errors){
			    var callBackErrorDiv = document.getElementById("callback_error");
		        callBackErrorDiv.innerHTML = alerttxt;
		        return false;
		}else{
			    var callBackErrorDiv = document.getElementById("callback_error");
			    callBackErrorDiv.innerHTML = "";
			    return true;
		}
	};

	// Wire up the success and failure handlers
	YAHOO.ycplant.container.dialog2.callback = { success: handleSuccess,
						     failure: handleFailure };
	
	// Render the Dialog
	YAHOO.ycplant.container.dialog2.render();
	YAHOO.util.Event.addListener("enquire1", "click", YAHOO.ycplant.container.dialog2.show, YAHOO.ycplant.container.dialog2, true);
}