 /* Snake v1.0.3
 * Fredrik Sšderberg
 * http://lotecsoftware.com/
 */
var SQ_SIZE = 10; // Pixelsize of a square.
var WIDTH = 50; // Board size in squares.
var HEIGHT = 40;
var QUEUE_MOVES = 2;
var TOP = 0; // Margins
var LEFT = 0;
var HUNGER_STEP = 10;
var STARVING_SHRINK_STEP = 10;
var START_SPEED = 4; // Squares per second.
var DIR_UP = 1;
var DIR_RIGHT = 2;
var DIR_DOWN = 3;
var DIR_LEFT = 4;
var MAX_PLAYERS = 2;
var PRE = "lotecSnake_";
var PATH = "";
var game;

function setup(parentID, path) {
	PATH = path;
	game = new SnakeGame();
	game.setup(parentID);
}

function GUI() {
	this.snakesHungerElement = new Array();
	this.snakesScoreElement = new Array();
	this.snakesSpeedElement = new Array();
	this.playfieldElement;
	this.pausedElement;
	this.highscoreElement;
//	this.textColor = {"red": "blue"};
	this.textColor = new Array();
	this.textColor[0] = "red";
	this.textColor[1] = "blue";

	this.killSnake = function(snake) {
		snake.body[0].element.src = PATH+"snakeDead.gif";
	};

	this.showPaused = function(paused) {
		if (paused) {
			this.pausedElement.style.visibility = "visible";
		} else {
			this.pausedElement.style.visibility = "hidden";
		}
	};

	this.setHunger = function(snakenr, value) {
		for (i=0; i<10; i++) {
			if (i < value-1) {
				this.snakesHungerElement[snakenr][i].src = PATH+"hunger"+snakenr+".gif";
			} else {
				this.snakesHungerElement[snakenr][i].src = PATH+"empty.gif";
			}
		}
	};

	this.setSpeed = function(snakenr, value) {
		this.snakesSpeedElement[snakenr].innerHTML =
			Math.round(value*100)/100;
	};

	this.setScore = function(snakenr, value) {
		this.snakesScoreElement[snakenr].innerHTML = value;
	};

	this.setHighscore = function(value) {
		this.highscoreElement.innerHTML = value;
	};

	this.addFood = function(item) {
		this.playfieldElement.appendChild(item.element);
	};

	this.removeFood = function(item) {
		item.element.style.visibility = "hidden";
	};

	this.setWallSolid = function(solid) {
		if (solid) {
			this.playfieldElement.style.border = "1px solid black";
		} else {
			this.playfieldElement.style.border = "1px dashed black";
		}
	};

	this.setup = function(parentID) {
		var parent = document.getElementById(parentID);
		this.gamewindowElement = document.createElement("div");
		this.gamewindowElement.id = PRE+"gamewindow";
		this.gamewindowElement.style.width = (WIDTH * SQ_SIZE + 100) + "px";
		this.gamewindowElement.style.height = (HEIGHT * SQ_SIZE) + "px";
//		this.gamewindowElement.style.border = "1px solid red";
		this.gamewindowElement.style.position = "relative";
		parent.appendChild(this.gamewindowElement);

		this.pausedElement = document.createElement("h1");
		this.pausedElement.id = PRE+"pause";
		this.pausedElement.align = "center";
		this.pausedElement.innerHTML = "PAUSED";
		this.pausedElement.style.position = "absolute";
		this.pausedElement.style.left = (WIDTH * SQ_SIZE / 2) + "px";
		this.pausedElement.style.top = "180px";
		this.pausedElement.style.zIndex = "10";
		this.pausedElement.style.visibility = "hidden";
		this.gamewindowElement.appendChild(this.pausedElement);

		this.playfieldElement = document.createElement("div");
		this.playfieldElement.id = PRE+"playfield";
		this.playfieldElement.style.position = "absolute";
		this.playfieldElement.style.width = (WIDTH * SQ_SIZE) + "px";
		this.playfieldElement.style.height = (HEIGHT * SQ_SIZE) + "px";
		this.playfieldElement.style.border = "1px solid black";
		this.playfieldElement.style.left = LEFT + "px";
		this.playfieldElement.style.top = TOP + "px";
		this.playfieldElement.innerHTML = "<h1 align=\"center\">Snake</h1><h2 align=\"center\">Press 1 or 2 to play</h2><h3 align=\"center\"><img src='"+PATH+"snake0.gif'/> Player 1 controls: Arrows<br/><img src='"+PATH+"snake1.gif'/> Player 2 controls: a/s/d/w<br/>Pause: P</h3><h4 align=\"center\"><img src=\""+PATH+"apple0.gif\" /><img src=\""+PATH+"apple1.gif\" /> same color as snake: increases speed and awards 10 points.<br/><img src=\""+PATH+"apple0.gif\" /><img src=\""+PATH+"apple1.gif\" /> different color : decreases speed and awards 20 points.<br/><img src=\""+PATH+"appleGold.gif\" /> : grows your snake 20 pieces and awards 50 points.<br/><img src=\""+PATH+"mushroom.gif\" /> : gives opponent 5 sec 150% speed boost and awards 1 point.<br/><br/>Eating an item switches wall between solid and transparent.<br/>The snake is a hungry beast. When the hunger-meter is full the snake will starve and needs to eat quickly. While starving your points will decrease and after a while your snake will shrink and eventually die.</h4>";
		this.gamewindowElement.appendChild(this.playfieldElement);

		var marginLeft = (LEFT + WIDTH * SQ_SIZE + 10) + "px";
		var boxWidth = 50 + "px";
		var el = document.createElement("div");
		var tmp;
		el.id = PRE+"score";
		el.align = "center";
		el.innerHTML = "Score";
		el.style.border = "1px solid black";
		el.style.position = "absolute";
		el.style.width = boxWidth;
		el.style.height = "4em";
		el.style.left = marginLeft;
		el.style.top = "5px";
		for (i=0; i<MAX_PLAYERS; i++) {
			tmp = document.createElement("div");
			tmp.id = PRE+"snake"+i+"score";
			tmp.align = "right";
			tmp.style.color = this.textColor[i];
			el.appendChild(tmp);
		}
		this.gamewindowElement.appendChild(el);

		el = document.createElement("div");
		el.id = PRE+"speed";
		el.align = "center";
		el.innerHTML = "Speed";
		el.style.border = "1px solid black";
		el.style.position = "absolute";
		el.style.width = boxWidth;
		el.style.height = "4em";
		el.style.left = marginLeft;
		el.style.top = "5em";
		for (i=0; i<MAX_PLAYERS; i++) {
			tmp = document.createElement("div");
			tmp.id = PRE+"snake"+i+"speed";
			tmp.align = "right";
			tmp.style.color = this.textColor[i];
			el.appendChild(tmp);
		}
		this.gamewindowElement.appendChild(el);
		
		el = document.createElement("div");
		el.id = PRE+"hunger";
		el.align = "center";
		el.innerHTML = "Hunger";
		el.style.border = "1px solid black";
		el.style.position = "absolute";
		el.style.width = boxWidth;
		el.style.height = "65px";
		el.style.left = marginLeft;
		el.style.top = "10em";
		for (i=0; i<MAX_PLAYERS; i++) {
			tmp = document.createElement("div");
			tmp.id = PRE+"snake"+i+"hunger";
			tmp.align = "right";
			for (j=0; j<10; j++) {
				var img = document.createElement("img");
				img.id = PRE+"snake"+i+"hunger"+j;
				img.src = PATH+"empty.gif";
				img.width = 5;
				img.height = 10;
				img.border = 0;
				tmp.appendChild(img);
			}
			if (i != 0) {
				el.appendChild(document.createElement("br"));
			}
			el.appendChild(tmp);
		}
		this.gamewindowElement.appendChild(el);

		el = document.createElement("div");
		el.align = "center";
		el.innerHTML = "Highscore";
		el.style.border = "1px solid black";
		el.style.position = "absolute";
		el.style.width = "80px";
		el.style.height = "2.5em";
		el.style.left = marginLeft;
		el.style.top = "20em";
		this.highscoreElement = document.createElement("div");
		this.highscoreElement.id = PRE+"highscoreLocal";
		this.highscoreElement.align = "center";
		el.appendChild(this.highscoreElement);
		this.gamewindowElement.appendChild(el);
	};
	
	this.init = function(cnt) {
		this.playersCounter = cnt;
		for (i=0; i<this.playersCounter; i++) {
			this.snakesScoreElement[i] = document.getElementById(PRE+"snake"+i+"score");
			this.snakesSpeedElement[i] = document.getElementById(PRE+"snake"+i+"speed");
			this.snakesHungerElement[i] = new Array();
		}
		for (i=0; i<10; i++) {
			for (j=0; j<this.playersCounter; j++) {
				// Could be changed to one long image and change style.width only.
				this.snakesHungerElement[j][i] = document.getElementById(PRE+"snake"+j+"hunger"+i);
				this.snakesHungerElement[j][i].src = PATH+"empty.gif";
				this.snakesHungerElement[j][i].align = "left";
				this.snakesHungerElement[j][i].style.margin = 0;
				this.snakesHungerElement[j][i].style.border = 0;
				this.snakesHungerElement[j][i].border = 0;
				this.snakesHungerElement[j][i].padding = 0;
				this.snakesHungerElement[j][i].hspace = 0;
			}
		}
		this.playfieldElement.style.border = "1px solid black";
		this.playfieldElement.innerHTML = "";
	};
}

function SnakeGame() {
	this.gui = new GUI();
	this.pause = false;
	this.waitStart = true;
	this.isWallSolid;
	var author = "Fredrik Sšderberg";
	this.timeoutItemSpawner = null;
	this.timeoutRemoveGold = null;
	this.appleGold = null;
	this.players = new Array();
	this.board = new Array();
	this.food;
	this.highscoreLocal;
	this.date;
	this.timeStart;
	this.timeTick;
	this.foodCount = 0;
	this.debug;
	this.playersCounter = 2;
	this.fps;
	this.fpsCounter;
	this.fpsTime;
	this.gameover = true;

	this.playerWillEat = function(player, score, speed, grow) {
		player.eat(score, speed, grow);
		this.toggleWall();
		if (this.foodCount == 1) {
			this.stopItemSpawner();
			this.startItemSpawner();
		}
		this.removeFoodAt(player.xTo, player.yTo);
	};
	
	this.addFood = function(item) {
		this.gui.addFood(item);
		this.board[item.y][item.x] = item;
		this.foodCount++;
	};
	
	this.removeFood = function(item) {
		item.remove();
		item = null;
		this.foodCount--;
	};

	this.setHighscore = function(value) {
		this.highscoreLocal = value;
		setCookie("SnakeHS", value, 3650);
		this.gui.setHighscore(value);
	};

	this.setup = function(parentID) {
		this.highscoreLocal = getCookie("SnakeHS");
		this.gui.setup(parentID);
		this.gui.setHighscore(this.highscoreLocal);
	};

	this.init = function() {
		if (this.timeoutItemSpawner != null) {
			clearTimeout(this.timeoutItemSpawner);
		}
		if (this.timeoutRemoveGold != null) {
			clearTimeout(this.timeoutRemoveGold);
		}
		this.date = new Date();
		this.timeStart = this.date.getTime();
		this.gui.init(this.playersCounter);
		this.food = new Array();
		this.foodCount = 0;
		this.fpsCounter = 0;
		this.gameover = false;
		this.isWallSolid = false;
		this.gui.setWallSolid(this.isWallSolid);

		// Init board.
		for (y=0; y<HEIGHT; y++) {
			this.board[y] = new Array();
			for (x=0; x<WIDTH; x++) {
				this.board[y][x] = null;
			}
		}
		// Init players.
		for(var i=0; i<this.playersCounter; i++) {
			this.players[i] = new Snake("snake"+i, i, 5, WIDTH-10-(i*5), HEIGHT/2, this.board, this.gui);
		}
		for (var i=0; i<this.playersCounter; i++) {
			this.players[i].changeSpeed(0);
			this.players[i].changeScore(0);
		}
		this.startItemSpawner();
	};

	this.start = function() {
		this.init();
		this.run();
	};

	this.startItemSpawner = function() {
		this.timeoutItemSpawner = setTimeout("game.startItemSpawner()", 10000);
		this.spawnItems();
	};

	this.stopItemSpawner = function() {
		clearTimeout(this.timeoutItemSpawner);
	};

	this.removeGold = function() {
		if (this.appleGold != null) {
			this.removeFoodAt(this.appleGold.x, this.appleGold.y);
		}
	};

	this.spawnItems = function() {
		if (!this.pause) {
			if (this.foodCount < 4) {
				this.spawnItem("apple0");
				this.spawnItem("apple1");
			}
			var rnd = Math.floor(Math.random() * 5);
			if (rnd == 2) {
				this.appleGold = this.spawnItem("appleGold");
				this.timeoutRemoveGold = setTimeout("game.removeGold()", 9000);
			} else if (rnd == 4) {
				this.spawnItem("mushroom");
			}
		}
	};

	this.spawnItem = function(type) {
		var x = Math.floor(Math.random() * WIDTH);		
		var y = Math.floor(Math.random() * HEIGHT);
		while (this.board[y][x] != null) {
			x = Math.floor(Math.random() * WIDTH);		
			y = Math.floor(Math.random() * HEIGHT);
		}
		for (var i=0; this.food[i]!=null; i++);
		this.food[i] = new Item(type, i, x, y, this.board, this.gui);
		this.addFood(this.food[i]);
		return this.food[i];
	};

	this.toggleWall = function() {
		this.isWallSolid = !this.isWallSolid;
		this.gui.setWallSolid(this.isWallSolid);
	};

	this.removeFoodAt = function(x, y) {
		f = this.board[y][x];
		this.food[f.nr] = null;
		this.removeFood(f);
	};

	this.boostAllBut = function(nr) {
		for (var i in this.players) {
			if (i != nr) {
				this.players[i].changeSpeed(2.75);
			}
		}
		var tmp = "game.removeBoostExcept('"+nr+"')";
		setTimeout(tmp, 5000);	
	};

	this.removeBoostExcept = function(nr) {
		for (var i in this.players) {
			if (i != nr) {
				this.players[i].changeSpeed(1/2.75);
			}
		}
	};

	this.run = function() { // Main loop
		if(!this.pause && !this.waitStart) {
			this.date = new Date();
			this.timeTick = this.date.getTime();
			this.updatePlayers();
		}
		/*
		if (this.fpsCounter == 10) {
			this.fps = 10000 / (this.timeTick - this.fpsTime);
			debug.innerHTML = "fps:" + this.fps;
			this.fpsCounter = 0;
			this.fpsTime = this.timeTick;
		}
		this.fpsCounter++;
		*/
		setTimeout("game.run()", 1);
	};
// TODO: Fixa sŒ att allas preparemove kšrs fšrst, annars kan man kšra in 
	// i svansen pŒ motspelaren.
	this.updatePlayers = function() {
		var i;
		var over = true;
		for (i=0; i<this.playersCounter; i++) {
			if (this.players[i].alive) {
				over = false;
				if ((this.timeTick - this.players[i].moved) > 
						(1000/this.players[i].speed)) {
					this.players[i].prepareMove(this.timeTick);
					if (ret = this.handleCollisions(this.players[i])) {
						this.players[i].move();
						this.players[i].handleHunger();
					}
				}
			}
		}
		this.gameover = over;
		
		if (this.gameover) {
			var max = this.highscoreLocal;
			for (var i in this.players) {
				if (this.players[i].score > max) {
					max = this.players[i].score;
				}
			}
			if (max > this.highscoreLocal) {
				this.setHighscore(max);
				alert("Game Over!\nNEW HIGHSCORE!\nClick OK to start again.");			
			} else {
				alert("Game Over!\nClick OK to start again.");			
			}
			this.init();
		}
	};

	this.keydown = function(e) {
		var key = e.keyCode;
//		alert("key="+key);
		if (!this.gameover) {
			if (key == 80) {
				this.mypause();
			}
			if (this.playersCounter >= 1) {
				if (this.players[0].dirChangeQueue.length <= QUEUE_MOVES) {
					if (key == 37) // Left Arrow
						this.players[0].dirChangeQueue.push(DIR_LEFT);
					if (key == 38) // Up Arrow
						this.players[0].dirChangeQueue.push(DIR_UP);
					if (key == 39) // Right Arrow
						this.players[0].dirChangeQueue.push(DIR_RIGHT);
					if (key == 40) // Down Arrow
						this.players[0].dirChangeQueue.push(DIR_DOWN);
				}
			}
			if (this.playersCounter >= 2) {
				if (this.players[1].dirChangeQueue.length <= QUEUE_MOVES) {
					if (key == 65) // Left A
						this.players[1].dirChangeQueue.push(DIR_LEFT);
					if (key == 87) // Up W
						this.players[1].dirChangeQueue.push(DIR_UP);
					if (key == 68) // Right D
						this.players[1].dirChangeQueue.push(DIR_RIGHT);
					if (key == 83) // Down S
						this.players[1].dirChangeQueue.push(DIR_DOWN);
				}
			}
		}

		if(this.waitStart && (key == 49 || key == 50))
		{
			this.waitStart = false;
			if (key == 49) {
				this.playersCounter = 1;
			} else {
				this.playersCounter = 2;
			}
			this.start();
		}
	};

	this.mypause = function() {
		if (!game.gameover) {
			game.pause = !game.pause;
			game.gui.showPaused(game.pause);
		}
	};

	this.p = function(str) {
		this.debug.innerHTML = str;
	};

	this.handleCollisions = function(player) {
		var hasCollided = false;
		// Check collision with border.
		if(player.xTo >= WIDTH) {
			if (this.isWallSolid) {
				player.die();
				hasCollided = true;
			} else {
				player.xTo = 0;
			}
		}
		else if(player.xTo < 0) {
			if (this.isWallSolid) {
				player.die();
				hasCollided = true;
			} else {
				player.xTo = WIDTH-1;
			}
		}
		else if(player.yTo >= HEIGHT) {
			if (this.isWallSolid) {
				player.die();
				hasCollided = true;
			} else {
				player.yTo = 0;
			}
		}
		else if(player.yTo < 0) {
			if (this.isWallSolid) {
				player.die();
				hasCollided = true;
			} else {
				player.yTo = HEIGHT-1;
			}
		}

		if (player.alive) {
			// Check collision with objects.
			if (this.board[player.yTo][player.xTo] != null) {
				// If snake eats its own color
				if ((this.board[player.yTo][player.xTo].type == "apple0" &&
						player.type == "snake0") ||
						(this.board[player.yTo][player.xTo].type == "apple1" &&
								player.type == "snake1")) {
					this.playerWillEat(player, 10, 1.5, 1);
				// If snake eats someone elses color.
				} else if (this.board[player.yTo][player.xTo].type == "apple0" ||
						this.board[player.yTo][player.xTo].type == "apple1") {
					this.playerWillEat(player, 20, 0.73, 1);
				} else if (this.board[player.yTo][player.xTo].type == "appleGold") {
					this.playerWillEat(player, 50, 1, 20);
					this.appleGold = null;
					clearTimeout(this.timeoutRemoveGold);
				} else if (this.board[player.yTo][player.xTo].type == "mushroom") {
					this.boostAllBut(player.nr);
					this.playerWillEat(player, 1, 1, 0);
				} else if (this.board[player.yTo][player.xTo].type == "snake0" ||
						this.board[player.yTo][player.xTo].type == "snake1") {
					hasCollided = true;
					player.die();
				}
			}
		}
		return !hasCollided;
	};
}

function Item(type, nr, x, y, board, gui) {
	this.board = board;
	this.gui = gui;
	this.id = PRE+type + "nr" + nr;
	this.type = type;
	this.x = x;
	this.y = y;
	this.element = document.getElementById(this.id);

	if (this.element == null) {
		this.element = document.createElement('img');
		this.element.id = this.id;
		this.element.src = PATH+this.type + ".gif";
		this.element.style.position = "absolute";
		this.element.style.left = x*SQ_SIZE + "px";
		this.element.style.top = y*SQ_SIZE + "px";
	} else { // If the element exist, just make it visible.
		this.element.style.visibility = "visible";
		this.element.style.left=this.x*SQ_SIZE + "px";
		this.element.style.top=this.y*SQ_SIZE + "px";
	}
	board[y][x] = this;
	this.gui.addFood(this);
	
	this.remove = function() {
		this.gui.removeFood(this);
		this.board[this.y][this.x] = null;
	};
	
	this.setPos = function(x, y) {
		this.x = x;
		this.y = y;
		this.element.style.top = y*SQ_SIZE + "px";
		this.element.style.left = x*SQ_SIZE + "px";
		this.board[y][x] = this;
	};
}

function Snake(type, nr, length, x, y, board, gui) {
	this.gui = gui;
	this.board = board;
	this.nr = nr;
	this.type = type;
	this.x = x; // Head x.
	this.y = y; // Head y.
	this.xTo = x; // Snake next coord.
	this.yTo = y;
	this.dir = DIR_UP;
	this.dirChangeQueue = new Array();
	this.speed = START_SPEED; // Squares per second.
	this.alive = true;
	this.moved = 0; // time when snake last moved.
	this.body = new Array();
	this.length = 1;
	this.grow = 0; // How much the snake should grow.
	this.score = 0;
	this.hungry = true; // If snake is hungry or not.
	this.hunger = 0; // How hungry snake is 0-9.
	this.withoutFood = 0; // How many squares snake has gone without food.

	// Init Snake.
	this.dirChangeQueue.push(DIR_UP);
	this.grow = length-1;
	this.body[0] = new Item(this.type, 0, x, y, board, gui);

	this.changeScore = function(amount) {
		if (amount == 0) {
			this.score = 0;
		} else {
			this.score += amount;
		}
		this.gui.setScore(this.nr, this.score);
	};

	this.changeSpeed = function(amount) {
		if (amount == 0) {
			this.speed = START_SPEED;
		} else {
			this.speed *= amount;
		}
		this.gui.setSpeed(this.nr, this.speed);
	};

	this.setHunger = function(amount) {
		this.hunger = amount;
		this.gui.setHunger(this.nr, amount);
	};

	this.eat = function(score, speed, grow) {
		this.changeScore(score);
		this.changeSpeed(speed);
		this.grow += grow;
		this.setHunger(0);
		this.withoutFood = 0;
	};

	this.die = function() {
		this.alive = false;
		this.gui.killSnake(this);
	};
	
	// Changes direction and updates position.
	this.prepareMove = function(time) {
		if (this.dirChangeQueue.length != 0) {
			var dirNew = this.dirChangeQueue.shift();
			var dirOpposite = dirNew - 2; // Get opposite dir.TODO: 
			if (dirOpposite < 1) {
				dirOpposite = 4 + dirOpposite;
			}
			if (this.dir != dirOpposite) {
				this.dir = dirNew; 
			}
		}
		this.moved = time;
		// Move head coords.
		switch (this.dir) {
			case DIR_UP: this.yTo--; break;
			case DIR_DOWN: this.yTo++; break;
			case DIR_LEFT: this.xTo--; break;
			case DIR_RIGHT: this.xTo++; break;
		}
		// Remove tail so we won't move into it when moving.
		var tail = this.body[this.length-1];
		var tx = tail.x;
		var ty = tail.y;
		this.board[ty][tx] = null;
	};

	// Moves the snake.
	this.move = function() {
		var tail = this.body[this.length-1];
		var tx = tail.x;
		var ty = tail.y;
		for (i=this.length-1; i>0; i--) {
			this.body[i].setPos(this.body[i-1].x, this.body[i-1].y, this.board);
		}
		this.body[0].setPos(this.xTo, this.yTo, this.board);
		// Tail has moved been moved in prepareMove.

		if (this.grow > 0) {
			this.length++;
			this.body[this.length-1] = 
				new Item(this.type, this.length-1, tx, ty, this.board, this.gui);
			this.grow--;
		} else if (this.grow < 0) {
			this.body[this.length-1].remove();
			this.length--;
			this.grow++;
			if (this.length == 0) {
				this.die();
			}
		}
	};

	this.handleHunger = function() {
		if (this.hungry) {
			this.withoutFood++;
			var tmp = this.withoutFood / HUNGER_STEP;
			this.setHunger(tmp);
			if (this.hunger > 10) { // Starving
				if (this.withoutFood % 2 == 0){
					this.changeScore(-1);
				}
				// Shrink snake every x:th step.
				if ((this.withoutFood - HUNGER_STEP*10) / STARVING_SHRINK_STEP  >= 1) {
					this.grow--;
					this.withoutFood -= STARVING_SHRINK_STEP;
				}
			}
		}
	};
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return 0;
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}