
(function($) {
	$.fn.danielForm = function(options) {
		
		var defaults = {
			processUrl: '',
			form_id: 'form',
			btnTitle: 'SEND',
	    	btnColor: '#999',
			border: 'solid 1px #000',
			btnWidth: 4
        }; 
		
        var options = $.extend(defaults, options);
		
		return this.each(function() {
			obj = $(this);
//----------------------------------------------------------------------------------------------------------			
//test for msie 
	jQuery.each(jQuery.browser, function(i) {
		if($.browser.msie){
			msie = true;
  		}else{msie = false;};
	});

//----Initial Settings	
	validMsg = 'Required';
	gcheck = 'http://www.nu-hope.com/images/grfx/buttons/check.gif';
	redx = 'http://www.nu-hope.com/images/grfx/buttons/redx.gif';
	blankSqr = 'http://www.nu-hope.com/images/grfx/buttons/15pxBlank.gif';
	upArrow = 'http://www.nu-hope.com/images/grfx/buttons/upArrowWht.gif';
	downArrow = 'http://www.nu-hope.com/images/grfx/buttons/downArrowWht.gif';
	
	
	$('form').append('<div id="button" style="text-align:center;width:'+options.btnWidth+'em;font-size:12pt;padding:5px;background-color:'+options.btnColor+';color:white;cursor:pointer;margin-left:5px;border:'+options.border+'">'+options.btnTitle+'</div>');
	$('#button').hover(function(){
		$(this).css("fontWeight","bold");				
	},function(){
		$(this).css("fontWeight","normal");					
	});
	 
	
	$('.valid').parent().css("position","relative");
	
//add green checkmark and red X 	
	if(msie==false){//if NOT Internet Explorer:
		$('.valid').after('<img class="chkx" src="'+blankSqr+'" />');
	}else{//if internet explorer, make adjustments here:
		$('.valid').after('<img class="chkx" src="'+blankSqr+'" />');
	};

//--------------------------------------------Functions-------------------------	
	//---Basic Test for field Value
	function valueTest(thisObj){
		if(thisObj.is('.email')){
			var thisVal = thisObj.attr('value');
			var reg = new RegExp('^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+[\.]{1}\.[A-Za-z]{1,4}$');
       		if(!reg.test(thisVal)){
				if(thisObj.is('.valid')){
					thisObj.css("border","solid red 1px");
					thisObj.next('img').attr('src', redx);
				}
			}else{
				if(thisObj.is('.valid')){
					thisObj.css("border","solid #0F75BC 1px");
					thisObj.next('img').attr('src', gcheck);
				}
			}
		}else{
			var thisVal = thisObj.attr('value');		
			var thisImg = thisObj.next('img').attr('src');
			if(thisVal!=""){
				thisObj.css("border","solid #0F75BC 1px");
				if(thisImg!=gcheck&&thisObj.is('.valid')){
					thisObj.next('img').attr('src', gcheck);
				};
			}else{
				thisObj.css("border","solid red 1px");
				if(thisImg!=redx&&thisObj.is('.valid')){
					thisObj.next('img').attr('src', redx);
				};
			};
		};
	};
	//---Skip Test	
	function skipTest(thisObj){
		errorCount = 0;
		//get index of current field
		var thisIndex = $('.valid').index(thisObj);
		//alert(thisIndex);
		for(var i=0;i<thisIndex;i++){
			var thisField = $('.valid:eq('+i+')');
			var thisImg = thisField.next('img').attr('src'); 
			thisType = thisField.attr('type');
			if(thisType!='radio'&&!thisField.is('.email')){
				var thisVal = $('.valid:eq('+i+')').attr('value');			
				if(thisVal!=""){				
					thisField.css("border","solid #0F75BC 1px");
					if(thisImg!=gcheck&&thisField.is('.valid')){
						thisField.next('img').attr('src', gcheck);
					};					
				}else{
					if(thisImg!=redx&&thisField.is('.valid')){
						thisField.next('img').attr('src', redx);
					};
					thisField.css("border","solid red 1px");
					errorCount++;
				};
			}else if(thisField.is('.email')){// if is email field
				var thisVal = $('.valid:eq('+i+')').attr('value');
				var reg = new RegExp('^[A-Za-z0-9._%+-]+@[Aa-z-Z0-9.-]+[\.]{1}\.[A-Za-z]{1,4}$');
       			if(!reg.test(thisVal)){
					if(thisImg!=redx&&thisField.is('.valid')){
						thisField.next('img').attr('src', redx);
					};
					thisField.css("border","solid red 1px");
					errorCount++;
				}else{
					thisField.css("border","solid #0F75BC 1px");
					if(thisImg!=gcheck&&thisField.is('.valid')){
						thisField.next('img').attr('src', gcheck);
					};	
				};
			}else{//if Radiofield
				var thisName = thisField.attr('name');
				var thisField = $('input[name='+thisName+']');
				var thisCount = 0;
				$('input[name='+thisName+']').each(function(){thisCount++;});
				rvalid=false;
				for(var ri=0;ri<=thisCount&&rvalid==false;ri++){
					thisOne = $('input[name='+thisName+']:eq('+ri+')');
					if(thisOne.attr('checked')&&thisField.is('.valid')){
						thisField.next('img').attr('src', blankSqr);
						thisOne.next('img').attr('src', gcheck);
						rvalid=true;
					}else{
						rvalid=false;
						if(ri==thisCount&&rvalid==false){
							$('input[name='+thisName+']').next('img').attr('src', redx);;		
						errorCount++;
						};
						
					};
				};
				
			};
		};
	};
	
	$('.valid').focus(function(){
		var thisObj = $(this);
		skipTest(thisObj);
	});
	$('input[type=radio]').click(function(){
		var thisName = $(this).attr('name');
		$('input[name='+thisName+']').next('img').attr('src', blankSqr);
		$(this).next('img').attr('src', gcheck);									 
	});
	/*
//--Just Auto Tab - No validation
	$('input.noValid').each(function(){
		var maxL = $(this).attr('maxlength');
		if(maxL!=-1&&maxL!=2147483647&&maxL!=524288){
			$(this).bind('keyup', function(event){						   
				var autoCurL = $(this).attr('value').length;
				if(autoCurL < maxL){
				}else{
					$(this).parent().next('div').children('input.noValid').focus();
				};
			});
		};
	});
*/

//--Auto Tab and Validate while Typing-------
	$('input').each(function(){
		$(this).bind('keyup', function(event){						   
			if($(this).is('.autoTab')){
				var maxL = $(this).attr('maxlength');
				if(maxL!=-1&&maxL!=2147483647&&maxL!=524288){
					var autoCurL = $(this).attr('value').length;
					if(autoCurL < maxL){
						$(this).next('img').attr('src', redx);
						$(this).css("border","solid red 1px");
					}else{
						$(this).css("border","solid #0F75BC 1px");
						$(this).next('img').attr('src', gcheck).next('input.autoTab').focus();
					};
				}
			}else{
				var thisObj = $(this);
				valueTest(thisObj);
			}
		});
	});	
		
	$('select.valid').bind('change', function(){
		thisObj = $(this);
		valueTest(thisObj);							   
	});
	
//---Validate for submitting
	$('#button').click(function(){
		var thisObj = $('.valid:last');	
		skipTest(thisObj);
		if(errorCount==2 || errorCount==0){
		$('form').trigger('submit')
	}else{
		alert(errorCount+' required fields seem to have been skipped\n\n A Red X has been placed next to each missed field.\n\nIf you are not sure what to enter in a field, just make your best guess and then use the comment area to better explain');
		};
	});
	
	$('#closeBtn').live('click',function(){window.close()});
	
	$('form').submit(function(){
		var formData = $(this).serialize();
		$.get(options.processUrl,formData,processData);
		function processData(data){
			//alert(data)
      		//if (data=='ok') {
				data1=1;
				if (data1==1) {
         		$('#'+options.form_id).html('<div style="margin-top:100px;text-align:center;"><strong>THANK YOU!</strong><p>&nbsp;</p>Your Sample Request has been successfuly submitted.  We will review your information and contact you shortly.</div><p></p><div id="closeBtn" style="color:#FFF;padding:3px;width:150px;text-align:center;border:solid 1px #CCCCCC;background:url(../images/grfx/buttons/nh_navbutton_bgDwn01.gif) bottom repeat-x;cursor:pointer; margin-left:auto; margin-right:auto;">Close Window</div>');
				
				$('#closeBtn').hover(function(){
					$(this).css("background","url(../images/grfx/buttons/nh_navbutton_bgOvr01.gif) bottom repeat-x");
				},function(){
					$(this).css("background","url(../images/grfx/buttons/nh_navbutton_bgDwn01.gif) bottom repeat-x");
				});
				
      		} else { 
          		$('#'+options.form_id).html('<div style="margin-top:100px;text-align:center;"><p>Sorry, an error has occured while attempting to process your request.  Please try again.</p></div>');
     		 };
    	}; // end processData
    	return false;
	});
	
//----------------------------------------------------------------------------------------------------------			
		});
	};		
})(jQuery);				
