// script for hiliting buttons when the mouse is over them

//preload images:
stdbasics = new Image(72,24);
stdbasics.src = "../assets/tn_stdbasics_off.png";
stdbasics_over = new Image(72,24);
stdbasics_over.src = "../assets/tn_stdbasics_over.png";
services = new Image(134,24);
services.src = "../assets/tn_services_off.png";
services_over = new Image(134,24);
services_over.src = "../assets/tn_services_over.png";
sites = new Image(94,24);
sites.src = "../assets/tn_sites_off.png";
sites_over = new Image(94,24);
sites_over.src = "../assets/tn_sites_over.png";
results = new Image(108,24);
results.src = "../assets/tn_results_off.png";
results_over = new Image(108,24);
results_over.src = "../assets/tn_results_over.png";
donate = new Image(54,24);
donate.src = "../assets/tn_donate_off.gif";
donate_over = new Image(54,24);
donate_over.src = "../assets/tn_donate_over.png";
home = new Image(54,24);
home.src = "../assets/tn_home_off.png";
home_over = new Image(54,24);
home_over.src = "../assets/tn_home_over.png";
whattests = new Image(250,51);
whattests.src = "../assets/bn_whattests_off.png";
whattests_over = new Image(250,51);
whattests_over.src = "../assets/bn_whattests_over.png";
getmy = new Image(221,51);
getmy.src = "../assets/bn_getmy_off.png";
getmy_over = new Image(221,51);
getmy_over.src = "../assets/bn_getmy_over.png";
donateb = new Image(113,49);
donateb.src = "../assets/bn_donate_off.jpg";
donateb_over = new Image(113,49);
donateb_over.src = "../assets/bn_donate_over.jpg";
pftr = new Image(293,49);
pftr.src = "../assets/bn_pftr_off.jpg";
pftr_over = new Image(293,49);
pftr_over.src = "../assets/bn_pftr_over.jpg";
qi = new Image(24,22);
qi.src = "../assets/qi_off.png";
qi_over = new Image(24,22);
qi_over.src = "../assets/qi_over.png";
getstarted = new Image(197,52);
getstarted.src = "../assets/bn_getstarted_off.png";
getstarted_over = new Image(197,52);
getstarted_over.src = "../assets/bn_getstarted_over.png";
works = new Image(116,24);
works.src = "../assets/tn_works_off.png";
works_over = new Image(116,24);
works_over.src = "../assets/tn_works_over.png";
testsites = new Image(234,52);
testsites.src = "../assets/bn_testsites_off.png";
testsites_over = new Image(234,52);
testsites_over.src = "../assets/bn_testsites_over.png";
doyourself = new Image(270,52);
doyourself.src = "../assets/bn_doyourself_off.png";
doyourself_over = new Image(270,52);
doyourself_over.src = "../assets/bn_doyourself_over.png";
nothanks = new Image(144,52);
nothanks.src = "../assets/bn_no_thanks_off.png";
nothanks_over = new Image(144,52);
nothanks_over.src = "../assets/bn_no_thanks_over.png";

print_off = new Image(98,49);
print_off.src = "../assets/print_off.gif";
print_over = new Image(98,49);
print_over.src = "../assets/print_over.gif";

function hiLite(imgDocID, imgObjName) {
		 document.images[imgDocID].src = eval(imgObjName + ".src");
}

// 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 isValidPhone(phone1, phone2, phone3) {
   var Char;
   var ValidNums = "0123456789";
   var theStr1 = new String(phone1);
	 var theStr2 = new String(phone2);
	 var theStr3 = new String(phone3);

   if ( theStr1.length != 3 ) {
     return false;
   }
	 
	 if ( theStr2.length != 3 ) {
     return false;
   }
	 
	 if ( theStr3.length != 4 ) {
     return false;
   }
	 
	 for (i = 0; i < 3; i++) 
   { 
       Char = theStr1.charAt(i); 
       if (ValidNums.indexOf(Char) == -1) {
	        return false;
      }
			
       Char = theStr2.charAt(i); 
       if (ValidNums.indexOf(Char) == -1) {
	        return false;
      }
			
			 Char = theStr3.charAt(i); 
       if (ValidNums.indexOf(Char) == -1) {
	        return false;
      }
		}	
		
		Char = theStr3.charAt(3); 
    if (ValidNums.indexOf(Char) == -1) {
	      return false;
		}		

   return true;
}

// Valid zip code check
function isValidZipCode(zip) {
   var Char;
   var ValidNums = "0123456789";
   var theStr = new String(zip);

   if ( theStr.length != 5 ) {
     return false;
   }
   
   j = 0;
  for (i = 0; i < theStr.length; i++) 
   { 
       Char = theStr.charAt(i); 
       if (ValidNums.indexOf(Char) == -1) {
	        return false;
       }
	   
     if (j == 0 && Char != "9") {
		   return false;
		 } else if (j == 1 && Char != "4") {
		   return false;
		 } else if (j == 2 && Char != "1") {
		   return false;
		 }
		 j++;
  }
   
   return true;
}

// Valid date check
function isValidDate(mob, dob, yob) {
   var Char;
   var ValidNums = "0123456789";
   var theStr1 = new String(mob);
	 var theStr2 = new String(dob);
	 var theStr3 = new String(yob);

   if ( theStr3.length != 4 ) {
     return false;
   }
	 
	 if ( theStr1.length == 0 ) {
     return false;
   }
	 
	 if ( theStr2.length == 0 ) {
     return false;
   }
   
  for (i = 0; i < theStr1.length; i++) 
   { 
       Char = theStr1.charAt(i); 
       if (ValidNums.indexOf(Char) == -1) {
	        return false;
       }
  }
	
	for (i = 0; i < theStr2.length; i++) 
   { 
       Char = theStr2.charAt(i); 
       if (ValidNums.indexOf(Char) == -1) {
	        return false;
       }
  }
	
	for (i = 0; i < theStr3.length; i++) 
   { 
       Char = theStr3.charAt(i); 
       if (ValidNums.indexOf(Char) == -1) {
	        return false;
       }
  }
   
   return true;
}

function isValidID(ID){
var Char;
var ValidNums = "0123456789";
var theStr = new String(ID);

if ( theStr.length != 9 ) {
     return false;
}
	 
for (i = 0; i < 9; i++) 
{ 
    Char = theStr.charAt(i); 
    if (ValidNums.indexOf(Char) == -1) {
	     return false;
    }
}		
			
   return true;
}

// ID Validation
function IDValidator(theForm) {

if (!isValidID(theForm.resultid.value)) {
    alert("Please enter a valid, 9-digit ID.");
    return false;
}

return true;
}


// Tests Selection Validation
function TestsValidator(theForm, ACG, GCG, OCG, HIV, Syphilis, Herpes) {
var return_value = false;

if (ACG == "checked") {
   if (theForm.ACG.checked == true) {
	    return_value = true;
	 }
}

if (GCG == "checked") {
   if (theForm.GCG.checked == true) {
	    return_value = true;
	 }
}

if (OCG == "checked") {
   if (theForm.OCG.checked == true) {
	    return_value = true;
	 }
}

if (HIV == "checked") {
   if (theForm.HIV.checked == true) {
	    return_value = true;
	 }
}

if (Syphilis == "checked") {
   if (theForm.Syphilis.checked == true) {
	    return_value = true;
	 }
}

if (Herpes == "checked") {
   if (theForm.Herpes.checked == true) {
	    return_value = true;
	 }
}


if (return_value == false) {
   alert("Please select at least one test.");
	 return false;
} else {
   return true;
}

}

// Quiz Validation
function QuizValidator(theForm) {

if (theForm.Male.checked == false && theForm.Female.checked == false && theForm.Transman.checked == false && theForm.Transwoman.checked == false) {
    alert("Please select your partners.");
    return false;
 }

if (theForm.Gender[0].checked == false && theForm.Gender[1].checked == false && theForm.Gender[2].checked == false && theForm.Gender[3].checked == false) {
    alert("Please select your gender.");
    return false;
 }
 
if (theForm.Age[0].checked == false && theForm.Age[1].checked == false && theForm.Age[2].checked == false) {
    alert("Please select your age.");
    return false;
 }
 
if (theForm.Race[0].checked == false && theForm.Race[1].checked == false && theForm.Race[2].checked == false && theForm.Race[3].checked == false && theForm.Race[4].checked == false && theForm.Race[5].checked == false) {
    alert("Please select your ethnicity/race.");
    return false;
 } 
 
// if (theForm.Symptoms[0].checked == false && theForm.Symptoms[1].checked == false) {
//    alert("Please indicate whether you are experiencing symptoms.");
//    return false;
// } 
 
// if (theForm.Exposed[0].checked == false && theForm.Exposed[1].checked == false) {
//    alert("Please indicate whether someone told you that you might have been exposed to an STD.");
//    return false;
// } 
 
// if (theForm.Oral.checked == false && theForm.Anal.checked == false && theForm.PV.checked == false) {
//    alert("Please select which kinds of sex you have.");
//    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.firstname.value == "") {
    alert("Please enter your first name.");
    return false;
}

if (theForm.lastname.value == "") {
    alert("Please enter your last name.");
    return false;
}

if (theForm.street.value == "") {
    alert("Please enter your address.");
    return false;
}

if (!isValidZipCode(theForm.zipcode.value)) {
    alert("Please enter a valid, 5-digit San Francisco zip code.");
    return false;
}

if (!isValidPhone(theForm.phone1.value, theForm.phone2.value, theForm.phone3.value)) {
    alert("Please enter a valid, 10-digit San Francisco phone number.");
    return false;
}

if (!isValidDate(theForm.mob.value, theForm.dob.value, theForm.yob.value)) {
    alert("Please enter your birthdate. For example, \"6/13/1971\"");
    return false;
}

if (theForm.gender.value == "") {
    alert("Please select your gender.");
    return false;
}

if (theForm.race.value == "") {
    alert("Please select your race.");
    return false;
}

if (theForm.lasttest.value == "") {
    alert("Please select where you had your last test.");
    return false;
}

if (theForm.recenttest[0].checked == false && theForm.recenttest[1].checked == false) {
    alert("Please indicate whether you have taken these tests in the last 6 months.");
    return false;
 }

  return true;
}
