// ActionScript Document
function eMailChange( etarget ) {
	for( var i = 0; i < etarget.childNodes.length; ++i )
		if( etarget.childNodes[i].nodeName == 'SPAN' )
			window.location = 'mailto:' + etarget.childNodes[i].innerHTML + '@illumaticltd.co.uk';
}

function $$( e ) {
	return document.getElementById( e );
}

function validateChild( t, e ) {
	if( t.value.match( e ) == null )
		return false;
	return true;
}

function nMail( form, formTarget ) {
	var formData = '';
	
	var iForm = false;
	function checkChildren( t ) {
		for( var i = 0; i < t.childNodes.length; ++i ) {
			if( t.childNodes[i].nodeName == 'INPUT' || t.childNodes[i].nodeName == 'TEXTAREA' ) {
				var e = t.childNodes[i];
				
				if( e.type == 'checkbox' )
					formData = formData + e.name + ': ' + e.checked + '|';
				else if( e.type == 'radio' && e.checked === true )
					continue;
				else {
					if(
						 ( ( e.name == 'name' || e.name == 'phone' ) && e.value.length < 2 ) ||
						 ( e.name == 'email' && ( e.value.match( /^.+@.+\.\w{2,6}/i ) == null ) ) ||
						 
						 ( ( e.name == 'call_name' || e.name == 'call_phone' ) && e.value.length < 2 ) ||
						 ( e.name == 'call_email' && ( e.value.match( /^.+@.+\.\w{2,6}/i ) == null ) ) ||

						 ( ( e.name == 'call_name' ) && e.value == 'Full Name' ) || 
						 ( ( e.name == 'call_phone' ) && e.value == 'Telephone Number' ) ||
						 ( ( e.name == 'call_email' ) && e.value == 'Email Address' )
					   ) {
						iForm = true;
						e.style.borderColor = 'red';
						e.focus() ;
						return false;
					} else {
						formData = formData + e.name + ': ' + e.value + '|';
						e.style.borderColor = 'green';
					}
						
				}
			} else
				if( !checkChildren( t.childNodes[i] ) ) return false;
		}
		return true;
	}
	checkChildren( form );
	if( !iForm )
		nJaxSend( formTarget, formData, form.id );
	
	return false;
}
function nJaxSend( url, data, targetform ) {
	var nJax;
	
	if (window.XMLHttpRequest) {
		nJax = new XMLHttpRequest();
	
	
	} else if (window.ActiveXObject) {
		nJax = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	else
		nJax = 0;
		
	if( !nJax ) {
		alert( 'Your browser does not support AJAX' );
		return false;
	}
	
	nJax.onreadystatechange = function() {
		if( nJax.readyState == 4 && nJax.status == 200 )
			//11 when two forms are in a page, 1 for only one form in a page
			if( nJax.responseText == '11' ) {
				if( targetform == 'contact_detail' ) {
					// Successful Email
					$$( 'thankyou' ).style.display = 'block';
					$$( 'detail-contact-form' ).style.display = 'none';
				} else if( targetform == 'formcontact' ) {
					var target = $$( 'formcontact' );
					
					var thankyou = document.createElement( 'div' );
					thankyou.innerHTML = 'Thank you for your request,<br>we will call you soon!<br><br><br>';
					
					target.parentNode.appendChild( thankyou );
					target.style.display = 'none';
					$$( 'thankyou' ).style.display = 'block';					
				}
				//$.scrollTo( '#top-page', 800 );
			}
	}
	
	formData = "RData=" + data;
	
	nJax.open( "POST", url, true );
	nJax.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
	nJax.setRequestHeader( "Content-length", formData.length );
	nJax.setRequestHeader( "Connection", "close" );
	nJax.send( formData );
}
