function trim(str, chars) { return ltrim(rtrim(str, chars), chars); } function ltrim(str, chars) { chars = chars || "\\s"; return str.replace(new RegExp("^[" + chars + "]+", "g"), ""); } function rtrim(str, chars) { chars = chars || "\\s"; return str.replace(new RegExp("[" + chars + "]+$", "g"), ""); } function checkEmail(frmName,elementName){ var fieldValue = document.forms[frmName].elements[elementName].value; var chkAt = fieldValue.indexOf("@"); var chkDot= fieldValue.indexOf("."); var chkHack= fieldValue.indexOf("@."); var chkHackA= fieldValue.indexOf(".@"); if((chkAt >= 0) && (chkDot >= 0) && (chkHack == -1) && (chkHackA == -1) ) { return true; } return false; } function validateContactForm(fieldName){ var errorArr = new Array(); if(!fieldName){ if(trim(document.contact.from.value)==""){ errorArr['from'] = "Please enter your email address"; }else if(!checkEmail('contact','from')){ errorArr['from'] = "Please enter a valid email address"; } if(trim(document.contact.thoughts.value)==""){ errorArr['thoughts'] = "Please let us know your thoughts"; } if(trim(document.contact.code.value)==""){ errorArr['code'] = "Please enter the Anti-spam code"; } if((errorArr['from'])&&(errorArr['from']!="")){ document.getElementById('fromErrTxt').innerHTML = errorArr['from']; document.contact.from.className = "redBorder"; }else{ document.getElementById('fromErrTxt').innerHTML = " "; document.contact.from.className = "whiteBorder"; } if((errorArr['thoughts'])&&(errorArr['thoughts']!="")){ document.getElementById('thoughtsErrTxt').innerHTML = errorArr['thoughts']; document.contact.thoughts.className = "redBorder"; }else{ document.getElementById('thoughtsErrTxt').innerHTML = " "; document.contact.thoughts.className = "whiteBorder"; } if((errorArr['code'])&&(errorArr['code']!="")){ document.getElementById('codeErrTxt').innerHTML = errorArr['code']; document.contact.code.className = "redBorder"; }else{ document.getElementById('codeErrTxt').innerHTML = " "; document.contact.code.className = "whiteBorder"; } }else{ switch(fieldName){ case "from": if(trim(document.contact.from.value)==""){ errorArr['from'] = "Please enter your email address"; }else if(!checkEmail('contact','from')){ errorArr['from'] = "Please enter a valid email address"; } if((errorArr['from'])&&(errorArr['from']!="")){ document.getElementById('fromErrTxt').innerHTML = errorArr['from']; document.contact.from.className = "redBorder"; }else{ document.getElementById('fromErrTxt').innerHTML = " "; document.contact.from.className = "whiteBorder"; } break; case "thoughts": if(trim(document.contact.thoughts.value)==""){ errorArr['thoughts'] = "Please let us know your thoughts"; } if((errorArr['thoughts'])&&(errorArr['thoughts']!="")){ document.getElementById('thoughtsErrTxt').innerHTML = errorArr['thoughts']; document.contact.thoughts.className = "redBorder"; }else{ document.getElementById('thoughtsErrTxt').innerHTML = " "; document.contact.thoughts.className = "whiteBorder"; } break; case "code": if(trim(document.contact.code.value)==""){ errorArr['code'] = "Please enter the Anti-spam code"; } if((errorArr['code'])&&(errorArr['code']!="")){ document.getElementById('codeErrTxt').innerHTML = errorArr['code']; document.contact.code.className = "redBorder"; }else{ document.getElementById('codeErrTxt').innerHTML = " "; document.contact.code.className = "whiteBorder"; } break; } } if((errorArr['from'])||(errorArr['thoughts'])||(errorArr['code'])){ return false; }else{ return true; } return false; }