String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
$(function (){

var inputFieldsArray = $(":input:not(:hidden)");

for(var i=0; i<inputFieldsArray.size(); i++)
{
	
	var liClass = $(inputFieldsArray[i]).parents("li").attr("class");
	
	if (liClass == "wcgRequired" || liClass == "wcgRequired floatLeft") 
	{
		
		if ($(inputFieldsArray[i]).attr("class") != 'wcgFieldNotRequired')
			{
				$(inputFieldsArray[i]).attr("wcgFieldType", "required");
					if ((($(inputFieldsArray[i]).attr("value") == null)) || ($(inputFieldsArray[i]).attr("value").trim().length == 0)) 
					{
						$(inputFieldsArray[i]).attr("wcgFieldState", "empty");
					}
					else 
					{
					$(inputFieldsArray[i]).attr("wcgFieldState", "notEmpty");
					}
				
			}
		
		 else 
			{
					$(inputFieldsArray[i]).attr("wcgFieldType", "notRequired");
					$(inputFieldsArray[i]).attr("wcgFieldState", "unknown");
			}
		
	}
	
	else 
		{
		
			$(inputFieldsArray[i]).attr("wcgFieldType", "notRequired");
			$(inputFieldsArray[i]).attr("wcgFieldState", "unknown");
		}

}

$(".checkFields, .buttonWide").click
	(
		function() {		
		
					var title= escape($('#_fid_17').val());		
					var comments = escape($('#comments').val());	
					var bus_name = escape($('#bus_name').val());	
					var inputFieldsArray = $(":input:not(:hidden)");
					var requiredButEmptyCount = 0;
					  for(var i = 0; i < inputFieldsArray.size(); i++)
						{
					 	   var liClass = $(inputFieldsArray[i]).parents("li").attr("class");			 	
			              
						     if (($(inputFieldsArray[i]).attr("value") == undefined)||($(inputFieldsArray[i]).attr("value") == null)||($(inputFieldsArray[i]).attr("value").trim().length == 0) )
						     
						      {
						
							  if (($(inputFieldsArray[i]).attr("name")!= undefined) ||($(inputFieldsArray[i]).attr("name")!= null))
							  {
							
									
								if (liClass == "wcgRequired wcgErrorRow" ||liClass == "wcgRequired")
								{
									
									if ($(inputFieldsArray[i]).attr("class") != 'wcgFieldNotRequired')
										{							
											$(inputFieldsArray[i]).attr("wcgFieldType", "required");
											$(inputFieldsArray[i]).attr("wcgFieldState", "empty");
											$(inputFieldsArray[i]).parents("li").addClass("wcgErrorRow");
											requiredButEmptyCount++;
										}
									else
										{					
											$(inputFieldsArray[i]).attr("wcgFieldState", "notEmpty");
											$(inputFieldsArray[i]).parents("li").removeClass("wcgErrorRow");
										}
										
								}
								else
								{
									$(inputFieldsArray[i]).attr("wcgFieldType", "notRequired");
									$(inputFieldsArray[i]).attr("wcgFieldState", "unknown");
								}	 			
							  }
							  else
							  {
								$(inputFieldsArray[i]).attr("wcgFieldType", "notRequired");
							 	$(inputFieldsArray[i]).attr("wcgFieldState", "unknown");
							  }			      
						     }
						}
						if (requiredButEmptyCount > 0)
						{
					
							$(".wcgError2").fadeIn(1500);
							if (requiredButEmptyCount < 2)
							{
								$(".wcgError2 span").html(requiredButEmptyCount + ' field. It has been highlighted below.');
							}
							else
							{
								$(".wcgError2 span").html(requiredButEmptyCount + ' fields. They have been highlighted below.');
							}
			
							window.scrollTo(0, 0);
							return false;
						
						}
									 
		}
		
);


$("#wcgPhoneNumber").blur
	(
		function() {
		//alert("Inside phone");
		var phone = $(this).attr("value").trim();
		var fieldType = $(this).attr("wcgfieldtype").trim();
		phone=phone.replace(/[^\d]/g, "");
		if(phone.length == 10){
			phone=phone.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3");
			$(this).parents("li:first").removeClass("wcgError");
			return true;
			}
		else if (fieldType=="required")
		{
		$(this).parents("li:first").addClass("wcgError");
		//alert("you have entered an invalid phone number");
		}
		$(this).attr("value", phone);

		}
		
		);	
			


$("#wcgEmailId").blur
(
	function(){
	//alert("Inside e-mail");
	var x = this.value.trim();
	var filter = /^([a-zA-Z0-9_\.\-\'])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) // Passed or empty
	{
		$(this).parents("li").removeClass("wcgErrorRow");
		return true;
	}
	else // Failed
	{	
		$(this).attr("value", x);
		$(this).parents("li").addClass("wcgErrorRow");
	}
	
    }
);	

$("#wcgEmailFB").blur
(
	function(){
	//alert("Inside e-mail");
	var x = this.value.trim();
	var filter = /^([a-zA-Z0-9_\.\-\'])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)||(x=="")) // Passed or empty
	{
		$(this).parents("li").removeClass("wcgErrorRow");
		return true;
	}
	else // Failed
	{	//alert("inside else");
		$(this).attr("value", x);
		$(this).parents("li").addClass("wcgErrorRow");
	}
	
	}
);	
$("#wcgPhoneNumberFB").blur
	(
		function() {
		//alert("Inside phone");
		var phone = $(this).attr("value").trim();
		var fieldType = $(this).attr("wcgfieldtype").trim();
		phone=phone.replace(/[^\d]/g, "");		
		if((phone.length == 10) ||(phone.length=="")){
			phone=phone.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3");
			$(this).parents("li").removeClass("wcgErrorRow");
			return true;
			}
		else 
		{		
		$(this).parents("li").addClass("wcgErrorRow");
		//alert("you have entered an invalid phone number");
		}
		$(this).attr("value", phone);

		}
		
		);	
			

$("#calDate").change
(

	function(){
	var calDate = $("#calDate");
		$(calDate).removeClass("wcgErrorRow");
	}
);

	$(":input").focus
	(
		function(){
			$(this).parents("li").removeClass("wcgErrorRow");
			$(this).parents("li").removeClass("wcgError");
			
		}
	);
	
		// Check if field is required
	// This should be set at the end of input field processing pipeline
	$(":input").blur
	(
		function(){
			if ($(this).attr("wcgFieldType") == "required"){
				if($(this).attr("value") == null){ // IE
					$(this).attr("wcgFieldState", "empty");
					$(this).parents("li").addClass("wcgErrorRow");
				}
				else if($(this).attr("value").trim().length == 0){
					$(this).attr("wcgFieldState", "empty");
					$(this).parents("li").addClass("wcgErrorRow");
				}else if($(this).attr("value").trim().length != 0){
					escape($(this).attr("value"));
					$(this).parents("li").removeClass("wcgErrorRow");
					$(this).attr("wcgFieldState", "notEmpty");
				}
			}else{
				$(this).attr("wcgFieldState", "unknown");
			}
		}
	);
	$(".checkRadio, .buttonWide").click
	(
		function() {
			/*Adding changes for ELMO starts here */					
			var requiredButEmptyCount = 0;	
			var checked = false;			  					  	
		  	var check_service_id = $("input[name='selected_check_service_id']");		  	
			var merchant_exist_id = $("input[name='is_existing_merchant']");
			var selected_hardware_ids = $("input[name='selected_hardware_ids']");
			var selected_hardware_own_type = $("input[name='selected_hardware_own_type']");			
		  	var count = $("input[name='selected_check_service_id']").length;
		  	var count1 = $("input[name='is_existing_merchant']").length;	
		  	var count2 = $("input[name='selected_hardware_ids']").length;
		  	var count3 = $("input[name='selected_hardware_own_type']").length;
		  	 for (var j=0;j<count; j++){
		  		 if (document.bundles.selected_check_service_id[j].checked)
		  		 {							  			
		  			checked = true;
		  		 }							  		 							  		 
		  	 }
		  	if (!checked) {		  		
		  		$(check_service_id).parents("li").addClass("wcgErrorRow");		  		
		  		requiredButEmptyCount++;
		  	}
		  	checked = false;							  	 
		  	 for (var j=0;j<count1; j++){
		  		 if (document.bundles.is_existing_merchant[j].checked)
		  		 {							  			
		  			checked = true;							  			
		  		 }							  		 
		  	 }	
		  	if (!checked) {		  		
		  		$(merchant_exist_id).parents("li").addClass("wcgErrorRow");		  		
		  		requiredButEmptyCount++;
		  		
		  	}	
		  	
		  	if (count2 >0){		  		
		  		checked = false;
		  		for (var j =0;j<count2;j++){
		  			if (document.bundles.selected_hardware_ids[j].checked)
			  		 {							  			
			  			checked = true;
			  		 }	
		  		}
		  		if (!checked) {		  		
		  			document.getElementById('selected_hardware_ids').style.color = 'red';	//just tried if it would work. It didnt
			  		requiredButEmptyCount++;
		  		}
				 if (count3 >1){
					 checked = false;
				  		for (var j =0;j<count3;j++){
				  			if (document.bundles.selected_hardware_own_type[j].checked)
					  		 {							  			
					  			checked = true;
					  		 }	
				  		}
				  		if (!checked) {		  		
					  		$(selected_hardware_own_type).parents("li").addClass("wcgErrorRow");	
					  		requiredButEmptyCount++;
				  		}				 
				 }
		  	}  						  									  						
			if (requiredButEmptyCount > 0)
			{
		
				$(".wcgError2").fadeIn(1500);
				if (requiredButEmptyCount < 2)
				{
					$(".wcgError2 span").html(requiredButEmptyCount + ' field. It has been highlighted below.');
				}
				else
				{
					$(".wcgError2 span").html(requiredButEmptyCount + ' fields. They have been highlighted below.');
				}

				window.scrollTo(0, 0);
				return false;
			
			}		
		}
	); /*changes end here */	
	
});


