/*
 * SmartWizard
 * a javascript wizard control
 * http://tech-laboratory.blogspot.com
 * 
 * Date: 04-AUG-2009
 * Version: 0.98
 */
 var ASF = false;

function validateEmailv2(email)
{
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}

function ANPASSWORD(objValue)
{
	var charpos = objValue.search("[^A-Za-z0-9]");

	if(objValue.length > 0 && charpos == -1) 
	{ 
		return true;
	}
	else
	{
		return false;
	}
}
 
(function($){  
    $.fn.smartWizard = function(options) {
        var defaults = {
              selectedStep: 0,  // Selected Step, 0 = first step
              errorSteps:[],    // Array Steps with errors
              enableAll:false,  // Enable All Steps, true/false
              animation:true,   // Animation Effect on navigation, true/false         
              validatorFunc:function(){return true;} // Step validation function, Step index will be passed
        };
        var options = $.extend(defaults, options);  
      
        return this.each(function() {
                obj = $(this); 
                var wizcurrent = 0;
      		      var steps = $("ul > li > a", obj);
      		      // Apply Default Style to Steps
      		      applyCSS($(steps, obj),"wiz-anc-default");
      		      // Hide All Steps on load
                hideAllSteps();
      		      
                $(steps, obj).bind("click", function(e){
                    e.preventDefault();
                    var isDone = $(this, obj).attr("isDone");
                    if(isDone == 1){
                        var selIdx = steps.index(this);  
                        applyCSS($(wizcurrent, obj),"wiz-anc-done"); 
                        selectStep(selIdx);
                    }
                });
                // activates steps up to the selected step
                for(i=0;i<steps.length;i++){
                  if(i<=options.selectedStep || options.enableAll==true){
                    activateStep(i);
                  }
                }
                // highlight steps with errors
                $.each(options.errorSteps, function(i, n){
                  //applyCSS(steps.eq(n),"wiz-anc-error");
                  setErrorStep(n)
                });
                
      		      selectStep(options.selectedStep);
                //Next Navigation
                $(".next", obj).bind("click", function(e){
                  e.preventDefault();  
                  var curStepIdx = $(steps, obj).index(wizcurrent);
                  if(options.validatorFunc(curStepIdx)){
					  
					  // User Info Validation
					  if(curStepIdx == 1)
					  {
						$("#firstname").css('background-color', '#FFFFFF');
						$("#lastname").css('background-color', '#FFFFFF');
						$("#phone").css('background-color', '#FFFFFF');
						$('#email').focus(); $("#email").css('background-color', '#FFFFFF');
						$("#company").css('background-color', '#FFFFFF');
						$("#addressline1").css('background-color', '#FFFFFF');
						$("#city").css('background-color', '#FFFFFF');
						$("#state").css('background-color', '#FFFFFF');
						$("#zip").css('background-color', '#FFFFFF');
						$("#whypartner").css('background-color', '#FFFFFF');
						$("#aboutyou").css('background-color', '#FFFFFF');
						$("#itcommunity").css('background-color', '#FFFFFF');
						$("#heardabout").css('background-color', '#FFFFFF');

						var haserror = false;
						if ($('#firstname').val() == ""){alert("Please enter your first name."); $('#firstname').focus(); $("#firstname").css('background-color', '#FFFF99'); }
						else if ($('#lastname').val() == ""){alert("Please enter your last name."); $('#lastname').focus(); $("#lastname").css('background-color', '#FFFF99');}
						else if ($('#phone').val() == ""){alert("Please enter your phone number."); $('#phone').focus(); $("#phone").css('background-color', '#FFFF99');}
						else if (!validateEmailv2($('#email').val())){alert("Please enter a valid email address."); $('#email').focus(); $("#email").css('background-color', '#FFFF99');}
						else if ($('#company').val() == ""){alert("Please enter your company name."); $('#company').focus(); $("#company").css('background-color', '#FFFF99');}
						else if ($('#addressline1').val() == ""){alert("Please enter your postal address."); $('#addressline1').focus(); $("#addressline1").css('background-color', '#FFFF99');}
						else if ($('#city').val() == ""){alert("Please tell us which city you live in."); $('#city').focus(); $("#city").css('background-color', '#FFFF99');}
						else if ($('#state').val() == ""){alert("Please tell us which state you live in."); $('#state').focus(); $("#state").css('background-color', '#FFFF99');}
						else if ($('#zip').val() == ""){alert("Please provide your postal or ZIP code."); $('#zip').focus(); $("#zip").css('background-color', '#FFFF99');}
						else if ($('#whypartner').val() == ""){alert("Please tell us briefly why you want to partner with us."); $('#whypartner').focus(); $("#whypartner").css('background-color', '#FFFF99');}
						else if ($('#aboutyou').val() == ""){alert("Please briefly describe your company."); $('#aboutyou').focus(); $("#aboutyou").css('background-color', '#FFFF99');}
						else if ($('#itcommunity').val() == ""){alert("Please tell us if you belong to any IT PRO organizations."); $('#itcommunity').focus(); $("#itcommunity").css('background-color', '#FFFF99');}
						else if ($('#heardabout').val() == ""){alert("Please tell us how you've heard about us."); $('#heardabout').focus(); $("#heardabout").css('background-color', '#FFFF99');}
						else
						{
							var nextStepIdx = curStepIdx+1;
							applyCSS($(wizcurrent, obj),"wiz-anc-done"); 
							selectStep(nextStepIdx);
						}
					  }
					  else if(curStepIdx == 2)
					  {
							
							if(!ASF)
							{
								// Send XML POST
								var xml_data = "<\?xml version='1.0'\?><root>" +
										"<application>" +
											"<firstname>" +  $('#firstname').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</firstname>" +
											"<middlename>" + $('#middlename').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</middlename>" +
											"<lastname>" + $('#lastname').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</lastname>" +
											"<title>" + $('#title').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</title>" +
											"<phone>" + $('#phone').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</phone>" +
											"<extension>" + $('#extension').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</extension>" +
											"<email>" + $('#email').val() + "</email>" +
											"<company>" + $('#company').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</company>" +
											"<addressline1>" + $('#addressline1').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</addressline1>" +
											"<addressline2>" + $('#addressline2').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</addressline2>" +
											"<city>" + $('#city').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</city>" +
											"<state>" + $('#state').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</state>" +
											"<zip>" + $('#zip').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</zip>" +
											"<country>" + $("#country").val().replace(/[^a-zA-Z 0-9]+/g,'') + "</country>" +
											"<website>" + $('#website').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</website>" +
											"<noemployees>" + $('#noemployees').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</noemployees>" +
											"<annualsalesservices>" + $('#annualsalesservices').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</annualsalesservices>" +
											"<annualsalessoftwarehardware>" + $('#annualsalessoftwarehardware').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</annualsalessoftwarehardware>" +
											"<targetcustomer>" + $('#targetcustomer').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</targetcustomer>" +
											"<businessfocus1>" + $('#businessfocus1').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</businessfocus1>" +
											"<businessfocus2>" + $('#businessfocus2').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</businessfocus2>" +
											"<businessfocus3>" + $('#businessfocus3').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</businessfocus3>" +
											"<registered>" + $('#registered').attr('checked') + "</registered>" +
											"<sbsc>" + $('#sbsc').attr('checked') + "</sbsc>" +
											"<certified>" + $('#certified').attr('checked') + "</certified>" +
											"<goldcertified>" + $('#goldcertified').attr('checked') + "</goldcertified>" +
											"<whypartner>" + $('#whypartner').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</whypartner>" +
											"<aboutyou>" + $('#aboutyou').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</aboutyou>" +
											"<itcommunity>" + $('#itcommunity').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</itcommunity>" +
											"<heardabout>" + $('#heardabout').val().replace(/[^a-zA-Z 0-9]+/g,'') + "</heardabout>" +
											"<password>" + $('#genpassword').val() + "</password>" +
										"</application></root>";

								// Send Request
								$.ajax({ 
								   url: "./inc/partner-application-go.php" 
								   , type: "POST"
								   , async: false
								   , contentType: "text/xml" 
								   , processData: false 
								   , data: xml_data 
								   , success:function(msg)
									 {
										$('#last_username').html($('#email').val());
										$('#last_password').html($('#genpassword').val());
									 }
								}); 
								


								ASF = true;
								var nextStepIdx = curStepIdx+1;
								applyCSS($(wizcurrent, obj),"wiz-anc-done"); 
								selectStep(nextStepIdx);
							}
							else
							{
							var nextStepIdx = curStepIdx+1;
							applyCSS($(wizcurrent, obj),"wiz-anc-done"); 
							selectStep(nextStepIdx);
							}
					  }
					  else
					  {
						var nextStepIdx = curStepIdx+1;
						applyCSS($(wizcurrent, obj),"wiz-anc-done"); 
						selectStep(nextStepIdx);
					  }
                  }
                });
                
                //Back Navigation
                $(".back", obj).bind("click", function(e){
                  e.preventDefault(); 
                  applyCSS($(wizcurrent, obj),"wiz-anc-done"); 
                  var prevIdx = $(steps, obj).index(wizcurrent)-1;
                  selectStep(prevIdx);
                });
            
                function selectStep(idx){
                    if(idx < steps.length && idx >= 0){
                      var selStepAnchor = $(steps, obj).eq(idx);
                      var selStepIdx =selStepAnchor.attr("href");
                      hideAllSteps();          
                      selStepAnchor.attr("isDone","1");
                      
                      if(options.animation==true){
                        $(selStepIdx, obj).fadeIn("fast");
                      }else{
                        $(selStepIdx, obj).show();
                      }
                      applyCSS($(selStepAnchor, obj),"wiz-anc-current");
                      wizcurrent = selStepAnchor;
                    }
                }
                
                function activateStep(idx){
                    var selStepAnchor = steps.eq(idx);
                    selStepAnchor.attr("isDone","1");
                    applyCSS($(selStepAnchor, obj),"wiz-anc-done");
                }

                function setErrorStep(idx){
                    var selStepAnchor = steps.eq(idx);
                    selStepAnchor.attr("isError","1"); 
                    $(selStepAnchor, obj).addClass("wiz-anc-error"); 
                }
                
                function unsetErrorStep(idx){
                    var selStepAnchor = steps.eq(idx);
                    selStepAnchor.attr("isError",""); 
                    $(selStepAnchor, obj).removeClass("wiz-anc-error"); 
                } 

                function hideAllSteps(){
            	    $(steps, obj).each(function(){
                        $($(this, obj).attr("href"), obj).hide();
                  });
                }
                
                function applyCSS(elm,css){
                    $(elm, obj).removeClass("wiz-anc-default");
                    $(elm, obj).removeClass("wiz-anc-current");
                    $(elm, obj).removeClass("wiz-anc-done");
                    $(elm, obj).addClass(css); 
                }
        });  
    };  
})(jQuery);

