// JavaScript Document
<!--
function display_state()
{
	var i = document.getElementById('SiteCountry').selectedIndex; 
	var sel_text = document.getElementById('SiteCountry')[i].text;
	if(sel_text == 'United States')
	{
		document.getElementById('state_input').style.display = 'none';
		document.getElementById('state_list').style.display = '';
	}
	else
	{
		document.getElementById('state_input').style.display = '';
		document.getElementById('state_list').style.display = 'none';
		document.getElementById('SiteState1').value = '';
	}
}
function update_contact(org_id)
{
	if(document.getElementById('del_contact').checked == true)
	{
		contact_value = document.getElementById('del_contact').value;
	}
	else
	{
		contact_value = 0;
	}
	//call AJAX
	var url = 'update_contact_chemical.php';
	var pars = 'org_id=' + org_id;
	pars += '&' + 'contact_value' + '=' + contact_value;
	pars += '&' + 'action' + '=' + 'contact';
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars, 
			onComplete: contact_chemical_alert
		});
}
function update_chemical(org_id)
{
	if(document.getElementById('del_chemical').checked == true)
	{
		chemical_value = document.getElementById('del_chemical').value;
	}
	else
	{
		chemical_value = 0;
	}
	//call AJAX
	var url = 'update_contact_chemical.php';
	var pars = 'org_id=' + org_id;
	pars += '&' + 'chemical_value' + '=' + chemical_value;
	pars += '&' + 'action' + '=' + 'chemical';
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars, 
			onComplete: contact_chemical_alert
		});
}
function contact_chemical_alert(response)
{
	//alert(response.responseText);
	var res = response.responseText.split("||");
	if(res[0] == '1')
	{
		if(res[1]=='0')
		{
			alert("You have selected to NOT ALLOW Author users to delete contacts.",3);
		}
		else if(res[1]=='1')
		{
			alert('You have selected to ALLOW Author users to delete contacts.');
		}
	}
	else if(res[0] == '2')
	{
		if(res[1]=='0')
		{
			alert('You have selected to NOT ALLOW Author users to edit or delete chemical information.');
		}
		else if(res[1]=='1')
		{
			alert('You have selected to ALLOW Author users to edit or delete chemical information.');
		}
	}
}
function update_logo(hasp_id)
{
	
	if(document.getElementById('display_logo').checked == true)
	{
		logo_value = document.getElementById('display_logo').value;
	}
	else
	{
		logo_value = 0;
	}
	//call AJAX
	var url = 'update_logo_hsp.php';
	var pars = 'hasp_id=' + hasp_id;
	pars += '&' + 'logo_value' + '=' + logo_value;
	pars += '&' + 'action' + '=' + 'logo';
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars, 
			onComplete: custom_alert
		});
}
function update_hsp(hasp_id)
{
	if(document.getElementById('display_hsp').checked == true)
	{
		hsp_value = document.getElementById('display_hsp').value;
	}
	else
	{
		hsp_value = 0;
	}
	var url = 'update_logo_hsp.php';
	var pars = 'hasp_id=' + hasp_id;
	pars += '&' + 'hsp_value' + '=' + hsp_value;
	pars += '&' + 'action' + '=' + 'hsp';
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars, 
			onComplete: custom_alert
		});
}
function custom_alert(response)
{
	//alert(response.responseText);
	var res = response.responseText.split("||");
	if(res[0] == '1')
	{
		if(res[1]=='0')
		{
			alert('You have selected NOT TO INCLUDE your company logo in the HASP PDF.');
		}
		else if(res[1]=='1')
		{
			alert('You have selected TO INCLUDE your company logo in the HASP PDF.');
		}
	}
	else if(res[0] == '2')
	{
		if(res[1]=='0')
		{
			alert('You have selected NOT TO INCLUDE Hazardous Substance Profiles in the HASP PDF document.');
		}
		else if(res[1]=='1')
		{
			alert('You have selected TO INCLUDE Hazardous Substance Profiles in the HASP PDF document.');
		}
	}
}
function text_box_val($array)
{

	for (i=0;i<$array.length;i++)
	{
		if (document.getElementById($array[i]).value == "")
		{
			alert("Please answer all the required fields.");
			document.getElementById($array[i]).focus();
			return false;
			break;
		}
	}
	return true;
}



function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function ss()
{
  for (i=0; i<document.forms[0].elements[0].length; i++) 
  { 
    document.forms[0].elements[0].options[i].selected = true; 
  }
}

function move(fbox, tbox)
{
	var arrFbox = new Array();
	var arrTbox = new Array();
	var arrLookup = new Array();
	var i;

	for (i = 0; i < tbox.options.length; i++)
	{
		arrLookup[tbox.options[i].text] = tbox.options[i].value;
		arrTbox[i] = tbox.options[i].text;
	}

	var fLength = 0;
	var tLength = arrTbox.length;
	
	for(i = 0; i < fbox.options.length; i++)
	{
		arrLookup[fbox.options[i].text] = fbox.options[i].value;
		if (fbox.options[i].selected && fbox.options[i].value != "")
		{
			arrTbox[tLength] = fbox.options[i].text;
			tLength++;
		}
		else
		{
			arrFbox[fLength] = fbox.options[i].text;
			fLength++;
		}
	}

	arrFbox.sort();
	arrTbox.sort();
	fbox.length = 0;
	tbox.length = 0;
	var c;
	
	for(c = 0; c < arrFbox.length; c++)
	{
		var no = new Option();
		no.value = arrLookup[arrFbox[c]];
		no.text = arrFbox[c];
		fbox[c] = no;
	}
	
	for(c = 0; c < arrTbox.length; c++)
	{
		var no = new Option();
		no.value = arrLookup[arrTbox[c]];
		no.text = arrTbox[c];
		tbox[c] = no;
   }
}



function getNewQuestion($parent_id, $disp_when, $question_num, $pageID) 
{
	$div = "q" + $parent_id + "_subs"
	var url = "get_questions_2.php?p=" + $parent_id + "&d=" + $disp_when + "&q=" + $question_num + "&page_id=" + $pageID;
	// Show the loading graphic
	document.getElementById($div).innerHTML = "<img width='10' height='10' alt='Loading' src='/images/tiny_red.gif' /> <font size='-5'>Loading...</font>";
	request.open("GET", url, true);
	request.onreadystatechange = function()
	{
		updatePage("q" + $parent_id + "_subs");
	}
	request.send(null);
}



function updatePage($div)
{
	if (request.readyState == 4)
	{
		if (request.status == 200)
		{
			//var response = request.responseText.split("|");
			//document.getElementById("order").value = response[0];
			//document.getElementById("address").innerHTML = response[1].replace(/\n/g, "");
			if ( request.responseText != 0 )
			{
				document.getElementById($div).innerHTML = request.responseText;
				
			}
			else
			{
				document.getElementById($div).innerHTML = "<img src='/images/blank.gif' height=16 width=1 align='right' />";
				//document.getElementById($div).innerHTML.replace(/\n/g, "");
			}
		}
		else
		{
			alert("status is " + request.status);
		}
	}
}

function radio_other($id)
{
	if ( document.getElementById("radio-other" + $id).checked )
	{
		document.getElementById("radio-option-other" + $id).innerHTML = "<img src='arrow.gif'><input id='radio-option-other-textbox" + $id + "' name='radio-option-other-textbox" + $id + "' type='text' size='50' maxlength='50' class='checkboxOtherBox'>";
		document.getElementById("radio-option-other-textbox" + $id).focus();
	}
	else
	{
		document.getElementById("radio-option-other" + $id).innerHTML = "";
	}
}


function dropdown_other($question_id, $input_id)
{
	
	var $htmlID = $question_id + "_" + $input_id + "_" + "dropdown-other";
	var $theDropdown = document.getElementById($htmlID);
	//var selectedItem = $theDropdown.selectedIndex;
	//var selectedText = $theDropdown.options[selectedItem].text;
	
	//alert(selectedText);
	
	if ( $theDropdown.value == "Other" )
	{
		document.getElementById($htmlID + "OtherContainer").innerHTML = "<img src='arrow.gif'><input id='" + $htmlID + "TextBox' name='" + $htmlID + "TextBox' type='text' size='50' maxlength='50' class='checkboxOtherBox'><input name='add_other' type='button' value='Add' onClick='addDropdownOption(" + $question_id + ", " + $input_id + ")'>";
		document.getElementById($htmlID + "textbox").focus();
	}
	else
	{
		document.getElementById($htmlID + "OtherContainer").innerHTML = "";
	}
}

function addDropdownOption($question_id, $input_id)
{
	$htmlID = $question_id + "_" + $input_id + "_" + "dropdown-other";

	// Add the user entered option to the dropdown list
	$opBox = document.getElementById($htmlID)
	$len = $opBox.options.length;
	
	$opBox.options[$len] = new Option( document.getElementById($htmlID + "TextBox").value, "||" + document.getElementById($htmlID + "TextBox").value, false, true);

	document.getElementById($htmlID + "OtherContainer").innerHTML = "";

}




// Returns an array of elements with the specified class name
function getElementsByClass(searchClass,node,tag)
{
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function sortSelect(obj) 
{
	var o = new Array();
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected);
		}
	if (o.length==0) { return; }
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
}

function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}




function screenkey(e,id, $qid) {
	var whichCode;
	whichCode = (window.Event) ? e.which : e.keyCode;
	if ( whichCode == "13" ) //see if they pressed the enter key
	{ 
		addCheckbox(id, $qid)
	}
	whichCode = null;
}


function insRow(tableID, asdf)
{
	// Get the current number of rows and columns
	var tbl=document.getElementById(tableID);
	var numRows = tbl.getElementsByTagName("TR").length;
	var numCols = tbl.getElementsByTagName("TR")[0].getElementsByTagName("TH").length-1;

	// Get the input IDs
	var inputs = get_inputs(tableID.replace(/[a-zA-Z]+/,"")).split("||");
	
	// Insert the row
	var row = tbl.insertRow(numRows);


	
	
	for (i=0; i<(numCols); i++)
	{
		// Insert the cell
		var cell = row.insertCell(i);
		
		// Update the new cells contents
		var formEle = document.getElementById(inputs[i]);
		if(formEle.tagName == "SELECT")
		{
			try
			{
				cell.innerHTML = formEle.options[formEle.selectedIndex].text + "<input name=\"" + inputs[i] + "_" + i + "\" type=\"hidden\" value=\"" + formEle.value + "\">";	
			}
			catch (err)
			{
				cell.innerHTML = "NULL";
			}
			
		}
		else
		{
			cell.innerHTML = formEle.value + "<input name=\"" + inputs[i] + "_" + i + "\" type=\"hidden\" value=\"" + formEle.value + "\">";
		}
	}
	
	// Insert the delete cell
	var cell = row.insertCell(i);
	cell.align = 'center';
	cell.innerHTML = "<input type='button' value='Del' onClick=\"delRow('" + tableID + "', this)\" />";
}

function get_inputs(qid)
{
	var obj = document.getElementById("myform");
	var inputs="";

	// Loop through the form elements
	for (i=0; i<obj.elements.length; i++)
	{
		
		var obID = obj.elements[i].id.split("_");
		
		if (obID[0] == qid)
		{
			inputs += obj.elements[i].id + "||";
		}
	}

	if(inputs.length > 0)
	{
		inputs = inputs.substring(0,inputs.length-2)
		return inputs;
	}
	else
	{
		return false;	
	}
}


function delRow(tableID, rowNum)
{

	var delRow = rowNum.parentNode.parentNode;
	var tbl = delRow.parentNode.parentNode;
	var rIndex = delRow.sectionRowIndex;
	tbl.deleteRow(rIndex);
}


function checkbox_other2($id, $qid)
{
	if ( document.getElementById("other" + $id).checked )
	{
		document.getElementById("checkbox-option-other" + $id).innerHTML = "<br /><img src='arrow.gif'><input id='checkbox-option-other-textbox" + $id + "' name='checkbox-option-other-textbox" + $id + "' type='text' size='50' maxlength='50' class='checkboxOtherBox' onkeyup='screenkey(event, " + $id + ", " + $qid + ")'><input name='add_other' type='button' value='Add' onClick='addCheckbox(" + $id + ", " + $qid + ")'>";
		document.getElementById("checkbox-option-other-textbox" + $id).focus();
	}
	else
	{
		document.getElementById("checkbox-option-other" + $id).innerHTML = "&nbsp;";
	}
}


function checkbox_other($id, $qid)
{
	var theOtherCB = document.getElementById("other" + $id);
		
	if ( theOtherCB.checked == true )
	{
		var theOtherContainer = document.getElementById("checkbox-option-other" + $id);

		// Create a break
		var newBr = document.createElement('br');
		//newBr.setAttribute('id','customCheckboxMOBr_' + $id + '_' + inNum);

		// Create an image
		var newImg = document.createElement('img');
		newImg.setAttribute('src', 'arrow.gif');

		// Create the checkbox and set its attributes
		var newBox = document.createElement("input");
		newBox.setAttribute('type', 'textbox');
		newBox.setAttribute('class','checkboxOtherBox');
		newBox.setAttribute('className','checkboxOtherBox');
		newBox.setAttribute('name', 'checkbox-option-other-textbox' + $id);
		newBox.setAttribute('id','checkbox-option-other-textbox' + $id);
		newBox.setAttribute('size',50);
		newBox.setAttribute('maxlength',150);
		newBox.onkeydown = function(e) { if (!e) var e = window.event; checkboxOnEnter(e, 'add_other_' + $qid); }
		
		// Create the Add button and set its attributes
		var newBox2 = document.createElement('input');
		newBox2.setAttribute('type', 'button');
		newBox2.setAttribute('name', 'add_other');
		newBox2.setAttribute('value','Add');
		newBox2.setAttribute('id','add_other_' + $qid);
		//newBox2.setAttribute('onClick', 'addCheckbox(' + $id + ', ' + $qid + ')');
		//newBox2.onClick = "addCheckbox(" + $id + ", " + $qid + ")";
		newBox2.onclick = function() { var temp = new Function("addCheckbox(" + $id + ", " + $qid + ")"); temp(); };

		theOtherContainer.appendChild(newBr);
		theOtherContainer.appendChild(newImg);
		theOtherContainer.appendChild(newBox);
		theOtherContainer.appendChild(newBox2);
		newBox.focus();

	}
	else
	{
		document.getElementById("checkbox-option-other" + $id).innerHTML = "&nbsp;";
	}
}


function addCheckbox($id, $qid) 
{
		// Get the container list
		var con = document.getElementById("checkbox_multi_other_container" + $id);
		
		// Get the input value
		var input_val = document.getElementById("checkbox-option-other-textbox" + $id).value;
		
		// Get a random string
		var inNum = Math.random()*5;
		
		// Create the checkbox and set its attributes
		var newBox = document.createElement('input');
		newBox.setAttribute('type', 'checkbox');
		newBox.setAttribute('class','checkboxMOOther');
		newBox.setAttribute('className','checkboxMOOther');
		newBox.setAttribute('name', $qid + '_' + $id + '_checkboxMultiOther[custom][]',0);
		newBox.setAttribute('id','customCheckboxMultiOther_' + $id + '_' + inNum);
		newBox.setAttribute('value', input_val);
		newBox.defaultChecked = true;
		
		// Create the label and set its attributes
		var newLabel = document.createElement('label');
		newLabel.innerHTML = "<strong>" + input_val + "</strong> [ <a href='javascript: removeCheckbox(" + inNum +"," + $id + ")'>Remove</a> ]";
		newLabel.setAttribute('class','checkboxLabel');
		newLabel.setAttribute('className','checkboxLabel');
		newLabel.setAttribute('id','customCheckboxMOLabel_' + $id + '_' + inNum);
		
		// Create a break
		var newBr = document.createElement('br');
		newBr.setAttribute('id','customCheckboxMOBr_' + $id + '_' + inNum);
		
		con.appendChild(newBr);
		con.appendChild(newBox);
		con.appendChild(newLabel);
		
		
		document.getElementById("other" + $id).checked = false;
		document.getElementById("checkbox-option-other" + $id).innerHTML = "";
		
}

function removeCheckbox(inNum, $id)
{
	var con = document.getElementById("checkbox_multi_other_container" + $id); 
	var box = document.getElementById("customCheckboxMultiOther_" + $id + "_" + inNum);
	var label = document.getElementById("customCheckboxMOLabel_" + $id + "_" + inNum);
	var br = document.getElementById("customCheckboxMOBr_" + $id + "_" + inNum);
	con.removeChild(box);
	con.removeChild(label);
	con.removeChild(br);	
}


// Used to delete a row in the chemical tool table
function delChemRow(chemTable, rowNum, chemID, inid, qid)
{
	//Remove the row from the table
	delRow(chemTable, rowNum);
	
	// Remove the ID from the hidden field
	var c = document.getElementById(qid + "_" + inid + "_" + "chemical_tool");
	var chemStr = c.value;
	
	chemStr = chemStr.replace(chemID,"");
	chemStr = chemStr.replace(/\|\|\|\|/,"||");
	chemStr = chemStr.replace(/^\|\|/,"");
	chemStr = chemStr.replace(/\|\|$/,"");
	
	c.value = chemStr
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate)
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}


//-->
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function confirmlogout() {
	ask = confirm("Are you sure you wish to logout? Click \'OK\' to logout.");
	if (ask) {
		return true
	}
	else {
		return false;	
	}
}

function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}

function ValPhone(Phone) {
	newphone = "";
	vars = '0123456789';
	x = 0;
	//trim  start and end of entry J.I.C.
	Phone = ltrim(Phone);

	//we need to check to see if they put a 1 in the first character i.e. 1-800....
		if (Phone.indexOf("1") == 0) {
			Phone = Phone.substring(1,Phone.length);
		}
	//strip all unallowed characters
	while (x <= (Phone.length)-1) {
		val = Phone.substring(x, x+1);
		if (vars.indexOf(val) >=0) {
			newphone +=  val;
		}
		x++;
	}
	if (newphone.length < 10) {
		return false;
	}
return true;
}
//-->