/*Author: Baghor Younes - http://webwizart.be
Twitter: @webwizart*/

/* W3bBase - base.js*/
/* Most used functionality */

var w3bBase = (function() {
var base = {

/************************
 DOM Manipulation 	
************************/
	
	"domHide": function (el) {
		el.style.display = 'none';	
	},
	"domShow": function (v) {
		v.style.display = 'block';	
	},
	"domAddEvent": function (el, evt, fn) {
	    if (el.addEventListener) {
		    el.addEventListener(evt, fn, false);
	    } else if (el.attachEvent) {
		    el.attachEvent('on' + evt, fn);
	    }
	},
	// Add/remove/has class functions from http://snipplr.com/view/3561/addclass-removeclass-hasclass/
	"domHasClass": function (el,cls) {
		return el.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
	},
	"domAddClass": function (el,cls) {
		if (!$.domHasClass(el,cls)) {
		  el.className += " " + cls;
		}
	},
	"domRemoveClass": function (el,cls) {
		if ($.domHasClass(el,cls)) {
		  var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		  el.className = el.className.replace(reg,' ');
		}
	},
	// getElementsByClass function from ddiaz
	"domGetElementsByClass":  function (searchClass,node,tag) {
		var classElements = [];
		if (!node) {
			node = document;
		}
		if (!tag) {
			tag = '*';
		}
		var el = node.getElementsByTagName(tag);
		var elLen = el.length;
		var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
		for (i = 0, j = 0; i < elLen; i++) {
			if (pattern.test(el[i].className)) {
				classElements[j] = el[i];
				j++;
			}
		}
		return classElements;
	},
	
/************************
 VALIDATION 		
************************/
	"valInit": function (){
			var inputfield= document.getElementsByClassName('validate');
			if (inputfield){
				for(i=0;i<inputfield.length;i++){		
				$.valDetect(i);
				}
			}	
	},
	"valDetect": function (i){
		var inputEl = document.getElementsByClassName('validate');
		var errorEl = document.getElementsByClassName('errorTip');
		for(j=0;j<inputEl.length;j++){
			if (errorEl[i].title == 'integer'){
				var fn = function(){$.valInteger(inputEl[i].id, errorEl[i].id)}; // change validation to valInteger;
				$.domAddEvent(inputEl[i], 'blur', fn);}
			if (errorEl[i].title == 'string'){
				var fn = function(){$.valString(inputEl[i].id, errorEl[i].id)}; // change validation to valInteger;
				$.domAddEvent(inputEl[i], 'blur', fn);}
			if (errorEl[i].title == 'demo'){
				var fn = function(){$.valDemoTest(inputEl[i].id, errorEl[i].id)}; // change validation to valInteger;
				$.domAddEvent(inputEl[i], 'blur', fn);}
		}
	},
	// Check for Empty Field
	"valEmpty": function (id, error){
		var e = window.document.getElementById(id).value;
			if (e == ""){
			    $.showBubble(error);
			}
	},
	"valString": function(id,error){
		var e = window.document.getElementById(id).value;
		if (!$.valEmpty(id,error)){
		    var reg = /[a-zA-Z]+/;
		    if (!e.match( reg)){
			$.showBubble(error); 
		    }
		    else{
			document.getElementById(error).style.display='none';
		    }
		}
	},
	"valInteger": function (id,error){
		var e = window.document.getElementById(id).value;
		if (!$.valEmpty(id,error)){
		    var reg = /^\d+$/;
		    if (!e.match( reg)){
			$.showBubble(error);
		    }else{
			document.getElementById(error).style.display='none';
		    }
		}
	},
	// Error Message Bubble
	// show's the Error Message
	// Here you can style the error bubble
	"showBubble": function (id){
		document.getElementById(id).style.display='block';
		document.getElementById(id).style.position='absolute';
		document.getElementById(id).style.width='150px';
		document.getElementById(id).style.color='#ffffff';
		document.getElementById(id).style.top='0px';
		document.getElementById(id).style.left='95%';
		document.getElementById(id).style.fontSize='12px';
		document.getElementById(id).style.background='red';
		document.getElementById(id).style.border='solid white 1px';
		document.getElementById(id).style.padding='3px';
		document.getElementById(id).style.borderRadius='0px 20px 20px 20px';
		document.getElementById(id).style.zIndex='25';
		document.getElementById(id).style.padding='5px';
		document.getElementById(id).style.paddingLeft='10px'; 
	},
	"hideBubble": function (id){
		document.getElementById(id).style.display='none';   
	},
	// A Check Routine after clicking submit
	"errorCheck": function (){
		document.getElementById("errorCheck").innerHTML ='';
		var arr = document.getElementsByClassName("errorTip");
			for (i=0; i< arr.length; i++){
			    if (arr[i].style.display !='none'){
					// FIll with your own code
					document.getElementById("errorCheck").innerHTML += "*" + arr[i].innerHTML +  "<br />"  ;
					document.getElementById("errorCheck").style.display = "block";
					document.getElementById("errorCheck").style.color='#ffffff';
					document.getElementById("errorCheck").style.fontSize='12px';
					document.getElementById("errorCheck").style.background='red';
					document.getElementById("errorCheck").style.border='solid white 1px';
					document.getElementById("errorCheck").style.padding='15px';
					document.getElementById("errorCheck").style.borderRadius='20px 20px 20px 20px';
					document.getElementById("errorCheck").style.position='absolute';
					document.getElementById("errorCheck").style.zIndex='25';
			    }
			    else{
				// FIll with your own code
			    }
			}
	},
	/************************
	 Initialization 	
	************************/
		"go": function () {
				$.valInit();
		}
    }
// alias
	var $ = base;
	return $;
	}());

