(function($){ 
	$.fn.validate = function (options) {

		var defaults = {
							requiredClassName: 'required',
							emailClassName: 'email',
							numClassName: 'number',
							urlClassName: 'url',
							phoneClassName: 'phone',
							editorClassName: 'reqeditor',
							imgClassName: 'uploadfile',
							passwordClassName: 'matchpassword',
							uniqueClassName: 'unique',

							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',
							ErrorClass: 'error',

							ErrorRequiredText: 'This field is required.',
							ErrorEmailText: 'Please enter a valid email address.',
							ErrorURLText: 'Please enter a valid URL.',
							ErrorFileText: 'Invalid File.', 
							ErrorConfirmPassText: 'Password mismatched.',
							ErrorUniqueText: 'Selected value not available.' 
						}

		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 objConfirmPass	= $("#"+ form.attr("id") +" ."+ options.passwordClassName) ;
				var objUnique	= $("#"+ form.attr("id") +" ."+ options.uniqueClassName) ;

				var StopExecution = false ;
				
				// Check Required 
				objRequired.each(function() {
					var objtype = $(this).attr('type') ;
					var objtitle = $(this).attr('title') ;

					switch(objtype) {
										case 'text':
										case 'password':
										case 'select-one':
										case 'textarea':
//											alert('y') ;
//										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(options.ErrorClass) ;
													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(options.ErrorClass) ;
											}
											break ;
										case 'select-multiple':
											var ValSelected = false ;
											$('#'+$(this).attr('id')+" :selected").each(function (i, selected) {
												ValSelected = true ;
											}) ;
											if (!ValSelected)
											{
												if(options.showErrorMessage)
												{
													$(this).addClass(options.ErrorClass) ;
													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(options.ErrorClass) ;
											}
											break ;
									}	// switch(objtype)
				}); 
				if (StopExecution) {
					//e.preventDefault() ;
					if(!options.showErrorMessage) 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(address != '' && reg.test(address) == false) {
													if(options.showErrorMessage)
													{
														$(this).addClass(options.ErrorClass) ;
														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;
											   }	// if(address != '' && reg.test(address) == false)
											   else
												{
													if (!$(this).hasClass(options.requiredClassName))
													{
														$(this).next().remove(options.ErrorTextContainer) ;
														$(this).removeClass(options.ErrorClass) ;
													}
												}
											break ;
									}	// switch(objtype)
																
									
				}); 
				//-------------------------------------------
				if (StopExecution) {
					//e.preventDefault() ;
					if(!options.showErrorMessage) return false ;
				}	// if (!StopExecution) 	
				// Check Number Validation
				objnumber.each(function() {
					// Separat Function which is used on key press 
				}); 
				//-------------------------------------------
				if (StopExecution) {
					//e.preventDefault() ;
					if(!options.showErrorMessage) return false ;
				}	// if (!StopExecution) 	
				
				// EDITOR VALIDATION STARTS 
				objEditor.each(function() {
					var ins_name = $(this).attr('name') ;
					var fieldname = $(this).attr('title') ;
//					alert(ins_name) ;
					switch(fieldname) {
						default:
							var oEditor = FCKeditorAPI.GetInstance(ins_name);
//							alert(oEditor.GetXHTML(true)) ;
							var content = oEditor.GetXHTML(true)
							if(content=='' || content=='<br />' ) {
								if(options.showErrorMessage)
								{
									$(this).addClass(options.ErrorClass) ;
									if (options.showErrorText)
									{
										$(this).next().remove(options.ErrorTextContainer) ;
										$(this).after(options.ErrorBefore+ options.ErrorRequiredText+options.ErrorAfter)
									}
									//$(this).after('<span class="errortext">"'+address+'" is an invalid URL</span>')
								}
								else
								{ 
									alert("\"" + address + "\" is an invalid URL");
								}
								StopExecution = true ;
								//return false ;
							}	//if(content=='')
						break ;
					}
				})	// objEditor.each
				//-------------------------------------------
				if (StopExecution) {
					//e.preventDefault() ;
					if(!options.showErrorMessage) 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(address != '' && reg.test(address) == false) {
  												    if(options.showErrorMessage)
													{
														$(this).addClass(options.ErrorClass) ;
														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;
											   }
											break ;
									}	// switch(objtype)
																
									
				}); 	// objurl.each
				//-------------------------------------------
				if (StopExecution) {
					//e.preventDefault() ;
					if(!options.showErrorMessage) 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
											break ;
									}	// switch(objtype)
																
									
				}); 	// objphone.each
				//-------------------------------------------
				if (StopExecution) {
					//e.preventDefault() ;
					if(!options.showErrorMessage) 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 oldfile_value = currentObj.attr('rel'); 	
										
										var isRequired = currentObj.hasClass(options.requiredClassName) ;
										if (!isRequired)
										{
											var VerifyValue = !(fld_value == '');	
										}
										else
										{
											var EmptyField = (oldfile_value == '' && fld_value == '') ;

											if (oldfile_value == '' && fld_value == '')
											{
												var VerifyValue = true ;
											}
											else if (oldfile_value != '' && fld_value == '')
											{
												var VerifyValue = false ;
											}
											else if (oldfile_value != '' && fld_value != '')
											{
												var VerifyValue = true ;
											}
											else if (oldfile_value == '' && fld_value != '')
											{
												var VerifyValue = true ;
											}
										}
										var FileType = currentObj.hasClass("img") ? "image" : (currentObj.hasClass("pdf") ? 'pdf' : '' ) ;
										
										
										if(FileType.toLowerCase()=="image" && VerifyValue)
										{
//										alert('YES')  ;
											if(!/(\.bmp|\.gif|\.jpg|\.jpeg|\.png)$/i.test(fld_value)) {
												InValidFile = true ;
											}
										}
										
										if(FileType.toLowerCase()=="pdf" && VerifyValue)
										{
											if(!/(\.pdf)$/i.test(fld_value)) {
												InValidFile = true ;
											}
										}

										
										if(InValidFile) {
//										alert(options.showErrorMessage)  ;
											if(options.showErrorMessage)
													{
//														alert('yes error') ;
														$(this).addClass(options.ErrorClass) ;
														if (options.showErrorText)
														{
															$(this).next().remove(options.ErrorTextContainer) ;
															$(this).after(options.ErrorBefore+(EmptyField ? options.ErrorRequiredText : options.ErrorFileText)+options.ErrorAfter)
														}
														//$(this).after('<span class="errortext">"'+address+'" is an invalid URL</span>')
													}else
														{ 
															if (EmptyField)
															{
																alert('Please make sure that '+objtitle+' field was properly compelted.') ;	
																
															}
															else
															  alert("\"" + objtitle + "\" is an invalid File Type");
														}
												  StopExecution = true
												  //return false;
										}//if(InValidFile) 
										else
										{
											$(this).next().remove(options.ErrorTextContainer) ;
											$(this).removeClass(options.ErrorClass) ;
										}
//										StopExecution = true ;
//										return false ;

					} //switch(objtype) 
							
					// Separat Function which is used on key press 
				}); 

				//-------------------------------------------
				objConfirmPass.each(function() {
					var objtype = $(this).attr('type') ;
					var objtitle = $(this).attr('title') ;
					switch(objtype) {
						case "password":
						case "text":
							var ConfirmValObj = $(this).attr('rel') ;
							if ($('#'+ConfirmValObj).val() != '' && $(this).val() != '' && $('#'+ConfirmValObj).val() != $(this).val()) 
							{
								if(options.showErrorMessage)
								{
									$(this).addClass(options.ErrorClass) ;
									if (options.showErrorText) {
										$(this).next().remove(options.ErrorTextContainer) ;
										$(this).after(options.ErrorBefore+ options.ErrorConfirmPassText+options.ErrorAfter) ;
									}
									//$(this).after('<span class="errortext">"'+address+'" is an invalid e-mail!</span>')
								}
								else
								{ 
									  alert("\"" + address + "\" is an invalid e-mail!");
								}
								StopExecution = true
							}
							else
							{
								if (!$(this).hasClass(options.requiredClassName))
								{
									$(this).next().remove(options.ErrorTextContainer) ;
									$(this).removeClass(options.ErrorClass) ;
								}
							}

							break ;
					}	// switch(objtype)
				}); 

				//-------------------------------------------

				objUnique.each(function() {
					var objtype		= $(this).attr('type') ;
					var objtitle	= $(this).attr('title') ;
					var funname		= $(this).attr('rel') ;
					switch(objtype)
					{
						case 'text':
							if (!eval(funname+'("'+$(this).val()+'")'))
							{
								if(options.showErrorMessage)
								{
									$(this).addClass(options.ErrorClass) ;
									if (options.showErrorText) {
										$(this).next().remove(options.ErrorTextContainer) ;
										$(this).after(options.ErrorBefore+ options.ErrorUniqueText+options.ErrorAfter) ;
									}
								}
								else
								{
									alert("\"" + address + "\" is an invalid e-mail!");
								}

								StopExecution = true ;
								$(this).focus() ;

							}
							else
							{
								if (!$(this).hasClass(options.requiredClassName))
								{
									$(this).next().remove(options.ErrorTextContainer) ;
									$(this).removeClass(options.ErrorClass) ;
								}
							}
							break ;
					}	// switch(objtype)
				}); 	// objUnique.each
				if (StopExecution) {
					//e.preventDefault() ;
					if(!options.showErrorMessage) return false ;
				}	// if (!StopExecution) 	

				//-------------------------------------------
				if (StopExecution) {
					//e.preventDefault() ;
					return false ;
				}	// if (!StopExecution) 	

				//-------------------------------------------

			}) ;		// form.submit
		});	// return this.each
	}		// $.fn.validate = function (options) 
})(jQuery) ;


