//--------------------------------------------------------------
// Copyright (C) 2006 Michael Schwarz (http://www.ajaxpro.info).
// All rights reserved.
//--------------------------------------------------------------
// Converter.js

// NameValueCollectionConverter
if(typeof Ajax == "undefined") Ajax={};
if(typeof Ajax.Web == "undefined") Ajax.Web={};
if(typeof Ajax.Web.NameValueCollection == "undefined") Ajax.Web.NameValueCollection={};

Ajax.Web.NameValueCollection = function(items) {
	this.__type = "System.Collections.Specialized.NameValueCollection";
	this.keys = [];
	this.values = [];

	if(items != null && !isNaN(items.length)) {
		for(var i=0; i<items.length; i++)
			this.add(items[i][0], items[i][1]);
	}
};
Object.extend(Ajax.Web.NameValueCollection.prototype, {
	add: function(k, v) {
		if(k == null || k.constructor != String || v == null || v.constructor != String)
			return -1;
		this.keys.push(k);
		this.values.push(v);
		return this.values.length -1;
	},
	containsKey: function(key) {
		for(var i=0; i<this.keys.length; i++) {
			if(this.keys[i] == key) return true;
		}
		return false;
	},
	getKeys: function() {
		return this.keys;
	},
	getValue: function(k) {
		for(var i=0; i<this.keys.length && i<this.values.length; i++) {
			if(this.keys[i] == k) return this.values[i];
		}
		return null;
	},
	setValue: function(k, v) {
		if(k == null || k.constructor != String || v == null || v.constructor != String)
			return -1;
		for(var i=0; i<this.keys.length && i<this.values.length; i++) {
			if(this.keys[i] == k) this.values[i] = v;
			return i;
		}
		return this.add(k, v);
	},
	toJSON: function() {
		return AjaxPro.toJSON({__type:this.__type,keys:this.keys,values:this.values});
	}
}, true);

// DataSetConverter
if(typeof Ajax == "undefined") Ajax={};
if(typeof Ajax.Web == "undefined") Ajax.Web={};
if(typeof Ajax.Web.DataSet == "undefined") Ajax.Web.DataSet={};

Ajax.Web.DataSet = function(t) {
	this.__type = "System.Data.DataSet,System.Data";
	this.Tables = [];
	this.addTable = function(t) {
		this.Tables.push(t);
	};
	if(t != null) {
		for(var i=0; i<t.length; i++) {
			this.addTable(t[i]);
		}
	}
};

// DataTableConverter
if(typeof Ajax == "undefined") Ajax={};
if(typeof Ajax.Web == "undefined") Ajax.Web={};
if(typeof Ajax.Web.DataTable == "undefined") Ajax.Web.DataTable={};

Ajax.Web.DataTable = function(c, r) {
	this.__type = "System.Data.DataTable,System.Data";
	this.Columns = [];
	this.Rows = [];
	this.addColumn = function(name, type) {
		this.Columns.push({Name:name,__type:type});
	};
	this.toJSON = function() {
		var dt = {};
		var i;
		dt.Columns = [];
		for(i=0; i<this.Columns.length; i++)
			dt.Columns.push([this.Columns[i].Name, this.Columns[i].__type]);
		dt.Rows = [];
		for(i=0; i<this.Rows.length; i++) {
			var row = [];
			for(var j=0; j<this.Columns.length; j++)
				row.push(this.Rows[i][this.Columns[j].Name]);
			dt.Rows.push(row);
		}
		return AjaxPro.toJSON(dt);
	};
	this.addRow = function(row) {
		this.Rows.push(row);
	};
	if(c != null) {
		for(var i=0; i<c.length; i++)
			this.addColumn(c[i][0], c[i][1]);
	}
	if(r != null) {
		for(var y=0; y<r.length; y++) {
			var row = {};
			for(var z=0; z<this.Columns.length && z<r[y].length; z++)
				row[this.Columns[z].Name] = r[y][z];
			this.addRow(row);
		}
	}
};

// ProfileBaseConverter
if(typeof Ajax == "undefined") Ajax={};
if(typeof Ajax.Web == "undefined") Ajax.Web={};
if(typeof Ajax.Web.Profile == "undefined") Ajax.Web.Profile={};

Ajax.Web.Profile = function() {
	this.toJSON = function() {
		throw "Ajax.Web.Profile cannot be converted to JSON format.";
	};
	this.setProperty_callback = function(res) {
	};
	this.setProperty = function(name, object) {
		this[name] = object;
		AjaxPro.Services.Profile.SetProfile({name:o}, this.setProperty_callback.bind(this));
	};
};

// IDictionaryConverter
if(typeof Ajax == "undefined") Ajax={};
if(typeof Ajax.Web == "undefined") Ajax.Web={};
if(typeof Ajax.Web.Dictionary == "undefined") Ajax.Web.Dictionary={};

Ajax.Web.Dictionary = function(type,items) {
	this.__type = type;
	this.keys = [];
	this.values = [];

	if(items != null && !isNaN(items.length)) {
		for(var i=0; i<items.length; i++)
			this.add(items[i][0], items[i][1]);
	}
};
Object.extend(Ajax.Web.Dictionary.prototype, {
	add: function(k, v) {
		this.keys.push(k);
		this.values.push(v);
		return this.values.length -1;
	},
	containsKey: function(key) {
		for(var i=0; i<this.keys.length; i++) {
			if(this.keys[i] == key) return true;
		}
		return false;
	},
	getKeys: function() {
		return this.keys;
	},
	getValue: function(key) {
		for(var i=0; i<this.keys.length && i<this.values.length; i++) {
			if(this.keys[i] == key){ return this.values[i]; }
		}
		return null;
	},
	setValue: function(k, v) {
		for(var i=0; i<this.keys.length && i<this.values.length; i++) {
			if(this.keys[i] == k){ this.values[i] = v; }
			return i;
		}
		return this.add(k, v);
	},
	toJSON: function() {
		return AjaxPro.toJSON({__type:this.__type,keys:this.keys,values:this.values});
	}
}, true);

// ProductCategoryConverter

var UPLOAD_FOLDER="/Data/Uploads/";
function ProductCategory(productCategoryId,parentProductCategoryId,header,headerImage,description,navCssClass,hexColor,products,galleryId) {
	this.__type="Mantis.CaesarStone.Components.ProductCategory";
	this.ProductCategoryId=productCategoryId;
	this.ParentProductCategoryId=parentProductCategoryId;	
	this.Header=header;	
	this.HeaderImage=UPLOAD_FOLDER+headerImage;	
	this.Description=description;
	this.NavCssClass=navCssClass;
	this.HexColor=hexColor;
	this.Products=products;
	this.GalleryId=galleryId;
}
ProductCategory.prototype.toJSON=function () {
	var o=[this.ProductCategoryId,this.ParentProductCategoryId,this.Header,this.HeaderImage,this.Description,this.NavCssClass,this.HexColor,this.Products,this.GalleryId];
	return AjaxPro.toJSON(o);
};

// ProductConverter
 
var UPLOAD_FOLDER="/Data/Uploads/";
function Product(productId, header,homepageHeader,description,smallImage, mediumImage, lobbyImage, largeImage, useImage1, useImage2, scanImage, serial, isNew,properties,productCategories){
	this.__type="Mantis.CaesarStone.Components.Product";

	this.ProductId = productId;
	this.Header=header;
	this.HomepageHeader=homepageHeader;
	this.Description=description;
	this.SmallImage=UPLOAD_FOLDER+smallImage;
	this.MediumImage=UPLOAD_FOLDER+mediumImage;
	this.LobbyImage=UPLOAD_FOLDER+lobbyImage;
	this.LargeImage=UPLOAD_FOLDER+largeImage;
	this.UseImage1=UPLOAD_FOLDER+useImage1;
	this.UseImage2=UPLOAD_FOLDER+useImage2;
	this.ScanImage=UPLOAD_FOLDER+scanImage;
	this.Serial=serial;			
	this.IsNew=isNew;	
	this.Properties=properties;
	this.ProductCategories=productCategories;


	this.toJSON=function(){
		var o=[this.ProductId, this.Header,this.HomepageHeader,this.Description,this.SmallImage,this.MediumImage,this.LobbyImage,this.largeImage,this.UseImage1,this.UseImage2,this.ScanImage,this.Serial,this.IsNew,this.Properties,this.ProductCategories];
		return AjaxPro.toJSON(o);
	}
}

// PropertyConverter
 
var UPLOAD_FOLDER="/Data/Uploads/";
function Property(propertyId, header, smallImage,type){
	this.__type="Mantis.CaesarStone.Components.Property";

	this.PropertyId = propertyId;
	this.Header=header;
	this.SmallImage=UPLOAD_FOLDER+smallImage;
	this.Type=type;


	this.toJSON=function(){
		var o=[this.PropertyId, this.Header,this.SmallImage,this.Type];
		return AjaxPro.toJSON(o);
	}
}

// RegionConverter

var UPLOAD_FOLDER="/Data/Uploads/";
function Region(regionId, header, queryName ){
	this.__type="Mantis.CaesarStone.Components.Region";

	this.RegionId = regionId;
	this.Header=header;
	this.QueryName=queryName;


	this.toJSON=function(){
		var o=[this.RegionId, this.Header,this.QueryName];
		return AjaxPro.toJSON(o);
	}
}

// CountryConverter

var UPLOAD_FOLDER="/Data/Uploads/";
function Country(countryId, header,subHeader, codeName, url,address, contactEmail, region){
	this.__type="Mantis.CaesarStone.Components.Country";

	this.CountryId = countryId;
	this.Header=header;
	this.SubHeader=subHeader;
	this.CodeName=codeName;
	this.Url=url;
	this.Address=address;
	this.ContactEmail=contactEmail;
	this.Region=region;



	this.toJSON=function(){
		var o=[this.CountryId, this.Header,this.SubHeader,this.CodeName, this.Url,this.Address,this.ContactEmail,this.Region];
		return AjaxPro.toJSON(o);
	}
}

// SimilarProductConverter
 
function SimilarProduct(productId, header, smallImage, serial ){
	this.__type="Mantis.CaesarStone.Components.SimilarProduct";

	this.ProductId = productId;
	this.Header=header;
	this.SmallImage=smallImage;
	this.Serial = serial;
	
	this.toJSON=function(){
		var o=[this.ProductId, this.Header,this.SmallImage,this.Serial];
		return AjaxPro.toJSON(o);
	}
}

