// BLOG COMMENT DATA VALIDATION

	function blogCommentValidation() {
		
		var name      = document.getElementById('blogCommentName');
		var nameError = document.getElementById('blogCommentNameError');
		if (name.value.length<2 || name.value.length>20) {
				name.style.background = '#FEE';
				nameError.innerHTML = 'Please enter your Full Name';
				nameError.style.display = '';
				name.focus();
				return false;
		} else if (name.value.length>=2 && name.value.length<=20) {
				validChar='ABCDEFGHIJKLMNOPQRSTUVWXYZ ';   // valid chars
				strlen=name.value.length;       // test string length
				nameUpper=name.value.toUpperCase(); // case insensitive
					// Now scan for illegal characters
					for(ckch=0;ckch<=strlen;ckch++){
					  	if(validChar.indexOf(nameUpper.charAt(ckch))<0){
							name.style.background = '#FEE';
							nameError.innerHTML = 'Invalid Characters';
							nameError.style.display = '';
							return false;
						}
					} 			
		}
		
	
		var email = document.getElementById('blogCommentEmail');
		var emailError = document.getElementById('blogCommentEmailError');
		if(email.value.length<7 || email.value.length>50) {
				email.style.background = '#FEE';
				emailError.innerHTML = 'Please enter a valid Email Address';
				emailError.style.display = '';
				email.focus();
				return false;
		} else if (email.value.length>=7 && email.value.length<=50) {
				validChar='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@.!#$%&\'*+-/=?^_`{|}~';   // valid chars
				strlen=email.value.length;       // test string length
				emailUpper=email.value.toUpperCase(); // case insensitive
				// CHECK FOR MORE THAN ONE @	
				if(emailUpper.split('@').length>2) {
					email.style.background = '#FEE';
					emailError.innerHTML = 'Please enter a valid Email Address';
					emailError.style.display = '';
					email.focus();
					return false;
				}	
				// CHECK FOR CONSECUTIVE DOTS	
				if(emailUpper.split('..').length>1) {
					email.style.background = '#FEE';
					emailError.innerHTML = 'Please enter a valid Email Address';
					emailError.style.display = '';
					email.focus();
					return false;
				}	
					// Now scan for illegal characters
					for(ckch=0;ckch<=strlen;ckch++){
					  	if(validChar.indexOf(emailUpper.charAt(ckch))<0){
							email.style.background = '#FEE';
							emailError.innerHTML = 'Please enter a valid Email Address';
							emailError.style.display = '';
							email.focus();
							return false;
						}
						// VALIDATE @ IS NOT FIRST AND NOT GREATER THAN 6TH-LAST CHARACTER A@XX.INFO
						at=emailUpper.indexOf('@');
						if(at<1 || at>(strlen-6)){
							email.style.background = '#FEE';
							emailError.innerHTML = 'Please enter a valid Email Address';
							emailError.style.display = '';
							email.focus();
							return false;
						}
					} 
				// VALIDATE DOT IS EITHER 3RD, 4TH OR 5TH-LAST CHARACTER 	
				if (emailUpper.charAt(strlen-3) == '.' || emailUpper.charAt(strlen-4) == '.' || emailUpper.charAt(strlen-5) == '.') {
								
				} else {
							email.style.background = '#FEE';
							emailError.innerHTML = 'Please enter a valid Email Address';
							emailError.style.display = '';
							email.focus();
							return false;
				}
				// VALIDATE DOT IS NOT FIRST, LAST OR 2ND-LAST CHARACTER	
				if (emailUpper.charAt(0) == '.' || emailUpper.charAt(strlen-1) == '.' || emailUpper.charAt(strlen-2) == '.') {
							email.style.background = '#FEE';
							emailError.innerHTML = 'Please enter a valid Email Address';
							emailError.style.display = '';
							email.focus();
							return false;
				}
					

		}
		
		
		/* CONFIRM EMAIL
		if((document.getElementById('emailConfirm').value)!=(document.getElementById('email').value)) 			{
				alert('You did not correctly confirm your email.');
				document.getElementById('emailConfirm').focus();
				return false;
		}
		*/
			
		
		var text = document.getElementById('blogCommentText');
		var textError = document.getElementById('blogCommentTextError');
		if (text.value.length<5) {
				text.style.background = '#FEE';
				textError.innerHTML = 'Please submit a comment';
				textError.style.display = '';
				text.focus();
				return false;
		}
		
		
	
	}
	
	
	
	function addConvValidation(c) {
		
		var name      = document.getElementById('blogCommentName'+c);
		var nameError = document.getElementById('blogCommentName'+c+'Error');
		if (name.value.length<2 || name.value.length>20) {
				name.style.background = '#FEE';
				nameError.innerHTML = 'Please enter your Full Name';
				nameError.style.display = '';
				name.focus();
				return false;
		} else if (name.value.length>=2 && name.value.length<=20) {
				validChar='ABCDEFGHIJKLMNOPQRSTUVWXYZ ';   // valid chars
				strlen=name.value.length;       // test string length
				nameUpper=name.value.toUpperCase(); // case insensitive
					// Now scan for illegal characters
					for(ckch=0;ckch<=strlen;ckch++){
					  	if(validChar.indexOf(nameUpper.charAt(ckch))<0){
							name.style.background = '#FEE';
							nameError.innerHTML = 'Invalid Characters';
							nameError.style.display = '';
							return false;
						}
					} 			
		}
		
	
		var email = document.getElementById('blogCommentEmail'+c);
		var emailError = document.getElementById('blogCommentEmail'+c+'Error');
		if(email.value.length<7 || email.value.length>50) {
				email.style.background = '#FEE';
				emailError.innerHTML = 'Please enter a valid Email Address';
				emailError.style.display = '';
				email.focus();
				return false;
		} else if (email.value.length>=7 && email.value.length<=50) {
				validChar='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@.!#$%&\'*+-/=?^_`{|}~';   // valid chars
				strlen=email.value.length;       // test string length
				emailUpper=email.value.toUpperCase(); // case insensitive
				// CHECK FOR MORE THAN ONE @	
				if(emailUpper.split('@').length>2) {
					email.style.background = '#FEE';
					emailError.innerHTML = 'Please enter a valid Email Address';
					emailError.style.display = '';
					email.focus();
					return false;
				}	
				// CHECK FOR CONSECUTIVE DOTS	
				if(emailUpper.split('..').length>1) {
					email.style.background = '#FEE';
					emailError.innerHTML = 'Please enter a valid Email Address';
					emailError.style.display = '';
					email.focus();
					return false;
				}	
					// Now scan for illegal characters
					for(ckch=0;ckch<=strlen;ckch++){
					  	if(validChar.indexOf(emailUpper.charAt(ckch))<0){
							email.style.background = '#FEE';
							emailError.innerHTML = 'Please enter a valid Email Address';
							emailError.style.display = '';
							email.focus();
							return false;
						}
						// VALIDATE @ IS NOT FIRST AND NOT GREATER THAN 6TH-LAST CHARACTER A@XX.INFO
						at=emailUpper.indexOf('@');
						if(at<1 || at>(strlen-6)){
							email.style.background = '#FEE';
							emailError.innerHTML = 'Please enter a valid Email Address';
							emailError.style.display = '';
							email.focus();
							return false;
						}
					} 
				// VALIDATE DOT IS EITHER 3RD, 4TH OR 5TH-LAST CHARACTER 	
				if (emailUpper.charAt(strlen-3) == '.' || emailUpper.charAt(strlen-4) == '.' || emailUpper.charAt(strlen-5) == '.') {
								
				} else {
							email.style.background = '#FEE';
							emailError.innerHTML = 'Please enter a valid Email Address';
							emailError.style.display = '';
							email.focus();
							return false;
				}
				// VALIDATE DOT IS NOT FIRST, LAST OR 2ND-LAST CHARACTER	
				if (emailUpper.charAt(0) == '.' || emailUpper.charAt(strlen-1) == '.' || emailUpper.charAt(strlen-2) == '.') {
							email.style.background = '#FEE';
							emailError.innerHTML = 'Please enter a valid Email Address';
							emailError.style.display = '';
							email.focus();
							return false;
				}
					

		}
		
		
		/* CONFIRM EMAIL
		if((document.getElementById('emailConfirm').value)!=(document.getElementById('email').value)) 			{
				alert('You did not correctly confirm your email.');
				document.getElementById('emailConfirm').focus();
				return false;
		}
		*/
			
		
		var text = document.getElementById('blogCommentText'+c);
		var textError = document.getElementById('blogCommentText'+c+'Error');
		if (text.value.length<5) {

				text.style.background = '#FEE';
				textError.innerHTML = 'Please submit a comment';
				textError.style.display = '';
				text.focus();
				return false;
		}
		
		
	
	}
	
	
	function clearCommentError(inputID) {
		
		document.getElementById(inputID).style.background = '#FFF';	
		document.getElementById(inputID + 'Error').innerHTML = '';	
		
		registerValidation();
		
	}
	
	
	
	
	function ajaxBlogComment() {
		
		
		if(blogCommentValidation() == false) {
		
			return false;
			
		}
		
		
		var ajaxRequest;  
		
		// CHECK BROWSER COMPATIBILILTY
		try{
			// FIREFOX, SAFARI, OPERA
			ajaxRequest = new XMLHttpRequest();
		} catch (e){
			// IE
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
					// ERROR
					alert("An unexpected error occured.");
					return false;
				}
			}
		}
		
		
		var blogCommentName   = document.getElementById('blogCommentName').value;
		var blogCommentEmail  = document.getElementById('blogCommentEmail').value;
		var blogCommentText   = document.getElementById('blogCommentText').value;
		var blogPostID        = document.getElementById('blogPostID').value;
		
		document.getElementById('blogLeaveComment').innerHTML = '<h3 align="center"><img src="/app/media/bgs/loading-white-bg.gif" /> Submitting Your Comment ...</h3>';
				
		var params = 'blogCommentName='+blogCommentName+'&blogCommentEmail='+blogCommentEmail+'&blogCommentText='+blogCommentText+'&blogPostID='+blogPostID;
		
		ajaxRequest.open("POST", "/app/modules/blog/ajaxBlogComment.php", true);
		//Send the proper header information along with the request
		ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajaxRequest.setRequestHeader("Content-length", params.length);
		ajaxRequest.setRequestHeader("Connection", "close");
		ajaxRequest.send(params); 

	
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){

				var ajaxDisplay = document.getElementById('blogLeaveComment');
				ajaxDisplay.innerHTML = ajaxRequest.responseText;
					
				return false;
				
			}
		}
		
	}
	
	
	
	function openAddConv(c) {
		
		document.getElementById('addConversationButton'+c).style.display = 'none';	
		document.getElementById('addConversation'+c).style.display = '';	
		
	}
	
	
	function ajaxAddConv(c) {
		
		if(addConvValidation(c) == false) {
		
			return false;
			
		}
		
		var ajaxRequest;  
		
		// CHECK BROWSER COMPATIBILILTY
		try{
			// FIREFOX, SAFARI, OPERA
			ajaxRequest = new XMLHttpRequest();
		} catch (e){
			// IE
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
					// ERROR
					alert("An unexpected error occured.");
					return false;
				}
			}
		}
		
		
		var blogCommentName   	= document.getElementById('blogCommentName'+c).value;
		var blogCommentEmail  	= document.getElementById('blogCommentEmail'+c).value;
		var blogCommentText   	= document.getElementById('blogCommentText'+c).value;
		var blogPostID        	= document.getElementById('blogPostID').value;
		var blogCommentParentID	= c;
		
		document.getElementById('addConversation'+c).innerHTML = '<h3 align="center"><img src="/app/media/bgs/loading-white-bg.gif" /> Submitting Your Comment ...</h3>';
				
		var params = 'blogCommentName='+blogCommentName+'&blogCommentEmail='+blogCommentEmail+'&blogCommentText='+blogCommentText+'&blogPostID='+blogPostID+'&blogCommentParentID='+blogCommentParentID;
		
		ajaxRequest.open("POST", "/app/modules/blog/ajaxBlogComment.php", true);
		//Send the proper header information along with the request
		ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajaxRequest.setRequestHeader("Content-length", params.length);
		ajaxRequest.setRequestHeader("Connection", "close");
		ajaxRequest.send(params); 

	
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){

				var ajaxDisplay = document.getElementById('addConversation'+c);
				ajaxDisplay.innerHTML = ajaxRequest.responseText;
				document.getElementById('addConversationButton'+c).style.display = '';
				
				return false;
				
			}
		}
		
	}
