// JavaScript Document

function setDDBox(getFldName, getFldValue){
//Sets Combo box value to value	
	var fldName = eval(getFldName);
	var fldValue = getFldValue;
   	var i="";  
   	for (var i = 0; i < fldName.length; i++) {
         if (fldName.options[i].value == fldValue) {
		          fldName.options[i].selected=true;     	   
		 }
   	}
}


function getDDValue(fldName){
//Gets Displayed value from DD
	var Indx = fldName.selectedIndex;
	return fldName.options[Indx].value;
}


function getDDBoxText(fldName){
//Gets Displayed text from DD
	var Indx = fldName.selectedIndex;
	return fldName.options[Indx].text;
}



function getRadio(getFldName){
	var fldName = eval(getFldName);
	var selectedValue = "";
	
	//If there's only one radio button
	if(fldName.length == null){
		return fldName.value;
	}
	
	//If there's more, then find the one that's checked.
	for (var i = 0; i < fldName.length; i++) {
		if (fldName[i].checked == true){
			if(selectedValue == ""){
				selectedValue = fldName[i].value;
			} else {
				selectedValue += ","+fldName[i].value;
			}
		}
	}	//end for loop
	
	return selectedValue;
}


function setRadio(getFldName, getFldValue){
	
	var fldName = eval(getFldName);
	var fldValue = getFldValue;
	var i="";

	for (var i = 0; i < fldName.length; i++) {
		if (fldName[i].value == fldValue){
			fldName[i].checked = true;
		}
	}
}


function setCheckBox(getFldName, getFldValue){
//If checkbox value and get value match, then checkbox is set to checked.
	var fldName = eval(getFldName);
	var fldValue = getFldValue;

	if (fldName.value == fldValue){
		fldName.checked = true;
	}
}



/*===== Node functions ======*/

function setNodeVal($getElement, $getValue){
//Sets a node/text area to value
	getNode = document.getElementById($getElement);
	getNode.childNodes[0].nodeValue = $getValue;
}


function getNodeVal($getElement){
//Returns node/text area value
	getNode = document.getElementById($getElement);
	return getNode.childNodes[0].nodeValue;
}

/* end node function */


function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function



//===== AJAX =====
function XMLArray(url, getFunction){
//Connects to Page(URL) and does function (getFunction)
	// code for Mozilla, etc.
	if (window.XMLHttpRequest){
	  xmlhttp=new XMLHttpRequest()
	  xmlhttp.onreadystatechange=eval(getFunction)
	  xmlhttp.open("GET",url,true)
	  xmlhttp.send(null)
	  
	  // code for IE
	}else if (window.ActiveXObject){
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		if (xmlhttp){
			xmlhttp.onreadystatechange = eval(getFunction)
			xmlhttp.open("GET",url,true)
			xmlhttp.send()
		}
	 }	// end if xmlhttprequest
	 
	 
}


//----- FUNCTIONS FOR XML Array -----

function state_GetDefaultTemplate(){
// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4){
	  // if "OK"
	  if (xmlhttp.status==200){
		 var response = xmlhttp.responseXML.documentElement;
		 
		 var Template = response.getElementsByTagName("Html")
		 document.form1.nlContent.value = Template[0].firstChild.nodeValue		 
	  }else{
	  	 alert("Problem retrieving XML data:" + xmlhttp.statusText)
	  }	// end if status == 200
		
	}	// end ready state
	
	return 'true'
}






//===== end AJAX =====

//Hide or show elements
function hideElement(getElem){
//Hides the font select area
 var ElemArea = document.getElementById(getElem);
 ElemArea.style.display = "none"
 
}
 
 
 
function showElement(getElem){
//Displays the font select area
 var ElemArea = document.getElementById(getElem); 
 ElemArea.style.display = "inline"
}

function showElementBlock(getElem){
//Displays the font select area
 var ElemArea = document.getElementById(getElem); 
 ElemArea.style.display = "block"
}



//===== FUNCTIONS FOR CONTEST FORMS ===== //

function checkContestSubmit(){
//Checks to make sure conest form was filled out properly.
	if(document.frmContest.firstname.value == ''){
		alert('Enter your First Name.')
		document.frmContest.firstname.focus()
		return false;
	}

	if(document.frmContest.surname.value == ''){
		alert('Enter your Last Name.')
		document.frmContest.surname.focus()
		return false;
	}

	if(getRadio(document.frmContest.gender) == ''){
		alert('Select Gender.')
		return false;
	}

	if(getRadio(document.frmContest.age) == ''){
		alert('Select Age Group.')
		return false;
	}

	if(getRadio(document.frmContest.email) == ''){
		alert('Enter your email.')
		document.frmContest.email.focus()
		return false;
	}
	
	if(getRadio(document.frmContest.ph_area) == ''){
		alert('Enter your Phone Number.')
		document.frmContest.ph_area.focus()
		return false;
	}

	if(getRadio(document.frmContest.ph_prefix) == ''){
		alert('Enter your Phone Number.')
		document.frmContest.ph_prefix.focus()
		return false;
	}
	
	if(getRadio(document.frmContest.ph_number) == ''){
		alert('Enter your Phone Number.')
		document.frmContest.ph_number.focus()
		return false;
	}
	
	if(getRadio(document.frmContest.confirm) == ''){
		alert('Enter the confirmation code.')
		document.frmContest.confirm.focus();
		return false;
	}



}



// end forms

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function





function openFeedback(nameFile){
//Opens the feed back form and populates it.
	var win = window.open("http://www.nsacanada.ca/Feedback/FeedBack_Input.php?Comp=NSA Canada&Progm=web&Env="+navigator.appName+" "+navigator.appVersion+"&FormName="+nameFile, "feedback", "height=600, width=550, resizable=yes, scrollbars=no");
	win.focus();
}



//----- UPDATE DD BOXES -----//
function clearList(list) {
//Clears dd box for ddProvState
    var i = 0;
    var o = list.options;

    for (i = o.length; i >= 0; --i){
		o[i] = null;
    list.disabled = true;
	}
}


function addElement(list, text_in, value_in){
//Updates dd box for ddProvState

    var o = list.options;
    var nIdx;
	if (o.length < 0){// IE for Mac 4.5 sets length to -1 if list is empty
		nIdx = 0;
	}else{
		nIdx = o.length;

	o[nIdx] = new Option(text_in, value_in);
	list.disabled = false;
	}
}


function clearList(list) {
//Clears dd box for ddProvState
    var i = 0;
    var o = list.options;

    for (i = o.length; i >= 0; --i){
		o[i] = null;
    list.disabled = true;
	}
}