/*
'==============================================================================
' file: functions.js
' author: Lee Shields (lshields@troux.com)
' created: 12/11/2006
' updated: 12/11/2006 LTS
' notes: This file is included on all pages that use the subBuildHeader subroutine in /includes/pagesetup.asp.
'==============================================================================
*/ 

//---------------------------------------------------------------------------
// UTILITY FUNCTIONS

function fncPopup(strName,strURL,intWidth,intHeight,intScroll,intResize) {
  if (strURL.length > 0) {
    var winPopup = window.open(strURL, strName, "width=" + intWidth + ",innerWidth=" + intWidth + ",height=" + intHeight + ",innerHeight=" + intHeight + ",directories=0, statusbar=0, menubar=0, resizable=" + intResize + ", scrollbars=" + intScroll + ", status=0, titlebar=0, toolbar=0");
  }
  return false;
}
    
function fncPopupImage(strURL, intWidth, intHeight) {
  var winImage = window.open(strURL, "winImage", "width=" + intWidth + ",innerWidth=" + intWidth + ",height=" + intHeight + ",innerHeight=" + intHeight + ", directories=0,menubar=0, resizable=1, scrollbars=0, status=0, titlebar=0, toolbar=0");
} 

function fncPopupImageDesc(strImagePath, intWidth, intHeight, strTitle, strDescription) {
  var winImage = window.open("/includes/popup.asp?imagepath=" + strImagePath + '&title=' + strTitle + '&width=' + intWidth + '&height=' + intHeight + '&description=' + strDescription, "winImage", "width=" + parseInt(intWidth+50) + ",innerWidth=" + parseInt(intWidth+80) + ",height=" + parseInt(intHeight+80) + ",innerHeight=" + parseInt(intHeight+75) + ", directories=0,menubar=0, resizable=1, scrollbars=0, status=0, titlebar=0, toolbar=0");
}


//---------------------------------------------------------------------------
// FORM FUNCTIONS

function fncValidateForm(oForm) {
  var strErrors = "";
  var oElements = oForm.elements;
  
  for (var i=0;i<oElements.length;i++) {
    // Validate each element using the fncValidFormField function which will
    //   return any errors found.
    strErrors = strErrors + fncValidFormField(oElements[i]);
  }
  
  if (strErrors.length > 0) {
    // Report any errors and return false
    alert("Please correct these errors before continuing: \n" + strErrors);
    return false;
  }
  else {
    // All elements have been validated, return true
    return true;
  }
}

function fncValidFormField(oElement) {
  // Returns error message if invalid
  // Form Elements can have the following additional attributes (which may lead to invalid XHTML)
  //   required - makes sure there is a value provided or selected
  //   validphone - ensures valid US/Canadian phone number
  //   validemail - ensures valid email address
  // Does not validate checkbox lists or radio buttons
  var strElementError = "";
  var strNiceName = oElement.name;
  var blnAllRequired = false;
  if (oElement.form) {
    if (oElement.form.getAttribute("required")) {
      blnAllRequired = true;
    }
  }
  
  if (oElement.getAttribute("nicename")) {
    strNiceName = oElement.getAttribute("nicename");
  }
  
  if (!oElement.disabled && oElement.getAttribute("type") != "hidden") {
    
    // Validate Required Elements
    if (blnAllRequired || oElement.getAttribute("required")) {
      
      // Check Required Text Elements (Text, Textbox, Textarea)
      if ((oElement.type.indexOf("text") >= 0 || oElement.type.indexOf("password") >= 0) && oElement.value.length <= 0) {
        strElementError = "Please provide a response for " + strNiceName;
      }
      
      // Check Required Drop-Down Boxes
      if (oElement.type.indexOf("select") >= 0) {
        var intNumSelected = 0;
        for (var j=0;j<oElement.length;j++) {
          if (oElement[j].selected && oElement[j].value.length > 0) {
            intNumSelected++;
          }
        }
          
        if (intNumSelected == 0) {
          strElementError = "Please select a response for " + strNiceName;
        }
      }
    }
    
    // Validate Phone Number
    if (oElement.getAttribute("validphone") && oElement.value.length > 0) {
      if (!fncValidUSCanadaPhone(oElement.value)) {
        strElementError = "Please provide a valid " + strNiceName;
      }
    }
    
    // Validate Email Addresses
    if (oElement.getAttribute("validemail") && oElement.value.length > 0) {
      if (!fncValidEmail(oElement.value)) {
        strElementError = "Please provide a valid " + strNiceName;
      }
    }
  }
  
  if (strElementError.length > 0) {
    strElementError = "\n - " + strElementError;
  }
  return strElementError;
}

function fncValidEmail(strEmail) {
	strInvalidChars = " /:,;'";
	if (strEmail == "" ) {
		return false;
	}
	for ( i = 0; i<strInvalidChars.length;  i++) {
		chrBadChar=strInvalidChars.charAt(i);
		if (strEmail.indexOf(chrBadChar,0) > -1) {
			return false;
		}
	}
	intPos=strEmail.indexOf("@",1)
	if (intPos == -1 ) {
		return false;
	}
	if (strEmail.indexOf("@",intPos+1) > -1 ) {
		return false;
	}
	intPeriodPos=strEmail.indexOf(".",intPos)
	if (intPeriodPos == -1 ) {
		return false;
	}
	if (intPeriodPos+3 > strEmail.length ) {
		return false;
	}
	return true;
}

function fncValidUSCanadaPhone(strPhone) {
	//if (strPhone.match(/\s*\(?\s*[2-9]\d{2}\s*\)?\s*([-.]?)\s*\d{3}\s*([-.]?)\s*\d{4}\s*/)) {
	//	return true;
	//}
	strPhone = strPhone.replace(/\s/g,"");
	if (strPhone.length >= 8) {
	  return true;
	}
	return false;
}


//---------------------------------------------------------------------------
// TAB FUNCTIONS

function fncTabOver(oTab) {
  if (oTab.className != "clsTabSelected") {
    fncTabSaveClass(oTab);
    oTab.className = "clsTabHover";
  }
  return true;
}

function fncTabOut(oTab) {
  fncTabRestoreClass(oTab);
  return true;
}

function fncTabSaveClass(oTab) {
  oTab._className = oTab.className;
  return true;
}

function fncTabRestoreClass(oTab) {
  if (oTab.className == "clsTabHover") {
    oTab.className = oTab._className;
  }
  return true;
}
    
    
//---------------------------------------------------------------------------
// COOKIE FUNCTIONS

function fncSetCookie (name, value, exp, path) {
 /*
    INPUT
      name (string) - name of the cookie
      value (string) - value of the cookie
      exp (string) - default is "never"
                     if "never" it sets exp to "Thu, 7 Dec 2113 01:00:00 UTC"
                     if "exp" it sets exp to "Fri, 13 Apr 1970 01:00:00 UTC"
                     if int (ie "5", "20", "60000") it translates to int hours from now
                     if valid GMT date then it uses that as exp
                     else it defaults to 840 hours (5 weeks)
      path (string)
 */
  if(exp == "") {
    exp = "never";
  }
  if (typeof(exp) == 'string') {
    if (exp == 'never') { 
      var strExp = "Thu, 7 Dec 2113 01:00:00 UTC";
    }
    else if (exp == 'exp' || exp == 'now') { 
      var strExp = "Fri, 13 Apr 1970 01:00:00 UTC";
    }
    else if (Date.parse(exp)) {
     var strExp = exp;
    }
  }
  else {
    exp = exp*1;
    if (isNaN(exp)) {
     var strExp = (new Date((new Date()).getTime() + 840*3600000)).toGMTString();
    }
    else {
     var strExp = (new Date((new Date()).getTime() + exp*3600000)).toGMTString();
    }
  }
  if (typeof(strExp) == "undefined") {
    var strExp = (new Date((new Date()).getTime() + 840*3600000)).toGMTString();
  }
  document.cookie = name + '=' + escape(value) + ((strExp)?(';expires=' + strExp):'') + ((path)?';path=' + path:'');
}


function fncReadCookie(name) {
  var firstChar, lastChar;
  var theBigCookie = document.cookie;
  firstChar = theBigCookie.indexOf(name);
  if(firstChar != -1) {
    firstChar += name.length + 1;
    lastChar = theBigCookie.indexOf(';', firstChar);
    if(lastChar == -1) lastChar = theBigCookie.length;
    return unescape(theBigCookie.substring(firstChar, lastChar));
  }
  else {
    return false;
  }
}


function fncKillCookie(name, path) {
 /*
    REQUIRED MODULES
      "fncReadCookie" function
      "fncSetCookie" function
 */
  var value = fncReadCookie(name);
  if(value) {
    fncSetCookie(name, '', 'exp')    
  }
}