// SMCrunch2 Version 2.0.1, Copyright (C) Smart Media Ltd 2001-2005. All Rights Reserved.
//
// !!! Note: JScript by default
//
var _info = navigator.userAgent;
var _ie = (_info.indexOf("MSIE") > 0 && _info.indexOf("Win") > 0 && _info.indexOf("Windows 3.1") < 0);
/// (c) Copyright Smart Media Limited, 2001-2006.
///
///This Software is provided under a license and may not be modified, copied or altered in any
///way by Licensee's own personnel or by any third party. This Software or parts of it,
///or its documentation may not be provided, copied, printed out or otherwise made available to
///any other person. Any attempt to copy, modify or reverse engineer the Software is strictly
///prohibited.
///
///This Software may only be used on the equipment it is licensed for.
///
///Title, copyright and all other proprietary rights in the Software and the Documentation and all
///parts and copies thereof shall remain vested with Smart Media Limited.
///
///This Software and information in it is subject to change without notice.
function checkASCII(file)
{
var rc = 0;
var colonCount = 0;
for (var i = 0;i < file.length;i++)
{
var c = file.charAt(i);
var cc = file.charCodeAt(i);
if (cc < 0x20 || cc > 0x7f)
{
rc = 1;
break;
}
else if (c == '?' || c == '*' || c == '"' || c == '<' || c ==  '>' || c ==  '|'  || c == '%')
{
rc = 2;
break;
}
else if (c == ':')
{
colonCount++;
if (colonCount > 1)
{
rc = 3;
break;
}
}
}
return rc;
}
function dealWithSpecial(e)
{
var obj = null;
var msg = null;
if (_ie)
{
obj = window.event.srcElement;
}
else
{
obj = e.target;
}
if (typeof obj.special != 'undefined')
{
var s = obj.special;
if (s != null)
{
if (s.charAt(0) == '$')
{
s = s.substring(1);
for (var i = 0;i < document.FORM1.elements.length;i++)
{
if (document.FORM1.elements[i].name == s)
{
while (1)
{
s = prompt(document.FORM1.elements[i].description, document.FORM1.elements[i].value);
if (s == null && !isblank(document.FORM1.elements[i].value))
break;
else if (s != null && !isblank(s))
{
document.FORM1.elements[i].value = s;
break;
}
}
break;
}
}
}
}
}
}
function displayHelp(e)
{
var obj = null;
var msg = null;
var ea = null;
if (_ie)
{
obj = window.event.srcElement;
}
else
{
obj = e.target;
}
if (typeof obj.error != 'undefined')
{
if (obj.error != "")
msg = obj.error;
}
if (typeof obj.ea != 'undefined')
{
if (obj.ea != "")
ea = obj.ea;
}
if (msg == null)
{
if (typeof obj.help != "undefined")
msg = obj.help;
}
if (msg != null)
{
window.status = msg;
}
else
{
window.status = "";
}
if (obj.calculation != null && obj.calculation.length != 0)
{
doCalculation(obj.calculation, obj.fname, ea)
}
}
function isblank(s)
{
if( s != null )
{
for (var i = 0;i < s.length;i++)
{
var c = s.charAt(i);
if (c != ' ' && c != '\n' && c != '\t')
return false;
}
}
return true;
}
function isdigit(c)
{
return (c>='0' && c<='9');
}
function isuppercase(value)
{
tflag=true;
for(c=0;c<value.length;c++)
{
j=value.charAt(c);
if(j >= 'a' && j <= 'z')
{
tflag=false;
break;
}
}
return tflag;
}
function isnum(value)
{
tflag=true;
for(c=0;c<value.length;c++)
{
j=value.charAt(c);
if(!isdigit(j))
{
if ((j == '-' || j == '+') && c == 0)
continue;
tflag=false;
break;
}
}
return tflag;
}
function isfloat(value)
{
tflag=true;
dpflag = false;
for(c=0;c<value.length;c++)
{
j=value.charAt(c);
if (j == '.' && !dpflag)
{
dpflag = true;
}
else if(!isdigit(j))
{
if ((j == '-' || j == '+') && c == 0)
continue;
tflag=false;
break;
}
}
return tflag;
}
function istime(t_buff)
{
tflag=true;
if (t_buff.length != 5 || t_buff.charAt(2) != ':')
tflag = false;
else
{
if (!isdigit(t_buff.charAt(0)) || !isdigit(t_buff.charAt(1)) || !isdigit(t_buff.charAt(3)) || !isdigit(t_buff.charAt(4)))
{
tflag = false;
}
else
{
h = parseInt(t_buff.substring(0,2), 10);
m = parseInt(t_buff.substring(3,5), 10);
if (h >= 24 || m >= 60)
tflag = false;
}
}
return tflag;
}
function get4digityear(twodigit)
{
var v = parseInt(""+twodigit,10);
if (!isNaN(v) && v < 100)
{
var pivotDate = 30;
var today = new Date();
var todayy = today.getYear();
if (todayy < 1000) todayy+=1900;
var century = Math.floor(todayy/100)*100;
var todayn  = (todayy-(100-pivotDate))%100;
var todaynd = Math.floor((todayy-(100-pivotDate))/100)*100 - century;
var todayp  = (todayy+pivotDate)%100;
var todaypd = Math.floor((todayy+pivotDate)/100)*100 - century;
if (v > todayn)
v += todaynd;
else if (v < todayp)
v += todaypd;
v += century;
return v;
}
return v;
}
function isdate(e)
{
x = e.value;
months=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
n=new Array();
if(x=="")
{
return false;
}
cc=0;
op=0;
j=""+x;
while((np=j.indexOf("/",op))>=0)
{
n[cc]=j.substring(op,np);
cc++;
op=np+1;
}
n[cc]=j.substring(op,j.length);
if(n.length<3)
{
return false;
}
c_day=parseInt(n[0],10);
if( isNaN(c_day) || !isnum(n[0]) )
return false;
c_mon=parseInt(n[1],10);
if( isNaN(c_mon) || !isnum(n[1]) )
return false;
c_yer=parseInt(n[2],10);
if( isNaN(c_yer) || !isnum(n[0]) )
return false;
if(c_yer<100)
{
c_yer=get4digityear(c_yer);
e.value = c_day+'/'+c_mon+'/'+c_yer;
}
if(0==(c_day*c_mon*c_yer))
{
return false;
}
if(((c_yer%4==0)&&!(c_yer%100==0))||(c_yer%400==0))months[1]=29;
if((c_mon<1)||(c_mon>12))
{
return false;
}
if((c_yer<1900)||(c_yer>2500))
{
return false;
}
if((c_day<1)||(c_day>months[c_mon-1]))
{
return false;
}
return true;
}
function checkrange(e)
{
if (e.fmin == null)
{
if (e.ftype == 'I') e.fmin = '-2147483648';
else if (e.ftype == 'M') e.fmin = '-922337203685477.00';
}
if (e.fmax == null)
{
if (e.ftype == 'I') e.fmax = '2147483647';
else if (e.ftype == 'M') e.fmax = '922337203685477.00';
}
if (e.fmin != null || e.fmax != null)
{
if (e.ftype == 'I')
{
var d = parseInt(e.value, 10);
if (e.fmin != null && d < parseInt(e.fmin, 10))
return false;
if (e.fmax != null && d > parseInt(e.fmax, 10))
return false;
}
else if (e.ftype == 'F' || e.ftype == 'M')
{
var d = parseFloat(e.value);
if (e.fmin != null && d < parseFloat(e.fmin))
return false;
if (e.fmax != null && d > parseFloat(e.fmax))
return false;
}
}
return true;
}
function range(e)
{
if (e.fmin == null)
{
if (e.ftype == 'I') e.fmin = '-2147483648';
else if (e.ftype == 'M') e.fmin = '-922337203685477.00';
}
if (e.fmax == null)
{
if (e.ftype == 'I') e.fmax = '2147483647';
else if (e.ftype == 'M') e.fmax = '922337203685477.00';
}
if (e.fmin != null && e.fmax != null)
return "must be between "+e.fmin+" and "+e.fmax;
else if (e.fmin != null)
return "must be greater than "+e.fmin;
else if (e.fmax != null)
return "must be less than "+e.fmax;
return "";
}
function checkRequireNotesIfChecked(e)
{
if (e.ftype == "E" && e.checked)
{
document.commentRequired = true;
if (document.commentFilledIn == null)
document.commentFilledIn = false;
if (document.commentRequired && document.commentFilledIn == false)
{
return true;
}
}
return false;
}
function isemail(field)
{
if( field == null || field.value == null || field.value == '' )
return true;
if( field.value.match(/^[!#$%&'*\+\-\/\w\=\?\^`\{\|\}~]+([\.][!#$%&'*\+\-\/\w\=\?\^`\{\|\}~]+)*@([A-Za-z0-9]([\-]*[A-Za-z0-9])*)+(\.[A-Za-z]([\-]*[A-Za-z0-9])*)*$/) == null )
{
return false;
}
return true;
}

function checkForm(obj, doAdvVal, checksection)
{
var errors = "";
var required = "";
var focusToSet = null;
var displayComments = false;
var uploaddisable = false;
var uploadelem = null;
if (doAdvVal == null || doAdvVal != false) doAdvVal = true;
for (var i = 0;i < obj.length;i++)
{
var e = obj.elements[i];
e.error = "";
if (e.type != "hidden" && e.type != "submit")
{
var fid = e.fid;
var ftype = e.ftype;
var vfunc = e.vfunc;
var name = e.name;
var description = e.description;
if( checksection == null || checksection == e.gname )
{
if (e.type == "select-one" || e.type == "select-multiple")
{
for (var j = 0;j < e.options.length;j++)
{
if (e.options[j].selected)
{
if (e.options[j].prompt)
{
if (e.mandatory)
{
required += "    "+e.description+"\n";
e.error = "Mandatory field, requires data.";
if (focusToSet == null)
{
focusToSet = e;
}
}
}
break;
}
}
}
else if (e.type == "radio")
{
var pfid = fid;
var selected = -1;
var selectedValue = "";
var j = 0;
var man = e.mandatory;
var start = i;
while (i < obj.length)
{
e = obj.elements[i];
if (e.type != "radio" || e.fid != pfid)
break;
if (e.checked)
{
selected = j;
selectedValue = e.value;
}
j++;
i++;
}
i--;
if (man && selected < 0)
{
required += "    "+description+"\n";
for (var a = start;a <= i;a++)
{
obj.elements[a].error = "Mandatory field, requires data.";
}
if (focusToSet == null)
{
focusToSet = obj.elements[start];
}
}
}
else if (e.type == 'file')
{
if (e.value != null && l(e.value).length != 0)
{
var rc = checkASCII(e.value)
if (rc == 1 || rc == 2 || rc == 3)
{
if (rc == 1)
{
errors += " Only ASCII characters allowed in filename for "+description+"\n";
}
else if (rc == 3)
{
errors += " Invalid filename for " + description + ". Filename can only contain 1 colon.\n";
}
else
{
errors += " Invalid character detected in filename. ?*\"<>|% are not allowed in filename for "+description+"\n";
}
if (focusToSet == null)
focusToSet = e;
}
}
var man = e.mandatory
if (!doAdvVal)
{
man = false;
}
if (man)
{
if (e.value == null || e.value.length == 0)
{
required += "    "+description+"\n";
if (focusToSet == null)
{
focusToSet = e;
}
}
}
else
{
if (e.value == null || l(e.value).length == 0)
{
uploaddisable = true;
uploadelem = e;
}
}
}
else
{
var blank = isblank(e.value);
if (e.mandatory && blank)
{
required += "    "+description+"\n";
e.error = "Mandatory field, requires data.";
if (focusToSet == null)
{
focusToSet = e;
}
}
else if (!blank)
{
if (e.vfunc != null && doAdvVal)
{
if (e.vfunc == "isuppercase")
{
if (!isuppercase(e.value))
{
errors += "    Uppercase characters only for "+description+"\n";
e.error = "Uppercase only.";
if (focusToSet == null)
{
focusToSet = e;
}
}
}
else if (e.vfunc == "checkList")
{
if (e.validated == false)
{
checkList(e);
return false;
}
}
else if (e.vfunc == "requireNotesIfChecked")
{
if (obj.smwfid != null && obj.instanceid)
{
if (checkRequireNotesIfChecked(e))
{
errors += "   Please add a comment for "+description+"\n";
e.error = "Comments Required.";
displayComments = true;
}
}
}
}
if (ftype == 'I')
{
if (!isnum(e.value))
{
errors += "    Number required for "+description+"\n";
e.error = "Requires number.";
if (focusToSet == null)
{
focusToSet = e;
}
}
else if (!checkrange(e))
{
errors += "    Invalid number for "+description+", "+range(e)+"\n";
e.error = "Invalid number entered ("+range(e)+").";
if (focusToSet == null)
{
focusToSet = e;
}
}
}
else if (ftype == 'M')
{
if (!isfloat(e.value))
{
errors += "    Money Value required for "+description+"\n";
e.error = "Requires Money Value.";
if (focusToSet == null)
{
focusToSet = e;
}
}
else if (!checkrange(e))
{
errors += "    Invalid money value for "+description+", "+range(e)+"\n";
e.error = "Invalid Money value entered ("+range(e)+").";
if (focusToSet == null)
{
focusToSet = e;
}
}
}
else if (ftype == 'F')
{
if (!isfloat(e.value))
{
errors += "    Decimal required for "+description+"\n";
e.error = "Requires decimal value.";
if (focusToSet == null)
{
focusToSet = e;
}
}
else if (!checkrange(e))
{
errors += "    Invalid decimal for "+description+", "+range(e)+"\n";
e.error = "Invalid decimal value entered ("+range(e)+").";
if (focusToSet == null)
{
focusToSet = e;
}
}
}
else if (ftype == 'D')
{
if (!isdate(e))
{
errors += "    Date required for "+description+"\n";
e.error = "Requires date (dd/mm/yy format).";
if (focusToSet == null)
{
focusToSet = e;
}
}
}
else if (ftype == 'C')
{
var abbr = e.abbreviation;
if( abbr != null && abbr.toUpperCase()=='EMAIL' )
{
if( !isemail(e) )
{
errors += "    Valid email address required for "+description+"\n";
e.error = "Requires valid email address.";
if (focusToSet == null)
{
focusToSet = e;
}
}
}
}
}
}
if (e.vfunc != null && e.vfunc != "" && doAdvVal)
{
if (e.vfunc != "isuppercase" &&
e.vfunc != "checkList" &&
e.vfunc != "requireNotesIfChecked")
{
var vfuncret = null;
eval('vfuncret='+e.vfunc);
if( vfuncret != null )
{
errors+=vfuncret;
}
}
}
}
}
}
if (!doAdvVal)
{
required = ""; 
}
if (errors != "" || required != "")
{
var msgString = "Please correct the following error(s) and re-submit\n\n";
if (required != "")
msgString += "- The following mandatory fields are empty:\n"+required+"\n";
if (errors != "")
msgString += "- The following errors occured:\n"+errors+"\n";
alert(msgString);
if (focusToSet != null)
focusToSet.focus();
if (displayComments)
{
var smwfid = obj.smwfid.value;
var instanceid = obj.instanceid.value;
var testMode = false;
if (obj.testMode != null && obj.testMode.value == "1")
testMode = true;
var currentpid = obj.pid.value;
sp_view_window('/smii/smwf/addcomments.asp?smwfid='+smwfid+'&instance_id='+instanceid+'&pid='+pid+(testMode?'&testMode=1':''));
}
return false;
}
if( uploaddisable == true )
{
if( uploadelem != null )
{
uploadelem.disabled = true;
}
obj.encoding = "application/x-www-form-urlencoded";
}
return true;
}
function checkCommentReq()
{
if (document.commentRequired != null && document.commentFilledIn != null && document.commentOn != null)
{
if (document.commentRequired && document.commentFilledIn == false)
{
var a = document.FORM1.TASK.value;
if ((document.commentOn & 8 ) == 8 && a == 'P')
{
return true;
}
if ((document.commentOn & 4 ) == 4 && a == 'R')
{
return true;
}
}
}
return false;
}
function performAction(a,pid)
{
var frm = document.FORM1;
frm.TASK.value = a;
frm.nextpid.value=pid;
var actions=null;
if (performAction.arguments.length > 2)
actions = performAction.arguments[2];
var doAdvVal = true;
if (a == 'R' || 
a == 'P' || 
a == 'H' || 
a == 'L') 
{
doAdvVal = false;
}
if(checkForm(frm, doAdvVal))
{
if (checkCommentReq())
{
alert("Please add a comment to this instance.");
var smwfid = frm.smwfid.value;
var instanceid = frm.instanceid.value;
var testMode = false;
if (frm.testMode != null && frm.testMode.value == "1")
testMode = true;
var currentpid = frm.pid.value;
sp_view_window('/smii/smwf/addcomments.asp?smwfid='+smwfid+'&instance_id='+instanceid+'&pid='+pid+(testMode?'&testMode=1':''));
return;
}
else
{
var ok = true;
if (a == 'A' && frm.pinreq.value==2) 
{
if (frm.testMode != null && frm.testMode.value == "1")
alert("A PIN will be required at this point");
else
{
pinwin = window.open('/smii/smwf/smfc/pininsert.asp',"win",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=300,height=175');
ok = false
}
}
if (ok)
{
if(actions != null)
{
if ((actions & 1) == 1) document.FORM_ACTIONS.Continue.disabled = true;
if ((actions & 2) == 2) document.FORM_ACTIONS.Approve.disabled = true;
if ((actions & 4) == 4) document.FORM_ACTIONS.Reject.disabled = true;
if ((actions & 8) == 8) document.FORM_ACTIONS.BounceBack.disabled = true;
if ((actions &16) == 16) document.FORM_ACTIONS.Putonhold.disabled = true;
if ((actions &32) == 32) document.FORM_ACTIONS.Saveforlater.disabled = true;
}
if (typeof tableSubmit == "function")
{
tableSubmit(frm);
}
frm.submit();
}
}
}
}
function doCalculation(calculation, fname, extraArgs)
{
var calcarray = calculation.split(" ");
var field1 = calcarray[0]+".value";
var operand = calcarray[1];
var field2 = calcarray[2]+".value";
if (operand == '+')
operand = 'plus';
else if (operand == '-')
operand = 'minus';
else if (operand == '*')
operand = 'multiply';
else if (operand == '/')
operand = 'divide';
if ( eval(field1) != null &&
eval(field1).length != 0 &&
eval(field2) != null &&
eval(field2).length != 0 &&
isfloat(eval(field1)) &&
isfloat(eval(field2)) )
{
parent.calculation.location.href='calculation.asp?fieldone='+eval(field1)+'&fieldtwo='+eval(field2)+'&operand='+operand+'&fname='+fname+((extraArgs!=null)?'&'+extraArgs:'');
}
else
{
parent.calculation.location.href='calculation.asp?fname='+fname+((extraArgs!=null)?'&'+extraArgs:'');
}
}
function l(s)
{
if (s == "") return "";
var i;
for (i = 0;i < s.length;i++)
{
if (s.charAt(i) != ' ')
break;
}
if (i >= s.length)
return "";
return s.substring(i);
}
function isalphaNumeric(s)
{
var compString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for (var i = 0;i < s.length;i++)
{
var c = s.charAt(i);
if (compString.indexOf(c) == -1)
return false;
}
return true;
}
function OpenSearch(fname, sqlid, smwfid, testMode)
{
var now = new Date();
var searchString = document.FORM1[fname].value;
var resstr = '';
var additionalargs = null;;
var validate = false;
if (OpenSearch.arguments.length > 4)
additionalargs = OpenSearch.arguments[4];
if (OpenSearch.arguments.length > 5)
validate = OpenSearch.arguments[5];
if( additionalargs != null )
{
var argarray = additionalargs.split('|');
var fdataarray = new Array();
var i=0;
for( i=0; i<argarray.length; i++ )
{
var elem = findElementInForm(document.FORM1,argarray[i]);
if( argarray[i] != null && elem != null && elem.value != null )
{
fdataarray[i] = elem.value;
elem.flInputField = true;
}
else
{
fdataarray[i] = '';
}
}
for( i=0; i<fdataarray.length; i++ )
{
if( i!=0)
resstr += '|'+fdataarray[i];
else
resstr = fdataarray[i];
}
}
var urlstr = 'posearch.asp?searchstr='+escape(searchString)+
'&fname='+fname+
'&sqlid='+sqlid+
'&testMode='+testMode+
'&x='+now.getTime();
if (smwfid != null)
urlstr+= '&smwfid='+smwfid;
if( resstr != null && resstr.length > 0 )
urlstr+= '&additionalcols='+escape(resstr);
if (validate)
urlstr+= '&validation=true';
if (validate)
parent.validator.location.href = urlstr;
else
{
var w = window.open(urlstr, 'Test', 'width=600,height=175,resizable=1,scrollbars=1')
flwin=w;
w.focus();
}
}
function checkList(e)
{
var fname = e.name;
var sqlid = e.sqlid;
var smwfid = e.form.smwfid.value;
if(e.ea != null && e.ea.indexOf("testMode=1") != -1)
var testMode = true;
else
var testMode = false;
var additionalcols = e.additionalcols;
OpenSearch(fname, sqlid, smwfid, testMode, additionalcols, true)
}
function findElement(e,description)
{
if( e != null )
{
var formelem = e.form;
return findElementInForm(formelem,description);
}
return null;
}
function findElementInForm(formelem,description)
{
if( formelem != null )
{
for(var i=0; i<formelem.length; i++)
{
if( formelem[i].description == description )
return formelem[i];
}
}
return null;
}
function findAbbrInForm(formelem,abbreviation)
{
if( formelem != null )
{
for(var i=0; i<formelem.length; i++)
{
if( formelem[i].abbreviation != null && formelem[i].abbreviation == abbreviation )
return formelem[i];
}
}
return null;
}
function resetValFlag(e)
{
var obj = null;
if (_ie)
{
obj = window.event.srcElement;
}
else
{
obj = e.target;
}
obj.validated=false;
}
