function Thesis(){
	this.Shop=new Thesis_Shop();
	this.Auth=new Thesis_Auth();
	try{
		$.cookie("Test","Test");
		if($.cookie("Test")!="Test"){
			alert("Для того щоб магазин працював увімкніть підтримку кукі у вашему браузері.");
			this.BackEndMode=this.BackEndMode_Ajax;
		}
		$.cookie("Test",null);
	}catch(e){
		this.BackEndMode=this.BackEndMode_Ajax;
		alert("Для того щоб магазин працював увімкніть підтримку кукі у вашему браузері.");
	}
}
Thesis.prototype={
	BackEndMode_Cookie:"Cookie",
	BackEndMode_Ajax:"Ajax",
	BackEndMode:this.BackEndMode_Cookie,
	Shop:null,
	Auth:null
};
function Thesis_Auth(){
}
Thesis_Auth.prototype={
	Field:{
		Registration_Login:{"Логін має бути не коротше 5 символів":/^.{5,}$/},
		Registration_Password:{"Пароль має бути не коротше 5 символів":/^.{5,}$/},
		Registration_PasswordConform:{
			"Підтвердження має співпадати з паролем":function($Auth,$Value){
				return $Value==$("#Registration_Password").val();
			}
		},
		Registration_Name:{"Ім`я має бути заповнене":/^.{3,}$/},
		Registration_Address:{"Адресс має бути заповнений":/^.{3,}$/},
		Registration_Mail:{"Електронна адреса має бути вказана вірно.":/^.+@.+(\..+)+$/},
		Registration_Phone:{},
		Registration_ICQ:{},
		Registration_AntiBot:{"Числа з зображення мають бути введені правельно":/^.{5}$/}
	},
	Registration:function(){
		if(this.Check()){
			$("#Form-Registration").submit();
		}
	},
	Check:function(){
		var $Return=true;
		for(var $ID in this.Field){
			if(!this.CheckField($ID)){
				$Return=false;
			}
		}
		return $Return;
	},
	CheckField:function($ID){
		var $Return=true;
		var $Element=$("#"+$ID);
		if($Element.length==0)return $Return;
//		var $Error=$("+ div",$Element);
		var $Error=$("#"+$ID+"_Error");
		var $Value=$Element.val();
		var $Message=[];
		for(var $CheckPhrase in this.Field[$ID]){
			var $Check=this.Field[$ID][$CheckPhrase];
			var $Checked=true;
			switch(true){
				case($Check instanceof RegExp):
					$Checked=$Value.match($Check);
				break;
				case($Check instanceof Function):
					$Checked=$Check(this,$Value);
				break;
			}
			if(!$Checked){
				$Message.push($CheckPhrase);
				$Return=false;
			}
		}
		$Error.html($Message.join("<br/>"));
		if($Return)$Element.removeClass("Error");
		else $Element.addClass("Error");
		return $Return;
	}
}
function Thesis_Shop(){
	this.WakeUp();
}
Thesis_Shop.prototype={
	Article:[],
	Discount:null,
	DiscountConfirmation:null,
	Payment:"Post",
	SetArticle:function($Class,$ID,$Price,$Weight,$Count){
		if($Count==null)$Count=1;
		$Count=Math.round(Math.abs(this.GetNumeric($Count)));
		if(!($Class in this.Article)){
			this.Article[$Class]=[];
		}
		if(!($ID in this.Article[$Class])){
			this.Article[$Class][$ID]={
				"Class":$Class,
				"ID":$ID,
				"Price":$Price,
				"Weight":$Weight,
				"Count":$Count
			};
		}else{
			this.Article[$Class][$ID]["Count"]+=$Count;
		}
		this.Sleep();
		return this;
	},
	GetCount:function($Class,$ID){
		return this.Article[$Class][$ID]["Count"]*1;
	},
	SetCount:function($Class,$ID,$Count){
		this.Article[$Class][$ID]["Count"]=Math.round(Math.abs(this.GetNumeric($Count)));
		this.Sleep();
		return this;
	},
	UnSetArticle:function($Class,$ID){
		delete this.Article[$Class][$ID];
		this.Sleep();
		return this;
	},
	GetPropertySum:function($Property,$Class,$ID){
		if($Property==null)$Property="Count";
		var $Count=0;
		switch(true){
			case($ID!=null&&$Class!=null):
				if($Property=="Count"){
					$Count+=this.Article[$Class][$ID][$Property];
				}else{
					$Count+=
						this.Article[$Class][$ID][$Property]*
						this.Article[$Class][$ID]["Count"];
				}
			break;
			case($ID==null&&$Class!=null):
				for($ID in this.Article[$Class]){
					$Count+=this.GetPropertySum($Property,$Class,$ID);
				}
			break;
			case($ID==null&&$Class==null):
				for($Class in this.Article){
					$Count+=this.GetPropertySum($Property,$Class);
				}
			break;
		}
		return $Count;
	},
	GetNumeric:function($String){
		return $String.toString().match(/-?[0-9]+([.]?[0-9]+)?/)[0]*1;
	},
	GetFloat:function($Sum,$Accuracy){
		if($Accuracy==null)$Accuracy=0;
		return Math.round($Sum*Math.pow(10,$Accuracy))/Math.pow(10,$Accuracy);
        /*$AccuracySum=Math.pow(10,$Accuracy);
        $Sum=Math.round($Sum*$AccuracySum)/$AccuracySum;
        var $Decs=$Sum%1;
        var $Nulls="";
        if($Decs==0){
            for(var $I=0; $I<$Accuracy; $I++){
            	$Nulls+="0";
            }
            return ($Sum-$Decs)+"."+$Nulls;
        }else{
            return ($Sum-$Decs)+"."+Math.round($Decs*$AccuracySum);
        }*/
	},
	GetDiscount:function(){
		switch(true){
            case(this.Discount=="1"&&this.DiscountConfirmation>0&&(this.DiscountConfirmation*1).toString().toLowerCase()!="nan"):
                return this.GetPropertySum("Price")*0.10;
	    	case (this.Discount=="2"&&this.DiscountConfirmation.toLowerCase() in {"alenka":null,"оленка":null,"алёнка":null}):
                return this.GetPropertySum("Price")*0.15;
            default:
                return 0;
        }
	},
	GetPriceWithoutDiscount:function(){
		var $Price=this.GetPropertySum("Price");
		if($Price>0)$Price+=(this.GetPropertySum("Weight")<=0.5)?16.00:21.00;
		return $Price;
	},
	SetPayment:function($Val){
		this.Payment=$Val;
		this.Sleep();
		return this;
	},
	GetPrice:function(){
		var $Price=this.GetPriceWithoutDiscount();
		return $Price-this.GetDiscount();
	},
	Void:function(){},
	Sleep:function(){
		$Data={
			Article:this.Article,
			Discount:this.Discount,
			DiscountConfirmation:this.DiscountConfirmation,
			Payment:this.Payment
		}
		$.cookie("Shop",Thesis_JSON.Pack($Data),{path:"/"});
		return this;
	},
	WakeUp:function(){
		if($.cookie("Shop")!=null){
			$Data=Thesis_JSON.UnPack($.cookie("Shop"));
			this.Article=$Data.Article;
			this.Discount=$Data.Discount;
			this.DiscountConfirmation=$Data.DiscountConfirmation;
			this.Paymen=$Data.Paymen;
		}
		return this;
	},
	Load:function($Data){
		var $Date=new Date();
		$("#Dialog-Buy").empty().load("/Buy/Screen.Page1.html","no-cache="+$Date.getTime()+"&"+$Data,function(){
//			$Thesis.Shop.Refresh();
		});
		return this;
	},
	SetDiscount:function($Val){
		this.Discount=$Val;
		this.Sleep();
		return this;
	},
	SetDiscountConfirmation:function($Val){
		this.DiscountConfirmation=$Val;
		this.Sleep();
		return this;
	},
	Refresh:function(){
		$("#Counter-Buy").text($Thesis.Shop.GetPropertySum("Count"));
		$("#Price-Buy").text($Thesis.Shop.GetFloat($Thesis.Shop.GetPrice(),2));
		var $Table=$("#Dialog-Buy table.Shop");
		var $Rows=$("tr",$Table);
		$Rows.slice(1,$Rows.length-3).each(function(){
			//5-Count,6-Price,7-Sum
			var $Count=$("td:eq(5) input",this);
			var $Price=$("td:eq(6)",this);
			var $Sum=$("td:eq(7)",this);
			$Count.val(Math.round(Math.abs($Thesis.Shop.GetNumeric($Count.val()))));
			$Sum.text($Thesis.Shop.GetFloat($Count.val()*$Price.text(),2));
		});
		$Rows.slice($Rows.length-3,$Rows.length-2).each(function(){
			var $Weight=$Thesis.Shop.GetPropertySum("Weight");
			var $Delivery=$Weight==0?0:$Thesis.Shop.GetFloat(($Weight<=0.5)?16.00:21.00,2);
			$("td:eq(5)",this).text(1);
			$("td:eq(6)",this).text($Delivery);
			$("td:eq(7)",this).text($Delivery);
		});
		$Rows.slice($Rows.length-2,$Rows.length-1).each(function(){
			var $Discount=$Thesis.Shop.GetDiscount();
			$("td:eq(5)",this).text($Discount>0?1:0);
			$("td:eq(6)",this).text(-$Thesis.Shop.GetFloat($Discount,2));
			$("td:eq(7)",this).text(-$Thesis.Shop.GetFloat($Discount,2));
		});
		$Rows.slice($Rows.length-1,$Rows.length).each(function(){
			$("th:eq(1)",this).text($Thesis.Shop.GetFloat($Thesis.Shop.GetPrice(),2));
		});
		return this;
	},
	Buy:function(){
		this.Load("Confirm[Buy]=true");
		return this;
	}
};
Thesis_JSON={
	Pack:function($Var){
		switch(typeof($Var)){
			case "string":
				return '"'+$Var.replace('"','\"')+'"';
			break;
			case "number":
				return $Var;
			break;
			case "object":
				var $String=[];
				for($Key in $Var)$String.push([this.Pack($Key),this.Pack($Var[$Key])].join(":"));
				return "{"+$String.join(",")+"}";
			break;
		}
		return null;
	},
	UnPack:function($String){
		eval("var $Var="+$String);
		return $Var;
	}
};
