// #################################################################################
// <SCRIPT>
// ID-3 ASP Library
// Javascript Object Definition Include File
// (c) 2004 ID-3 Technologies inc. All right reserved
// #################################################################################



// Object base object
// Added : 15-04-2004
// base property :
//		constructor
//		prototype
//
// -----------------------------------------
Object.prototype._type = 0;

// inherits
// allow inheritance of objects
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Object.prototype.inherits = function(objSuperClass){
		for (sProperty in objSuperClass){
			this[sProperty] = objSuperClass[sProperty]; 
		}
	};

	
// createGUID
// allow creation of Globally Unique iDentifier
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Object.prototype.createGUID = function (booIncludeBracket){
		booIncludeBracket = (typeof(booIncludeBracket)=="undefined")?true:booIncludeBracket;
		var strGUID = new String();
		var intRnd = new Number();
		
		for (i=1;i<=36;i++){
			intRnd = (Math.random() * 100);
			
			// character
			if (Math.random() < (1 / 2)){
				while (intRnd > 26){
					intRnd -= 26;
				}
				strGUID += String.fromCharCode(intRnd + 65);
			}
			// number
			else{
				strGUID += intRnd.toString().charAt(0);
			}
			
			if ((i%6 == 0) && (i<36) ){
				strGUID += "-";
			}
		}
		
		if (booIncludeBracket){
			strGUID = "{" + strGUID + "}"
		}
		this.guid = strGUID;
		return(strGUID);
	}

Object.prototype.guid = Object.createGUID();


// addMethod
// Create a method for the object
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Object.prototype.addMethod = function (fctRef){
		fctRef._type = 1;
		eval("this." + fctRef.getName() + " = fctRef");
	}
	
	
// addProperty
// Create a property for the object
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Object.prototype.addProperty = function (strName,Type,varDefaultValue){
		Type = (typeof(Type)=="undefined")?"undefined":Type;
		
		switch (Type){
			case "undefined":
				this[strName] = new String();
				break;
			default:
				this[strName] = new Type();
				this[strName]._type = 1;
				this[strName].setParent(this);
		}
		
		if (typeof(varDefaultValue)!="undefined"){
			this[strName] = varDefaultValue;
		}
		
	};
	
	
// setParent
// set the parent object of this object
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Object.prototype.setParent = function(objParent){
		this["parentObject"] = objParent;
	};


// enumItems
// enumerate object content
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Object.prototype.enumItems = function(){
		for (sProperties in this){
			alert(this[sProperties]);
		}
	};


// exist
// verify if object exist
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Object.prototype.exist = function(objToCheck){
		if (objToCheck == null){
			return(false);
		}
		else if (typeof(objToCheck) == 'undefined'){
			return(false);
		}
		else{
			return(true);
		}
	};



// getName
// get the name of the object
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Object.prototype.getName = function (alertName){
		alertName = (typeof(alertName)=="undefined")?false:alertName;
		
		var sName = new String();
		sName = this.toString();
		
		if ((sName == "") || (sName == "[object Object]") ){
			sName = new String(this.constructor);
		}
		
		if (alertName){
			alert(sName);
		}
		
		sName = sName.substr(0,sName.indexOf("("))
		sName = sName.substr(String("function ").length);
		return (sName)
	}
	
// clone
// copy item property to another
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*Object.prototype.clone = function(objToClone){
	for (sProperties in objToClone){
		if (typeof(this[sProperties]) != "undefined"){
			this[sProperties] = objToClone[sProperties];
		}
	}	
}*/

Object.prototype.reboundTo = function(funcname,funcargs){
	var sArgs = new String();
	
	for (var i=0;i<funcargs.length;i++){
		sArgs += "funcargs[" + i + "]";
		if (i<funcargs.length - 1){
			sArgs += ",";
		}
	}
	
	eval("funcname(" + sArgs + ")");
}

Object.prototype.raiseEvent = function(eventname){
	var sArgs = new String();
	var eventRef;
	var returnValue;
	eventRef = this.getName() + "_" + eventname;
	
	for (var i=1;i<arguments.length;i++){
		sArgs += "arguments[" + i + "]";
		if (i<arguments.length - 1){
			sArgs += ",";
		}
	}
	if (typeof(window[eventRef]) == "function"){
		returnValue = eval("window[eventRef](" + sArgs + ")");
		returnValue = (typeof(returnValue)=="undefined")?true:returnValue;
		return(returnValue);
	}
	else{
		return(true);
	}
}
// callPrototype
// prototype the object and return wether it was called or not
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Object.prototype.callPrototype = function (){
		if (typeof(this["_prototype_called"]) == "undefined"){
			this["_prototype_called"] = false;
		}
		else{
			this["_prototype_called"] = true;
		}
		return(this["_prototype_called"]);
	}
	

// getElementByGUID
// search for subobject with the specified GUID
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Object.prototype.getElementByGUID = function(strGUID){
		var sProp;
		var oReturn;
		
		if (this.guid == strGUID){
			return(this);
		}
		else{
			for (sProp in this){
				if ((this[sProp] != null) && (sProp != "arrayObject")){
					if (sProp != "parentObject"){
						switch (typeof(this[sProp])){
							case "object":
							case "function":
							case "array":
								if (this[sProp]._type == 1){
									oReturn = this[sProp].getElementByGUID(strGUID);
									if (oReturn != null){
										return(oReturn);
									}
								}
								break;
						}
					}
				}
			}
		}
		return(null);
	}


// Array base object
// Added : 15-04-2004
// -----------------------------------------------------------------

// add
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Array.prototype.add = function(varItem,strKey){
		strKey = (typeof(strKey) != "undefined")?strKey:this.length;
		varItem.arrayObject = this;
		varItem.arrayIndex = strKey;
		var oItem = {
				value:varItem,
				key:strKey
			}
		this[this.length] = oItem;
	};

// clear
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Array.prototype.clear = function(){
		for(var i=0;i<=this.length;i++){
			this.pop();
		}
	};
	
// last
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Array.prototype.last = function(){
		if (this.length > 0){
			return(this[this.length -1].value);
		}
		else{
			return(null);
		}
	};
	
// remove
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Array.prototype.remove = function(strKey){
		var oNewArray = new Array();
		for(var i=0;i<this.length;i++){
			if (isNaN(strKey)){
				if (this[i].key != strKey){
					oNewArray[oNewArray.length] = this[i];
				}
			}
			else{
				if (i != strKey){
					oNewArray[oNewArray.length] = this[i];
				}
			}
		}
		
		this.clear();
		
		for(var i=0;i<oNewArray.length;i++){
			this[i] = oNewArray[i];
		}
	};

// item
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Array.prototype.items = function(strKey){
		if (typeof(strKey) == "string"){
			for(var i=0;i<this.length;i++){
				if (this[i].key == strKey){
					return(this[i].value);
				}
			}
		}
		else{
			return(this[strKey].value)
		}		
	};
	

// count
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Array.prototype.count = function(){
		return(this.length);
	};
	


// getElementByGUID
// search for subobject with the specified GUID
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Array.prototype.getElementByGUID = function(strGUID){
		var sProp
		var oReturn
		for (var i=0;i<this.length;i++){
			if (typeof(this[i].value) == "undefined"){
				switch (typeof(this[i])){
					case "object":
					case "array":
						if(this[i].guid == strGUID){
							return(this[i]);
						}
						else{
							oReturn = this[i].getElementByGUID(strGUID);
							if (oReturn != null){
								return(oReturn);
							}
						}
				}
			}
			else{
				if(this[i].key == strGUID){
					return(this[i].value);
				}
				else{
					switch (typeof(this[i].value)){
						case "object":
						case "array":
							oReturn = this[i].value.getElementByGUID(strGUID);
							if (oReturn != null){
								return(oReturn);
							}
							break;
					}
				}
			}
		}
		return(null);
	};


// String base object
// Added : 15-04-2004
//
// -----------------------------------------

String.prototype.replace2 = function(findString, replaceString){
	var returnString = new String(this);
	var sStart,sEnd;
	
	if (returnString.indexOf(findString) >= 0){
		sStart = returnString.substring(0,returnString.indexOf(findString));
		sEnd = returnString.substring(returnString.indexOf(findString)+findString.length);
		
		returnString = sStart + replaceString + sEnd;
	}
	
	return(returnString)
}

String.prototype.toNumber = function(){
	var strToTransform = this.toString();
	if (isNaN("1,1")){
		return Number(strToTransform.replace(",","."));
	}
	else{
		return Number(strToTransform.replace(".",","));
	}
}

String.prototype.truncate = function(lngLength,strTruncChar){
	strTruncChar = (typeof(strTruncChar)=="undefined")?"...":strTruncChar;
	var strReturn = new String(this.toString());
	
	if (strReturn.length > lngLength){
		return(strReturn.substr(0,lngLength) + strTruncChar);
	}
	else{
		return(strReturn);
	}
}	

String.prototype.toEmail = function (){
		if (this.m_strInner.search(/[0-9|a-z|A-Z|\.]+@[0-9|a-z|A-Z|\-]+\.[0-9|a-z|A-Z|\-]+/) < 0){
			return('');
		}
		return(m_strInner.replace(/ /gi,''));	
	}
	
String.prototype.toHTTPUrl = function (){
		var strNew = '';
		this.m_strInner = this.m_strInner.toLowerCase();
		
		if(this.m_strInner.indexOf('@') > 0){
			if (this.m_strInner.search(/[0-9|a-z|A-Z|\.]+@[0-9|a-z|A-Z|\-]+\.[0-9|a-z|A-Z|\-]+/) < 0){

				return(strNew);
			}
		}
		else if (this.m_strInner.search(/[0-9|a-z|A-Z|:\/]+\.[0-9|a-z|A-Z|\-]+\.[0-9|a-z|A-Z|\-]+/) < 0) {
			return(strNew);
		}
		
		if (this.m_strInner.indexOf('://') < 0){
			if(this.m_strInner.indexOf('@') > 0){
				if (this.m_strInner.indexOf('mailto:') < 0){
					strNew = 'mailto:' + this.m_strInner;
				}
			}
			else if (this.m_strInner.indexOf('javascript:') < 0){
				strNew = 'http://' + this.m_strInner;
			}
			else
				strNew = this.m_strInner;
		}
		else{
			strNew = this.m_strInner
		}
		
		return(strNew.replace(/ /gi,''));
	}
	
String.prototype.toIP = function (){
		var strNew = '',strTemp = '';
		var arrPacket = new Array(0,0,0,0);
		
		if (this.m_strInner.search(/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/) >= 0) {
			return(this.m_strInner)
		}
		
		this.m_strInner = this.m_strInner.replace(/\./gi,'').substr(0,12);
		
		if (isNaN(this.m_strInner) == false){
			for (var iPacket = 0;iPacket<4;iPacket++){
				arrPacket[iPacket] = FindIpPacket(this.m_strInner.substr(strTemp.length),iPacket + 1);
				strTemp += arrPacket[iPacket];
			}
			
			strNew = arrPacket[0] + '.' + arrPacket[1] + '.' + arrPacket[2] + '.' + arrPacket[3]
		}		
		
		return(strNew);
	}
	
String.prototype.findIpPacket = function (str,index){
		if (str.charAt(0) == '0'){
			return('0');
		}
		
		if ( str.length >= (7-index) ){
			if (Number(str.substr(0,3)) > 255)
				return('255');
			else
				return(str.substr(0,3));
		}
		
		if ( str.length == (6-index) ){
			return(str.substr(0,2));
		}
		
		return(str.substr(0,1));
		
	}
	
String.prototype.toCreditCardNum = function (){
		var strNew = '';
		this.m_strInner = this.m_strInner.replace(/ /gi,'').substr(0,16);
		
		if (isNaN(this.m_strInner) == false){
			for (var i=1;i<=this.m_strInner.length;i++){
				strNew += this.m_strInner.charAt(i-1);
				if ( (i % 4) == 0){
					strNew += ' ';
				}
			}
		}
		
		return(strNew)
	}
	
String.prototype.toSIN = function (){
		var strNew = '';
		this.m_strInner = this.m_strInner.replace(/ /gi,'').substr(0,9);
		
		if (isNaN(this.m_strInner) == false){
			for (var i=1;i<=this.m_strInner.length;i++){
				strNew += this.m_strInner.charAt(i-1);
				if ( (i % 3) == 0){
					strNew += ' ';
				}
			}
		}
		
		return(strNew)
	}
	
String.prototype.toNAPhone = function(){
		var strNew = '';
		var bLongNum = true;
		
		this.m_strInner = this.m_strInner.replace(/[()\.\-#p ]/gi,'');
		
		if ((this.m_strInner.length <= 7) || ( (this.m_strInner.length>7)&&(this.m_strInner.length<10) ) ){
			bLongNum = false;
		}
		
		if (this.m_strInner.length == 7){
			if (isNaN(this.m_strInner)){
				return('');
			}
		}
		else if (this.m_strInner.length > 7){
			if (isNaN(this.m_strInner.substr(0,10))){
				return('');
			}
		}
		else{
			return('');
		}
		
		for (var i=0;i<this.m_strInner.length;i++){
			switch (i){
				case 0:
					if (bLongNum)
						strNew += '(';
					break;
				case 3:
					if (bLongNum){
						strNew += ') ';
					}
					else{
						strNew += '-';
					}
					break;
				case 6:
					if (bLongNum)
						strNew += '-';
					break;
				case 7:
					if(bLongNum == false)
						strNew += ' #';
					break;
				case 10:
					strNew += ' #';
					break;
			}
			strNew += this.m_strInner.charAt(i);
		}
		return(strNew)
	}
	
String.prototype.toCAZipCode = function (){
		var strNew = '';
		
		this.m_strInner = this.m_strInner.toUpperCase();
		this.m_strInner = this.m_strInner.replace(/ /,'');
		
		for (var i=1;i <= this.m_strInner.length;i++){
			if (i == 4){
				strNew += ' ';
			}
			
			if ((i % 2) == 0){
				if (isNaN(this.m_strInner.charAt(i-1)) == false) {
					strNew += this.m_strInner.charAt(i-1)
				}
				else
					return('');
			}
			else{
				if (isNaN(this.m_strInner.charAt(i-1))) {
					strNew += this.m_strInner.charAt(i-1)
				}
				else
					return('');			
			}
		}
		return(strNew)
	}	

String.prototype.trim = function (){
	var sLTrim = new String();
	var sTrim = new String();
	
	for(var i=0;i<this.length;i++){
		if (this.charAt(i) != " "){
			sLTrim = this.substr(i);
			break;
		}
	}
	
	for(var i=sLTrim.length;i>0;i--){
		if (sLTrim.charAt(i) != " "){
			sTrim = sLTrim.substr(0,i + 1);
			break;
		}
	}
	return(sTrim);	
}
	
// Date base object
// Added : 15-04-2004
// -----------------------------------------------------------------
Date.prototype.getFormattedTime = function(strFormat){
		strFormat = (typeof(strFormat)=="undefined")?"hh:mm:ss":strFormat;
		var arrFormat = strFormat.split(":");
		var sVar = new String();
		var sTime = new Array();
		for (var i=0;i<arrFormat.length;i++){
			switch(arrFormat[i]){
				case "hh":
					sVar = this.getHours().toString();
					if(sVar.length==1){
						sVar = "0" + sVar;
					}
					sTime[i] = sVar;
					break;
				case "h":
					sTime[i] = this.getHours();
					break;
				case "mm":
					sVar = this.getMinutes().toString();
					if(sVar.length==1){
						sVar = "0" + sVar;
					}
					sTime[i] = sVar;
					break;
				case "m":
					sTime[i] = this.getMinutes();
					break;
				case "ss":
					sVar = this.getSeconds().toString();
					if(sVar.length==1){
						sVar = "0" + sVar;
					}
					sTime[i] = sVar;
					break;
				case "s":
					sTime[i] = this.getSeconds();
					break;	
			}
		}
		return(sTime.join(":"));
	};
	
// Date base object
// Added : 15-04-2004
// -----------------------------------------------------------------
Date.prototype.timeToSeconds = function(){
		var lngSeconds = new Number();
		lngSeconds = this.getSeconds();
		lngSeconds += (this.getMinutes() * 60);
		lngSeconds += (this.getHours() * 3600);
		
		return(lngSeconds);
	};

// Boolean base object
// Added : 21-05-2004
// -----------------------------------------------------------------
String.prototype.toBool = function(sTrue,sFalse){
	if(this == true){
		return(sTrue);
	}
	else{
		return(sFalse);
	}
}

// Document base Object
// Added : 14-09-2006
// -----------------------------------------------------------------
document.readCookie = function(key,defaultValue){
	var strCookieList = this.cookie;
	var arrCookie = strCookieList.split(";");
	var arrValue;
	var strValue;
	
	for (var i=0;i<arrCookie.length;i++){
		arrValue = arrCookie[i].split("=");
		strValue = arrValue[0].trim();
		
		if (arrValue[0].trim() == key){
			return(arrValue[1]);
		}
	}
	return(defaultValue);
}


// showModalDialog
// Added : 25-04-2007
// Add the "modal window" capability to firefox
// -----------------------------------------------------------------
/*
if (typeof(window.showModalDialog) != "undefined"){
window.prototype.showModalDialog = function(sURL, vArguments, sFeatures){
	// Enable the "UniversalBrowserWrite" privilege in the privilegeManager
	netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");
	try{
		var objWnd = window.open(sURL, 'ModalDialog', 'chrome=yes,modal=yes');
		return(objWnd.document.window.returnValue);
	}
	catch(e){
		return(null);
	}
}

}
*/

