/*
 * JavaScript validation v1.0
 * http://ubm-global.com/
 * Author: Vivekanand Mishra
 * Date: 2009-07-22 17:34:21 -0500 (wed, 22 July 2009)
 * Revision: 1
 */
 if (!this.FormValiditor){
	
	var FormValiditor = function(){
				
		return {
			/*
			 * Example of calling 
			 * if(!FormValiditor.filterFormInput('Numeric', 'filedID'))
			 * {
			 *  	alert("Unsupported characters in Custom: Pieces field!\n Enter only numeric values.");
			 *		return false;
			 *	}
			 */
			filterFormInput: function(filterType, fieldID) {
				var inputString = $('#'+fieldID).val();				
				var strValidChars = "";
				var message = "";
				
				switch(filterType)
				{				
				case "Email":
				  strValidChars = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				  message = "only proper email (abc@xyz.com).";
				  break;
				

				case "Numeric":
				  strValidChars = /^[0-9]+$/;
				  message = "only valid numeric value.";
				  break;
				case "AlphaNumeric":
				  strValidChars = /^[a-zA-Z0-9 ]+$/;
				  message = "only alphanumeric text; space characters allowed.";
				  break;
				case "AlphaNumeric-Space":
				  strValidChars = /^[a-zA-Z0-9]+$/;
				  message = "only alphanumeric text.";
				  break;
				case "AlphaNumericComma":
				  strValidChars = /^[a-zA-Z0-9\,]+$/;
				  message = "only alphanumeric text; comma characters allowed.";
				  break;
				case "Float":
				  strValidChars = /^[0-9\.]+$/;
				  message = "only float value.";
				  break;
				  case "Phone":
				  strValidChars = /^[0-9-()]+$/;
				  message = "only valid phone number.";
				  break;
				 
				}						
				
				if (!inputString.match(strValidChars)) 
				{
					return message;
				}
				else
				{
					return false;
				}
			},
			checkBlankField: function(fieldID) {
				var field_label = fieldID.replace(/_/g, " ");
				
				if(!$('#'+fieldID).val().replace(/^\s+|\s+$/g,""))
				{
					return "The "+field_label+" field cannot be blank!";
					
				}
				return false;
			},
			checkFormFields: function(fieldsAndChecks, ID) {			
				var fields = fieldsAndChecks.split(",");
				
				for(var i=0; i<fields.length; i++)
				{
					var fields_validationType = fields[i].split(":");					
					var field = fields_validationType[0];					
					var validationTypes = fields_validationType[1].split("+");
					
						for(var j=0; j<validationTypes.length; j++)
						{  
							var field_label = field.replace(/_/g, " ");
						
							switch(validationTypes[j])
							{
								case "3rdPartyEmail":
									var splitedEmail = document.getElementById(field).value.split('@');
									var emailNotallowed = ['gmail.com', 'yahoo.com', 'msn.com'];
									if(in_array(splitedEmail[1],emailNotallowed)===false)
									{
										alert("3rd party email ID's (@gmail @yahoo @msn etc) are not allowed, please use yourname@yourcompanydomainname");
										$('#'+field).focus();
										return false;
									}
								  break;
							case 'Required':															
								if(!$('#'+field).val().replace(/^\s+|\s+$/g,""))
									{
										if($('#'+field).get(0).tagName == "SELECT")
										{
											alert("Select appropriate "+field_label+" from the list!");
										}else if($('#'+field).attr('type') == "file")
										{
											alert("The "+field_label+" must be uploaded!");
										}
										else if($('#'+field).attr('type') == "text")
										{
											alert("The "+field_label+" field cannot be blank!");
										}
										else
										{
											alert("The "+field_label+" field cannot be blank!");
										}
										$('#'+field).focus();
										return false;
									}							
							break;
							case 'Selected':
								var field_value = document.getElementById(field).value;
								if(field_value == '')
								{
									alert("The "+field_label+" field should be selected!");
									return false;
								}
								break;
								case 'Clickable':
								var field_value = document.getElementById(field).value;
								if(field_value == '' || field_value == '-')
								{
									alert("Please click on country and then submit a button.");
									return false;
								}
								break;
								
							case 'Unique':
								var getMessage = this.checkFieldAvailability(field, ID);								
								if(getMessage) {  return false;}
								break;
							case 'Length':
								if($('#'+field).val().length < 2)
								{

									alert("The "+field_label+" should contain atleast "+ID+" characters!!");
									$('#'+field).focus();
									return false;
								}
							case 'OptionalURL':
								if($('#'+field).val()!=''){	
									var tomatch= /http:\/\/[A-Za-z0-9\.-]{2,}\.[A-Za-z]{2}/
									var tomatch_secure= /https:\/\/[A-Za-z0-9\.-]{2,}\.[A-Za-z]{2}/
									if (!tomatch.test($('#'+field).val()) && !tomatch_secure.test($('#'+field).val()))
									 {
										alert("The "+field_label+" field should contain valid url!");
										$('#'+field).focus();
										return false;
									 }									
									
								}
							case 'OptionalRadioRequired':
								if($('#Related_Type').val()=='video'){				
									if($("#from_file").is(':checked') == false && $("#from_link").is(':checked') == false && $("#from_embed").is(':checked') == false){
										alert("Please select type.");
										$('#'+field).focus();
										return false;
									 }else if($('#Related_File').val()=='' && $("#from_file").is(':checked')){
										alert("Please select related file.");
										$('#'+field).focus();
										return false;
									 }else if($('#file_link').val()=='' && $("#from_link").is(':checked')){					alert("Please enter link.");
										$('#'+field).focus();
										return false;
									 }else if($('#embed_code').val()=='' && $("#from_embed").is(':checked')){
										alert("Please enter video's embed code");
										$('#'+field).focus();
										return false;
									 }
								  }	
								  else{
									if($('#Related_Article').val()=='' && $('#Related_Type').val()=='article'){
										alert("Please select related article.");
										$('#'+field).focus();
										return false;
									}else if($('#Related_File').val()=='' && $('#Related_Type').val()!='article'){
										alert("Please select related file.");
										$('#'+field).focus();
										return false;
									}
								  }

								break;	
							default:
							
								var getMessage = this.filterFormInput(validationTypes[j], field);
								if(getMessage) {alert("The "+field_label+" field should contain "+getMessage); 
								
								$('#'+field).focus();
								return false;}
							}
						}
					}
			return true;
			},
			checkAvailability: function(fieldID) {				
				var emailID = $("#"+fieldID).val();				
				$.ajax({
					 type: "GET",
					 url: "ajax-process.php?email="+emailID,
					 success: function(msg){
						alert( msg );
						}
				 });
			return true;
			},checkAccountAvailability: function(fieldID) {				
				var emailID = $("#"+fieldID).val();				
				$.ajax({
					 type: "GET",
					 url: "front-ajax.php?email_address="+emailID,
					 success: function(msg){
						alert( msg );
						}
				 });
			return true;
			},			
			checkFieldAvailability: function(fieldID, ID) {				
				var fieldVal = $("#"+fieldID).val();
				var status_message = "";
				if(!ID) ID=0;
				$.ajax({
					 type: "GET",
					 async: false,
					 url: "ajax-process.php?unique="+fieldID+"&value="+fieldVal+"&id="+ID,
					 success: function(msg){						
						status_message = msg;
						if(msg)
						 {
							alert( msg );
							
						 }						 
						}
				 });
			return status_message;
			}, 
			checkTranslationForm: function(langFieldID, typeFieldID, status,begining) {				
				var langID = $("#"+langFieldID).val();
				var typeVal = $("#"+typeFieldID).val();
				var contentStatus = $("#"+status).val();
				var page = 1;
				
					if($("#page_id").val())
					{
						var page = $("#page_id").val();
						
					}
				if(begining == 'start')
				{
					var page = 1;
				}
				
				if(!langID) {alert("Select appropriate language!\n The Language and Content Type both are necessary.");
							 $("#"+langFieldID).focus();
							 return false}
				if(!typeVal) {alert("Select appropriate content type!\n The Language and Content Type both are necessary.");
							 $("#"+typeFieldID).focus();
							 return false}
				$.ajax({
					 type: "GET",
					 url: "../ajax-process.php?languageID="+langID+"&content="+typeVal+"&status="+contentStatus+"&page="+page,
					 success: function(msg){
						$("#publish-error-message").hide();
						$("#publish-success-message").hide();
						$("#loadPageContent").html( msg );
						}
				});
			return true;
			}
		};		
	}();
}


function in_array (needle, haystack, argStrict) {  
    var key = '', strict = !!argStrict; 

        for (key in haystack) {
            if (haystack[key] == needle) { 
				return false;
            }
        }

     //return true;
}
