(function($){
  $.fn.ScrollCenter = function(options) {
    var pos = {
      sTop : function() {
        return window.pageYOffset || document.documentElement && document.documentElement.scrollTop ||	document.body.scrollTop;
      },
      wHeight : function() { 
        return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
      },
      sLeft	: function(){
      	return window.pageXOffset || document.documentElement && document.documentElement.scrollLeft ||	document.body.scrollLeft;
      },
      wWidth : function(){
      	return window.innerWidth || document.documentElement && document.documentElement.clientWidth || document.body.clientWidth;
      }
    };
    return this.each(function(index) {
      if (index == 0) {
        var $this = $(this);
        var elHeight = $this.height();
        var elWidth = $this.width();
		    var elTop = pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2);
		    var elLeft = pos.sLeft() + (pos.wWidth() / 2) - (elWidth / 2);
      
		
		$this.css({
          position: 'absolute',
          marginTop: '0',
          //top: elTop,
          top: '200px',
          left:elLeft
        });
        
         /*$this.animate({ 
	          marginTop: '0',
	          top: elTop,
	          left:elLeft
	      }, "fast","swing" );*/

      }
      
    });
  };

})(jQuery);


var Upgrade = function(){
	return {
		Init:function(){
				jQuery("#box_upgrade_licesne_input_key").keyup(function(e){
					//fix activation code mistakes
					if (e.which != 32 && e.which != 13 && e.which != 0 && e.which!=8 &&
						e.which != 37 && e.which != 39 && e.which != 38 && e.which != 40 && //arrows
						e.which != 45 && e.which != 36 && e.which != 33 && e.which != 46 && e.which != 35 && e.which != 34 // insert home pgup delete end pgdown 
						){
						
					
						var value = this.value;
						value = value.replace(/ |\t|\n|,|\.|\/|\?|@|#|$|%|^|&|\*|\(|\)|\-|=|\+/g,'');
						
						value = value.split('-');
						
						value = value.join('');
						
						var j=3,k=1; // steps
						var tmpvalue = '';
						
						for (var i = 0; i<value.length; i++){
							
							tmpvalue = tmpvalue+''+value.charAt(i);
							
							if (i==11) break;
							if (k==j){
								tmpvalue = tmpvalue+'-';
								j++;
								k=0;
							}
							k++;
						}
						this.value = tmpvalue;
						
						jThis = jQuery(this);
						/*if (value.length >= 12){
							
							
							
							jQuery('#box_upgrade_licesne_actions_description').html('Checking Key').attr('class','loading');
							var url = '/order/upgrade_options.php';
							var params = jQuery("#fmUpgradeLicense").serializeArray();
							jThis.attr('disabled','disabled');
							jQuery.getJSON(url,params,function(data){
								jQuery('#box_upgrade_licesne_options').html(data.html);
								if (data.ok==true){
									jQuery('#box_upgrade_licesne_actions_description').html('Upgrade Available').attr('class','ok');
									jQuery('#fmUpgradeLicenseSubmit').attr('class','upgradeContinueOn').unbind().attr('href','#continue').click(function(){jQuery('#fmUpgradeLicense').unbind().submit();});
									jQuery('#box_upgrade_licesne_options').find('select').removeAttr('disabled');
								}else{
									jQuery('#box_upgrade_licesne_actions_description').html(data.msg).attr('class','ko');
									jQuery('#fmUpgradeLicenseSubmit').attr('class','upgradeContinueOff').removeAttr('href').unbind();
									jQuery('#fmUpgradeLicense').unbind().submit(function(){return false;});
									jQuery('#box_upgrade_licesne_options').find('select').attr('disabled','disabled');
									if (data.msgalert!=''){
										alert(data.msgalert);
									}
								}
								jThis.removeAttr('disabled');
								jThis.blur().focus();
							});
						}*/
					}
				});
				jQuery('#fmOrder').submit(function(){
					
				});
				jQuery("a[href$='#key']").click(function(){
					jQuery('#quota').val(this.rel);
					jQuery("input[name='order_checked']").val(0);
					jQuery("input[name='submitted']").val(0);
					jQuery("input[name='agree']").val(0);
					jQuery("input[name='use_prev_billing']").val(0);
					jQuery("input[name='existingKey']").val(0);
					jQuery('#fmOrder').submit();
				});
				jQuery("a[href$='#free']").click(function(){
					jQuery('#quota').val(this.rel);
					jQuery("input[name='order_checked']").val(1);
					jQuery("input[name='submitted']").val(1);
					jQuery("input[name='agree']").val(1);
					jQuery("input[name='use_prev_billing']").val(0);
					jQuery("input[name='existingKey']").val(1);
					jQuery('#fmOrder').submit();
				});
				
				jQuery("#fmUpgradeKey").submit(function(){
					var current = jQuery(this);
					var current_data = current.serializeArray();
					
					var hiddenParams = {'__request_type__':'ajax'};
					
					for (var i in hiddenParams){
						if (current_data instanceof Array){
							var obj = {'name':i,'value':hiddenParams[i]};
							current_data[current_data.length] = obj;
						}else if(current_data instanceof Object){
							current_data[i] = hiddenParams[i];	
						}
					}
					var button_html = jQuery('#submit_btn').html();
					jQuery('#submit_btn').html('<span style="font-size:12px;">Loading...</span>');
					jQuery.ajax(
							{
								type:current.attr('method'),
								url:current.attr('action'),
								data:current_data,
								dataType:'json',
								success: function(data){
									if (data.success == '1'){
										jQuery("#fmUpgradeKey").unbind().submit();
									}else{
										jQuery('#submit_btn').html(button_html);
										jQuery('.msg_title','#_popup').html(data.msg.title);
										jQuery('.msg_content','#_popup').html(data.msg.message);
										jQuery('#_popup').ScrollCenter().show();
									}
									
								},
								error:function(data){
									jQuery('#submit_btn').html(button_html);
									jQuery('.msg_title','#_popup').html("Internal error");
									jQuery('.msg_content','#_popup').html("Request failed, please try again later");
									jQuery('#_popup').ScrollCenter().show();
									
								}
							}
					);
					return false;
				});
				
				jQuery("#_popup_close").click(function(){
					jQuery('#_popup').hide();
				});
		},
		SubmitUpgradeForm:function(){
			
		}
	}
}();
jQuery(function(){
	Upgrade.Init();
});
