﻿/*<script id="jsString" language="javascript">
 * uses String functions in Utility.js
 * Author: Joel D'Souza
 * jdsouza@horizonlines.com
 * Last Updated: Dec 13, 2006
*/
var P = new constructorUtility()

function constructorUtility() {
    //if (document.all['P'] == undefined) { alert('Dear Programmer please set the "id" attribute of Utility.js to "P".'
    //            + '\n\nExample: <script id="P" language="javascript" src="../uiCommon/Utility.js" debug="true"></script>') }
    
    //Properties:
    this.Debug = constructor_Debug_P(document.getElementById('P').debug)
    
    //Methods:
    this.RemoveBlanks = fn_RemoveBlanks_P
    this.LTrim = fn_LTrim_P
    this.RTrim = fn_RTrim_P
    this.Trim = fn_Trim_P
    
    this.IsBlank = fn_IsBlank_P
    this.IsDigit = fn_IsDigit_P
    this.IsLetter = fn_IsLetter_P
    this.MultiDimenArray = fn_MultiDimenArray_P
    this.Is = fn_Is_P
    this.Get = fn_Get_P
    this.NewGuid = fn_Get_Guid_From_Server_P
	this.GetScriptAttribute = fn_Get_Script_Attribute_P
	this.isGuid = fn_Check_Guid_RegExp
}

function fn_Check_Guid_RegExp(guid) {
	//valid guid format : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx or xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
	if(!guid) return false;
	GuidRegExp = /^[{|\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[\)|}]?$/
	//alert('guid? = ' + GuidRegExp.test(guid))
	return GuidRegExp.test(guid);	
}

function fn_RemoveBlanks_P(s) {
    if (s == null) { return '' }
 	var temp = ""
 	for(var i=0; i < s.length; ++i) { var c = s.charAt(i); if (c != " ") temp += c }
 	return temp
}

function fn_LTrim_P(str) {
    for(var i=0; str.charAt(i) == " "; i++);
    return str.substring(i, str.length);
}
function fn_RTrim_P(str) {
    for(var i=str.length-1; str.charAt(i) == " "; i--);
    return str.substring(0, i + 1);
}
function fn_Trim_P(str) { return fn_LTrim_P(fn_RTrim_P(str)); }

//Returns true if value is null or contains only spaces
function fn_IsBlank_P(val) {
    if (val == null) { return true; }
    
    for (var i=0; i < val.length; i++) {
	    if ( (val.charAt(i) != ' ') && (val.charAt(i) != '\t') && (val.charAt(i) != '\n') && (val.charAt(i) != '\r') ) {
		    return false;
	    }
    }
    
    return true;
}

// or just use 'isNaN(number)'.
function fn_IsDigit_P(num) {
    if (num.length > 1) { return false; }
    var string = "1234567890";
    if (string.indexOf(num) != -1) { return true; } else { return false; }
}
function fn_IsLetter_P(num) {
    if (num.length > 1) { return false; }
    var string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if (string.indexOf(num) != -1) { return true; } else { return false; }
}

//EX: var arrUpdFields=MultiDimenArray(100,2); arrUpdFields[0][0] = "sam";
function fn_MultiDimenArray_P(iRows,iCols)  { 
	var i, j; 
	var arr = new Array(iRows); 
	
	for (i=0; i < iRows; i++) { 
       arr[i] = new Array(iCols); 
       for (j=0; j < iCols; j++) { arr[i][j] = ""; } 
   } 
   
   return(arr); 
}

function fn_Is_P(obj) {
    if (obj != undefined) { if (obj != null) { if ( (obj != '') || (obj == false) ) { return true } } }
    return false
}
function fn_Get_P(obj) {
    if ( fn_Is_P(obj) ) { return obj }
    if (P.Debug) { alert('arguments.length = ' + arguments.length) }
    
    for(var i=1; i < arguments.length; i++) { 
        if (P.Debug) { alert('arguments[' + i + '] = ' + arguments[i]) }
        if ( fn_Is_P(arguments[i]) ) { return arguments[i] }
    }
    
    return ''
}

function constructor_Debug_P(obj) {
    //alert('constructor_Debug_P = ' + obj)
    
    if (obj != undefined) { if (obj != null) { 
        if (obj == 'true') { return true } 
        if (obj == 'false') { return false } 
    } }
    return false
}

function fn_Get_Guid_P_old() {	//returns a guid of size 32
	var guid = "{"; 
	for (var i = 1; i <= 32; i++) 
	 { 
	 var n = Math.floor(Math.random() * 16.0).toString(16); 
	 guid += n; 
	 if ((i == 8) || (i == 12) || (i == 16) || (i == 20)) 
	  guid += "-"; 
	 } 
	guid += "}"; 
	return(guid); 
}

function fn_Get_Guid_P() {
    //Start: Generate a unique GUID (only works in IE).
    var xGUID = null;
    try { xGUID = new ActiveXObject("Scriptlet.TypeLib").GUID.substr(1,36); } catch(e) { }
    if (xGUID != null) { return xGUID }
    //End: Generate a unique GUID (only works in IE).
    
    //Otherwise, generate a GUID that is fairly unique (per millisecond).
    var Hex = new Array('0','1','2','3','4','5','6','7','8', '9','a','b','c','d','e','f');
	var OutB = '{';
	
	for (count = 0; count < 32; count++) {
		if ((count == 8) || (count == 12) || (count == 16) || (count == 20)) { OutB += '-'; }
		OutB += Hex[ Math.floor( Math.random() * 16 ) ];
	}
	
	//alert('guid = ' + OutB.toUpperCase() + '}')
	return OutB.toUpperCase() + '}';
}

function fn_Get_Script_Attribute_P(jsID, attributeName) {
    var arrScript = document.getElementsByTagName("script"); 

    for (var i = 0; i < arrScript.length; i++) { 
        if (arrScript[i].getAttribute("id") == jsID) { 
        	var jsTemp = arrScript[i].getAttribute(attributeName); 
        	if (jsTemp == 'false') {
        		jsTemp = false
        	} else if (jsTemp == 'true') {
        		jsTemp = true
        	}
        	
        	return jsTemp
       	}
    }
}

function fn_Get_Guid_From_Server_P() {return 'ServerGeneratedGUID_from_Utility.js.aspx'; }
