//
// DEVELOPER NOTES:
//
//    the comments are there for dev only
//    they should be deleted from any production code
//
//

var imagesOn = new Array();
var imagesOff = new Array();

var iNames = new Array('q1_a1', 'q1_a2', 'q1_a3', 'q1_a4',
                       'q2_a1', 'q2_a2',
                       'q3_a1', 'q3_a2', 'q3_a3', 'q3_a4',
                       'q4_a1', 'q4_a2'
                      );

// Preload
if( document.images ) {
   for( i=0; i < iNames.length; i++) {
      imagesOn[i] = new Image();
      imagesOn[i].src = "evaluate-your-needs-tool/img/eyn_" + iNames[i] + "_on.gif";
      imagesOff[i] = new Image();
      imagesOff[i].src = "evaluate-your-needs-tool/img/eyn_" + iNames[i] + ".gif";
   }
}

function clean(){
	obj = document.eynform;
	obj.eyntype.value = "";
    obj.eynsize.value = "";
    obj.eynloc.value  = "";
    obj.eynskin.value = "";
}

function Verify(){
	obj = document.eynform;
	if(obj.eyntype.value == "" ){
		alert("Please, what type of wound is it?");
		return false;
	}
	
	else if(obj.eynsize.value == ""){
		alert("Please, what size is the wound?");
		return false;
	}
	
	else if(obj.eynloc.value == ""){
		alert("Please, where is the wound located?");
		return false;
	}
	
	else if(obj.eynskin.value == ""){
		alert("Please, what type of skin surrounds the wound?");
		return false;
	}
	
	else{
		eynGetRecommendations(obj);
		obj.submit();
		
	}
}

function imgOn( num ) { if( document.images ) { document.images[iNames[num]].src = imagesOn[num].src; } }
function imgOff( num ) { if( document.images ) { document.images[iNames[num]].src = imagesOff[num].src; } }

function eynAnswer( num, x ) {
   if( num >= 0 && num <= 3 ) {
      imgOff(0); imgOff(1); imgOff(2); imgOff(3);
      imgOn(num);
      document.getElementById('eyntype').value = x;
   } else if( num >= 4 && num <= 5 ) {
      imgOff(4); imgOff(5);
      imgOn(num);
      document.getElementById('eynsize').value = x;
   } else if( num >= 6 && num <= 9 ) {
      imgOff(6); imgOff(7); imgOff(8); imgOff(9);
      imgOn(num);
      document.getElementById('eynloc').value = x;
   } else if( num >= 10 && num <= 11 ) {
      imgOff(10); imgOff(11);
      imgOn(num);
      document.getElementById('eynskin').value = x;
   } else {
     // never get here
   }
}



// check the page query string for our form information
var queryStringArgs = new Array();

function getQueryStringArgs() {
   var queryString = window.location.search.substring(1);
   var queryParams = queryString.split('&');
   for( var i = 0; i < queryParams.length; i++ ) {
      var queryPos = queryParams[i].indexOf('=');
      if( queryPos > 0 ) {
         var queryKey = queryParams[i].substring( 0, queryPos );
         var queryVal = queryParams[i].substring( queryPos+1 );
         queryStringArgs[queryKey] = queryVal;
      }
   }
}

function checkQueryStringArgs() {
   if( queryStringArgs['eyntypea'] == "a" || 
       queryStringArgs['eyntypea'] == "b" || 
       queryStringArgs['eyntypea'] == "c" || 
       queryStringArgs['eyntypea'] == "d" ) {
         eynSetType(queryStringArgs['eyntypea']);
   }
}

function eynSetType(val) {
	var f = document.getElementById( "eynform" );
	// for( var i = 0; i < f.eyntype.length; i++ ) {
	//    if( f.eyntype[ i ].value == val ) {
	//       f.eyntype[ i ].checked = true
	//    }
	// }
	// update the first question state
	if( val == "a" ) { eynAnswer(0, 'a'); }
	if( val == "b" ) { eynAnswer(1, 'b'); }
	if( val == "c" ) { eynAnswer(2, 'c'); }
	if( val == "d" ) { eynAnswer(3, 'd'); }
   
	document.getElementById("").value = val;
}

function eynGetRecommendations( f ) {

   var eynType = "";             // value for the "eyntype" radio group
   var eynSize = "";             // value for the "eynsize" radio group
   var eynLoc = "";              // value for the "eynloc" radio group
   var eynSkin = "";             // value for the "eynskin" radio group

   var coverOneProduct = "0";
   var coverTwoProduct = "0";
   var rollProduct = "0";
   var tapeProduct = "0";
   var kitProduct = "0";

   eynType = f.eyntype.value;
   eynSize = f.eynsize.value;
   eynLoc = f.eynloc.value;
   eynSkin = f.eynskin.value;
   
   
   eynType = eynType;
   eynSize = eynSize;
   eynLoc = eynLoc;
   eynSkin = eynSkin;
   

    //alert( eynType + " : " + eynSize + " : " + eynLoc + " : " + eynSkin );

   if( eynType == "a" ) {           // cuts/scrapes
      coverTwoProduct = 0;          // no coverTwo for cuts/scrapes
      kitProduct = 0;               // no kits for cuts/scrapes
      if( eynSize == "a" ) {        // if < 2in
         coverOneProduct = 1        // 2x2 gauze
         rollProduct = 1;           // 2x2.5 rolled guaze
         if( eynSkin == "a" ) {     // if sensitive
            tapeProduct = 4;        // paper tape
         } else {                   // otherwise
            tapeProduct = 1;        // cloth tape
            if( eynLoc == "a" ) {   // is this the hand/foot
               tapeProduct = 2;     // waterproof tape
            }
         }
      } else {                      // if > 2 in
         coverOneProduct = 2        // 4x4 guaze
         if( eynLoc == "a" ) {      // is hand/foot
            rollProduct = 3;        // kling
         } else {                   // otherwise
            rollProduct = 2;        // 4x2.5 rolled guaze
         }
         if( eynSkin == "a" ) {     // if sensitive
            tapeProduct = 4;        // paper tape
         } else {                   // otherwise
            tapeProduct = 1;        // cloth tape
            if( eynLoc == "a" ) {   // unless this the hand/foot then
               tapeProduct = 2;     // waterproof tape
            }
         }
      }


   } else if( eynType == "b" ) {    // burns
      coverTwoProduct = 0;          // no coverTwo for burns
      kitProduct = 1;               // burn kit
      if( eynSize == "a" ) {        // if < 2in
         coverOneProduct = 3        // cooling patch
         rollProduct = 0;           // no roll
         tapeProduct = 0;           // no tape
      } else {                      // if > 2 in
         coverOneProduct = 4        // triple layer pad
         tapeProduct = 3;           // paper tape
         if( eynLoc == "a" ) {      // if hand/foot
            rollProduct = 3;        // kling
         } else {                   // else
            rollProduct = 2;        // 4x2.5 rolled guaze
         }
      }


   } else if( eynType == "c" ) {    // stitch/post surgery
      if( eynSize == "a" ) {        // if < 2in
         coverOneProduct = 7;       // antibotic pad
      } else {                      // otherwise
         coverOneProduct = 5;       // waterproof pad
      }
      coverTwoProduct = 0;          // no coverTwo for stitch
      rollProduct = 0;              // no roll for stitch
      tapeProduct = 0;              // no tape for stitch
      kitProduct = 2;               // post surgery kit


   } else if( eynType == "d" ) {    // ulcer
      coverOneProduct = 6;          // adaptic pad
      coverTwoProduct = 1;          // toper pad
      tapeProduct = 4;              // paper 2x5
      kitProduct = 0;               // no kit
      if( eynSize == "a" ) {        // if < 2 in
         rollProduct = 3;           // kling
      } else {                      // if > 2 in
         rollProduct = 2;           // gauze 4x2.5
      }

   } else {
      formAction = formAction;      // we should never get here
   }

   // now set the values for the products
   document.getElementById("eync1").value = coverOneProduct;
   document.getElementById("eync2").value = coverTwoProduct;
   document.getElementById("eynr1").value = rollProduct;
   document.getElementById("eynt1").value = tapeProduct;
   document.getElementById("eynk1").value = kitProduct;

   
   // and allow the form to submit
   return true;
   
}


// nod to http://simonwillison.net/2004/May/26/addLoadEvent/
function addLoadEvent(func) {
   var oldonload = window.onload;
   if( typeof window.onload != 'function' ) {
      window.onload = func;
   } else {
      window.onload = function() {
         if( oldonload ) {
            oldonload();
         }
         func();
      }
   }
}

addLoadEvent(function() {
   getQueryStringArgs(); 
   checkQueryStringArgs() // LOOK HERE!!!!! add the submitted value of the EYN tool from the home page to this listener
   clean();
});
