/*
	Copyright 2006 Kalifiye.net

	Coded by Semih TURNA
	a.k.a PsyChaos <psychaos[@]gmail[.]com>
*/

var baseSrc = $('jsKalifiye').src;
var qS = baseSrc;
	qS = (qS.indexOf('?') != -1) ? qS.substr(qS.indexOf('?')+1,qS.length) : '';

var browser=navigator.appName + " " + navigator.appVersion;
var getkey=browser.substring(0, 12);

if (browser.substring(0, 9)=="Microsoft"){
	document.writeln("<"+"script type=\"text/javascript\" src=\""+ qS +"js/png.js\">"+"<"+"/script>");
}
document.writeln("<"+"script type=\"text/javascript\" src=\""+ qS +"js/base64.js\">"+"<"+"/script>");
document.writeln("<"+"script type=\"text/javascript\" src=\""+ qS +"js/JSON.js\">"+"<"+"/script>");
document.writeln("<"+"script type=\"text/javascript\" src=\""+ qS +"js/effects.js\">"+"<"+"/script>");
document.writeln("<"+"script type=\"text/javascript\" src=\""+ qS +"js/window.js\">"+"<"+"/script>");
document.writeln("<"+"script type=\"text/javascript\" src=\""+ qS +"js/flashobject.js\">"+"<"+"/script>");
document.writeln("<"+"script type=\"text/javascript\" src=\""+ qS +"js/sifr.js\">"+"<"+"/script>");
document.writeln("<"+"script type=\"text/javascript\" src=\""+ qS +"js/sifr-config.js\">"+"<"+"/script>");

var KALIFIYE = Class.create();
	KALIFIYE.prototype = {
		initialize : function(){
		},

		__doPostBack : function(o){
			(new KALIFIYE()).ViewState().serialize(o);
				(new KALIFIYE()).ViewState().__hiddenPostBackElement(o);
		},
		
		/* ClientSide ViewState v0.0.1b - [12.06.2006] */

		ViewState : function(){
			var vs = {
				initialize : function(dbg){
					var _forms = $$('FORM');
						_forms.each(function(f){
							if(f.getAttribute('kalifiye:viewstate') == "true"){	
								Event.observe(f, 'submit', function(){ (new KALIFIYE()).__doPostBack(f); }, false);
							}
						});
				},

				__hiddenViewStateElement : function(o){
					var vse = document.createElement('INPUT');
						vse.setAttribute('id',		'__VIEWSTATE:'+ o.id);
						vse.setAttribute('name',	'__VIEWSTATE:'+ o.id);
						vse.setAttribute('type',	'hidden');

					o.insertBefore(vse, o.firstChild);
				},

				__hiddenPostBackElement : function(o){
					var pbe = document.createElement('INPUT');
						pbe.setAttribute('id',		'__POSTBACK:'+ o.id);
						pbe.setAttribute('name',	'__POSTBACK:'+ o.id);
						pbe.setAttribute('type',	'hidden');
						pbe.setAttribute('value',	'true');


					o.insertBefore(pbe, o.firstChild);
				},

				serialize : function(o){
					var params = '';
					var _elements = Form.getElements(o);
						_elements.each(function(e){
							if(e.id != '__VIEWSTATE:'+ o.id){
								if(e.type == 'text' || e.type == 'hidden' || e.type == 'textarea'){
									//if(e.value.length != '') BUG ?
									params += '\''+ e.id +'\' : {\'value\' : \''+ encodeURIComponent(e.value) +'\', \'display\' : \''+ ((e.style.display == '') ? 'inline' : e.style.display) +'\'},';
								}
								else if((e.type == 'radio' || e.type == 'checkbox') && e.checked){
									//(e.type == 'radio' || e.type == 'checkbox') && e.checked) BUG?
									params += '\''+ e.id +'\' : {\'value\' : \''+ encodeURIComponent(e.value) +'\', \'display\' : \''+ ((e.style.display == '') ? 'inline' : e.style.display) +'\'},';
								}
								else if(e.type == 'select-one'){
									//(e.type == 'select-one') && e.selectedIndex != -1 BUG?
									params += '\''+ e.id +'\' : {\'value\' : \''+ encodeURIComponent(e.options[e.selectedIndex].value) +'\', \'display\' : \''+ ((e.style.display == '') ? 'inline' : e.style.display) +'\'},';
								}
								else if(e.type == 'select-multiple'){
									//(e.type == 'select-multiple') && e.selectedIndex != -1 BUG?
									var mt = '';
									for(var m = 0; m < e.length; m++){
										if(e.options[m].selected){
											mt += '\''+ encodeURIComponent(e.options[m].value) +'\',';
										}
									}
									mt = '['+ mt.substr(0,mt.length-1) +']';
									params += '\''+ e.id +'\' : {\'value\' : '+ mt +', \'display\' : \''+ ((e.style.display == '') ? 'inline' : e.style.display) +'\'},';
								}
							}
						});

						eval('var json_object = {'+ params.substr(0,params.length-1) +'};');
						if(json_object){
							if($('__VIEWSTATE:'+ o.id)){
								$('__VIEWSTATE:'+ o.id).value = encode64(json_object.toJSONString());
							}
							else{
								throw('__VIEWSTATE:'+ o.id +' kontrolü bulunamadı!');
							}
						}
						else{
							throw('json_object isimli nesne yaratılamadı!');
						}
				},

				deserialize : function(o,debug){
					if($('__VIEWSTATE:'+ o.id) && $('__VIEWSTATE:'+ o.id).value.length != 0){
						var json_object = decode64($('__VIEWSTATE:'+ o.id).value).parseJSON();
							var _elements = Form.getElements(o);
								_elements.each(function(e){
									if(e.id != '__VIEWSTATE:'+ o.id){
										var tmpObject = (typeof eval('json_object.'+ e.id) != 'undefined') ? eval('json_object.'+ e.id) : null;
										if(tmpObject){
											if(e.type == 'text' || e.type == 'hidden' || e.type == 'textarea'){
												e.value = decodeURIComponent(tmpObject.value);
												e.style.display = tmpObject.display;
											}
											else if(e.type == 'radio'){
												if(e.value == decodeURIComponent(tmpObject.value)){
													e.checked = true;
												}
												e.style.display = tmpObject.display;
											}
											else if(e.type == 'checkbox'){
												if(e.value == decodeURIComponent(tmpObject.value)){
													e.checked = true;
												}
												e.style.display = tmpObject.display;
											}
											else if(e.type == 'select-one'){
												var options = e.getElementsByTagName('option');
													options = $A(options);
													options.each(function(sb){
														if(sb.value == decodeURIComponent(tmpObject.value)){
															sb.selected = true;
														}
													});
												e.style.display = tmpObject.display;
											}
											else if(e.type == 'select-multiple'){
												var options = e.getElementsByTagName('option');
													options = $A(options);
													options.each(function(sb){
														for(var i = 0; i < tmpObject.value.length; i++){
															if(sb.value == decodeURIComponent(tmpObject.value[i])){
																sb.selected = true;
															}
														}
													});
												e.style.display = tmpObject.display;
											}
										}
									}
								});

						if(parseInt(debug) == 1) this.getFormElements_JSONString(o);
					}
					
				},

				getFormElements_JSONString : function(o){
					var fm = document.createElement('DIV');
						fm.id = 'FM_JSONString';
						fm.innerHTML = '<pre>'+ decode64($('__VIEWSTATE:'+ o.id).value) +'</pre>';
					

					//document.body.insertBefore(fm, o.nextSibling);
					o.insertBefore(fm, o.firstChild);
				}
			}

			return vs;
		},
		
		/* Validator v0.0.1b - [19.07.2006]  */
		
		validator : function(){
			var v = {
				form : null,
				controls : [],
				
				initialize : function(o){
					var _self = this;
						_self.form = $(o);

					if(_self.form){
						Form.getElements(_self.form).each(function(c){
							if(c.id != '__VIEWSTATE:'+ _self.form.id && c.id != '__POSTBACK:'+ _self.form.id && c.type != 'submit' && c.type != 'button'){
								_self.controls.push(c);
							}
							if(c.getAttribute('kalifiye:callback')) c.onclick = _self.callback;
						});
					}
					else{
						alert('Validator Yüklenirken Hata Oluştu!');
					}
				},
				
				validateFactory : function(){
					var vf = {
						pattern : {
							'password' 			: /(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,15})$/,
							'email' 			: /^[\w\.=-]+@[\w\.-]+\.[\w\.-]{2,4}$/,
							'date' 				: /^(0?[1-9]|[12][0-9]|3[01])[/ \- .](0?[1-9]|1[012])[/ \- .](19[0-9]{2}|[2][0-9][0-9]{2})$/,
							'web' 				: /^(http)s?:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?$/,
							'lettersonly' 		: /^[a-zA-Z]*$/,
							'alphanumeric' 		: /^\w*$/,
							'integer' 			: /^-?\d\d*$/,
							'positiveinteger' 	: /^\d\d*$/,
							'number' 			: /^-?(\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/,
							'creditcard'		: /^((67\d{2})|(4\d{3})|(5[1-5]\d{2})|(6011))(-?\s?\d{4}){3}|(3[4,7])\d{2}-?\s?\d{6}-?\s?\d{5}$/
						},
						
						isEmpty : function(o){
							return (o.value == '' && o.value.length == 0);
						},
						
						maxlength : function(o,mxv){
							return (parseInt(o.value.length) > parseInt(mxv));
						},
						
						minlength : function(o,mnv){
							return (parseInt(o.value.length) < parseInt(mnv));
						},
						
						equalto : function(a,b){
							return (b.value == a.value);
						},
						
						regex : function(o,p){
							return p.test(o.value);
						},
						
						minselected : function(o,mnv){
							return (parseInt(o.length) < parseInt(mnv));
						},
						
						maxselected : function(o,mxv){
							return (parseInt(o.length) > parseInt(mxv));
						}
					}
					return vf;
				},
				
				callback : function(){
					var formControl = new v.validateFactory();
					var flag = true;
					var message = new StringBuilder();
						message.append('Aşağıdaki Hatalar Oluştu:\r\n\r\n');
					var firstControl, ec = 0;
					
					v.controls.each(function(c){
						var att_required 		= c.getAttribute('kalifiye:required');
						var att_message 		= c.getAttribute('kalifiye:message');
						var att_focus 			= c.getAttribute('kalifiye:focus');
						var att_minlength 		= c.getAttribute('kalifiye:minlength');
						var att_maxlength 		= c.getAttribute('kalifiye:maxlength');
						var att_pattern 		= c.getAttribute('kalifiye:pattern');
						var att_compare 		= c.getAttribute('kalifiye:compare');
						var att_invalidindex 	= c.getAttribute('kalifiye:invalidindex');
						var att_invalidvalue	= c.getAttribute('kalifiye:invalidvalue');
						var att_minselected 	= c.getAttribute('kalifiye:minselected');
						var att_maxselected 	= c.getAttribute('kalifiye:maxselected');
								
						if(att_required){
							if(formControl.isEmpty(c)){
								flag = false; ec++;
								message.append('- '+ att_message +'\n');
							}
						}
						else if(att_minlength || att_maxlength){
							var mm = (formControl.minlength(c, att_minlength) || formControl.maxlength(c, att_maxlength)) ? false : true;
							if(!mm){
								flag = false; ec++;
								message.append('- '+ att_message +'\n');
							}
						}
						else if(att_pattern){
							switch(att_pattern){
								case 'password': 
									if(!formControl.regex(c, formControl.pattern.password)){
										flag = false;  ec++;
										message.append('- '+ att_message +'\n');
									}
									break;
								case 'email':
									if(!formControl.regex(c, formControl.pattern.email)){
										flag = false;  ec++;
										message.append('- '+ att_message +'\n');
									}
									break;
								case 'date':
									if(!formControl.regex(c, formControl.pattern.date)){
										flag = false;  ec++;
										message.append('- '+ att_message +'\n');
									}
									break;
								case 'web':
									if(!formControl.regex(c, formControl.pattern.web)){
										flag = false;  ec++;
										message.append('- '+ att_message +'\n');
									}
									break;
								case 'creditcard':
									if(!formControl.regex(c, formControl.pattern.creditcard)){
										flag = false;  ec++;
										message.append('- '+ att_message +'\n');
									}
									break;
								case 'positiveinteger':
									if(!formControl.regex(c, formControl.pattern.positiveinteger)){
										flag = false;  ec++;
										message.append('- '+ att_message +'\n');
									}
									break;
							}
						}
						else if(att_compare){
							if(!formControl.equalto(c, $(att_compare))){
								flag = false;  ec++;
								message.append('- '+ att_message +'\n');
							}
						}
						else if(att_invalidindex){
							if(c.selectedIndex == parseInt(att_invalidindex)){
								flag = false; ec++;
								message.append('- '+ att_message +'\n');
							}
						}
						else if(att_invalidvalue){
							if(c.options[c.selectedIndex].value == att_invalidvalue){
								flag = false; ec++;
								message.append('- '+ att_message +'\n');
							}
						}
						else if(att_minselected || att_maxselected){
							var mm = (formControl.minselected(c, att_minselected) || formControl.maxselected(c, att_maxselected)) ? false : true;
							if(!mm){
								flag = false; ec++;
								message.append('- '+ att_message +'\n');
							}
						}
						
						if(att_focus){
							if(!firstControl && ec == 1) firstControl = c;
						}
					});
					
					if(!flag) alert(message.toString()); if(firstControl) firstControl.focus();
					return flag;
				}
			}
			
			return v;
		},
		
		/* Password Meter v0.0.1b - [20.07.2006] */
		
		passwordMeter : function(){
			var pc = {
				rate : 0,
				
				process : function(password, callback){
					var len = password.length;
					
						// Password Length
							if(len < 5)
								this.rate += 3;
							else if(len > 4 && len < 8)
								this.rate += 6;
							else if(len > 7 && len < 16)
								this.rate += 12;
							else if(len > 15)
								this.rate += 18;
								
						// Letters
							if(password.match(/[a-z]/))
								this.rate += 1;
							
							if(password.match(/[A-Z]/))
								this.rate += 5;
								
						// Numbers
							if(password.match(/\d+/))
								this.rate += 5;
							
							if(password.match(/(.*[0-9].*[0-9].*[0-9])/))
								this.rate += 5;
								
						// Special Char
							if(password.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))
								this.rate += 5;
							
							if(password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))
								this.rate += 5;
								
						// Combos
							if(password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))
								this.rate += 2;
							
							if(password.match(/([a-zA-Z])/) && password.match(/([0-9])/))
								this.rate += 2;
								
							if(password.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
								this.rate += 2;
								
								
					callback(this.rate);
				}
			}
			return pc;
		},
		
		/* Modal Window */
		
		application : function(){
			var app = {				
				openDialog : function(msg){
					Dialog.alert('<div style=\"text-align:left;padding:3px;font-size:11px\">'+ msg +'</div>', 
                		{windowParameters : {className : 'alphacube', width : 350, showEffect : Element.show}, okLabel : 'kapat'});
				}
			}
			return app;
		}
		
		/*  */
	};
	
	/* multiple selectbox v0.0.1b - [03.08.2006] */
	KALIFIYE.prototype.multipleBox = function(mltp){
		var m = {
			maxlength : 5,
			nList : null,
			
			add : function(a,b){
				var m1 = $(a), m2 = $(b), flag;
					if(m1 && m2){
						for(var i = 0; i < m1.length; i++){
							if(m1.options[i].selected){
								if(m2.length == 0){
									flag = true;
								}
								else{
									flag = this.control(m2, m1.options[i].value);
								}
								
								if(flag) this.createOption({'control' : m2, 'text' : m1.options[i].text, 'value' : m1.options[i].value});
							}
						}
						
						this.selectAll(m2);
					}
					else{
						alert('Multiple nesneler bulunamadı!');
					}
			},
			
			remove : function(b){
				var m2 = $(b);
					if(m2){
						for(var i = (m2.length -1); i >= 0; i--){
							if(m2.options[i].selected) m2.options[i] = null;
						}
					
						this.selectAll(m2);
					}
					else{
						alert('Multiple nesneler bulunamadı!');
					}
			},
			
			addAll : function(a,b){
				var m1 = $(a), m2 = $(b), flag;
					if(m1 && m2){
						for(var i = 0; i < m1.length; i++){
							if(m2.length == 0){
								flag = true;
							}
							else{
								flag = this.control(m2, m1.options[i].value);
							}
							
							if(flag) this.createOption({'control' : m2, 'text' : m1.options[i].text, 'value' : m1.options[i].value});
						}
						
						this.selectAll(m2);
					}
					else{
						alert('Multiple nesneler bulunamadı!');
					}
			},
			
			removeAll : function(b){
				var m2 = $(b);
					for (tmp in m2.options) m2.options[0] = null;
			},
			
			selectAll : function(b){
				var m2 = $(b);
					for(var i = 0; i < m2.length; i++){
						m2.options[i].selected = true;	
					}
			},
			
			control : function(){
				var args = this.control.arguments, flag = true;
				for(var i = 0; i < args[0].length; i++){
					if(args[1] == args[0].options[i].value){
						flag = false;
						break;
					}
				}
				
				return flag;
			},
			
			createOption : function(c){	
				var nO = document.createElement("OPTION");
					nO.value 	= c.value;
					nO.text 	= c.text;
				c.control.options.add(nO);
			}
		}
		
		return m;
	};
	
	/* TabView v0.0.1b - [10.08.2006] */
	KALIFIYE.prototype.tabView = function(){
		var tv = {
			tabObject : null,
			
			initialize : function(n){
				this.tabObject = $('kalifiye_tabView').getElementsByTagName('div');
				if(this.tabObject){
					var id = 0;
					for(var i = 0; i < this.tabObject.length; i++){
						if(this.tabObject[i].className == 'tabView_content'){
							id++;
							this.tabObject[i].id = 'tabView_content__'+ id;
							
							if(!parseInt(n)){
								if(id == 1) { this.tabObject[i].style.display = 'block'; this.current(0); }
							}
								
								var t = $('tabView_content__'+ n);
									if(t) { t.style.display = 'block'; this.current(n-1); }
						}
					}
				}
			},
			
			tab : function(n){
				var tvC = $('tabView_content__'+ n);
				if(tvC && this.tabObject){
					for(var i = 0; i < this.tabObject.length; i++){
						if(this.tabObject[i].className == 'tabView_content'){
							this.tabObject[i].style.display = 'none';
						}
					}
					this.current(n-1);
					tvC.style.display = 'block';
				}
			},
			
			current : function(n){
				if(this.tabObject){
					var a = $('tabView_navList').getElementsByTagName('a');
					
					for(var i = 0; i < a.length; i++){
						a[i].className = '';
					}
					a[n].className = 'current';
				}
			}
		}
		
		return tv;
	};
	
	
	/* HTML Controls Storage v0.0.1b - [14.08.2006] */
	KALIFIYE.prototype.htmlControls = function(param){
		var hC = {
			controls : null,
						
			initialize : function(){
				this.controls = new Array();
				
				if(param) this.add(param);
			},
			
			add : function(param){
				if(param && this.controls.isArray()) this.controls.push(param);
			},
			
			clear : function(){
				if(this.controls.isArray()) this.controls.length = 1
			}		
		}
		
			hC.initialize();
		return hC;
	};
	

var StringBuilder = function(param){
	this.strings = new Array();
	this.append(param);
};

StringBuilder.prototype.append = function(param){
	if(param) this.strings.push(param);
};

StringBuilder.prototype.clear = function(){
	this.strings.length = 1
};

StringBuilder.prototype.toString = function(){
	return this.strings.join('');
};

Array.prototype.isArray = function(){
	if(this.constructor.toString().indexOf("Array") == -1)
		return false;
	else
		return true;
};

Array.prototype.inArray = function(param){
	var l = this.length;
	var i = l;
	if(l > 0){
		do{
			if(this[i] == param) return true;
		}while(i--);
	}
	return false;
};


/* ViewState Load */
	Event.observe(window, 'load', (new KALIFIYE()).ViewState().initialize, false);
/* IE - PNG Configure */
	if(window.attachEvent){
		Event.observe(window, 'beforeprint', function(){ beforePrint(); }, false);
		Event.observe(window, 'afterprint', function(){ afterPrint(); }, false);
	}

function PopupAc(theURL,winname,features) { window.open(theURL,winname,features); }

function karakterfiltre(deger) {
	var yaz = new String();
	var GecerliDizi = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,_-";
	var chars = deger.value.split("");

		for (i = 0; i < chars.length; i++) {
			if (GecerliDizi.indexOf(chars[i]) != -1)
				yaz += chars[i];
				else
				alert("Uyarı : Kullanıcı Adı ve Şifre alanlarında \nTürkçe ve özel karakterler kullanamazsınız.");
				deger.focus();
		}

	if (deger.value != yaz)
		deger.value = yaz;
}

function AdreseGit(theURL) {
	var theURL;
	window.location = theURL;
	}