/* JavaScript Document
AUTHOR: Damir Smiljanec Copyright 2004
class for Form location routines 
Used mainky for manipulating with form.action strings
ussage: var someForm= FormURL(oForm) 
someform.action= www.croator.com?someName=someValue&someName=someValue
someform.InitURIParms(); // to ensure initialisation dor all properties and variables
*/
function showURIParms(){
//var URIParmsList = new Array;
//URIParmsList= this.UnEscapedURITxt.split('&');
var iLoop = 0;
var argumentString='';
	for (iLoop= 0; iLoop < this.URIParmsList.length ; iLoop++){
		argumentString +=  '\n' + iLoop + ' ' + unescape(this.URIParmsList[iLoop]);
	}
	alert('.showURIParms()\nthis.action=' + this.action
	+'\nthis.BaseURL=' + this.BaseURL
	+'\nthis.URIParmsList.length=' + this.URIParmsList.length
	+ argumentString  );
} //end function showUriParms()

function getProps(obj, objName){
// initialize accumulative variable
var result = "";
// loop through properties
for (var i in obj){
	// if current property is an object call function for it
	if (typeof obj[i] == "object"){
	result +="<BR>--------------------"+ obj[i] + " " + objName + "." + i+"-------------------------------------------------<BR>";
		result += getProps(obj[i], objName + "." + i)+ "<BR>";
	}else{
	result += objName + "." + i + " = " + obj[i] + "<BR>";
	}
}
// return final result;
return result;
}

function InitURIParms(){
//******************************************
// fills array with URI names and values
//******************************************
 this.BaseURLLen= (this.action.indexOf('?')>-1) ? this.action.indexOf('?') : this.action.length;
 this.BaseURL = this.action.substr(0, this.BaseURLLen );
 this.HasURIParms= (this.action.indexOf('?', this.BaseURLLen)>-1) ? true : false;
 this.UnEscapedURITxt= unescape(this.action.substr(this.BaseURLLen, this.action.length));
 this.UnEscapedURITxt = (this.HasURIParms) ? this.UnEscapedURITxt.substr(1) : this.UnEscapedURITxt;
 if (this.UnEscapedURITxt.length >0){
   this.URIParmsList = this.UnEscapedURITxt.split('&');
 }
} // end function InitURIParms()

function appendURIParm(URIname,URIvalue){
//****************************************************
// appends  array of URI names and values with new one
//****************************************************
 if (URIname == undefined || URIname == null || URIname == ''
 	 || URIvalue == undefined) return;
 URIname = (URIname.indexOf('=', length.URIname - 1) > -1) ? URIname : URIname +'=';  
 URIname = (URIname.indexOf('&', 0) > -1) ? URIname : URIname +'&';  
 if (isNaN(URIvalue) ){
 	URIvalue= (URIvalue.indexOf('=', 0 ) > -1) ? URIvalue.substr(1) : URIvalue ;
 }
 this.URIParmsList.push(URIname + URIvalue);
 
} // end function appendURIParmURIParm()
function removeURIParm(URIname){
//******************************************
// removes  URIParm from array of URI names and values
//******************************************
// formal control of input parameters
if (URIname == undefined || URIname == null || URIname == '') return ;

//search value must be with & and without & 
var strippedURIParm;
var strippedURIName;
//strip leading amp if exists
strippedURIName =(URIname.indexOf('&',0)>-1) ? URIname.substr(1) : URIname;
//strip trailing '=' and value if exists
strippedURIName =(strippedURIName.indexOf('=')>-1) ? strippedURIName.substr(0,strippedURIName.indexOf('=')) : strippedURIName;

var iLoop;
 for (iLoop= 0; iLoop < this.URIParmsList.length ; iLoop++){
  //strip leading amp if exists
  strippedURIParm =(this.URIParmsList[iLoop].indexOf('&',0)>-1) ? this.URIParmsList[iLoop].substr(1) : this.URIParmsList[iLoop];

  //strip trailing '=' and value if exists
  strippedURIParm =(strippedURIParm.indexOf('=')>-1) ? strippedURIParm.substr(0,strippedURIParm.indexOf('=')) : strippedURIParm;
	  
	 if ((strippedURIParm === strippedURIName) || strippedURIParm === '' ){
	   	// remove from array
		this.URIParmsList.splice(iLoop, 1);
	  } // end if
	} // end for
} // end function removeURIParm()
function replaceURIParm(URIname,URIvalue){
//******************************************
// removes  URIParm from array of URI names and values
// and appends  URIParm from array of URI names and values
//******************************************
// formal control of input parameters
 if (URIname == undefined || URIname == null || URIname == ''
 	 || URIvalue == undefined) return;
	this.removeURIParm(URIname);
 	this.appendURIParm(URIname,URIvalue);
} // end function replaceURIParm()

function composeFormAction(URIname,URIvalue){
// compose full form action 
// if arguments list is empty or undefined
// collects action from array URIParmsList
	if (!(URIname == undefined || URIname == null || URIname == ''
 		 || URIvalue == undefined)){
	 	this.replaceURIParm(URIname,URIvalue)
	 }
	// if exist at least one  parameter in URI list

	if (this.URIParmsList.length >0){
		if (this.URIParmsList[0] != '?' ) this.action=this.BaseURL + '?';
	}else{
		this.action = this.BaseURL ;
	}

	var iLoop;
	var tmpParm = new Array;
	var URIString ='';
	for (iLoop= 0; iLoop < this.URIParmsList.length ; iLoop++){
		if (this.URIParmsList[iLoop] == '?') continue;

		tmpParm = this.URIParmsList[iLoop].split('=');
		tmpParm[0]= (tmpParm[0].indexOf('&',0) >-1) ? tmpParm[0].substr(1) : tmpParm[0]; // clear first &
		URIString += (URIString.length) ? '&' : '';
		URIString += tmpParm[0] + '=' + escape(tmpParm[1]);
   } // end for
		this.action += URIString;
	this.InitURIParms();// reestablish URIParmsList 
} // end function composeFormAction()

// definition of FormURL object type C O N S T R U C T O R
function FormURL(oForm){
	var myForm = oForm;

//	this.FormName = oForm.name;
	this.action= myForm.action;
	this.BaseURLLen= (this.action.indexOf('?')>-1) ? this.action.indexOf('?') : this.action.length;
	this.BaseURL = this.action.substr(0, this.BaseURLLen );
	this.HasURIParms= (this.action.indexOf('?', this.BaseURLLen)>-1) ? true : false;
	this.UnEscapedURITxt= unescape(this.action.substr(this.BaseURLLen, this.action.length));
	this.UnEscapedURITxt = (this.HasURIParms) ? this.UnEscapedURITxt.substr(1) : this.UnEscapedURITxt;
	this.EscapedURITxt= this.action.substr(this.BaseURLLen, this.action.length);
//************************************** METHODS ****************************************
	this.showURIParms = showURIParms; // method
	this.InitURIParms = InitURIParms;   // method 
	this.removeURIParm = removeURIParm;  // method
	this.appendURIParm = appendURIParm; // method
	this.replaceURIParm = replaceURIParm; // method
    this.URIParmsList = new Array; // array of URI names and values 
	this.composeFormAction = composeFormAction;
	this.InitURIParms(); // init array of of URI names and values  (URIParmsList )
} // end function FormURL(oForm)
