
var sdr = {};

sdr.controlTypeEnum = {
    text : 1,
    email : 2
};

sdr.mail = {

	pageId : '',
	name : '',
	from : '', 
	whenCall : '', 
	phone : '',
	address : '', 
	verifNumber : '',
	enquiryType : null,
	parentNode : null,

	
	getCookie : function () {
	
		var cookieName = "tntcon=";
		var cookieArray = document.cookie.split(';');
		
		for(var i=0;i < cookieArray.length;i++) {
			var thisCookie = cookieArray[i];
			
			while (thisCookie.charAt(0)==' ') {
				thisCookie = thisCookie.substring(1, thisCookie.length);
			}
			
			if (thisCookie.indexOf(cookieName) == 0) {
				return thisCookie.substring(cookieName.length, thisCookie.length);
			}
		}
		return null;

	},
	
    createSoapPacket : function () {
	
		var webServiceHeader = '<?xml version=\"1.0\"?>' +
							   '<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"' +
							   ' SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"' +
							   ' xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\"' +
							   ' xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">' +
							   '<SOAP-ENV:Header/><SOAP-ENV:Body>' +
							   '<SendMailMessage xmlns=\"http://tempuri.org/\">' +
							   '<pageId>' + decodeURI(this.pageId) + '</pageId>' +
							   '<from>' + decodeURI(this.from) + '</from>' +
							   '<name>' + decodeURI(this.name) + '</name>' +
							   '<address>' + decodeURI(this.address) + '</address>' +
							   '<phone>' + decodeURI(this.phone) + '</phone>' +
							   '<whenCall>' + decodeURI(this.whenCall) + '</whenCall>' +
							   '<verifNumber>' + decodeURI(this.verifNumber) + '</verifNumber>' +
							   '<verifCode>' + decodeURI(this.getCookie()) + '</verifCode>' +
							   '</SendMailMessage>' +
							   '</SOAP-ENV:Body></SOAP-ENV:Envelope>';							   

		return webServiceHeader;
    },	
	
	
    createSoapPacketForType : function () {
	
		var webServiceHeader = '<?xml version=\"1.0\"?>' +
							   '<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"' +
							   ' SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"' +
							   ' xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\"' +
							   ' xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">' +
							   '<SOAP-ENV:Header/><SOAP-ENV:Body>' +
							   '<SendMailMessageWithType xmlns=\"http://tempuri.org/\">' +
							   '<pageId>' + decodeURI(this.pageId) + '</pageId>' +
							   '<from>' + decodeURI(this.from) + '</from>' +
							   '<name>' + decodeURI(this.name) + '</name>' +
							   '<address>' + decodeURI(this.address) + '</address>' +
							   '<phone>' + decodeURI(this.phone) + '</phone>' +
							   '<whenCall>' + decodeURI(this.whenCall) + '</whenCall>' +
							   '<verifNumber>' + decodeURI(this.verifNumber) + '</verifNumber>' +
							   '<verifCode>' + decodeURI(this.getCookie()) + '</verifCode>' +
							   '<enquiryType>' + decodeURI(this.enquiryType) + '</enquiryType>' +
							   '</SendMailMessageWithType>' +
							   '</SOAP-ENV:Body></SOAP-ENV:Envelope>';							   

		return webServiceHeader;
    },

	onCallBack : function (responseXML, webMethod) {
		
		if (sdr.mail.parentNode) {
		
			var XMLNode = responseXML.getElementsByTagName(webMethod + 'Result');			// SendMailMessageResponse - SendMailMessage
			XMLSuccessful = XMLNode.item(0).firstChild.nodeValue;
		
			if (XMLSuccessful === 'true') {
			
				// remove rows
				for (var index = (sdr.mail.parentNode.rows.length-1); index > 0; index--) {
					sdr.mail.parentNode.deleteRow(index);
				}
			
				var newRow = sdr.mail.parentNode.insertRow(sdr.mail.parentNode.rows.length);
				newRow.className = "htmlpdf";
				
				var newCell = newRow.insertCell(0);
				newCell.style.padding = "5px";
				newCell.colSpan = 2;
				newCell.innerHTML = "<br /><br /><br /><br />Your call back request has been sent. One of our experts will contact you at the time specified. " + 
									"<br /><br />Thank you for your enquiry.";	
				
			} else {
				sdr.mail.onError();
			}
		}
	},
	
	onError : function () {
	
		alert('Error : Email has not been sent please try again.');
	},
	
	invokeMethod : function (getSoapPacket, webMethod) {	
		
		// var url = "http://localhost/mailer_services/mailService.asmx";
		var url = "http://www.crippssolicitors.co.uk/mailer_services/mailService.asmx";
		
		var soapPacket = this.mailSend(url, getSoapPacket, sdr.mail.onCallBack, sdr.mail.onError, webMethod);
	},

	mailSend : function (url, params, callBack, onErrorCallBack, webMethod) {

		var ajaxRequest;
		var thisCallBack = callBack;
		var errorCallBack = onErrorCallBack;

		var GetCallBack = function () {

			if (ajaxRequest !== null && ajaxRequest.readyState === 4 && ajaxRequest.status === 200) {

				if (thisCallBack) {
	                thisCallBack(ajaxRequest.responseXML, webMethod);
				}
			} else if (ajaxRequest !== null && ajaxRequest.readyState === 4 && (ajaxRequest.status && ajaxRequest.status !== 200)) {
				errorCallBack();
			}
		};

		//Browser Support Code
		try {
	        // Opera 8.0+, Firefox, Safari
			ajaxRequest = new XMLHttpRequest();
		}
		catch (e) {
	        // Internet Explorer Browsers
			try {
	            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (ea) {
	            try {
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (eb) {
	                // Something went wrong
					alert("Your browser broke!");
					return false;
				}
			}

	        // Something went wrong
			alert("Your browser broke!");
			return false;
		}

		// Create a function that will receive data sent from the server
		ajaxRequest.onreadystatechange = GetCallBack;
		ajaxRequest.open("POST", url, true);
		ajaxRequest.setRequestHeader("SOAPAction", "http://tempuri.org/" + webMethod);
		ajaxRequest.setRequestHeader('Content-Type', 'text/xml');
		ajaxRequest.send(params);
	}, 	
	
	getControlValue : function (controlId) {
	
		var controlValue = ''; 
	
		if (document.getElementById(controlId)) {
			controlValue = document.getElementById(controlId).value;
		}
		
		return controlValue;
	},
	
	testControlValue : function (controlId, controlType) {
	
		var isValid = false;
	
		if (document.getElementById(controlId)) {
			if (controlType === sdr.controlTypeEnum.text) {
				isValid = document.getElementById(controlId).value.length > 0;
			} 
			else if (controlType === sdr.controlTypeEnum.email) {
			
				var regExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				isValid = regExp.test(document.getElementById(controlId).value);
			}
		}
		
		if (isValid) {
			document.getElementById('error_' + controlId).style.visibility = 'hidden';
		}
		else {
			document.getElementById('error_' + controlId).style.visibility = 'visible';
		}

		return isValid;
	},
	
	sendMail : function (pageId, name, from, whenCall, phone, address, verifNumber, parentNode, enquiryType) {	
		
		this.pageId = this.getControlValue(pageId);
		this.name = this.getControlValue(name);
		this.from = this.getControlValue(from);
		this.whenCall = this.getControlValue(whenCall);
		this.phone = this.getControlValue(phone);
		this.address = this.getControlValue(address);
		this.verifNumber = this.getControlValue(verifNumber);
		this.parentNode = document.getElementById(parentNode);
		
		if (enquiryType) {
		 this.enquiryType = this.getControlValue(enquiryType);
		}
		
		var fromValid = this.testControlValue(from, sdr.controlTypeEnum.email);
		var nameValid = this.testControlValue(name, sdr.controlTypeEnum.text);
		var whenCallValid = this.testControlValue(whenCall, sdr.controlTypeEnum.text);
		var phoneValid = this.testControlValue(phone, sdr.controlTypeEnum.text);
		var addressValid = this.testControlValue(address, sdr.controlTypeEnum.text);
		var verifNumberValid = this.testControlValue(verifNumber, sdr.controlTypeEnum.text);
		
		if (fromValid && nameValid && whenCallValid && phoneValid && addressValid && verifNumberValid) {
		
			// invoke email method
			if (enquiryType) {
						
				if (this.testControlValue(enquiryType, sdr.controlTypeEnum.text)) {
					this.invokeMethod(this.createSoapPacketForType(), 'SendMailMessageWithType');
				} else {
					alert('Please complete the form.');
					return false;
				}
				
			} else {
				this.invokeMethod(this.createSoapPacket(), 'SendMailMessage');
			}	
			
		} else {
			alert('Please complete the form.');
			return false;
		}
    }
};
