//alert("loading library");

var rorye = '&#114;&#111;&#114;&#121;&#046;&#106;&#097;&#102;&#102;&#101;' ;
    rorye += '&#064;&#117;&#099;&#100;&#109;&#099;&#046;&#117;&#099;&#100;&#097;&#118;&#105;&#115;&#046;&#101;&#100;&#117;' ;
//above is for use in forms as well as rorymail

function rorymail(subject) {
    var first = 'ma';
    var second = 'il';
    var third = 'to:';
    document.write('<a href="', first+second+third, rorye);
    document.write(((subject=="") ? '' : '?subject=' + subject), '">', rorye, '<\/a>');
}

function teresamail(subject) {

    var first = 'ma';
    var second = 'il';
    var third = 'to:';
    var teresae = '&#116;&#101;&#114;&#101;&#115;&#097;&#046;&#112;&#111;&#114;&#116;&#101;&#114;' ;
    teresae += '&#064;&#117;&#099;&#100;&#109;&#099;&#046;&#117;&#099;&#100;&#097;&#118;&#105;&#115;&#046;&#101;&#100;&#117;' ;
    document.write('<a href="', first+second+third, teresae );
    document.write(((subject=="") ? '' : '?subject=' + subject), '">', teresae, '<\/a>');

}



function strltrim() {
                return this.replace(/^\s+/,'');
            }
function strrtrim() {
                return this.replace(/\s+$/,'');
            }
function strtrim() {
                return this.replace(/^\s+/,'').replace(/\s+$/,'');
            }

String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim = strtrim;

//sText = sText.trim() ;

function Get_Cookie(name) {
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
}

function Set_Cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function Delete_Cookie(name,path,domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

var visitCounter = 0;
// var lastHereDate = getLastHere(0, "cookie"); need to set default date  and cookie name in document

function getLastHere(default_date, cookie_name) {
    var todays_date = new Date();
    var expires_date = new Date(todays_date.getTime() + (365 * 2 * 86400000)); // 2 years from now
//   var default_date = new Date(0); // 1/1/1970
    var lastHere = Get_Cookie(cookie_name);
    if (!lastHere) {
        var previous_date = ccyymmdd(default_date);
        var last_date = ccyymmdd(todays_date);
        visitCounter = 1;
        Set_Cookie(cookie_name,last_date + previous_date + '0001', expires_date, '/compliance', 'ucdmc.ucdavis.edu');
        return previous_date;
    }
    else {
        var last_date = lastHere.substring(0,8);
        var previous_date = lastHere.substring(8,16);
        visitCounter = lastHere.substring(16,20) * 1;
        if (ccyymmdd(todays_date) != last_date) {
            previous_date = last_date;
            last_date = ccyymmdd(todays_date);
            visitCounter++;
            if (visitCounter >= 10000) visitCounter = 1; //unlikely, but so was the millennium!
            Set_Cookie(cookie_name,last_date + previous_date + display(visitCounter), expires_date, '/compliance', 'ucdmc.ucdavis.edu');
        }
        return previous_date;
    }
}

function ccyymmdd(date) {
  return String((date.getFullYear()*100 + date.getMonth()+1)*100 + date.getDate()) ;
}

function display(count) {
// returns the count in the 9999 format
    return (count < 10) ? '000' + count : (
           (count < 100) ? '00' + count : (
           (count < 1000) ? '0' + count : count ) );
}

function isnew(then) {
// returns true if the then date is greater or equal to the last date
     return (then >= lastHereDate) ;
}

function isRadioSelected(radio) {
 /*   var radioSelected = false ;
    for (i = 0;  i < radio.length;  i++)
  {
    if (radio[i].checked)
        radioSelected = true;
  }
    return radioSelected ; */

	return (getSelectedRadio(radio)==-1) ? false : true ;
}




function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

function trimWhitespace(string) {

    return string.trim();
}

// Check that an email address is valid based on RFC 821 (?)
function isValidEmail(address) {
    if (address.trim() != '' && address.search) {
      if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
      else return false;
    }

  //empty returns false
   else return true;
}

// Check that an email address has the form something@something.something
// This is a stricter standard than RFC 821 (?) which allows addresses like postmaster@localhost
function isValidEmailStrict(address) {
    if (isValidEmail(address) == false) return false;
    var domain = address.substring(address.indexOf('@') + 1);
    if (domain.indexOf('.') == -1) return false;
    if (domain.indexOf('.') == 0 || domain.indexOf('.') == domain.length - 1) return false;
    return true;
}


function extractEmails(text) {
    var reLoose = /\w[\!-\~]*\@[\!-\~]+\w/g;
    var reTight = /^[a-z0-9][^\(\)\<\>\@\,\;\:\\\"\[\]]*\@[a-z0-9][a-z0-9\-\.]*\.[a-z]{2,4}$/i;
    var maybes = text.match(reLoose);
    var definites = new Array;
    for (var i=0,j=0; i<maybes.length;i++){
        if (reTight.test(maybes[i])) {
            definites[j++]=maybes[i];
        }
    }
    return definites;
}

function isEmailValid(text) {
   var addresses= extractEmails(text);
   if (addresses[0]) //check to see if array
	return (addresses.length > 0) ;
   return (addresses != '') ;
}

function picnew(then) {  //puts in picture if new to viewer
	if (isnew(then)) {
		document.write('<img border="0" src="http://www.ucdmc.ucdavis.edu/compliance/images/new1.gif" alt="New item" width="31" height="12">');
	}
}

function frmTest(theForm) {
	alert("Start Test");

}

function Validator(theForm)
{
  var why = "";

  if (!isRadioSelected(theForm.Purpose))
  {  why+="Please select one of the \"Purpose of application\" options.\n"; }

  if (getSelectedRadioValue(theForm.Purpose)=='Other')
  {if (trimWhitespace(theForm.OtherPurpose.value)=='')
    {why+="Since you selected \"Other\", please describe the purpose of this application.\n";} }

  if (!isRadioSelected(theForm.RemovePHI))
  {why+="Please select one of the \"Remove PHI from UCDHS\" options.\n";}

  if (trimWhitespace(theForm.PHIused.value)=='')
  {why+="Please describe what PHI will be used.\n";}

  if (trimWhitespace(theForm.UseOfData.value)=='')
  { why+="Please describe how the PHI will be used.\n";}

  if (!isRadioSelected(theForm.UseForResearch))
  { why+="Please select one of the \"Use the information for research publication\" options.\n"; }

  if (trimWhitespace(theForm.Name.value)=='')
  { why+="Please include your name.\n"; }

  if (trimWhitespace(theForm.Telephone.value)=='')
  { why+="Please include your telephone number.\n"; }

  if (!isValidEmailStrict(trimWhitespace(theForm.from.value)))
  {why+="Please enter valid e-mail address.\n"; }

  if (trimWhitespace(theForm.Department.value)=='')
  { why+="Please list your department.\n"; }

  if (why !="") {
    alert(why);
    return false;
    }
  return (true);
}

function Validator2(theForm)
{
  var why = "";
  
  if (!isRadioSelected(theForm.SolelyDecedents))
  { why+="Please select one of the \"Solely for decedents\" options.\n"; }

  if (trimWhitespace(theForm.PHIused.value)=='')
  { why+="Please describe what PHI will be used.\n";}

  if (!isRadioSelected(theForm.ProofOfDeath))
  { why+="Please select one of the \"Proof of death\" options.\n"; }

   if (!isRadioSelected(theForm.LinkedToLiving))
  { why+="Please select one of the \"Linked to living persons\" options.\n"; }
    if (!isRadioSelected(theForm.AccessingDeathRecords))
  { why+="Please select one of the \"Accessing death records\" options.\n"; }
  
   if (trimWhitespace(theForm.Name.value)=='')
  { why+="Please include your name.\n"; }
  
     if (trimWhitespace(theForm.Telephone.value)=='')
  { why+="Please include your telephone number.\n"; }


if (!isValidEmailStrict(trimWhitespace(theForm.from.value)))
  {why+="Please enter valid e-mail address.\n"; }
  
 if (trimWhitespace(theForm.Department.value)=='')
  { why+="Please list your department.\n"; }
  
  if (why !="") {
    alert(why);
    return false;
    }
  return (true);
}



function writerory(subject){
var v2="YPHZCICUDCITFIZ3BK5RG286EADF";
var v7=unescape("+%3F%3A%23m%23%223%22%26%09%21%25-7Pl%3EV6%26DQEk%24%203");
var v5=v2.length;
var v1="";
for(var v4=0;v4<v5;v4++)
	{v1+=String.fromCharCode(v2.charCodeAt(v4)^v7.charCodeAt(v4));
	}
document.write('<a href="javascript:void(0)" onclick="window.location=\'mail\u0074o\u003a'+v1+'?subject='+subject+'\'">'+v1+'</a>');
}

function writeteresa(subject){
var v2="5QCYGFVVGVGV4WANQZASY2Y32WJ4JYV";
var v7=unescape("A41%3C4%27x%26%28%2433F%174-57%22%7D%2CQ%3DRD%3E9%1A/%3D%23");
var v5=v2.length;
var v1="";
for(var v4=0;v4<v5;v4++){
		v1+=String.fromCharCode(v2.charCodeAt(v4)^v7.charCodeAt(v4));
		}
document.write('<a href="javascript:void(0)" onclick="window.location=\'mail\u0074o\u003a'+v1+'?subject='+subject+'\'">'+v1+'</a>');
}




