// File by webNeat : webneat@gmail.com
$(document).ready(function(){
// Table Sorter    
     $("#myTable").tablesorter();
// Smart Wizard Starts Here    	
  		$('#wizard').smartWizard({transitionEffect:'slideleft',
		keyNavigation: false,
          onLeaveStep:leaveAStepCallback,
          onFinish:onFinishCallback,
          enableFinishButton:false});

      function leaveAStepCallback(obj){
        var step_num= obj.attr('rel');
        return validateSteps(step_num);
      }
      
      function onFinishCallback(){
       if(validateAllSteps()){
        $('form').submit();
       }
      }
            
		});
        
// Smart Wizard Starts Here    	
    function validateAllSteps(){
       var isStepValid = true;
       
       if(validateStep1() == false){
         isStepValid = false;
         $('#wizard').smartWizard('setError',{stepnum:1,iserror:true});         
       }else{
         $('#wizard').smartWizard('setError',{stepnum:1,iserror:false});
       }
       
       if(validateStep2() == false){
         isStepValid = false;
         $('#wizard').smartWizard('setError',{stepnum:2,iserror:true});         
       }else{
         $('#wizard').smartWizard('setError',{stepnum:2,iserror:false});
       }
	          
       if(!isStepValid){
         // $('#wizard').smartWizard('showMessage','Please correct the errors in the steps and continue');
       }
              
       return isStepValid;
    } 	
		
		
		function validateSteps(step){
		  var isStepValid = true;
      // validate step 1
      if(step == 1){
        if(validateStep1() == false ){
          isStepValid = false; 
          //$('#wizard').smartWizard('showMessage','Please correct the errors in step'+step+ ' and click next.');
          $('#wizard').smartWizard('setError',{stepnum:step,iserror:true});         
        }else{
          $('#wizard').smartWizard('setError',{stepnum:step,iserror:false});
        }
      }
	  
	  // validate step 2
      if(step == 2){
        if(validateStep2() == false ){
          isStepValid = false; 
         // $('#wizard').smartWizard('showMessage','Please correct the errors in step'+step+ ' and click next.');
          $('#wizard').smartWizard('setError',{stepnum:step,iserror:true});         
        }else{
          $('#wizard').smartWizard('setError',{stepnum:step,iserror:false});
        }
      }
	  
	  if(step == 3){
        if(validateStep3() == false ){
          isStepValid = false; 
         // $('#wizard').smartWizard('showMessage','Please correct the errors in step'+step+ ' and click next.');
          $('#wizard').smartWizard('setError',{stepnum:step,iserror:true});         
        }else{
          $('#wizard').smartWizard('setError',{stepnum:step,iserror:false});
        }
      }

      return isStepValid;
    }
		
		function validateStep1(){
       var isValid = true; 
	   
       // Validate Title
       var title = $('#title').val();
       if(!title && title.length <= 5){
         isValid = false;
         $('#msg_title').html('Please fill the title').show();
       }else{
         $('#msg_title').html('').hide();
		 $('td#show_title').html(title);
       }
       // Validate url
       var url = $('#url').val();
	   if(/^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z]|\d|-|\.|_|~)|[!\$&'\(\)\*\+,;=]|:)+))\])|((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=])*)(:\d*)?)(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*|(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)){0})(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(url)) {
		  $('#msg_url').html('').hide();
		  $('td#show_url').html(url);
		} else {
		  isValid = false;
         $('#msg_url').html('Please insert a valid URL').show();
		}
	   // Validate lcond
       var lcond = $('#lcond').val();
       if(!lcond || lcond.length <= 5){
         isValid = false;
         $('#msg_lcond').html('Please fill the least condition ').show();
       }else{
         $('#msg_lcond').html('').hide();
		 $('td#show_lcond').html(lcond);
       }
	   // Validate value
       var value = $('#value').val();
	   value = Math.floor(value);
       if(!value){
         isValid = false;
         $('#msg_value').html('Value is an integer').show();
       }else{
		$('#value').val(value);
         $('#msg_value').html('').hide();
		 $('td#show_value').html(value);
       }
		var cat = $('#cat').val();
	   $('td#show_cat').html(cat);
	   var invest = $('#invest').val();
	   $('td#show_invest').html(invest);
	  
	  return isValid;
    }
	
    
    function validateStep2(){
      var isValid = true;   	  
      var instr = $('#instr').val();
	  instr = instr.replace(/(<([^>]+)>)/ig,"");
       if(!instr || instr.length<10){
         isValid = false;
         $('#msg_instr').html('Please fill the instructions ').show();
       }else{
         $('#msg_instr').html('').hide();
		 $('td#show_instr').html(instr);
       }
	return isValid;
    }
	
	function validateStep3(){
      var isValid = true;  
		var gender = $('#gender').val();
	   $('td#show_gender').html(gender);
	   var age = $('#age').val();
	   $('td#show_age').html(age);
	   var prem = $('#prem').val();
	   $('td#show_prem').html(prem);

	var setting = $("input[name='setting']:checked").val();
	$('td#show_contries').html("");
	switch(setting){
		case "all" :
			$('td#show_contries').html("All countries");
		break;
		case "region" :
			$("input[type='checkbox']:checked").each(
			function(){
			$('td#show_contries').append($(this).val()+"<br />");
			});		
		break;
		case "en_skeap" :
			$('td#show_contries').html("English Speaking Countries");
		break;
		case "usuk" :
			$('td#show_contries').html("US/UK/CA/AUS");
		break;
		case "manual" :
			$("#dest option").each(
			function(){
			$('td#show_contries').append($(this).val()+"<br />");
			});		
		break;
		}
	
	   return isValid;
    }
 // Smart Wizard Ends Here    	

