String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
String.prototype.ltrim = function() { return this.replace( /^\s*/, '' ); };
String.prototype.rtrim = function() { return this.replace( /\s*$/, '' ); };

function noCache(uri){return uri.concat(/\?/.test(uri)?"&":"?","noCache=",(new Date).getTime(),".",Math.random()*1234567)};

function Collection() {
/* --CCollection object-- */
     var lsize = 0;
	 
     this.add = _add;
     this.remove = _remove;
     this.isEmpty = _isEmpty;
     this.size = _size;
     this.clear = _clear;
     this.clone = _clone;
	 this.insertAt = _insertAt;
	 this.replaceItem = _replaceItem;
	 
     function _add(newItem) {
     /* --adds a new item to the collection-- */
          if (newItem == null) return;

          lsize++;
          this[(lsize - 1)] = newItem;
		  //Erencans, 26.04.2007
		  return lsize-1;		  
     }

     function _remove(index) {
     /* --removes the item at the specified index-- */
          if (index < 0 || index > this.length - 1) return;
          this[index] = null;

          /* --reindex collection-- */
          for (var i = index; i <= lsize; i++)
               this[i] = this[i + 1];

          lsize--;
     }

     function _isEmpty() { return lsize == 0 }     /* --returns boolean if collection is/isn't empty-- */

     function _size() { return lsize }     /* --returns the size of the collection-- */

     function _clear() {
     /* --clears the collection-- */
          for (var i = 0; i < lsize; i++)
               this[i] = null;

          lsize = 0;
     }

	 //Erencans, 26.04.2007
	 //Verilen itemi verilen indexe göre araya sokar.
	 function _insertAt(index,item)
	 {
		  var item2;
		  var item3;
		  
		  item2=item;
		  for (var i = 0; i < lsize; i++) {
			   if (index==i){
					item2=this[i];
					this[i]=item;
			   }
			   else if (i>index){
					item3=this[i];
					this[i]=item2;
					item2=item3;
			   }
			}
			if (index<=lsize) this.add(item2);
		   
	 }
	 
	 //erencans, 07.06.2007
	 //verilen indexdeki itemleri yer değiştirir.
	 function _replaceItem(firstIndex,secondIndex){
	 	var dummy;
	 	dummy=this[firstIndex];
	 	this[firstIndex]=this[secondIndex];
	 	this[secondIndex]=dummy;
	 }
	
     function _clone() {
     /* --returns a copy of the collection-- */
          var c = new Collection();

          for (var i = 0; i < lsize; i++)
               c.add(this[i]);

          return c;
     }
}

//Tanım kategorisi şartı sınıfı
function ClassDefCategoryCondition(){
	this.categoryID=0;
	this.conditionList=new Collection();
	
	this.generateConditionStr=function _generateConditionStr(){
		var conditionStr="";
		var arrFormData="";
		var value="";
		
		for(var ii=0;ii<this.conditionList.size();ii++){
			//Form verilerinde şartlar checkbox larda seçtirildiği için kendi aralarında virgül ile ayrılıyor.
			//Aralarında ise boru karakteri ile ayırt ediliyor. Ayrıca virgüller OR anlamına geliyor.
			if (this.conditionList[ii].type==1 && this.conditionList[ii].value!=""){ //Eğer for tipinde ise ve alanlar değilde formun kendisi seçilmiş ise girme.
				value=this.conditionList[ii].value;
				//value=value.substr(0,value.length-1);
				arrFormData=value.split("|");
				
				var displayStr="";
				for(var kk=0;kk<arrFormData.length;kk++){
					var arrConditionInfo=arrFormData[kk].split(",");
					displayStr+=arrConditionInfo[1] + " <b>VEYA</b> "; //1. numaralı elementte görünmesini istediğimiz yazı bulunuyor.
				}
				conditionStr+=displayStr.substr(0,displayStr.length-13); // VEYA 
			}
			else
				conditionStr+=this.conditionList[ii].displayValue;
		}
		
		return conditionStr;
	}
	
	this.generateXML=function _generateXML(){
		var xml="<XML_DATA>";
		var condition;
		
		xml+="<CONDITIONS>"
		for(var ii=0;ii<this.conditionList.size();ii++){
			condition=this.conditionList[ii];
			xml+="<CONDITION CAT_ID='"+this.categoryID+"' FORM_ID='"+condition.formID+"' FIELD_ID='"+condition.fieldID+"' TYPE='"+condition.type+"' ";
			xml+="VALUE='"+condition.value.replace(RegExp("&&","g"),"[]")+"' DISPLAY_VALUE='"+condition.displayValue+"' ";
			xml+="INSERT_RIGTH='"+condition.insertRight.toString()+ "' UPDATE_RIGHT='"+condition.updateRight.toString()+"' ";
			xml+="DELETE_RIGHT='"+condition.deleteRight.toString() + "' FILE_UPLOAD_RIGHT='"+condition.fileUploadRight.toString()+"' ";
			xml+="QUERY_RIGHT='"+condition.queryRight.toString() + "' RECORD_LIST_RIGHT='"+condition.recordListRight.toString()+"' ";
			xml+="LIST_AUTO_CHOOSE_FIELD='"+condition.listAutoChooseFieldRight.toString() + "' LOGIN_REQUIRED='"+condition.loginRequired.toString()+"' ";
			xml+="RIGHT_PRIORITY='"+condition.rightPriority +"' LINE_NUMBER='"+condition.lineNumber+"'></CONDITION>";
		}
		xml+="</CONDITIONS></XML_DATA>";
		return xml;
	}
}

//Tanım kategorisi şartı
function DefCategoryCondition(){
	this.type=0; //1=form verisi,2-metin
	this.formID=0;
	this.formName="";
	this.fieldID=0;
	this.value=0; //form verisi ise seçilen değerlerin id leri...formid field id value id gibi...
	this.displayValue=""; //şart görüntülendiğinde görünecek değer.
	this.lineNumber=-1;
	
	this.insertRight=false;
	this.updateRight=false;
	this.deleteRight=false;
	this.fileUploadRight=false;
	this.queryRight=false;
	this.recordListRight=false;
	this.listAutoChooseFieldRight=false;
	this.loginRequired=false;
	this.rightPriority=1;
	
	this.getDisplayValue=function _getDisplayValue(){
		if (this.type==2 || this.type==3)
			return this.displayValue;
		else if (this.type==1){
			if (this.value!=""){
				var value=this.value;
				//value=value.substr(0,value.length-1);
				arrFormData=value.split("|");
				
				var displayStr="";
				for(var kk=0;kk<arrFormData.length;kk++){
					var arrConditionInfo=arrFormData[kk].split(",");
					displayStr+=arrConditionInfo[1] + " VEYA "; //3. numaralı elementte görünmesini istediğimiz yazı bulunuyor.
				}
				displayStr=displayStr.substr(0,displayStr.length-6); // VEYA 
			}
			else if (this.displayValue!=""){
				displayStr=this.displayValue;
			}
			return displayStr;
		}
		else return "Boş Koşul";
	}
}

function SelectedFormValueList(){
	this.valueList=new Collection();
	
	this.generateSelectedsXML=function _generateSelectedsXML(){
		var xml="<XML_DATA>";
		for(var ii=0;ii<this.valueList.size();ii++){
			var formValue=this.valueList[ii];
			xml+="<FORM_VALUE FORM_ID='"+formValue.formID+"' FIELD_ID='"+formValue.fieldID+"' CONDITION_TYPE='"+formValue.conditionType+"' ";
			xml+="CONDITION_VALUE='"+formValue.conditionValue.toString().replace(RegExp("&&","g"),"[]")+"' DISPLAY_TEXT='"+formValue.displayText+"'>"
			xml+="<CONDITION_LIST>";
			for(var kk=0;kk<formValue.conditionList.valueList.size();kk++){
				xml+=formValue.conditionList.generateSelectedsXML();
			}
			xml+="</CONDITION_LIST>";
			xml+="</FORM_VALUE>"; 
		}
		xml+="</XML_DATA>";
		
		return xml;
	}
}

function ClassFormValue(){
	this.formID=0;
	this.fieldID=0;
	this.displayText="";
	this.conditionType=0;
	this.conditionValue="";
	this.conditionList=new SelectedFormValueList(); //Eğer şart listesi birden fazla ise bu listeye alıyoruz.
	/*	
	this.generateXML=function _generateXML(){
		xml="<FORM_VALUE FORM_ID='"+this.formID+"' FIELD_ID='"+this.fieldID+"' CONDITION_TYPE='"+this.conditionType+"' ";
		xml+="CONDITION_VALUE='"+this.conditionValue.replace(RegExp("&&","g"),"[]")+"' DISPLAY_TEXT='"+this.displayText+"'></FORM_VALUE>"
		return xml;
	}
	*/
}

function ClassStyleList(){
	this.styles=new Collection();
}

function ClassStyle(){
	this.styleID=0;
	this.styleName="";
	this.cssText="";
}

//Erencan tarafından geliştirilmiştir. 05.05.2007
function StyleGenerator() {
     var lsize = 0;
	 
     this.add = _add;
     this.remove = _remove;
     this.isEmpty = _isEmpty;
     this.size = _size;
     this.clear = _clear;
     
	 this.generateStyle = function _generateStyle()
        {
			var css = "";
			for (var i= 0;i < lsize;i++){
				if (this[i].styleValue.trim() != "")
					css+=this[i].styleName + ":" + this[i].styleValue + ";";
			}
			return css;
        }
     
     //Verilen objeye stili uygular.
     this.applyToObject = function _applyToObject(objectId)
        {
			for (var i= 0;i < lsize;i++){
				//Display stilinde görünürde olması için boşluk atanması gerekiyor.
				this[i].styleValue=this[i].styleValue.trim();
				if (this[i].styleValue != "" || this[i].styleName=="display"){
					if (this[i].styleValue.indexOf("#")>-1){
						this[i].styleValue=this[i].styleValue.replace("#","");
						var str="000000"+this[i].styleValue;
						this[i].styleValue=str.substr(str.length-6,6);
					}
					eval("document.getElementById(\""+objectId+"\").style."+this[i].styleName+"='"+this[i].styleValue+"';");
				}
			}            
        }
     
     //Verilen objedeki stilleri generator objesine yükler.Bu objeyi haliyle daha sonra istediğimiz yerde kullanabiliriz.
     this.loadStyle = function loadStyleToGenerator(object)
        {
            for (s in object.style){
                //Css text i ayrı tutuyoruz. Çünkü başka bir objeye uygularken css text i de eski özellikler ile uyguluyor.
				if (object.style[s] != "" && s!="cssText"){
                    this.add(s,object.style[s]);
                }
             }
                
        }
     
	 this.getStyleByName = function _getStyleByName(styleName)
	    {
		  for (var k=0;k < lsize;k++)
		  {
			if (this[k].styleName==styleName) break;
		  }
		  
		  if (k > lsize - 1) return null
			else return this[k];
	    }
	 
	 function _style(){
		this.styleName="";
		this.styleValue="";
	 }
	 
     function _add(styleName,styleValue) {
          if (styleName == null) return;
          if (typeof styleValue!="string") return;
		  if (!isNaN(styleName)) return;
		  
		  var style=this.getStyleByName(styleName);
		  if (style != null)
		  {
			style.styleName=styleName;
			style.styleValue=styleValue;
		    return;	
		  }
		  
		  style = new _style();
		  style.styleName=styleName;
		  style.styleValue=styleValue;
		  
          lsize++;
          this[(lsize - 1)] = style;
     }
	 

     function _remove(styleName) {	
		  for (var k= 0;k < lsize;k++)
			if (this[k].styleName==styleName) break;
			   
          if (k > lsize - 1) return;
          this[k] = null;

          for (var i = k; i <= lsize; i++)
               this[i] = this[i + 1];

          lsize--;
     }

     function _isEmpty() { return lsize == 0 }

     function _size() { return lsize }

     function _clear() {
          for (var i = 0; i < lsize; i++)
               this[i] = null;

          lsize = 0;
     }
}

function validateForm(formObj){

	var element;
	var formValue;
	var validated=true;
	var pwdFieldValue="";
	
	formObj.calculateFieldValues();
	for(var ii=0;ii<formObj.elements.size();ii++){
		element=formObj.elements[ii];
		if (formObj.myAccountForm==false || (formObj.myAccountForm && element.elementType!=CONST_FIELD_PASSWORD && element.elementType!=CONST_CONFIRM_PWD)){		
			if (element.elementType==CONST_FIELD_PASSWORD)
				pwdFieldValue=$("field_"+element.uniqeID()).value;
			
			if (element.elementType==CONST_FIELD_HTML_EDITOR){
				$("field_"+element.uniqeID()).value=tinyMCE.getInstanceById("field_"+ element.uniqeID()).getHTML();
			}
			//Şifre alanlarında eğer kayıt mevcut ve şifre alanı girilmemiş ise şifrenin güncellmesi için validate yapmıyoruz.
			else if (!(formObj.mapID>0 && pwdFieldValue=="" && (element.elementType==CONST_FIELD_PASSWORD || element.elementType==CONST_CONFIRM_PWD))){
				if (element.validObj!=null && element.elementType!=CONST_FIELD_DIV)
					if (!element.validObj.validate()) validated=false;
			}
			
			if (validated){
				if (element.remindLastValue==true){ 
					createCookie(userName,$("field_"+element.uniqeID()).value,365);
				}
			}
		}
	}
	
	return validated;
}

function formatCurrency(strValue,currencyID)
{
	if (strValue==null) return;
	if (currencyID==null) currencyID=0;

	if (strValue=="") strValue=0;
	
	//strValue=strValue.toString().replace(".","");
	//strValue=strValue.toString().replace(",",".");
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+'.'+
		dblValue.substring(dblValue.length-(4*i+3));
	if (currencyID==0)
		return (((blnSign)?'':'-') + dblValue + ',' + strCents);
	else
		return (((blnSign)?'':'-') + dblValue + ',' + strCents);
}

function formatFloatNumber(strValue)
{
	if (strValue==null) return;
	strValue=strValue.toString().replace(",","");
	strValue=strValue.toString().replace(".",",");
	return strValue;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


//Aynı özelliklerinin değişmesi gereken elementlerde hepsine , ile ayırarak birden set eder.
function setMultipleElementProps(js){
	var propIndex=js.indexOf(".");
	var prop=js.substring(propIndex+1,js.length);
	var elements=js.substring(0,propIndex);
	
	var arrElement=elements.split(",");
	
	for (var ii=0;ii<arrElement.length;ii++){
		var evalStr="$('"+arrElement[ii]+"')."+prop;
		eval(evalStr);
	}
}

function setEditorValue(uniqeID,html,setHTML){
	var ed=tinyMCE.getInstanceById(uniqeID);
	if (ed!=null){
		if ($(ed.editorId)){
			$(ed.editorId).style.width='100%';
			if (setHTML) ed.setHTML(html);
			
			$(ed.editorId+"_image").onclick="";
			$(ed.editorId+"_image").href="javascript:showFileManagerForEditor('"+uniqeID+"');";
		}
	}
}

function showFormAsHtmlOutput(formID){
	var url="/admin/common.php?progress=getFormAsHtmlOutput&formID="+formID;
	
	if ($("newForm")) $("newForm").remove();
	
	var newForm=document.createElement("FORM");
	
	var formObject=formDesign["form_"+formID];
	for(var ii=0;ii<formObject.elements.size();ii++){
		var element=formObject.elements[ii];
		var newInput=document.createElement("INPUT");
		newInput.name="field_"+element.elementID+"_"+(element.index+1);
		newInput.id="field_"+element.elementID+"_"+(element.index+1);
		newInput.type="hidden";
		newInput.value=$("field_"+element.elementID+"_"+element.index).innerHTML;
		newForm.appendChild(newInput);
	}
	
	document.body.appendChild(newForm);
	
	newForm.action=url;
	newForm.method="post";
	newForm.target="_blank";
	newForm.submit();	
}

function parseResponseText(responseText){
	this.html="";
	this.jsonStr="";
	
	var arrResponseText=responseText.split("|");
	html=arrResponseText[0];
	jsonStr=arrResponseText[1];
	
	return this;
}

function setCheckboxValues(formID,fieldID,fromReport){
	var formObj=formDesign["form_"+formID];
	if (!fromReport)
		eval("var checkElements=form_"+formID+".checkField_"+fieldID);
	else
		eval("var checkElements=formQueryParams.checkField_"+fieldID);
	
	var selectedValues="";
	//Eğer tek bir element değil ise checked null oluyor.
	if (checkElements.checked==null)
		for(var ii=0;ii<checkElements.length;ii++){
			if (checkElements[ii].checked)
				selectedValues+=checkElements[ii].value+",";
		}
		if (selectedValues.length>0) selectedValues=selectedValues.substr(0,selectedValues.length-1);
	else{
		if (checkElements.checked)
			selectedValues+=checkElements.value;
	}
		
	
	$("field_"+fieldID).value=selectedValues;
	
}
