/* '------------------------------------------------------------------------------ ' Copyright Notice '------------------------------------------------------------------------------ ' This document and all its contents are ' Copyright (c) 2003 Thindata ' All Rights Reserved ' Unauthorized use is strictly prohibited ' http://www.thindata.com/ '------------------------------------------------------------------------------ ' '------------------------------------------------------------------------------ ' AppName = CAF-FCA Canadian Apprenticeship Forum '============================================================================== ' ' VSS Version: $Revision: 10 $ ' Last Modified by: $Author: Ttf $ ' Last Checkin: $Date: 10/03/03 5:13p $ ' Last Modification Time: $Modtime: 10/03/03 11:44a $ ' FileName: $Workfile: global.routines.js.inc $ ' ' Description: Include File for global javascript functions '============================================================================== ' DO NOT EDIT VERSION INFO ABOVE - Automatically updated by SourceSafe at Check-in '------------------------------------------------------------------------------ ' BEGIN Global Server-side Variables ' ----------------------------------------------------------------------------- '------------------------------------------------------------------------------ ' END Global Server-side Variables ' ----------------------------------------------------------------------------- */ //================================================================================== // BEGIN DataVMObject - Provides Data Validation and Manipulation for forms. //================================================================================== /* IF ARGUMENTS NOT PROVIDED, CONSTRUCTOR PROVIDES FOLLOWING DEFAULT VALUES this.FieldName = ""; this.DataType = 1; //DataType to be validated must be a: 0 - boolean, 1 - string, 2 - numeric this.Length = 0; //Length Max for Data this.RegExp = false; //false if No character validation OR string containing Regular Expression text OR regular expression Object to use this.Required = false; //True - Data cannot be null, False - Data can be null this.ProhibitChars = false; //False if No character validation OR string containing list of individual characters to be prohibited this.TransformNull = 0; //0 - no change, 1 - make null if null, 2 - make "" if null this.SingleQuotes = false; //True - wrap Data in single quotes, False do nothing this.StripSpaces = false; //True - Strip leading and trailing spaces if any, False - do nothing */ function DataVMObject(data, fieldName, datatype, length, regExp, isRequired, prohibitChars, transformNull, singleQuotes, stripSpaces) { //Constructor for Data Validation & Manipulation Object this.Data = data; //Data value to be validated and/or manipulated this.FieldName = fieldName; //FieldName provided for possible future use this.DataType = datatype; //DataType to be validated must be a: 0 - boolean, 1 - string, 2 - numeric this.Length = length; //Length Max for Data, 0 do nothing this.RegExp = regExp; //False if No character validation OR string containing Regular Expression text OR regular expression Object to use this.Required = isRequired; //True - Data cannot be null, False - Data can be null this.ProhibitChars = prohibitChars; //False if No character validation OR string containing list of individual characters to be prohibited this.TransformNull = transformNull; //0 - no change, 1 - make null if null, 2 - make "" if null this.SingleQuotes = singleQuotes; //True - wrap Data in single quotes, False do nothing this.StripSpaces = stripSpaces; //True - Strip leading and trailing spaces if any, False - do nothing this.ErrorType = 0; //Following block provides default values for object construction if not provided if (fieldName == null) this.FieldName = ""; if (datatype == null) this.DataType = 1; if (length == null) this.Length = 0; if (regExp == null) this.RegExp = false; if (isRequired == null) this.Required = false; if (prohibitChars == null) this.ProhibitChars = false; if (transformNull == null) this.TransformNull = 0; if (singleQuotes == null) this.SingleQuotes = false; if (stripSpaces == null) this.StripSpaces = false; //Object methods this.Validate = Validate; // Perform only validation functions this.Manipulate = Manipulate; // Perform only manipulation functions this.ProcessData = ProcessData; // Perform both manipulation and validation this.UpdateData = UpdateData; // Reset Data value with provided value } // Methods Definitions************************************************* function ProcessData(obj) { Manipulate(obj); if(!Validate(obj)) return false; // Data is not valid else return true; // Data is valid } function Validate(obj) { if (!CheckRequired(obj)) return false; // ErrorType 4 if Fail if (!CheckDataType(obj)) return false; // ErrorType 1 if Fail if (!CheckLength(obj)) return false; // ErrorType 2 if Fail if (!CheckRegExp(obj)) return false; // ErrorType 3 if Fail if (!CheckProhibitChars(obj)) return false; // ErrorType 5 if Fail return true; } function UpdateData(obj, NewData) { obj.Data = NewData; } function Manipulate(obj) { TransformNull(obj); StripSpaces(obj); SingleQuotes(obj); return true; } // Begin DataVMObject Helper Functions**************************************************** function CheckProhibitChars(obj) { if (!obj.ProhibitChars) return true; else { var tmp = ""; var strToTest = new String(obj.Data); var strProhibited = new String(obj.ProhibitChars); var strProhibitedLen = strProhibited.length; var item_length = strToTest.length; for (index = 0; index < item_length; index++) // for each Char in Data { for (index2 = 0; index2 < strProhibitedLen; index2++) //for each Char in ProhibitChars { if (strToTest.charAt(index) == strProhibited.charAt(index2)) //Contains prohibited chars, therefore Data Not Valid { obj.ErrorType = 5; return false; } } } return true; } } function StripSpaces(obj) { if (obj.StripSpaces == true) { var tmp = ""; var strToTest = new String(obj.Data); var item_length = strToTest.length; var item_length_minus_1 = strToTest.length - 1; for (index = 0; index < item_length; index++) { if (strToTest.charAt(index) != ' ') { tmp += strToTest.charAt(index); } else { if (tmp.length > 0) { if (strToTest.charAt(index+1) != ' ' && index != item_length_minus_1) { tmp += strToTest.charAt(index); } } } } obj.Data = tmp; } } function SingleQuotes(obj) { if (obj.SingleQuotes == true) { var temp = new String(obj.Data); if (temp != "null") // Wrap all values in single quotes unless it is null temp = "'" + temp + "'"; obj.Data = temp; } } function TransformNull(obj) { switch (obj.TransformNull) { case 0: // do nothing break; case 1: //make null if null if (obj.Data == "") obj.Data = null; break; case 2: //make "" if null if (obj.Data == null) obj.Data = ""; break; } } function CheckRegExp(obj) { if (obj.RegExp != false) { var strOption = "none"; var strTemp = new String(obj.RegExp); if (strTemp.substr(strTemp.length-3) == "/gi") { strTemp = strTemp.replace(/gi$/, ""); strOption = "gi"; } if (strTemp.substr(strTemp.length-3) == "/ig") { strTemp = strTemp.replace(/ig$/, ""); strOption = "ig"; } if (strTemp.substr(strTemp.length-2) == "/g") { strTemp = strTemp.replace(/g$/, ""); strOption = "g"; } if (strTemp.substr(strTemp.length-2) == "/i") { strTemp = strTemp.replace(/i$/, ""); strOption = "i"; } if(strTemp.charAt(0,1) == "/" && strTemp.charAt(strTemp.length-1, 1) == "/") { var pattern = new RegExp("^\/") strTemp = strTemp.replace(pattern, ""); pattern = new RegExp("\/$"); strTemp = strTemp.replace(pattern, ""); } if (strOption == "none") { //Regular expression string provided pattern = new RegExp(strTemp); } else { //Regular expression string provided pattern = new RegExp(strTemp, strOption); } var strData = new String(obj.Data); if (strData.length == 0 && obj.Required == false) return true; else { if(pattern.test(obj.Data)) // Pattern found return true; else { obj.ErrorType = 3; //Error, pattern not found return false; } } } else return true; } function CheckRequired(obj) { if (obj.Required) { var Data = new String(obj.Data); if(Data) { if (obj.Data == null || Data == "" || Data.length == 0 ) { obj.ErrorType = 4; return false; } else return true; } else if (Data == false) return true; else { obj.ErrorType = 4; return false; } } else return true; } function CheckLength(obj) { if (obj.Length > 0) { var strTemp = new String(obj.Data); if (strTemp.length <= obj.Length) return true; else { obj.ErrorType = 2; return false; } } else return true; } function CheckDataType(obj) { switch (obj.DataType) { case 0: //boolean if (obj.Data == true || obj.Data == false || obj.Data == 1 || obj.Data == 0 || obj.Data.toUpperCase() == "TRUE" || obj.Data.toUpperCase() == "FALSE") return true; else { obj.ErrorType = 1; return false; } break; case 1: //string //do nothing for now return true; break; case 2: //numeric if (isNaN(obj.Data)) { obj.ErrorType = 1; return false; } else return true; break; case 3: //date var lTest; lTest = Date.parse(obj.Data); var strData = obj.Data; //below test to ensure we do not try to validate data if it is null and not required if ((obj.Required == false && strData.length > 0) || obj.Required == true) { if (isNaN(lTest)) { obj.ErrorType = 1; return false; } else { lTest = obj.Data lTest = lTest.split("/") if (lTest[0] > 12 || lTest[0] < 1 || typeof(lTest[0]) == 'undefined') { //wrong month obj.ErrorType = 1; return false; } if (lTest[1] > 31 || lTest[1] < 1 || typeof(lTest[1]) == 'undefined') { //wrong day obj.ErrorType = 1; return false; } if (lTest[2] > 3000 || lTest[2] < 1900 || typeof(lTest[2]) == 'undefined') { //wrong year obj.ErrorType = 1; return false; } return true; } } else return true; break; } } //------------------------------------------------------------------- // SetElemFocus(input_object) // Set the focus to form element referenced in argument // Handles text fields, radio, checkbox, select fields //------------------------------------------------------------------- function SetElemFocus(formelement) { //element can't receive focus if it is hidden if (formelement.type != "hidden") { if (formelement[0] == null || (formelement.type == "select-one" || formelement.type == "select-multiple")) formelement.focus(); else formelement[0].focus(); } } //------------------------------------------------------------------- // GetInputValue(input_object) // Get the value of any form input field // Multiple-select fields are returned as comma-separated values // (Doesn't support input types: button,file,password,reset,submit) //------------------------------------------------------------------- function GetInputValue(obj) { if ((typeof obj.type != "string") && (obj.length > 0) && (obj[0] != null) && (obj[0].type=="radio")) { for (var i=0; i 0) && (obj[0] != null) && (obj[0].type=="checkbox")) { //if (obj.length > 0 && obj[0] != null) //{ var val = ""; for (var i=0; i 0) { val = val.substring(0,val.length-1); // remove trailing comma } return val; } if (obj.type=="checkbox") { if (obj.checked == true) { return obj.value; } return ""; } if (obj.type=="select-one") { if (obj.options.length > 0) { return obj.options[obj.selectedIndex].value; } else { return ""; } } if (obj.type=="select-multiple") { var val = ""; for (var i=0; i 0) { val = val.substring(0,val.length-1); // remove trailing comma } return val; } return ""; } //End DataVMObject Helper Functions *********************************************************** //================================================================================= // END DataVMObject - Provides Data Validation and Manipulation for forms. //================================================================================= // Helper function- works like printf() in C where string containing %x is replaced with array args[] // parameters, such as: var newstr = fmt("Test %x 2 3 %x", Array("1","4")); function fmt(string, args) { var str = new String(string); var res; // the result string. res = ""; var pos; // the current position in the args array. pos = 0; var i; for(i=0;i< str.length;i++) { //' found a fmt char. if (str.charAt(i)=="%") { if (i=0;i--) { box.options[i]=null } } } function OpenLink(strURL) { window.open(strURL,'popupLink','toolbar=yes, location=yes, directories=yes, status=yes, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=no, width=800, height=600'); } function openWindow(url, target, width, height, left, top){ var x = window.open (url, target,"status=no,toolbars=no,scrollbars=no,width=" + width + ",height=" + height + ",resize=no,left=" + left + ",top=" + top) x.focus(); } function openScrollWindow(url, target, width, height, left, top){ window.open (url, target,"status=no,toolbars=no,scrollbars=yes,width=" + width + ",height=" + height + ",resize=no,left=" + left + ",top=" + top) } // -->