(function($){ 
	$.fn.validate = function (options) {

		var defaults = {
							requiredClassName: 'required',
							emailClassName: 'email',
							numClassName: 'number',
							urlClassName: 'url',
							phoneClassName: 'phone',
							editorClassName: 'reqeditor',
							imgClassName: 'uploadfile',

							showErrorMessage: true,	// FALSE WILL SHOW ALERT, TRUE WILL SHOW ERROR BORDER WITH MSG.
							showErrorText: true,	// TRUE WILL SHOW ERROR MSG WITH BORDER, FALSE WILL SHOW ERROR BORDER ONLY

							ErrorBefore: '<div class="errortext">' ,
							ErrorAfter: '</div>',
							ErrorTextContainer: 'div',

							ErrorRequiredText: 'This field is required.',
							ErrorEmailText: 'Please enter a valid email address.',
							ErrorURLText: 'Please enter a valid URL.',
							ErrorFileText: 'Invalid File.',
							ErrorPhoneText: 'Please enter a 10 digit phone number'
						}

		var options = $.extend(defaults, options) ;				

		return this.each (function () {

			var form = $(this);		
			
			if(!form.is("form")) return;
									
			
			
			form.submit(function() {
			
				var objRequired = $("#"+ form.attr("id") +" ."+ options.requiredClassName) ;
				var objemail = $("#"+ form.attr("id") +" ."+ options.emailClassName) ;
				var objnumber = $("#"+ form.attr("id") +" ."+ options.numClassName) ;
				var objurl = $("#"+ form.attr("id") +" ."+ options.urlClassName) ;
				var objphone = $("#"+ form.attr("id") +" ."+ options.phoneClassName) ;
				var objEditor	= $("#"+ form.attr("id") +" ."+ options.editorClassName) ;
				var objUploadFile	= $("#"+ form.attr("id") +" ."+ options.imgClassName) ;
			
				var StopExecution = false ;
				
				// Check Required 
				objRequired.each(function() {
					var objtype = $(this).attr('type') ;
					var objtitle = $(this).attr('title') ;
					
					
				

					switch(objtype) {
										case 'text':
										case 'select-one':
										case 'textarea':
										case 'file':
											if($(this).val() == '' || $(this).attr('alt') == $(this).val())
											{
											//	alert('Please make sure that '+objtitle+' field was properly compelted.') ;
												if($(this).attr('alt') == $(this).val()) $(this).val('') ;
												if(options.showErrorMessage)
												{
													$(this).addClass("error") ;
													if (options.showErrorText) {
														$(this).next().remove(options.ErrorTextContainer) ;
														$(this).after(options.ErrorBefore+ options.ErrorRequiredText+options.ErrorAfter)
													}	// if (showErrorText)
												}else
													{
														alert('Please make sure that '+objtitle+' field was properly compelted.') ;	
													}
												StopExecution = true ;
												$(this).focus() ;
											//	return false ;
												//e.preventDefault() ;
												
											}else
												{
													$(this).next().remove(options.ErrorTextContainer) ;
													$(this).removeClass("error") ;
												}
											break ;
										case 'select-multiple':
											var ValSelected = false ;
					
											$('#'+$(this).attr('id')+" :selected").each(function (i, selected) {
												ValSelected = true ;
											}) ;
											if (!ValSelected)
											{
												if(options.showErrorMessage)
												{
													$(this).addClass("error") ;
													if (options.showErrorText) {
														$(this).next().remove(options.ErrorTextContainer) ;
														$(this).after(options.ErrorBefore+ options.ErrorRequiredText+options.ErrorAfter)
													}
												}else
													{
														alert('Please make sure that '+objtitle+' field was properly compelted.') ;
													}
												StopExecution = true ;
												$(this).focus() ;
												//return false ;
												//e.preventDefault() ;
											}
											else
												{
													$(this).next().remove(options.ErrorTextContainer) ;
													$(this).removeClass("error") ;
												}
											break ;
									}	// switch(objtype)
																
									
				}); 
				if (StopExecution) {
					//e.preventDefault() ;
					//return false ;
				}	// if (!StopExecution) 	
				//-------------------------------------------
				
				// Check Email Validation
				objemail.each(function() {
					var objtype = $(this).attr('type') ;
					var objtitle = $(this).attr('title') ;
					
					
				

					switch(objtype) {
										case 'text':
											var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
											   var address = $(this).val(); //
											   if(reg.test(address) == false) {
												  if(options.showErrorMessage)
													{
														$(this).addClass("error") ;
														if (options.showErrorText) {
															$(this).next().remove(options.ErrorTextContainer) ;
															$(this).after(options.ErrorBefore+ options.ErrorEmailText+options.ErrorAfter) ;
														}
														//$(this).after('<span class="errortext">"'+address+'" is an invalid e-mail!</span>')
													}else
														{ 
															  alert("\"" + address + "\" is an invalid e-mail!");
														}
												  StopExecution = true
												  //return false;
											   }
											   else
												{
													$(this).next().remove(options.ErrorTextContainer) ;
													$(this).removeClass("error") ;
												}
											break ;
									}	// switch(objtype)
																
									
				}); 
				//-------------------------------------------
				if (StopExecution) {
					//e.preventDefault() ;
					//return false ;
				}	// if (!StopExecution) 	
				// Check Number Validation
				objnumber.each(function() {
					// Separat Function which is used on key press 
				}); 
				//-------------------------------------------
				if (StopExecution) {
					//e.preventDefault() ;
					//return false ;
				}	// if (!StopExecution) 	
				
				// EDITOR VALIDATION STARTS 
				objEditor.each(function() {
					var ins_name = $(this).attr('name') ;
					var fieldname = $(this).attr('title') ;

					switch(fieldname) {
						default:
							var oEditor = FCKeditorAPI.GetInstance(ins_name);
							var content = oEditor.GetXHTML(true)
							if(content=='') {
								alert('Please make sure the '+fieldname+' field was properly completed.');
								StopExecution = true ;
								//return false ;
							}	//if(content=='')
							else
							{
								$(this).next().remove(options.ErrorTextContainer) ;
								$(this).removeClass("error") ;
							}
						break ;
					}
				})	// objEditor.each
				//-------------------------------------------
				if (StopExecution) {
					//e.preventDefault() ;
					//return false ;
				}	// if (!StopExecution) 	
				
				// Check URL Validation
				objurl.each(function() {
					var objtype = $(this).attr('type') ;
					var objtitle = $(this).attr('title') ;
					
					
				

					switch(objtype) {
										case 'text':
											var reg = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
											   var address = $(this).val(); // 
											   if(reg.test(address) == false) {
  												    if(options.showErrorMessage)
													{
														$(this).addClass("error") ;
														if (options.showErrorText)
														{
															$(this).next().remove(options.ErrorTextContainer) ;
															$(this).after(options.ErrorBefore+ options.ErrorURLText+options.ErrorAfter)
														}
														//$(this).after('<span class="errortext">"'+address+'" is an invalid URL</span>')
													}else
														{ 
															  alert("\"" + address + "\" is an invalid URL");
														}
												  StopExecution = true
												  //return false;
											   }
											   else
												{
													$(this).next().remove(options.ErrorTextContainer) ;
													$(this).removeClass("error") ;
												}
											break ;
									}	// switch(objtype)
																
									
				}); 
				//-------------------------------------------
				if (StopExecution) {
					//e.preventDefault() ;
					//return false ;
				}	// if (!StopExecution) 	
				
				// Check phone Validation
				objphone.each(function() {
					var objtype = $(this).attr('type') ;
					var objtitle = $(this).attr('title') ;

					switch(objtype) {
										case 'text':
											// phone validation
											var re= /\D/;
											// test for this format: (xxx)xxx-xxxx
											var re2 = /^\({1}\d{3}\)\d{3}-\d{4}/; 
											// test for this format: xxx-xxx-xxxx
											//var re2 = /^\d{3}-\d{3}-\d{4}/;
											
											for (i=0; i<$(this).val().length;i++){
											var num=$(this).val();
											
											var newNum;
											 if (num != "" && re2.test(num)!=true){
											   if (num != ""){
												 while (re.test(num)){
												 num = num.replace(re,"");
												 }
											   }
											
											  if (num.length != 10){
  												    if(options.showErrorMessage)
													{
														$(this).addClass("error") ;
														if (options.showErrorText)
														{
															$(this).next().remove(options.ErrorTextContainer) ;
															$(this).after(options.ErrorBefore+ options.ErrorPhoneText+options.ErrorAfter)
														}
														//$(this).after('<span class="errortext">"'+address+'" is an invalid URL</span>')
													}else
														{ 
															  alert("Please enter a 10 digit phone number");
														}
												  StopExecution = true
												 // return false;
												}
											   else {
													 // for format (xxx)xxx-xxxx
													// newNum = '(' + num.substring(0,3) + ')' + num.substring(3,6) + '-' + num.substring(6,10);
													 // for format xxx-xxx-xxxx
													  newNum = num.substring(0,3) + '-' + num.substring(3,6) + '-' + num.substring(6,10);
													 $(this).val(newNum);
													$(this).next().remove(options.ErrorTextContainer) ;
													$(this).removeClass("error") ;
												 }
											   }
											  }
											break ;	// case 'text':
									}	// switch(objtype)
																
									
				}); 
				//-------------------------------------------
				if (StopExecution) {
					//e.preventDefault() ;
					//return false ;
				}	// if (!StopExecution) 	


				// Check Number Validation
				objUploadFile.each(function() {
					var objtype = $(this).attr('type') ;
					var objtitle = $(this).attr('title') ;
					
					switch(objtype) {
										case 'file':
										var InValidFile = false ;
										var currentObj = $(this) ;
										var fld_value = currentObj.val(); 	
										
										var FileType = currentObj.hasClass("img") ? "image" : (currentObj.hasClass("pdf") ? 'pdf' : '' ) ;
										
										
										if(FileType.toLowerCase()=="image")
										{
											if(!/(\.bmp|\.gif|\.jpg|\.jpeg|\.png)$/i.test(fld_value)) {
												InValidFile = true ;
											}
										}
										
										if(FileType.toLowerCase()=="pdf")
										{
											if(!/(\.pdf)$/i.test(fld_value)) {
												InValidFile = true ;
											}
										}

										
										if(InValidFile) {
											if(options.showErrorMessage)
													{
														$(this).addClass("error") ;
														if (options.showErrorText)
														{
															$(this).next().remove(options.ErrorTextContainer) ;
															$(this).after(options.ErrorBefore+ options.ErrorFileText+options.ErrorAfter)
														}
														//$(this).after('<span class="errortext">"'+address+'" is an invalid URL</span>')
													}else
														{ 
															  alert("\"" + objtitle + "\" is an invalid File Type");
														}
												  StopExecution = true
												  //return false;
										}//if(InValidFile) 
										else
										{
											$(this).next().remove(options.ErrorTextContainer) ;
											$(this).removeClass("error") ;
										}
										
					} //switch(objtype) {
							
					// Separat Function which is used on key press 
				}); 
				//-------------------------------------------
				if (StopExecution) {
					//e.preventDefault() ;
					return false ;
				}	// if (!StopExecution) 	

			}) ;
		});
	}
})(jQuery) ;


