// These Common Scripts can go in the // (include them in 3 websites: /Graeme, /MathHelp, and /Links, example code: // ) // --------------- Common Data Constructors --------------- // a "vector" object has properties X and Y. function vector(X, Y) { this.X = X; this.Y = Y;} // --------------- Common HTML builders --------------- // table functions function Htable() { var argv = Htable.arguments; var argc = argv.length; if (argc==0) { return "";} if (argc==1) { return "" + argv[0] + "
";} var x=""; for (var i=1; i" + x + ""; } function Hframe(t) { return Htable("border=10 cellspacing=0 cellpadding=4",Htr(Htd(t)));} function Htr() { var argv = Htr.arguments; var argc = argv.length; var x = ""; for (var i=0; i" + argv[i] + "";} return x;} function Htd() { var argv = Htd.arguments; var argc = argv.length; var x = ""; for (var i=0; i" + y + "";} return x;} // --------------- Common document manipulation functions --------------- function BreakOutOfFrame() { // see http://www.thesitewizard.com/archive/framebreak.shtml if (top.location != location) { top.location.href = document.location.href ; } } // @@@@@ // @@@@@ * * * * * * * * * * Client-Side Functions * * * * * * * * * * // @@@@@ function DataTypeCheck(a,type) { // modified 9/12/99 gwm if (type=="int") { var checkOK = "0123456789-,"; var allValid = true; var decPoints = 0; var allNum = ""; for (i = 0; i < a.length; i++) { ch = a.charAt(i); for (j = 0; j < checkOK.length; j++) if (ch == checkOK.charAt(j)) break; if (j == checkOK.length) { allValid = false; break;} if (ch != ",") allNum += ch; } return (allValid); } if (type=="numeric") { var checkOK = "0123456789-,."; var allValid = true; var decPoints = 0; var allNum = ""; for (i = 0; i < a.length; i++) { ch = a.charAt(i); for (j = 0; j < checkOK.length; j++) if (ch == checkOK.charAt(j)) break; if (j == checkOK.length) { allValid = false; break;} if (ch == "." && decPoints==0) decPoints=1; if (ch == "." && decPoints!=0) { allValid = false; break;} if (ch != ",") allNum += ch; } return (allValid); } if (type=="email") { // valid email addreses have no special chars other than "-_.", exactly one @, and at least one . after the @. var checkOK = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_@."; var allValid = true; var atSigns = 0; var decPoints = 0; for (i = 0; i < a.length; i++) { ch = a.charAt(i); for (j = 0; j < checkOK.length; j++) if (ch == checkOK.charAt(j)) break; if (j == checkOK.length) { allValid = false; break;} if (ch == "@") { atSigns++;} if (ch == "." && atSigns==1) { decPoints++;} } return (allValid && atSigns==1 && decPoints>0); } if (type=="zipcode") { // valid zipcodes are nnnnn or nnnnn-nnnn var checkOK = "0123456789-"; var allValid = true; if (a.length!=5 && a.length!=10) return(false); if (a.length==10 && a.charAt(5)!="-") return(false); for (i = 0; i < a.length; i++) { ch = a.charAt(i); for (j = 0; j < checkOK.length; j++) if (ch == checkOK.charAt(j)) break; if (j == checkOK.length) { allValid = false; break;} if (ch == "-" && i!=5) { allValid = false; break;} } return (allValid); } if (type=="usphone") { // valid us phone numbers have exactly 10 digits plus some special characters. var checkOK = "0123456789-() "; var allValid = true; var allNum = ""; for (i = 0; i < a.length; i++) { ch = a.charAt(i); for (j = 0; j < checkOK.length; j++) if (ch == checkOK.charAt(j)) break; if (j == checkOK.length) { allValid = false; break;} if (j<10) { allNum += ch;} } return (allValid && allNum.length==10); } if (type=="ssn") { // valid ssn must have exactly 9 digits plus some special characters. var checkOK = "0123456789- "; var allValid = true; var allNum = ""; for (i = 0; i < a.length; i++) { ch = a.charAt(i); for (j = 0; j < checkOK.length; j++) if (ch == checkOK.charAt(j)) break; if (j == checkOK.length) { allValid = false; break;} if (j<10) { allNum += ch;} } return (allValid && allNum.length==9); } if (type=="date") { // valid date is mm/dd/yy or mm/dd/yyyy, where 0<=yy<=99 or 1800<=yy<=2099. 00 is a leap year. 1800, 1900 are not leap years. var checkOK = "0123456789/"; var allValid = true; var mm=0; var dd=0; var yy=0; var slashes = 0; for (i = 0; i < a.length; i++) { ch = a.charAt(i); for (j = 0; j < checkOK.length; j++) if (ch == checkOK.charAt(j)) break; if (j == checkOK.length) { allValid = false; break;} if (ch == "/") { slashes++;} else if (slashes==0) mm=mm*10+j; else if (slashes==1) dd=dd*10+j; else if (slashes==2) yy=yy*10+j; } if (slashes!=2) allValid=false; else if (mm<1 || mm>12) allValid=false; else if (yy>99 && yy<1800) allValid=false; else if (yy>2199) allValid=false; else if (dd<1) allValid=false; else if (dd>31) allValid=false; else if (mm==2 && dd>29) allValid=false; else if (mm==4 && dd>30) allValid=false; else if (mm==6 && dd>30) allValid=false; else if (mm==9 && dd>30) allValid=false; else if (mm==11 && dd>30) allValid=false; else if (mm==2 && dd==29 && yy%4!=0) allValid=false; else if (mm==2 && dd==29 && yy%100==0 && yy%400!=0) allValid=false; return (allValid); } if (type=="text") { var allValid = true; return (allValid); } return(false); } function trim(a) { return(rtrim(ltrim(a))); } function ltrim(a) { while(a.length>0){ if (a.charAt(0)==" ") a=a.substr(1); else return(a); } return(a); } function rtrim(a) { while(a.length>0){ if (a.charAt(a.length-1)==" ") a=a.substr(0,a.length-1); else return(a); } return(a); }