﻿if (!Array.prototype.push) {
	Array.prototype.push=function () {
		for (var iArg=0;iArg<arguments.length;iArg++) this[this.length]=arguments[iArg];
		return this.length;
	}
}
Array.prototype.indexOf=function (v) {
	var i=0;
	while (i<this.length && this[i]!=v) i++;
	return i==this.length ? -1 : i;
}
Array.prototype.remove=function (iIndex) {
	if (iIndex<0 || iIndex>=this.length) return;
	for (var i=iIndex+1;i<this.length;i++) this[i-1]=this[i];
	this.length--;

	return this;
}
String.prototype.ToRX=function () {
	return this.replace(/(\(|\)|\{|\}|\[|\]|\:|\^|\$|\!|\=|\+|\*|\/|\,|\-|\||\?)/g,"\\$1");
}
String.prototype.toRX=String.prototype.ToRX;
Object.prototype.Hash=function (funcs) {
	var s="\n";
	for (var i in this) {
		if (typeof this[i]=="function" && !funcs) continue;
		s+=i+"\t\t"+this[i]+"\n";
	}
	return s;
}

Text.Date=function (date,format) {
	if (!date instanceof Date) date=new Date(date) || new Date();

	format=format || "h:I:S d/m/Y";
	format=format.split("");

	var a=[];
	for (var i=0;i<format.length;i++) a.push(Date.Format[format[i]] ?  Date.Format[format[i]](date) : format[i]);

	return a.join("");
}
// formats date
Date.prototype.Format=function (format) {
	return Text.Date(this,format);
}
/*
Intervals

d: 1-31
D: 01-31
m: 1-12
M: 01-12
n: Jan-Dec
N: January-December
B: ינואר-דצמבר
y: 00-99
Y: 1-9999
h: 0-23
H: 00-23
i: 0-59
I: 00-59
s: 0-59
S: 00-59
*/
Date.Format={
	Months:["January","February","March","April","May","June","July","August","September","October","November","December"],
	HebMonths:["ינואר","פברואר","מרץ","אפריל","מאי","יוני","יולי","אוגוסט","ספטמבר","אוקטובר","נובמבר","דצמבר"],
	
	d:function (o) { return o.getDate(); },
	D:function (o) { return Text.Pad(this.d(o)); },
	m:function (o) { return o.getMonth()+1; },
	M:function (o) { return Text.Pad(this.m(o)); },
	n:function (o) { return this.N(o).substr(0,3); },
	N:function (o) { return this.Months[this.m(o)-1]; },
	B:function (o) { return this.HebMonths[this.m(o)-1]; },
	y:function (o) { return (""+this.Y(o)).substr(2); },
	Y:function (o) { return o.getFullYear(); },
	h:function (o) { return o.getHours(); },
	H:function (o) { return Text.Pad(this.h(o)); },
	i:function (o) { return o.getMinutes(); },
	I:function (o) { return Text.Pad(this.i(o)); },
	s:function (o) { return o.getSeconds(); },
	S:function (o) { return Text.Pad(this.s(o)); }
}

Object.Primitive=[String,Number,RegExp,Boolean,Date];
Object.prototype.Clone=function () {
	if (Object.Primitive.indexOf(this.constructor)>-1) return this;
	var o=new this.constructor();
	for (p in this) o[p]=this[p].nodeName ? this[p] : typeof this[p]=="object" ? this[p].Clone() : this[p].valueOf();
	return o;
}

function copy(s) {
	if (window.clipboardData) clipboardData.setData("Text",s);
}

function QS() {
}
QS.Init=function () {
	this.Length=0;
	this.Data=[];

	var qs=location.href.split("?")[1],
		item;

	if (!qs) return;

	qs=qs.split("&");
	for (var i=0;i<qs.length;i++) {
		item=qs[i].split("=");
		item[1]=unescape(item[1]) || "";
		this.Data[item[0].toLowerCase()]=item[1];
		this.Data[i]=new QS.Item(item[0],item[1]);
		this.Length++;
	}
}
QS.Get=function (item) {
	return this.Data[(item+"").toLowerCase()];
}
QS.Item=function (name,value) {
	this.Name=name;
	this.Value=value;
}
QS.Item.prototype.toString=function () {
	return this.Name+"="+this.Value;
}
QS.Add=function () {
	var a;
	// find out what agruments sent

	// "name","value"
	if (typeof arguments[0]=="string" && arguments[1]!=undefined) a=[new this.Item(arguments[0],arguments[1])];
	// new QS.Item("name","value")
	else if (arguments[0] instanceof Array) a=arguments[0];
	else a=arguments;
	// else [new QS.Item("name","value"),new QS.Item("name2","value2")]

	var qs="",
		hash="",
		rxQS,
		item,value;

	if (/\?([^#]*)(#.*)?$/.test(location.href)) {
		qs=RegExp.$1;
		hash=RegExp.$2;
	}

	for (var i=0;i<a.length;i++) {
		if (!(a[i] instanceof QS.Item)) continue;
		item=a[i].Name;
		value=a[i].Value;

		rxQS=new RegExp("(^|&)"+item.toRX()+"=?[^\&#]*(&|$)");
		qs=rxQS.test(qs) ? qs.replace(rxQS,"$1"+item+"="+value+"$2") : qs+(qs ? "&" : "")+item+"="+value;
	}
	location.href="?"+qs+hash;
}

function Cookies() {
}
Cookies.Get=function (name) {
	if (DOM.doc.cookie && new RegExp("\\b"+name+"=([^;]*)").test(DOM.doc.cookie)) return unescape(RegExp.$1);
}
Cookies.Set=function (name,value) {
	var d=new Date();
	d.setTime(d.getTime()+365*24*3600*1000);

	DOM.doc.cookie=name+"="+escape(value)+"; expires="+d.toGMTString();
	//DOM.doc.cookie=name+"="+escape(value)+"; path=/; expires="+d.toGMTString();
}
Cookies.Del=function (name) {
	if (this.Get(name)) DOM.doc.cookie=name+"=; expires=Thu, 01-Jan-70 00:00:01 UTC";
}

QS.Init();

function Text() {
}
Text.Pad=function (s,n) {
	n=n || 2;
	return "000000000000000000000".substr(0,n-(""+s).length)+s;
}