/* Settings */

var intMaxChars=2000;         /* Maximum characters permitted for comments */

/*          */
/*************************************************************\
 *  DYNIFS - Dynamic IFrame Auto Size v1.0.0
 *
 *  Copyright (C) 2006, Markus (phpMiX)
 *  This script is released under GPL License.
 *  Feel free to use this script (or part of it) wherever you need
 *  it ...but please, give credit to original author. Thank you. :-)
 *  We will also appreciate any links you could give us.
 *  http://www.phpmix.org
 *
 *  Enjoy! ;-)
\*************************************************************/

var DYNIFS = {
  // Storage for known IFrames.
  iframes: {},
  // Here we save any previously installed onresize handler.
  oldresize: null,
  // Flag that tell us if we have already installed our onresize handler.
  ready: false,
  // The document dimensions last time onresize was executed.
  dim: [-1,-1],
  // Timer ID used to defer the actual resize action.
  timerID: 0,
  // Obtain the dimensions (width,height) of the given document.
  getDim: function(d) {
    var w=200, h=200, scr_h, off_h;
    if( d.height ) { return [d.width,d.height]; }
    with( d.body ) {
      if( scrollHeight ) { h=scr_h=scrollHeight; w=scrollWidth; }
      if( offsetHeight ) { h=off_h=offsetHeight; w=offsetWidth; }
      if( scr_h && off_h ) h=Math.max(scr_h, off_h);
    }
    return [w,h];
  },
  // This is our window.onresize handler.
  onresize: function() {
    // Invoke any previously installed onresize handler.
    if( typeof this.oldresize == 'function' ) { this.oldresize(); }
    // Check if the document dimensions really changed.
    var dim = this.getDim(document);
    if( this.dim[0] == dim[0] && this.dim[1] == dim[1] ) return;
    // Defer the resize action to prevent endless loop in quirksmode.
    if( this.timerID ) return;
    this.timerID = setTimeout('DYNIFS.deferred_resize();', 10);
  },
  // This is where the actual IFrame resize is invoked.
  deferred_resize: function() {
    // Walk the list of known IFrames to see if they need to be resized.
    for( var id in this.iframes ) this.resize(id);
    // Store resulting document dimensions.
    this.dim = this.getDim(document);
    // Clear the timer flag.
    this.timerID = 0;
  },
  // This is invoked when the IFrame is loaded or when the main window is resized.
  resize: function(id) {
    // Browser compatibility check.
    if( !window.frames || !window.frames[id] || !document.getElementById || !document.body )
      return;
    // Get references to the IFrame window and layer.
    var iframe = window.frames[id];
    var div = document.getElementById(id);
    if( !div ) return;
    // Save the IFrame id for later use in our onresize handler.
    if( !this.iframes[id] ) {
      this.iframes[id] = true;
    }
    // Should we inject our onresize event handler?
    if( !this.ready ) {
      this.ready = true;
      this.oldresize = window.onresize;
      window.onresize = new Function('DYNIFS.onresize();');
    }
    // This appears to be necessary in MSIE to compute the height
    // when the IFrame'd document is in quirksmode.
    // OTOH, it doesn't seem to break anything in standards mode, so...
    if( document.all ) div.style.height = '0px';
    // Resize the IFrame container.
    agt=navigator.userAgent.toLowerCase();
    if ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1)) {
       // Opera 9 needs a slight delay, so getDim can get the correct dimensions.
       setTimeout('var dim = DYNIFS.getDim(window.frames[\''+id+'\'].document); document.getElementById(\''+id+'\').style.height = (dim[1]+30) + \'px\';', 1);
    }
    else {
       var dim = this.getDim(iframe.document);
       div.style.height = (dim[1]+30) + 'px';
    }
  }
};

//********************************
//*
//* Function called on submit for field validation
//*
//********************************

function fnSubmit()
{
  fnTrim('foFeedback');                                                              // Trim text fields
  var var_Focus=fnMissing('foFeedback','tbName','Name','taComment','Comments');
  if (var_Focus>0)
  {
    return false;
  }
  else
  {
    /* Check Comment length*/
    var frm=document.forms['foFeedback'];
    var_CurrentLength=frm.taComment.value.length;
    var_MissingMsg="Please restrict your comments to 2000 characters or less. \nYour current comment size is "+ var_CurrentLength +" characters";
    if(var_CurrentLength > 2000)
    {
      alert(var_MissingMsg);
      frm.taComment.focus();
      return false;
    }
    else
    {
      /* Display status message */
      var var_status = document.getElementById('dvStatus');
      var_status.innerHTML="Please wait while we submit your comment.";
      return true;
    }
  }

}

//********************************
//* Trim function
//* - This generic function accepts the form name and trims all text/text area fields in the form
//********************************
function fnTrim(formName)
{
  var frm=document.forms[formName];
  for(i=0;i<frm.elements.length;i++)
  {
    // if((frm.elements[i].type=='text') || (frm.elements[i].type=='textarea') || (frm.elements[i].type=='file'))
    if((frm.elements[i].type=='text') || (frm.elements[i].type=='textarea'))
    {
      frm.elements[i].value=frm.elements[i].value.replace(/^\s+/, '').replace(/\s+$/, ''); // Trim whitespaces
    }
  }

}

//********************************
//* Missing fields validation
//* - This is a generic function accepting the form name and field name/label pairs as arguments.
//*   It trims the fields, alerts the user (if missing fields are found),sets the focus to the first erraneous field and returns the argument number of the first missing field.
//*
//********************************
function fnMissing(formName)
{
  var_MissingMsg="The following field(s) are missing:";
  var_Missing=0;
  var_ArgLength=arguments.length;
  var frm=document.forms[formName];
/*
  for(i=1;i<arguments.length;i+=2)
  {
    fr=eval(frm.elements[arguments[i]]);
    fr.value=fr.value.replace(/^\s+/, '').replace(/\s+$/, ''); // Trim whitespaces
  }
*/
  for(i=1;i<arguments.length;i+=2)
  {
    fr=eval(frm.elements[arguments[i]]);
    if(fr.value=='')
    {
       if(var_Missing==0)
       {
        var_Missing=Math.ceil(i/2);
       }

        var_MissingMsg=var_MissingMsg+"\n - "+arguments[i+1];                // Append Error Message
    }

  }
  if (var_Missing>0)
  {
    alert(var_MissingMsg);
    frm.elements[arguments[parseInt(2*var_Missing-1)]].focus();             // Set focus on first erraneous field
    return(var_Missing);
  }
  else return 0;
}



/*
AJFORM - World's Easiest JavaScript AJAX ToolKit
ajform.js
http://redredmusic.com/brendon/ajform/
Brendon Crawford <brendy@gmail.com>
Jim Manico <jim@manico.net>

## v1.3 UPDATED 2005-11-25 ##

  ** BSD LICENSE *********************************************************

  Copyright (c) 2005 Brendon Crawford
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
     derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

  ************************************************************************

*/

    STATIC_DOM = new Object;
      STATIC_DOM.setEventListener = function( eventName , functionName ) {
        if( document.addEventListener ) {
         this.addEventListener( eventName , functionName , false );
        }
        else if( document.attachEvent ) {
         this.attachEvent( "on" + eventName , functionName );
        }
      }
      STATIC_DOM.getSourceElement = function() {
        if( window.event != undefined ) {
         return window.event.srcElement;
        }
        else {
         return this;
        }
      }
      STATIC_DOM.getEvent = function(e) {
       thisEvent = new Object;
        //get the event object
        if( window.event != undefined ) {
         eventElm = window.event;
        }
        else if(e != undefined) {
         eventElm = e;
        }
        //get the event target
        if( eventElm.target ) {
         eventElm.targetElm = eventElm.target;
        }
        else if( eventElm.srcElement ) {
         eventElm.targetElm = eventElm.srcElement;
        }
        //get pageX|pageY
        if( eventElm.pageX && eventElm.pageY ) {
         thisEvent.pageX = eventElm.pageX;
         thisEvent.pageY = eventElm.pageY;
        }
        else if( eventElm.clientX && eventElm.clientY ) {
         thisEvent.pageX = eventElm.clientX + document.body.scrollLeft;
         thisEvent.pageY = eventElm.clientY +  document.body.scrollTop;
        }
        //get elementX|elementY
        if( eventElm.offsetX && eventElm.offsetY ) {
         thisEvent.elementX = eventElm.offsetX;
         thisEvent.elementY = eventElm.offsetY;
        }
        else {
         documentLeft = STATIC_DOM.getAbsoluteLeft( eventElm.targetElm );
         documentTop = STATIC_DOM.getAbsoluteTop( eventElm.targetElm );
         thisEvent.elementX = eventElm.pageX - documentLeft;
         thisEvent.elementY = eventElm.pageY - documentTop;
        }
       return thisEvent;
      }
      STATIC_DOM.getAbsoluteTop = function(thisObject) {
       var totalTop = 0;
        while( thisObject != null && thisObject != document.body ) {
         totalTop += parseInt( thisObject.offsetTop );
         thisObject = thisObject.offsetParent;
        }
       return totalTop;
      }
      STATIC_DOM.getAbsoluteLeft = function(thisObject) {
       var totalLeft = 0;
        while( thisObject != null && thisObject != document.body ) {
         totalLeft += parseInt( thisObject.offsetLeft );
         thisObject = thisObject.offsetParent;
        }
       return totalLeft;
      }

    window.setEventListener = STATIC_DOM.setEventListener;


    AJForm = new Object;
      AJForm.STATUS = {
        'SUCCESS' : 0 ,
        'HTTP_OBJECT_FAILED' : 1 ,
        'FILE_UPLOAD_FAILED' : 2 ,
        'SERVER_ERROR' : 3 ,
        'ENCODE_UNSUPPORTED' : 4
      };
      AJForm.getHTTPRequest = function() {
        if( typeof window.ActiveXObject != 'undefined' ) {
          try {
           doc = new ActiveXObject("Microsoft.XMLHTTP");
          }
          catch(e) {
           return false;
          }
        }
        else if( typeof XMLHttpRequest != 'undefined' ) {
          try {
           doc = new XMLHttpRequest();
          }
          catch(e) {
           return false;
          }
        }
       return doc;
      }

      AJForm.activateForm = function() {
       this.getSourceElement = STATIC_DOM.getSourceElement;
       thisForm = this.getSourceElement();
       preRetVal = true;

        if( thisForm.ajform.preCallback != null ) {
         preRetVal = thisForm.ajform.preCallback( thisForm );
        }
        //if the preProcess function returrns false, then we will not send data to the server
        if( preRetVal ) {
         //form.submit() is mapped to AJForm.submitForm
         postRetVal = thisForm.ajform_submit();
         return postRetVal;
        }
        else {
         return false;
        }
      }

      /*
        FORM SUBMISSION:
          submitForm()
        SCRIPTED SUBMISSION:
          form.submitForm( [callbackFunction]] )
      */
      AJForm.submitForm = function(){
        //if a second argument was specified
        if( AJForm.submitForm.arguments.length ) {
         userFunc =  eval( AJForm.submitForm.arguments[0] );
        }
        //if a callback was specified in the form
        else if( this.ajform.postCallback != null ) {
         userFunc = this.ajform.postCallback;
        }
        //If not a valid callback or no callback at all, then return
        if( userFunc == null      ||
          userFunc == ''        ||
          userFunc == undefined ||
          userFunc == 'undefined' ) {
         return true;
        }
        //check to see if we have proper UTF8 support
        if( AJForm.URLEncode('') == null ) {
         userFuncVal = userFunc( null , AJForm.STATUS['ENCODE_UNSUPPORTED'] , "Browser does not support proper JavaScript URL encoding." );
         return userFuncVal;
        }

        //no action specified
        if( (file = this.getAttribute('action')) == null ) {
         file = new String( window.location );
        }
        //intiialize httpRequest object and see if it failed
        if( !(doc = AJForm.getHTTPRequest()) ||
          !(docLoader = AJForm.getHTTPRequest()) ) {
         userFuncVal = userFunc( null , AJForm.STATUS['HTTP_OBJECT_FAILED'] , "Could not initiate HTTPRequest." );
         return userFuncVal;
        }
       dataStr = "";
       //construct values
       childList = this.getElementsByTagName('*');
        for( var e = 0; e < childList.length; e++ ) {
         thisInput = childList[e];
          //only get form elements
          if( thisInput.nodeName.toLowerCase() == 'input' ) {
           thisElmType = thisInput.getAttribute('type');
           thisElmType = ( thisElmType == null ) ? 'text' : thisElmType.toLowerCase();
          }
          else if( thisInput.nodeName.toLowerCase() == 'button' ) {
           thisElmType = 'button';
          }
          else if( thisInput.nodeName.toLowerCase() == 'textarea' ) {
           thisElmType = 'textarea';
          }
          else if( thisInput.nodeName.toLowerCase() == 'select' ) {
           thisElmType = 'select';
          }
          else {
           continue;
          }
          //do not handle elements with no names
          if( thisInput.name == '' || thisInput.name == 'undefined' ) {
           continue;
          }
          /*
            Account for different element types
          */
          //file upload
          if( thisElmType == "file" ) {
           userFuncVal = userFunc( null , AJForm.STATUS['FILE_UPLOAD_FAILED'] , "Unable to handle file uploads." );
           return userFuncVal;
          }
          //checkbox | radio
          else if( thisElmType == "checkbox" || thisElmType == "radio" ) {
            if( !thisInput.checked ) {
             continue;
            }
          }
          //image | submit
          else if( thisElmType == "image" || thisElmType == "submit" ) {
            //only include images|submits which were submitted
            if( thisInput != this.ajform.submitter.elm ) {
             continue;
            }
            //server side image map coordinates
            if( thisElmType == "image" ) {
             imgConName = AJForm.URLEncode( thisInput.name );
             imgConNameX = imgConName + ".x";
             imgConNameY = imgConName + ".y";
             imgConValX = new String(this.ajform.submitter.x);
             imgConValY = new String(this.ajform.submitter.y);
             dataStr += imgConNameX + "=" + imgConValX;
             dataStr += "&";
             dataStr += imgConNameY + "=" + imgConValY;
             dataStr += "&";
            }
          }

          //JMM- 11/18 fix 3 - added this clause for multiple select handling
          if (thisElmType == 'select') {
           selectName = thisInput.name;
           selectValue = thisInput.value

          //this hack send all select options, selected or not, shit!
            for (var sIndex=0; sIndex<thisInput.length; sIndex++) {
              if (thisInput.options[sIndex].selected) {
               controlName = AJForm.URLEncode( thisInput.name );
               controlValue = AJForm.URLEncode( thisInput.options[sIndex].value );
                if( e ) {
                dataStr += "&";
                }
               dataStr += (thisInput.name + "=" + controlValue );
              }
            }
          }
          else {
            //JMM 11/18 fix 1 = switched location of this and dataStr code below
            //argument separator
            if( e ) {
             dataStr += "&";
            }

           //encode the data
           controlName = AJForm.URLEncode( thisInput.name );
           controlValue = AJForm.URLEncode( thisInput.value );
           dataStr += (controlName + "=" + controlValue);
          }
        }

        //JMM 11/18 fix 2 - added & before ajform here
        //identify this as being an ajform set
        dataStr += "&ajform=1";
        doc.onreadystatechange = function() {
          // only if req shows "loaded"
          if (doc.readyState == 4) {
            //get server message
            if( doc.statusText == 'undefined' || doc.statusText == undefined ) {
             thisStatusText = "HTTP Code " + doc.status + "\nNo server message available."
            }
            else {
             thisStatusText = "HTTP Code " + doc.status + "\nServer responded, '" + doc.statusText + "'";
            }
            // only if "OK"
            if (doc.status == 200) {
             userFunc( doc.responseText , AJForm.STATUS['SUCCESS'] , "Operation completed successfully.\n" + thisStatusText );
            }
            else {
             userFunc( doc.responseText , AJForm.STATUS['SERVER_ERROR'], "A server error ocurred.\n" + thisStatusText );
            }
          }
        }
        requestType = this.getAttribute('method');
        requestType = ( requestType == null ) ? 'get' : requestType;
          //METHOD
        if( requestType.toLowerCase() == "get" ) {
         file += (file.match(/\?/)) ? ("&" + dataStr) : ("?" + dataStr);
         doc.open( "GET", file, true );
         doc.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded; charset=UTF-8" );
         doc.send('');
        }
        else if( requestType.toLowerCase() == "post" ) {
         doc.open( "POST", file, true );
         doc.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded; charset=UTF-8" );
         //JMM - turn this on to see whats about to goto server
         //alert(dataStr);
         doc.send(dataStr);
        }
       return false;
      }

      AJForm.init = function() {
        for( var i = 0; i < document.forms.length; i++ ) {
         submitStr = AJForm.getAttributeText(document.forms[i] , 'onsubmit');
          //if an onsubmit attribute exists
          if( submitStr == null ) {
           continue;
          }
         submitActionList = submitStr.split( ";" );
         pre_callback = null;
         post_callback = null;
          for( s = 0; s < submitActionList.length; s++ ) {
           arg_post = submitActionList[s].match( /(ajform:)?([A-Za-z\.]+)\s*\(.*?\)/ );
            if( RegExp.$1 ) {
             post_callback = RegExp.$2;
            }
            else if( RegExp.$2 ) {
             pre_callback = RegExp.$2;
            }
          }

          //if this is a specified AJFORM handler
          if( post_callback != null ) {
           document.forms[i].ajform = new Object;
           document.forms[i].ajform.preCallback = eval(pre_callback);
           document.forms[i].ajform.postCallback = eval(post_callback);
           document.forms[i].onsubmit = AJForm.activateForm;

           document.forms[i].ajform.submitter = new Object;
           document.forms[i].ajform.submitter.elm = null;
           document.forms[i].ajform.submitter.x = null;
           document.forms[i].ajform.submitter.y = null;

           document.forms[i].ajform_submit = AJForm.submitForm;

           //prepare the submit buttons
           inputList = document.forms[i].getElementsByTagName('input');
            for( y = 0; y < inputList.length; y++ ) {
             thisInput = inputList[y];
             thisInputType = thisInput.getAttribute( 'type' );
              if( thisInputType == null ) {
               continue;
              }
              if( thisInputType == 'submit' || thisInputType == 'image' ) {
               thisInput.setEventListener = STATIC_DOM.setEventListener;
               thisInput.setEventListener( 'click' , AJForm.setSubmitStatus );
              }
            }
          }
        }
      }

      AJForm.getAttributeText = function(elm , attVal) {
       thisAttribute = elm.getAttribute(attVal);
        if( thisAttribute == 'undefined' || thisAttribute == null ) {
         return null;
        }
        if( (typeof thisAttribute).toLowerCase() == 'function' ) {
         attStr = new String(thisAttribute);
         attStr.match( /{\s*([\s\S]+?)\s*}/ );
         attText = RegExp.$1;
        }
        else {
         attText = thisAttribute;
        }
       return attText;
      }

      AJForm.setSubmitStatus = function(e) {
       this.getSourceElement = STATIC_DOM.getSourceElement;
       thisElm = this.getSourceElement();
       thisEvent = STATIC_DOM.getEvent(e);

       thisElm.form.ajform.submitter.elm = thisElm;
       thisElm.form.ajform.submitter.x = thisEvent.elementX;
       thisElm.form.ajform.submitter.y = thisEvent.elementY;
      }

      /*
        Concept and certain code portions courtesy of
        Mathias Schäfer <molily at gmx dot de> 2005-09-18 06:58
      */
      AJForm.URLEncode = function(str) {
        if( typeof encodeURIComponent != 'undefined' &&
          typeof encodeURIComponent !=  undefined ) {
         code = encodeURIComponent(str);
         code = code.replace( /%20/g , "+" );
         return code;
        }
        else {
         return null;
        }
      }

      //SET THE LISTENER TO INITIALIZE THE ACTIONS
      window.setEventListener( 'load' , AJForm.init );


    function getReturnData( data , statusCode , statusMessage) {
    //AJFORM failed. Submit form normally.
    var var_status = document.getElementById('dvStatus');
    if( statusCode != AJForm.STATUS['SUCCESS'] ) {
     var_status.innerHTML="There was an error processing your request. You may send your comments via email to Dalhousie.News@dal.ca ";
     return true;
    }
    //AJFORM succeeded.
    else {
    /* v1.2 Start - Display status on page and hide form  */
      var_status.innerHTML=data;
      var var_frm = document.getElementById('foFeedback');
      var_frm.style.display='none';
     /* v1.2 End */
    }
  }

/* Count characters typed in text area */

function fnCharType() {setTimeout("fnCharCount()",200);}
function fnCharCount() {
  var objFrm=document.getElementById("foFeedback");
  var strComment=objFrm.taComment.value;
  var var_status = document.getElementById('dvStatus');
  intChars=strComment.length;
  intRemaining=intMaxChars-intChars;
  objRemaining=document.getElementById("dvChars");
  if(intRemaining >= 0)
  {
    objRemaining.innerHTML="characters remaining: "+intRemaining;
    var_status.innerHTML="&nbsp;";
  }
  else
  {
    objRemaining.innerHTML="characters remaining: 0";
    var_status.innerHTML="Please restrict your comments to 2000 characters or less. <br />Your current comment size is "+intChars+" characters";
  }

}

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
**/

function echeck(str) {

    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1){
       alert("Invalid E-mail ID")
       return false
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       alert("Invalid E-mail ID")
       return false
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        alert("Invalid E-mail ID")
        return false
    }

     if (str.indexOf(at,(lat+1))!=-1){
        alert("Invalid E-mail ID")
        return false
     }

     if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert("Invalid E-mail ID")
        return false
     }

     if (str.indexOf(dot,(lat+2))==-1){
        alert("Invalid E-mail ID")
        return false
     }

     if (str.indexOf(" ")!=-1){
        alert("Invalid E-mail ID")
        return false
     }

     return true
  }

