// Side-nav Image Pre-loads
if (document.images) {
var sidebarButtons = new Array();
    sidebarButtons[0] = new Image();
    sidebarButtons[0].src = "../assets/sn_home_on.gif";
    sidebarButtons[1] = new Image();
    sidebarButtons[1].src = "../assets/sn_aboutyou_on.gif";
	sidebarButtons[2] = new Image();
    sidebarButtons[2].src = "../assets/sn_aboutus_on.gif";
	sidebarButtons[3] = new Image();
    sidebarButtons[3].src = "../assets/sn_services_on.gif";
	sidebarButtons[4] = new Image();
    sidebarButtons[4].src = "../assets/sn_syphilis_on.gif";
	sidebarButtons[5] = new Image();
    sidebarButtons[5].src = "../assets/sn_stdbasics_on.gif";
	sidebarButtons[6] = new Image();
    sidebarButtons[6].src = "../assets/sn_drk_on.gif";
	sidebarButtons[7] = new Image();
    sidebarButtons[7].src = "../assets/sn_providers_on.gif";
	sidebarButtons[8] = new Image();
    sidebarButtons[8].src = "../assets/gl_hoursdirect_on.gif";
	}

// Syphilis Testing Pop-ups
var newWin;
function subWin(pageName) {
if (!newWin || newWin.closed) {
var winH = (window.innerHeight)? window.innerHeight : document.body.offsetHeight;
var NH = (winH >= 500)? winH-50 : 400;
newWin = window.open(pageName,'PopUp','width=550,height='+NH+',scrollbars,resizable,toolbar,menubar');
newWin.moveTo(0,0);
newWin.focus();
return true;
} else {
newWin.focus();
return true;
}
}

// About You Chooser
function AboutYouChooser() {
if ((document.AboutYou.youare.options[document.AboutYou.youare.selectedIndex].value == "") || (document.AboutYou.yourpartner.options[document.AboutYou.yourpartner.selectedIndex].value == "") || (document.AboutYou.yourage.options[document.AboutYou.yourage.selectedIndex].value == "")) { 
alert('We need to know a little more about you before we can present customized STD information. \nPlease select your gender, your partner\'s gender and your age. \nDon\'t worry, it\'s anonymous and confidential and no data is reported.');
return false;
}
}

// STD Basics Pop-ups
var newWin;
function STDWin(pageName) {
if (!newWin || newWin.closed) {
newWin = window.open(pageName,'PopUp','width=310,height=280');
newWin.moveTo(0,0);
newWin.focus();
return true;
} else {
newWin.focus();
return true;
}
}

// Sized pop-up
var otherWin;
function sizedWin(pageName,width,height) {
if (!otherWin || otherWin.closed) {
otherWin =
window.open(pageName,'OtherPopUp','width='+width+',height='+height);
otherWin.moveTo(0,0);
return true;
} else {
otherWin.focus();
return true;
}
}

// Valid name field check
function isName(name) {
   var Char;
   var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
   var theStr = new String(name);

   if ( theStr.length == 0 ) {
     return false;
   }
   
   for (i = 0; i < theStr.length; i++) 
   { 
       Char = theStr.charAt(i); 
       if (ValidChars.indexOf(Char) == -1) {
	     return false;
       }
   }
   
   return true;
}

// Valid phone number check
function isPhone(phone) {
   var Char;
   var ValidNums = "0123456789";
   var ValidChars = "0123456789 ,.()-extension";
   var theStr = new String(phone);

   if ( theStr.length == 0 ) {
     return false;
   }
   
   j = 0;
   for (i = 0; i < theStr.length; i++) 
   { 
       Char = theStr.charAt(i); 
       if (ValidChars.indexOf(Char) == -1) {
	     return false;
       }
	   
	   if (ValidNums.indexOf(Char) != -1) {
//	     if (j == 0 && Char != "4") {
//		   return false;
//		 } else if (j == 1 && Char != "1") {
//		   return false;
//		 } else if (j == 2 && Char != "5") {
//		   return false;
//		 }
		 j++;
       }
   }
   
   if (j < 10) {
     return false;
   }
   
   return true;
}

// Valid zip code check
function isZip(phone) {
   var Char;
   var ValidNums = "0123456789";
   var ValidChars = "0123456789 -";
   var theStr = new String(phone);

   if ( theStr.length == 0 ) {
     return false;
   }
   
   j = 0;
   for (i = 0; i < theStr.length; i++) 
   { 
       Char = theStr.charAt(i); 
       if (ValidChars.indexOf(Char) == -1) {
	     return false;
       }
	   
	   if (ValidNums.indexOf(Char) != -1) {
	     if (j == 0 && Char != "9") {
		   return false;
		 } else if (j == 1 && Char != "4") {
		   return false;
		 } 
		 j++;
       }
   }
   
   if (j < 5) {
     return false;
   }
   
   return true;
}

// Prescription Form Validation
function PrescriptionValidator(theForm) {
var winRef;

if (!isName(theForm.firstname.value)) {
    alert("Please enter your first name (letter characters only).");
    return false;
}

if (!isName(theForm.lastname.value)) {
    alert("Please enter your last name (letter characters only).");
    return false;
}

if (theForm.Age.checked == false) {
    alert("Prescriptions are available to women over the age of 18. Please go back to the form and check the box that says you're over 18.");
    return false;
}

if (!isPhone(theForm.phone.value)) {
    alert("Please enter a valid phone number, including area code.");
    return false;
}

if (!isZip(theForm.yourZip.value)) {
    alert("Please enter a valid San Francisco zip code.");
    return false;
}

  return true;
}


function IsDateValid(op){
    var val = new Object();
        
    val.dateorder = "mdy";
    val.cutoffyear = "2029";
    val.century = "2000";

    function GetFullYear(year) {
        return (year + parseInt(val.century)) - ((year < val.cutoffyear) ? 0 : 100);
    }
    
    var num, cleanInput, m, exp;    
    var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\s*$");

    m = op.match(yearFirstExp);
           
    var day, month, year;
    
    if (m != null && (m[2].length == 4 || val.dateorder == "ymd")) {
        day = m[6];
        month = m[5];
        year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10))
    }
    else {       
        if (val.dateorder == "ymd"){
            return false;		
        }						
        var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
        m = op.match(yearLastExp);
        if (m == null) {
            return null;
        }
        if (val.dateorder == "mdy") {
            day = m[3];
            month = m[1];
        }
        else {
            day = m[1];
            month = m[3];
        }
        
        year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10))
    }
    
    month -= 1;
    
    var date = new Date(year, month, day);   
    var returnValue = (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
    
    if(returnValue == null)
    {
        return false;
    }
    
    return true;
}

// Valid e-mail check
function isEmailAddr(email) {
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0) {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}


// Form Validation
function FormValidator(theForm) {

if (theForm.theirEmail0.value == "" && theForm.theirEmail1.value == "" && theForm.theirEmail2.value == "" && theForm.theirEmail3.value == "" && theForm.theirEmail4.value == "" && theForm.theirEmail5.value == "") {
    alert("Please enter the e-mail address of at least one recipient.");
    return false;
}

if ((theForm.theirEmail0.value != "" && !isEmailAddr(theForm.theirEmail0.value)) || (theForm.theirEmail1.value != "" && !isEmailAddr(theForm.theirEmail1.value)) || (theForm.theirEmail2.value != "" && !isEmailAddr(theForm.theirEmail2.value)) || (theForm.theirEmail3.value != "" && !isEmailAddr(theForm.theirEmail3.value)) || (theForm.theirEmail4.value != "" && !isEmailAddr(theForm.theirEmail4.value)) || (theForm.theirEmail5.value != "" && !isEmailAddr(theForm.theirEmail5.value))) {
    alert("Please enter a valid recipient's e-mail address, as in \"theirname@theirdomain.com\".");
    return false;
}
   
if (theForm.yourEmail.value == "" || (theForm.yourEmail.value != "" && !isEmailAddr(theForm.yourEmail.value))) {
    alert("Please enter your valid e-mail address, as in \"yourname@yourdomain.com\"");
    return false;
}
  
  return true;
}
