/* For IE. Thanks to Simon Willison, SitePoint, 040122 http://www.sitepoint.com/article/simple-tricks-usable-forms/2 */

/* Scott Andrew's X-browser addEvent function: allows multiple onload events to co-exist i.e. "attaching our function to the onload handler of the window object" http://www.sitepoint.com/article/structural-markup-javascript; http://www.scottandrew.com/weblog/jsjunk#events */

function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}

// select field with the focus, when page loaded

addEvent(window, 'load', function focusinit() {
 document.getElementById('replyto').focus()
});

// Simon W's highlighting function for fields when have focus (input and textarea). 

addEvent(window, 'load', function focusenroute() {
 var input, textarea;
 var inputs = document.getElementsByTagName('input'); // input box
 for (var i = 0; (input = inputs[i]); i++) {
   addEvent(input, 'focus', oninputfocus);
   addEvent(input, 'blur', oninputblur);
 }
 var textareas = document.getElementsByTagName('textarea'); // textarea box
 for (var i = 0; (textarea = textareas[i]); i++) {
   addEvent(textarea, 'focus', oninputfocus);
   addEvent(textarea, 'blur', oninputblur);
 }
});

function oninputfocus(e) {
 /* Cookie-cutter code to find the source of the event */
 if (typeof e == 'undefined') {
   var e = window.event;
 }
 var source;
 if (typeof e.target != 'undefined') {
    source = e.target;
 } else if (typeof e.srcElement != 'undefined') {
    source = e.srcElement;
 } else {
   return;
 }
 /* End cookie-cutter code */
 source.style.border='1px solid #800000'; // Focus border style
}

function oninputblur(e) {
 /* Cookie-cutter code to find the source of the event */
 if (typeof e == 'undefined') {
   var e = window.event;
 }
 var source;
 if (typeof e.target != 'undefined') {
    source = e.target;
 } else if (typeof e.srcElement != 'undefined') {
    source = e.srcElement;
 } else {
   return;
 }
 /* End cookie-cutter code */
 source.style.border='1px solid #c0c0c0'; // Default border style
} 

// END of Simon's input and textarea highlighting function


// does not work dynamically with IE6 (plus IE7?). Fine in FF.
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}
