$(function(){
	
/*----- 
Website Default URL Address -----*/	
pro = document.location.protocol;
hst = document.location.hostname;
uri = pro + '//' + hst + '/';
	



/*----- 
Popup Frame -----*/
pop_box = function(m,t){
	
	var w,f,c,h,b,btn,ldr,wth,hgh,opc=false;
	
	removePopBox();
	
	var captcha = document.getElementById('captcha');
	
	if(typeof opt !== "undefined"){
		if(opt.loader){ldr  = true;}
		if(opt.opc){opc = true;}	
	}
	
	w = document.createElement('div');
	f = document.createElement('div');
	c = document.createElement('div');
	h = document.createElement('div');
	b = document.createElement('div');
	btn = document.createElement('div');
	
	w.setAttribute('id', 'pp_wrap');
	f.setAttribute('id', 'pp_frame');
	c.setAttribute('id', 'pp_content');
	h.setAttribute('id', 'pp_header');
	b.setAttribute('id', 'pp_bottom');
	btn.setAttribute('id', 'close_pop');
	
	document.body.appendChild(w);
	document.body.appendChild(f);
	f.appendChild(btn);
	f.appendChild(h);
	f.appendChild(c);
	f.appendChild(b);
	
	if(window.outerHeight){
	wth = window.innerWidth;	
	hgh = window.innerHeight;}
	else{
	wth = document.body.clientWidth;	
	hgh = document.body.clientHeight;}
	
	if(opc){
	$(w).css({
	'position':'absolute',
	'padding':'0','margin':'0',
	'top':'0','right':'0','bottom':'0','left':'0', 'background':'#000', 'z-index':'999999',opacity:0.50});}
	
	else if(!opc) {
	$(w).css({
	'position':'absolute',
	'top':'0', 'width': wth, 'height': hgh, 'background':'#000','z-index':'999999',opacity:0.01});}
	
	$(f).css({
	'position':'fixed',
	'top':'20%',
	'left':'50%',
	'margin-left':'-275px',
	'width':'550px',
	'padding':'40px 10px 10px 10px',
	'background':'url(\''+uri+'lib/js/validate/imgs/pp_bck.png\')',
	'color':'#333',
	'border':'none',
	'-moz-box-shadow':'0px 5px 10px #000',
	'-webkit-box-shadow':'0px 5px 10px #000',
	'box-shadow':'0px 5px 10px #000','z-index':'999999999'});
		
	$(btn).css({
	'position':'absolute',
	'display':'block',
	'margin':'0 0 0 0',
	'border':'none',
	'top':'0',
	'right':'-0',
	'padding':'0',
	'font-weight':'bold',
	'font-size':'15px',
	'width':'40px',
	'height':'15px',
	'color':'#FFF',
	'text-align':'center',
	'background':'url(\''+uri+'lib/js/validate/imgs/close.png\') 50% 50% no-repeat'});

	$(h).css({
	'display':'block','font-size':'15px','font-weight':'bold','font-family':'arial',
	'padding':'5px 10px','border':'1px solid #333','border-bottom':'none','background':'#303030','color':'#DDD'});
	
	if(typeof t == "undefined"){t = '';}else{t = t;}
	
	h.innerHTML = t;
	
	$(c).css({
	'display':'block',
	'padding':'15px 10px','border':'1px solid #FFF','background':'#FFF'});
	
	if(ldr){
	c.innerHTML = '<p class="align_center"> <h2 class="lgray align_center" style="font-size:19px;"><img src="'+uri+'imgs/ajx/ajx3.gif" width="20px"> Loading...</h2></p>';
	setTimeout(function(){c.innerHTML = m;},1000);}
	else{c.innerHTML = m;}
	
	$(f).draggable();
	
	w.onclick = removePopBox;
	
	btn.onclick = removePopBox;
	
	};
	
	removePopBox = function(){$('.busy').hide();$('#pp_wrap').remove();$('#pp_frame').remove();}
	
	// => Form validation
	validate = function(uri){
				
	var o,eo,ajx,gbl,rqd,dada,err,id,cap,elm,jsn,res,rl,tbx,rad,fld,isc,isn,ise,isu,isz,isp,isd,iss,uri,evr,bsy,agr,f,r,a,m,i,t,c,s,g,op,ec,l=false;
	
	uri = uri + 'lib/js/validate/';
	
	f  = document.forms;
	r  = $('[class=required]');
	op = $('[class=optional]');
	 	
	// => Busy Wrapper	
	$(f).each(function(){$(this).prepend('<div class="busy">')});
	

	// <= end options *******************************************************************/
	

	// => CREATE AN EMPTY ARRAY FOR ERRORS		
	err = [];
	
	// => ERRORS OBJECT
	eo = {
	
	// => Warning icon
	wImg : '<img src="'+uri+'imgs/warning.png"> ',
	
	// => info icon
	iImg : '<img src="'+uri+'imgs/info.png"> ',
	
	// => Review form message
	rView : 'Please review form before submiting.',
	
	// => Required fields
	requires : 'Please fill out all Highlighted fields on the form.',
	
	// => Complete
	complt : ' Please complete.',
	
	// => No spaces
	nospace : 'Spaces are not allowed.',
	
	// => Special characters
	special : 'Invalid Character.',
	
	// => Email message
	email : 'Please provide a valid email.', 
	
	// => Phone numbers
	phone : 'Formats: 555-555-5555, 1-555-555-5555',
	
	// => Zip code
	zip : 'Provide 5 diget zip-code',
	
	// => URL Format 
	url : 'Formats: www.example.com or http://example.com',
	
	// show messages
	mssg:function(i,m){if($.trim($(i).val()) == ""){$(i).prev().html("")}else{$(i).prev().html(m)};}
		
	};
	
	
	// => Get set options ********************************************************/
	if(typeof opt !== "undefined" && (opt)){
		
	// => Form autocomplete
	if(opt.autoOff){$(f).attr('autocomplete', 'off')};
	
	// => Email verification
	if(opt.verifyEmail){evr = true;};
		
	// => Ajax verification option
	if(opt.fnc){gbl = true};
	
	if(opt.json){jsn = 'json';}
	
	if(opt.mssg){eo.requires = opt.mssg;}
	
	} 
	
	
	// => FORM DATA OBJECT
	o = {
		 
	// => REGULAR EXPRESSIONS
	regx:function(v,t){
			
	switch(t){
	case 0:res = isc.test(v);break;
	case 1:res = iss.test(v);break;
	case 2:res = isn.test(v);break;
	case 3:res = isd.test(v);break;
	case 4:res = isp.test(v);break;
	case 5:res = isz.test(v);break;
	case 6:res = isu.test(v);break;
	case 7:res = ise.test(v);break;
		}
		return res;
	},
	
	// => GET AND SET LABELS
	labels:function(i){		
	/*$(i).each(function(){
		$(this).each(function(){
		if($(this).prev() !== 'label'){
		$(this).before('<label for="'+this.id+'"></label>');
				}	
			});
		});*/
	},
	
	// => Captcha Code
	
	
	reloadCaptcha:function(){
	if(typeof captcha != "undefined"){
	document.getElementById('captcha').src=''+uri+'captcha/capcha_show.php?'+Math.random();}
	else{return false;}},
	
	// get valid fields
	vld:function(f){	
	err.length = 0;	
	r = $(f).find('.required');
	$.each(r,function(i,v){
	con = $(v).attr('valid');	
	if(con !== "undefined"){	
	if(con == "false" && $.trim($(v).val()) !== ""){	
	err.push(v);o.setcon($(v), 'wrn');	
				}
			}			
		});	
		if(err[0]){return true;}
	},
	
	// => Show Busy signal
	busy:function(bsy,f){
	$(bsy).css({
	'position':'absolute',
	'background':'url(\''+uri+'imgs/ajx2.gif\') 50% 50% no-repeat',
	'width':$(f).width(),'height':$(f).height(),'display':'block', 'z-index':'9999'})
	},
	
	// => ADD SHADOWS
	shdw:function(i,c){
	$(i).css({
	'-moz-box-shadow':'0px 0px 5px '+c+'',
	'-webkit-box-shadow':'0px 0px 5px '+c+'',
	'box-shadow':'0px 0px 5px '+c+''
		});
	},
	
	// => REMOVE SHADOW
	removeShdw:function(i){
	$(i).css({
	'-moz-box-shadow':'0px 0px 0px',
	'-webkit-box-shadow':'0px 0px 0px',
	'box-shadow':'0px 0px 0px'
		});		
	},
	
	// => BACK TO DEFAULT
	dfl:function(i){$(i).removeAttr('valid')},
		
	// => STOP PROCESS
	stp:function(i){$(i).attr('valid','false');},
	
	// => CONTINUE PROCESS
	con:function(i){$(i).attr('valid','true');},
	
	// => SET ELEMENT STYLE
	setStyle:function(i,c){$(i).removeClass('nfo ltr rqd pss wrn').addClass(c);},
	
	// => SET CONDITION
	setcon:function(i,c){
	if(c == 'dfl'){$(i).removeClass('nfo ltr rqd pss wrn');return false;}
	if(c == "pss"){$(i).attr('valid','true');}
	else{$(i).attr('valid','false');}
	$(i).removeClass('nfo ltr rqd pss wrn').addClass(c);
	},
	
	// => REMOVE ELEMENT STYLE
	removeStyle:function(i){$(i).removeClass('ldr rqd nfo ltr pss wrn');},
	
	// => CHECK ALL EMPTY FIELDS
	required:function(f){
		
	err.length = 0;
	
	f = $(f).attr('id');
	
	r = $('#'+f).find('.required');
	
	for (var i=0; i < r.length; i++) {
		
	if($.trim(r[i].value) == ""){
	err.push(r[i]);
	o.setcon(r[i], 'rqd');
		}
	};
	

	// => Get errors
	if(err[0]){	
	pop_box('<p class="align_center"><b>'+eo.iImg+eo.requires+'</b></p>');
	return true;
		}	
	},
	
	// Checkbox agree buttons
	agree:function(i){
	var m = "";			
	agr = 	$('#'+i).find('[class=agree]');
	for(var a=0;a < agr.length;a++){
	if(!agr[a].checked){
	if($(agr[a]).attr('mssg')){
	m += eo.wImg+"<b>"+[$(agr[a]).attr('mssg')]+"</b><br/>";}
	else{m = "Checkbox must be check";}
			}	
		}
	if(m !== ""){pop_box(m);return true;}	
	},
	
	// => CHECK FOR SPECIAL CHARACTERS
	spch:function(i,v){
	if(o.regx(v,0) == false){
	o.setcon(i, 'ltr');eo.mssg(i,eo.special);
		}	
	},
	
	// => CHECK FOR WHITE SPACES
	nspw:function(i,v){
	if(o.regx(v,1) == false){
	o.setcon(i, 'wrn');eo.mssg(i,eo.nospace);
		}
	},
	
	// => NEED WHITE SPACES
	ndspw:function(i,v){
	if(o.regx(v, 1) == false){
	o.setcon(i, 'pss');eo.mssg(i,null);}
	else{o.setcon(i, 'nfo');eo.mssg(i,eo.complt);
		}
	},
	
	// => NUMBERS VALIDATION
	isn:function(i,v){
	if(o.regx(v,2) == true){
	o.setcon(i, 'pss');eo.mssg(i,null);}
	else{o.setcon(i, 'nfo');eo.mssg(i,'Numbers only and decimal points only.');}
	},
	
	// => PHONE NUMBER VALIDATION
	isp:function(i,v){
	if(o.regx(v,4) == true){
	o.setcon(i, 'pss');eo.mssg(i,null);}
	else{o.setcon(i, 'nfo');eo.mssg(i,eo.phone);
		}
	},
	
	// => PHONE NUMBER VALIDATION
	isp:function(i,v){
	if(o.regx(v,4) == true){
	o.setcon(i, 'pss');}else{o.setcon(i, 'nfo');
		}
	},
	
	// => ZIP CODE VALIDATION
	isz:function(i,v){
	if(o.regx(v,5) == true){
	o.setcon(i, 'pss');eo.mssg(i,null)}
	else{o.setcon(i, 'nfo');eo.mssg(i,eo.zip)		
		}
	},
	
	// => URL VALIDATION
	isu:function(i,v){
	if(o.regx(v,6) == true){
	o.setcon(i, 'pss');eo.mssg(i,null);}
	else{o.setcon(i, 'nfo');eo.mssg(i,eo.url);
		}
	},
	
	// => CHECK EMAIL ADDRESSS
	iseml:function(i,v){
	if(o.regx(v,7) == true){
	o.setcon(i, 'pss');eo.mssg(i,null);	
	}else{o.setcon(i, 'nfo');
	eo.mssg(i,eo.email);
		}
	},
	
	// => VERIFY EMAIL ADDRESS
	Everify:function(i,v){
	o.iseml(i,v);
	if((evr == true) && $(i).attr('valid') == 'true'){
	o.setcon(i, 'ldr');eo.mssg(i,'Please wait.');
	$.get(opt.verifyEmail + '?e='+v,function(d){
	if(d !== "valid"){
	o.removeStyle(elm);
	o.setcon(i, 'ltr');eo.mssg(i,d);return false;}
	else{o.removeStyle(elm);o.setcon(i, 'pss');eo.mssg(i,null);}
			});
		}
	},
	
	// => VERIFY PASSWORD
	verifypswd:function(i){
	v = $.trim(i.val());
	c = o.regx(v, 0);
	s = o.regx(v, 1);
	if(v == ""){o.removeStyle(elm);}
	if(c == false){o.setcon(i, 'ltr');}
	if(s == false){o.setcon(i, 'wrn');}
	if(v.length < password.len){o.setcon(i, 'nfo');
	eo.mssg(i,'Password must be '+password.len+' or more characters in lenght')}
	else{eo.mssg(i,null);return true;}	
	},
	
	// => PASSWORD FEILDS
	psw:function(){
	if(typeof password !== "undefined" && (password)){
	pswd1 = $('#'+password.pswd);
	pswd2 = $('#'+password.compare);
	pswd1.bind('keyup blur',function(){
	pswd2.val("");
	if($.trim(pswd1.val()) ==""){return false;}
	if(o.verifypswd($(this)) !== true){return false;}
	else{o.setcon($(this), 'pss');}
	});
	pswd2.bind('keyup blur',function(){
	if($.trim(pswd2.val()) ==""){return false;}
	if(o.verifypswd($(this)) !== true){return false;}
	else if($(this).val() !== pswd1.val()){o.setcon($(this), 'wrn');eo.mssg($(this),'Password do not match.')}
	else{o.setcon($(this), 'pss');}
			});
		}
	}	
	
	} // <= end form data object
	
	// => Get all input, textarea fields
	var inpt = document.getElementsByTagName('input');
	var textarea = document.getElementsByTagName('textarea');
		
	$(r).focus(function(){
		
	o.shdw($(this), '#69f');
	
	c = $(this).attr('class').replace("required", "");
	c = c.replace("option", "");
	c = c.replace(" ", "");
	
	arr = [c];
	if($.inArray('rqd',arr) == 0){
	$(this).animate({backgroundPosition: '99% 50%'},0);
	$(this).animate({backgroundPosition: '150% 50%'},500, function(){
	o.removeStyle($(this),'nfo ltr rqd');
	$(this).animate({backgroundPosition: '99% 50%'},0)
			});
		}
	});
	
	//=> Validate input fields
	$(r).bind('keyup blur',function(){		
	elm = $(this);
	v = $.trim(elm.val());
	c = elm.attr('rel');
	o.removeShdw(elm);	
	if(elm.attr('id') !== "captcha_code"){	
	valdt(elm,v,c);}
	});
	
	// => Default validation
	valdt = function(elm,v,c){	
	o.removeStyle(elm);
	if(elm.attr('id') !== "captcha_code"){o.removeShdw(elm);
	if(v == ""){elm.prev().html("");o.shdw($(this), 'white');o.dfl(elm);return false;}
	if(c == 'complete'){o.ndspw(elm,v);o.spch(elm,v);}
	if(c == 'email'){o.Everify(elm,v);o.nspw(elm,v);o.spch(elm,v);}
	if(c == 'phone'){o.isp(elm,v);o.spch(elm,v);}
	if(c == 'zip'){o.isz(elm,v);o.spch(elm,v);}
	if(c == 'url'){o.isu(elm,v);o.spch(elm,v);}
	if(c == 'number'){o.isn(elm,v);o.nspw(elm,v);o.spch(elm,v);}
	if(!c){if(o.regx(v,0) == false){
	o.setcon(elm, 'ltr');eo.mssg(elm,eo.special);}
	else{o.setcon(elm, 'pss');}
			}
		}
	}
	
	// Process password
	if(typeof password !== "undefined"){o.psw();}
	
	// => Get textboxs
	if(typeof opt !== "undefined"){
	
	if(opt.checkBoxs){	
		
	checkBoxs = function(){		
	$.each(opt.checkBoxs,function(i,v){
	$('#'+i).click(function(){
	if(this.checked){$(this).attr('value',v)}
	else if(!this.checked){$(this).removeAttr('value')}			
			});
		});	
	}()};
	};
	
	// => Captcha Code *************************************************************************************/

	// => Run Captcha code reload function on click action
	var cph_load = document.getElementById('cpch_load')
	if(cph_load){cph_load.onclick = function(){o.reloadCaptcha();return false;}}
	
	// => Validate Captcha code
	var cph = document.getElementById('captcha_code');
	$(cph).bind('keyup',function(){
		
		elm = $(this);
		
		if(this.value == ""){o.removeStyle(elm);eo.mssg(elm,'Type above code here.');return false;}
		
		o.setcon($(this), 'nfo');
		eo.mssg($(this),'Keep typing');
		
		if(this.value.length >= 5){o.removeStyle(elm);	
		$.post(''+uri+'captcha/ajax-capcha.php',{cpch: this.value}, function(d){
		if(d !== "pss"){o.setcon(elm, 'wrn');eo.mssg(elm,d);o.reloadCaptcha();return false;}
		else{o.setcon(elm, 'pss');eo.mssg(elm,null);}	
				});
			}
	});	
	
	// <= Captcha Code *************************************************************************************/
		
	
	// => FORMS submit	************************************************************************************/
	$(f).submit(function(){
				
	f = $(this);
	i = $(f).attr('id');
	m = $(f).attr('method');
	a = $(f).attr('action');
	
	$('form').removeAttr('sbmt');
	
	
	
	$(f).attr('sbmt', ''+this.id+'');
	
	var bsy = $(f).find('[class=busy]');

	// Get fields value
	$(r).each(function(){elm = $(this);v = $.trim(elm.val());c = elm.attr('rel');valdt(elm,v,c);});
	
	// => Get empty fields
	rqd = o.required($(f));
	if(rqd == true){return false;}
	
	// valide fields
	if(o.vld($(f))){pop_box('<p class="align_center"><b>'+eo.wImg+"Please review the form before submiting.</b></p>");return false;};
	
	// => Agree to terms
	if(typeof opt !== "undefined"){
	if(opt.agree){var agr = o.agree(i); if(agr == true){return false;}}}
	
	// => Get Errors
	if(err[0]){pop_box(eo.wImg+eo.rView);return false;}
	
	// => Show busy signal
	o.busy(bsy,f);	
	
	// => Global function
	if(gbl){opt.fnc.doPrcss();
	if(document.getElementById('captcha_code')){o.reloadCaptcha();}
	return false;};
				
	// => Form return value / and Process 
	if(typeof opt !== "undefined"){
		
	if(opt.ajx){	
		
	/*// => POST Method submition	
	if(m.toLowerCase() == 'post'){
	$.post(uri+a,{frmDt:$(f).serialize()}, function(d){
		$('.busy').hide();
		if($.trim(d) !== ""){
			pop_box(d);
		}
	});}*/
		
	
	return false;}}
	
	o.reloadCaptcha();
		
	});
	
	/*******************************************************************************************************************/
	
	
	// => GET AND SET LABELS
	o.labels(r);
	
	// Checks for special characters in field elment 
	isc=new RegExp(/^[a-z0-9\_\@\-\.\?\(\)\'\:\,\/\\ ]+$/i);
	
	// Checks for spaces in field elment 
	iss=new RegExp(/^[\S]+$/i);
	
	// Checks that field elment has numbers only
	isn=new RegExp(/^[0-9\. ]+$/i);
	
	// Checks that field elment has valid Date
	isd=new RegExp(/^([\d]|1[0,1,2])\/([0-9]|[0,1,2][0-9]|3[0,1])\/\d{4}$/);
	
	// Checks that field elment has valid phone number
	isp=new RegExp(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
	
	// Checks that field elment has valid 5 digets zip-code
	isz=new RegExp(/^([0-9]{5})+$/i);
	
	// Checks that field elment has valid URL Address
	isu=new RegExp(/^(http[s]?:\/\/|ftp:\/\/)?(www\.)?[a-zA-Z0-9-\.]+\.(com|org|net|mil|edu|ca|co.uk|com.au|gov|us|co)([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/);
	
	// Checks that field elment has valid Email Address
	ise=new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	
	
}(uri);	
	
	
	
});
