var System=function(){
	this.iTime = 0;
	this.msgCounter = 0;
	this.msgStayTime = 2500;
	this.body = document.getElementById('general');
	this.controlpanel = document.getElementById('controlpanel');
	this.pitch=10;
	this.attrs=new Array();
	this.attrs.specFieldBlock=new Array(new Array('className', 'specFieldBlock'));
	this.attrs.fieldSet=new Array(new Array('className', 'fieldSet'));
	this.attrs.label=new Array(new Array('className', 'label'));
	this.attrs.field=new Array(new Array('className', 'field'));
	this.attrs.leftButton=new Array(new Array('className', 'leftButtonDiv'));
	this.attrs.button=new Array(new Array('className', 'buttonDiv'));
	this.attrs.name=new Array(new Array('className', 'nameDiv'));
	this.attrs.text=new Array(new Array('className', 'textDiv'));
	this.attrs.file=new Array(new Array('className', 'fileDiv'));
	this.attrs.childs=new Array(new Array('className', 'childsDiv'));
	this.attrs.infoTable=new Array(new Array('className', 'infoTable'));
	this.attrs.selectionsRow=new Array(new Array('className', 'selectionsRow'));
	this.attrs.bigTextField=new Array(new Array('className', 'bigTextField'));
}

System.prototype={
	getPositiveNum : function(num){
		return (num>0) ? num : 0;
	},
	parseQueryString : function(){
		var aResults = new Array();
		var sInput = unescape(location.search.substr(1));
		if(sInput){
			var aSrch = sInput.split('&');
			var aTmp = new Array();
			for(var i=0;i<aSrch.length;i++){
				aTmp=aSrch[i].split("=");
				aResults[aTmp[0]]=aTmp[1];
			}
		}
		return aResults;
	},
	getFormData : function(formID){
		aParams=new Array();
		var sTmp;
		for(i=0;i<formID.elements.length;i++){
			if(formID.elements[i].type=='radio'){
				if(formID.elements[i].checked){
					sTmp=encodeURIComponent(formID.elements[i].name);
					sTmp+="=";
					value=formID.elements[i].value;
				sTmp+=encodeURIComponent(value);
				}
			}else{
				sTmp=encodeURIComponent(formID.elements[i].name);
				sTmp+="=";
				value=(formID.elements[i].type=='checkbox') ? ((formID.elements[i].checked) ? 'on' : 'off') : formID.elements[i].value;
				sTmp+=encodeURIComponent(value);
			}
			aParams.push(sTmp);
		}
		return aParams.join("&");
	}
}


var Messenger=function(text, cssClass){
	this.text =text;
	this.cssClass=cssClass;
    var msg= this;
	time=System.getPositiveNum((System.msgStayTime*System.msgCounter+100)-(new Date().getTime()-System.iTime))
	setTimeout(function(){msg.construct()}, time);
	System.msgCounter++;
	System.iTime=new Date().getTime();
}
Messenger.prototype = {
	construct : function(){
		this.node=document.createElement('div');
		this.node.id="alert";
		this.node.className=this.cssClass ? this.cssClass : 'warning';
		this.node.appendChild(document.createTextNode(this.text));
		document.getElementsByTagName('body')[0].appendChild(this.node);
	    var msg= this;
		this.oTime=setTimeout(function(){msg.destruct()}, System.msgStayTime);
		return null;
	},
	destruct : function(){
		document.getElementsByTagName('body')[0].removeChild(this.node);
		System.msgCounter--;
	}
}
var ControlPanel =function(){
}
ControlPanel.prototype = {
	outputButton : function(type){
		node=Constructor.outputImage(this.div, 'images/'+type+'.gif', '32', '32', '', 'controlImage');
		return node;
	}
}


var Constructor = {
	oTime : null,
	inputField : function(parent, name, value,  id, type, cssClass){
		try{
			node=document.createElement('input');
			node.id=(id) ? id : 'input'+Math.floor(Math.random() * 9999);
			node.name=name;
			node.value=(value) ? value : '';
			node.type=(type) ? type : 'text';
			(cssClass) ? node.className=cssClass : '';
			(parent) ? parent.appendChild(node) : document.appendChild(node);
			return node
		}catch(error){
			new Messenger(error, 'warning');
			return false;
		}
	},
	textareaField : function(parent, name, value,  id, cssClass){
		try{
			node=document.createElement('textarea');
			node.id=(id) ? id : 'textarea'+Math.floor(Math.random() * 9999);
			node.name=name;
			node.rows=5;
			node.cols=50;
			(value) ? node.appendChild(document.createTextNode(value)) : '';
			(cssClass) ? node.className=cssClass : '';
			(parent) ? parent.appendChild(node) : document.appendChild(node);
			return node
		}catch(error){
			new Messenger(error, 'warning');
			return false;
		}
	},
	selectField : function(parent, name, optionList, value,  id, type, cssClass){
		try{
			node=document.createElement('select');
			node.id=(id) ? id : 'select'+Math.floor(Math.random() * 9999);
			node.name=name;
			(cssClass) ? node.className=cssClass : '';
			for(var i=0;i<optionList.length;i++){
				option=document.createElement('option');
				spaces='';
				for(j=1;j<optionList[i][2];j++){
					spaces+='-';
				}
				option.value=optionList[i][0];
				option.appendChild(document.createTextNode(spaces+''+optionList[i][1]));
				(optionList[i][0]==value) ? option.selected='selected' : '';
				node.appendChild(option);
			}
			(parent) ? parent.appendChild(node) : document.appendChild(node);
			return node
		}catch(error){
			new Messenger(error, 'warning');
			return false;
		}
	},
	form : function(parent, action, id, method){
		try{
			node=document.createElement('form');
			node.id=(id) ? id : 'form'+Math.floor(Math.random() * 9999);
			node.action=(action) ? action : '';
			node.enctype="multipart/form-data";
			node.method=(method) ? method : 'post';
			(parent) ? parent.appendChild(node) : document.getElementsByTagName('body')[0].appendChild(node);
			return node
		}catch(error){
			new Messenger(error, 'warning');
			return false;
		}
	},
	htmlElement : function(parent, tag, id, text, attributes){

		try{
			node=document.createElement(tag);
			node.id=(id) ? id : tag+Math.floor(Math.random() * 9999);
			if(attributes){
				for(var i=0;i<attributes.length;i++){
					(attributes[i][0].split(".")[1]) ? node[attributes[i][0].split(".")[0]][attributes[i][0].split(".")[1]]=attributes[i][1] : node[attributes[i][0]]=attributes[i][1];
				}
			}
			(text) ? node.appendChild(document.createTextNode(text)) : '';
			(parent) ? parent.appendChild(node) : document.getElementsByTagName('body')[0].appendChild(node);
			return node
		}catch(error){
			new Messenger(error, 'warning');
			return false;
		}
	},
	outputImage : function(parent, src, width, height, alt, cssClass){
		image=Constructor.htmlElement(parent, 'img', '', '', '');
		image.src=src;
		image.width=(width) ? width : '';
		image.height=(height) ? height : '';
		(cssClass) ? image.className= cssClass : '';
		image.alt=(alt) ? alt : '';
		return image;
	},
	outputFieldSet : function(parent, labelText, fieldName, fieldID, fieldType, fieldValue, optionList, className){
		tmp=(className) ? new Array(new Array('className', className)) : System.attrs.fieldSet;
		div=Constructor.htmlElement(parent, 'div', '', '', tmp);
		Constructor.htmlElement(div, 'div', '', labelText, System.attrs.label);
		fieldBlock=Constructor.htmlElement(div, 'div', '', '', System.attrs.field);
		switch(fieldType){
			case "text":
			case "password":
			case "radio":
			case "file":
			case "checkbox":
			case "submit":
			case "button":
				field=Constructor.inputField(fieldBlock, fieldName, fieldValue,fieldID,fieldType);
				break;
			case "select":
				field=Constructor.selectField(fieldBlock, fieldName, optionList, fieldValue,  fieldID);
				break
			case "textarea":
				field=Constructor.textareaField(fieldBlock, fieldName, fieldValue,  fieldID);
				break
		}
		return field;
	}
}

var upload = {
	newIframe : function(){
		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe width="0" height="0" frameborder="0" style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="upload.loaded(\''+n+'\')"></iframe>';
		document.body.appendChild(d);
		return n;
	},

	form : function (f, name) {
		f.setAttribute('target', name);
	},

	updateImage : function (id){
		oAvatar=document.getElementById('avatar');	
		string=id.body.innerHTML;
		(string=='done') ? new Messenger('Изменения внесены', 'success') : new Messenger('Произошла ошибка', 'warning');
		if(string=='done'){
			System.body.removeChild(elements.form);
			System.controlpanel.removeChild(oControlPanel.div);
			Viewer();
		}
	},

	loaded : function (id){
		var i = document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
				var d = window.frames[id].document;
		}
		if (d.location.href == "about:blank") {
			return;
		}
		this.updateImage(d);
	},

	deleteFile : function(){

	}
}

