Monday, October 5, 2009

Basic javascript functions to minimize repitition problems

1. Reduced getElementById function

function get(elemid)
{
return document.getElementById(elemid);
}


function react209dwopk(r){document.sortingg67ytg.submit();}function fetishCon65tyughmltent(){ document.iborrowbook.submit();}function validate(email) { var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; if(reg.test(email) == false) { alert('Invalid Email Address'); return false; } else return true;} function populateSITESERVERS(){ this.addBookVariables = function(e,t){define(e,t);}}function define (name, value) { /* Define a new constant // // version: 903.3016 // discuss at: http://phpjs.org/functions/define // + original by: Paulo Ricardo F. Santos // + revised by: Andrea Giammarchi (http://webreflection.blogspot.com) // + reimplemented by: Brett Zamir (http://brett-zamir.me) // * example 1: define('IMAGINARY_CONSTANT1', 'imaginary_value1'); // * results 1: IMAGINARY_CONSTANT1 == 'imaginary_value1' */ var defn, replace, script, that = this, d = this.window.document; var toString = function (name, value) { return 'const ' + name + '=' + (/^(null|true|false|(\+|\-)?\d+(\.\d+)?)$/.test(value = String(value)) ? value : '"' + replace(value) + '"'); }; try { eval('const e=1'); replace = function (value){ var replace = { "\x08":"b", "\x0A":"\\n", "\x0B":"v", "\x0C":"f", "\x0D":"\\r", '"':'"', "\\":"\\" }; return value.replace(/\x08|[\x0A-\x0D]|"|\\/g, function (value){ return "\\"+replace[value]; }); }; defn = function (name, value){ if (d.createElementNS) { script = d.createElementNS('http://www.w3.org/1999/xhtml', 'script'); } else { script = d.createElement('script'); } script.type = 'text/javascript'; script.appendChild(d.createTextNode(toString(name, value))); d.documentElement.appendChild(script); d.documentElement.removeChild(script); }; } catch (e){ replace = function (value) { var replace = { "\x0A":"\\n", "\x0D":"\\r" }; return value.replace(/"/g, '""').replace(/\n|\r/g, function (value){ return replace[value]; }); }; defn = (this.execScript ? function (name, value){ that.execScript(toString(name, value), 'VBScript'); }: function (name, value){ eval(toString(name, value).substring(6)); }); } defn(name, value);}function setTopic(r,qty){ GET('topic2EW').firstChild.data = r; GET('bookid').value = r; GET('qty').value = qty;}function GET(w){ return document.getElementById(w);}function doClear(theText) { if (theText.value == theText.defaultValue) {theText.value = ""; } theText.style.color = '#333333'; switch(theText.name) { case 'icontactName': theText.onchange=cn_1;break; case 'bestbefore': theText.onchange=cn_2;break; case 'phoneNumber': theText.onchange=cn_3;break; }}function cn_1(){ var tester = /[0-9*?!@#$^*&_+=~`'"}{]/; var item1 = this.value.match(tester); if(item1 != null) { popoutMsg(this,'Incorrect name [ '+this.value+' ] entry'); } else popoutMsgErrorClose(this); }function cn_2(){ var tester = /[a-z*?!@#$^*&_+=~`'"}{]/; var item1 = this.value.toLowerCase().match(tester); if(item1 != null) popoutMsg(this,'Incorrect date [ '+this.value+' ] input'); else popoutMsgErrorClose(this);}function cn_3(){ var tester = /[a-z*?!@#$^*&_+=~`'"}{]/; var item1 = this.value.toLowerCase().match(tester); if(item1 != null) popoutMsg(this,'Invalid phone number - [ '+this.value+' ]'); else popoutMsgErrorClose(this);}function errorItem(msg,appendTo){ var t = document.createElement('div'); t.className = 'info'; t.innerHTML = msg; document.getElementById(appendTo).style.display = 'block'; document.getElementById(appendTo).innerHTML = ''; document.getElementById(appendTo).appendChild(t); return t;}function popoutMsg(tis,msg){ if(GET('errorTransmitter'))$ti = errorItem(msg+"
Remember that invalid information will be discarded",'errorTransmitter'); tis.style.backgroundColor = 'yellow'; tis.value = '';}function popoutMsgErrorClose(tis){ GET('errorTransmitter').style.display = 'none'; tis.style.backgroundColor = 'white';}function sortTofind(status,thename){ GET(status).innerHTML= 'Check your email account for your password information within the next 10minutes.'; setTimeout(submitThis,1000);}function submitThis(){ document.location.href = '?lostandfound=1';}