/**
 * msds.js
 * Jan 4, 2008 11:20:55 AM
 * @author Andrew Abramov
 */

function enableRefreshBtn()
{
	$('submitRefresh').enable();
}

function disableEnableBtn(btnID, isEnable)
{
	if(isEnable)
	{
		if( $(btnID) != null ) $(btnID).enable();
	}
	else
	{
		if( $(btnID) != null ) $(btnID).disable();
	}
}

function renderHIData(hi_id) 
{
	isProcced = ($('isDataChanged').value == 1) ? confirm('You have made changes that will be lost if you click OK.') : true;
	
	message = '<br> Fetching data... Please wait.';
	
	$('hiGeneral').replace('<tbody id="hiGeneral">' + message + '</tbody>');
	$('hiEnvnt').replace('<tbody id="hiEnvnt">' + message + '</tbody>');
	$('hiSftnt').replace('<tbody id="hiSftnt">' + message + '</tbody>');

    if (isProcced) 
    {
    	$('isDataChanged').value = 0;
		$('isGeneralChanged').value = 0;
		$('isNotesChanged').value = 0;
		$('messageDataModified').hide();
    	
		$('hi_tabstd').setStyle({ height: '20' });
		$('hi_tabs').show();
		$('hi_data').show();
	
		var ajaxUrl = $('contextPath').value + '/acEditHIsAjax.do';
		var myParams = Form.serialize('acEditHIs', getHash=true);
	
		myParams.hi_id = hi_id;
	
		myParams.hiTab = 'hiGeneral';
		new Ajax.Updater('hiGeneral', ajaxUrl, {parameters: myParams});
	
		myParams.hiTab = 'hiEnvnt';
		new Ajax.Updater('hiEnvnt', ajaxUrl, {parameters: myParams});
	
		myParams.hiTab = 'hiSftnt';
		new Ajax.Updater('hiSftnt', ajaxUrl, {parameters: myParams});
    }
}

function doOnHIGeneralChanged()
{
	$('messageDataModified').show();
	$('isDataChanged').value = 1;
	$('isGeneralChanged').value = 1;
}

function doOnHINotesChanged()
{
	$('messageDataModified').show();
	$('isDataChanged').value = 1;
	$('isNotesChanged').value = 1;
}

function doOnDDChanged(submitForm, flag_name)
{
	if($(flag_name) != null)
	{
		$(flag_name).value = 1;
	}

	submitForm.form.submit();
}

function hideTable(tableId)
{
	if($(tableId) != null)
	{
		$(tableId).hide();
	}
}

function hideById(id)
{
	if($(id) != null)
	{
		$(id).hide();
	}
}

function doUpdateFieldsAndLabels(selectObj, elementToUpdateName)
{
	var elementToUpdate = $(elementToUpdateName);
	var index = selectObj.selectedIndex;
	
	if (index >= 0) 
	{
		var type = selectObj.id;
		var id = selectObj.options[index].value;
		var textForUpdate = selectObj.options[index].label;
		var textName = selectObj.options[index].lName;
		var textLastInventory = selectObj.options[index].lLastInventory;
	
		if(elementToUpdate != null && textForUpdate != null)
		{
			while(elementToUpdate.lastChild != null)
			{
				elementToUpdate.removeChild(elementToUpdate.lastChild);
			}
		
			if (type == "searchOrganization")
			{
				textForUpdate = "";
			}
			else if(type == "searchArea")
			{
				textForUpdate = "Area: " + textForUpdate;
				$('areaName').value = textName;
				$('areaLastInventory').value = textLastInventory;
			}
			else if(type == "searchSite")
			{
				textForUpdate = "Site: " + textForUpdate;
				$('siteName').value = textName;
				$('siteLastInventory').value = textLastInventory;
			}
			else if(type == "searchBuilding")
			{
				textForUpdate = "Building: " + textForUpdate;
				$('buildingName').value = textName;
				$('buildingLastInventory').value = textLastInventory;
			} 
			else if(type == "searchRoom")
			{
				textForUpdate = "Room: " + textForUpdate;
				$('roomName').value = textName;
				$('roomLastInventory').value = textLastInventory;
			}
			
			elementToUpdate.appendChild(document.createTextNode(textForUpdate));
		}
	}
}

function doOnAjaxDDChanged(selectObj, cxPath, isHomePage)
{
	var ajaxUrl = $(cxPath).value+'/aJaxBaseAction.do';
	
	var index = selectObj.selectedIndex;
	if (index >= 0) 
	{
		var id = selectObj.options[index].value;
		var type = selectObj.id;
		$('changedDDId').value = selectObj.id;
		
		cleanDD(type);
		
		if (type == "searchOrganization" || type == "searchArea" || type == "searchSite" || type == "searchBuilding") 
		{
			displayWaitMessage(type, "updating ... please wait.");
			enableDisableBtn(0);
			new Ajax.Request(ajaxUrl, 
				{
					method: 'post',
					parameters: 
						{
							type: type,
							id: id,
							isHomePage: isHomePage
						},				
					onSuccess: fillDDFromAjaxResponse,
					onFailure: displayErrorMessage
				});
		}
		
		doOnLoadViewLocation();
	}
}

function enableDisableBtn(value){
	if(value==1){
		if( $('viewHomeAdminActionForm') != null ) $('viewHomeAdminActionForm').enable();
		if( $('viewHomeExternalActionForm') != null ) $('viewHomeExternalActionForm').enable();
		if( $('location') != null ) $('location').enable();
	}else{
		if( $('viewHomeAdminActionForm') != null ) $('viewHomeAdminActionForm').disable();
		if( $('viewHomeExternalActionForm') != null ) $('viewHomeExternalActionForm').disable();
		if( $('location') != null ) $('location').disable();
	}
}

function doOnMSAjaxCancel(selectObj){
	// create it later	
	//	doOnMSAjaxDDChanged(selectObj);
}


function doOnMSAjaxDDChanged(selectObj)
{
	var ajaxUrl = $('contextPath').value+'/ajaxOrganizationAction.do';
	
	$('messageSaving').hide();
	$('messageErr').hide();
	$('messageOk').hide();
	$('messageDataModified').show();
	$('isDataChanged').value = 1;
	
	var index = selectObj.selectedIndex;
	if (index >= 0) 
	{
		var id = selectObj.options[index].value;
		var type = selectObj.id;
		$('changedDDId').value = selectObj.id;

		if(type == "mfg_id") 
		{
			$('ftMfg').show();
		}
		else if(type == "spl_id")
		{
			$('ftSpl').show();
		}
		
		new Ajax.Request(ajaxUrl, {
				method: 'post',
				parameters: {
					type: type,
					id: id
				},
				
				onSuccess: fillMSFromAjaxResponse,
				onFailure: function(){
						$('ftMfg').hide();
						$('ftSpl').hide();
				}
			});
		}
}

// Oraganization functions ///////////////////////////////////////////////////////////////

function validateViewOrganizationForm(){
	if ( $('byPassBodyUnloadCheck').value == 1 ){
		$('byPassBodyUnloadCheck').value = 0;
		return true;
	}else{
		return validateViewOrgForm();
	}
}

function setByPassBodyUnloadCheck(){
	$('byPassBodyUnloadCheck').value = 1;
}

function setIsDeletePressed(){
	$('isDeletePressed').value = 1;
}

function validateViewOrgForm(){

	if ($('isDeletePressed') != null && $('isDeletePressed').value == 1) {
		$('isDeletePressed').value = 0;
//		$('isDataChanged').value = 0;
		return confirm("You are about to delete " + $('name').value);
	}else if ($('isDataChanged').value == 1) {
		$('isDataChanged').value = 0;
		return true;
	} else {
			alert("There is nothing to save.");
			return false;
	}
}

function doOnOrgDataChanged()
{
	if( $('organizationIdNew') != null && $('organizationIdNew').value < 1 ){
		alert('Please select an Organization first');
		return;
	}
	
	$('messageDataModified').show();
	$('isDataChanged').value = 1;
	
	if( $('customer_flag').checked ) $('submitEditLocation').enable();
	else $('submitEditLocation').disable() ;
	
	reString = /\w+/

	if (reString.test($('email').value)) {
		$('hrefEmail').href = "mailto:" + $('email').value;
		$('hrefEmail').update('Email');
	}else{
		$('hrefEmail').href = "#";
		$('hrefEmail').update('email');
	}
	
	$('websites_cell').childElements().invoke('remove');
	$('websites_cell').cleanWhitespace(); 

	if (reString.test($('website').value)) {
		getWebsites($('website').value).each(function(name, index){ 
			$('websites_cell').insert("<a id='website"+index+"' target='_blank' href='http://"+name+"'>"+(index+1)+"</a> ");
	 	});
	}

}

function doOnDataChanged()
{
	$('messageDataModified').show();
	
	if($('isDataChanged') != null)
	{
		$('isDataChanged').value = 1;
	}
}

function doDisplayAlert()
{
	alert('Please select an Organization first');
}

function doOnDataNotChanged()
{
	$('messageDataModified').hide();
	if($('isDataChanged') != null)
	{
		$('isDataChanged').value = 0;
	}
	
}

function doOnMSAjaxSubmitSave()
{
//	$('messageOk');
//	$('messageErr');
	
	var ajaxUrl = $('contextPath').value+'/ajaxOrganizationAction.do';
	var productId = $('productId').value;

	var index = $('mfg_id').selectedIndex;
	var mfgId = $('mfg_id').options[index].value;
	
	var index = $('spl_id').selectedIndex;
	var splId = $('spl_id').options[index].value;
	
	var type = "submitAdd";

	if (productId > 0) 
	{
		$('messageDataModified').hide();
		$('messageErr').hide();
//		$('messageOk').hide();
		$('messageSaving').show();
		
		new Ajax.Request(ajaxUrl, {
				method: 'post',
				parameters: {
					type: type,
					id: productId,
					mfgId: mfgId,
					splId: splId
				},
				
				onSuccess: function(transport){
					$('messageSaving').hide();
					$('isDataChanged').value = 0;
					$('isNewProduct').value = 0;
					
					var json = transport.responseText.evalJSON();
					
					if(!json || json.retCode == null || json.retCode != 1){
						$('messageErr').show();
					}else{
//						$('messageOk').show();
					}
				},
				onFailure: function(){
					alert("NOT DONE");
					$('messageSaving').hide();
					$('messageErr').show();
				}
			});
		}
}




function cleanDD(type)
{
	if(type == "searchOrganization"){
		$('searchArea').options.length = 0;
		if( $('searchSite') != null ) {
			$('searchSite').options.length = 0;
			$('searchBuilding').options.length = 0;
			$('searchRoom').options.length = 0;
		}
	}else if(type == "searchArea"){
		$('searchSite').options.length = 0;
		$('searchBuilding').options.length = 0;
		$('searchRoom').options.length = 0;
	}else if(type == "searchSite"){
		$('searchBuilding').options.length = 0;
		$('searchRoom').options.length = 0;
	}else if(type == "searchBuilding"){
		$('searchRoom').options.length = 0;
	} 
}

function displayWaitMessage(type, message){

	var dd;
	
	if(type == "searchOrganization"){
		dd = $('searchArea');
	}else if(type == "searchArea"){
		dd = $('searchSite');
	}else if(type == "searchSite"){
		dd = $('searchBuilding');
	}else if(type == "searchBuilding"){
		dd = $('searchRoom');
	}else if(type == "efOrganization"){
		dd = $('efArea');
	}else if(type == "efArea"){
		dd = $('efSite');
	}else if(type == "efSite"){
		dd = $('efBuilding');
	}else if(type == "efBuilding"){
		dd = $('efRoom');
	} 
	
	dd.options.length = 0;

	var newEl = document.createElement("option");
			
	newEl.setAttribute('value', '-1');
	var txt = document.createTextNode(message);
	newEl.appendChild(txt);
			
	dd.appendChild(newEl);
}

function displayWaitMessage1(target, message){

	var dd;
	
	if(target == "organization"){
		dd = $('organization');
	}else if(target == "area"){
		dd = $('area');
	}else if(target == "site"){
		dd = $('site');
	}else if(target == "building"){
		dd = $('building');
	}else if(target == "room"){
		dd = $('room');
	}else if(target == "efOrganization"){
		dd = $('efOrganization');
	}else if(target == "efArea"){
		dd = $('efArea');
	}else if(target == "efSite"){
		dd = $('efSite');
	}else if(target == "efBuilding"){
		dd = $('efBuilding');
	}else if(target == "efRoom"){
		dd = $('efRoom');
	} 
	
	if( dd!=null )
	{
		dd.options.length = 0;
	
		var newEl = document.createElement("option");
				
		newEl.setAttribute('value', '-1');
		var txt = document.createTextNode(message);
		newEl.appendChild(txt);
				
		dd.appendChild(newEl);
	}
}

function fillMSFromAjaxResponse(transport){
	
	var json = transport.responseText.evalJSON();
	if(!json) alert("no Json object");

	var type = $('changedDDId').value;



	if (type == "mfg_id") {

		$('mfg_address').value = (json.address==null) ? "" : json.address; 				//	ADDRESS			12	VARCHAR2
		$('mfg_po_box').value = (json.po_box==null) ? "" : json.po_box; 				//  								THERE IS NO COLUMN IN DB
		$('mfg_city').value = (json.city==null) ? "" : json.city; 					//	CITY			12	VARCHAR2
		$('mfg_prov').value = (json.prov==null) ? "" : json.prov; 					//	PROV			12	VARCHAR2
		$('mfg_postal').value = (json.postal_code==null) ? "" : json.postal_code; 			//	POSTAL_CODE		12	VARCHAR2
		$('mfg_country').value = (json.country==null) ? "" : json.country; 				//	COUNTRY			12	VARCHAR2
		$('mfg_contact_name').value = (json.contact_name==null) ? "" : json.contact_name; 	//  								THERE IS NO COLUMN IN DB
		$('mfg_telephone').value = (json.telephone==null) ? "" : json.telephone; 			//	TELEPHONE		12	VARCHAR2
		$('mfg_fax').value = (json.fax==null) ? "" : json.fax; 						//	FAX				12	VARCHAR2
		$('mfg_email1').href = "mailto:" + ((json.email1==null) ? "" : json.email1); 				//	EMAIL			12	VARCHAR2
		$('mfg_email1').update(((json.email1==null) ? "" : json.email1));
		
		$('mfg_websites_cell').childElements().invoke('remove');
		$('mfg_websites_cell').cleanWhitespace(); 
		getWebsites(json.website).each(function(name, index){ 
			$('mfg_websites_cell').insert("<a id='mfg_website"+index+"' target='_blank' href='http://"+name+"'>"+name+"</a> ");
	 	});
		
//		$('mfg_website').href = "http://" + ((json.website==null) ? "" : json.website); 				//	WEBSITE			12	VARCHAR2 
		//$('mfg_website').update(((json.website==null) ? "" : json.website));
		
	//	$('mfg_logo').value = (json.logo_file==null) ? "" : json.logo_file;				//  LOGO_FILE 		12	VARCHAR2
		$('mfg_editAction').href = "viewOrganization.do?id=" + ((json.id==null) ? "" : json.id);
	}else if(type == "spl_id"){
		$('spl_address').value = (json.address==null) ? "" : json.address; 				//	ADDRESS			12	VARCHAR2
		$('spl_po_box').value = (json.po_box==null) ? "" : json.po_box; 				//  								THERE IS NO COLUMN IN DB
		$('spl_city').value = (json.city==null) ? "" : json.city; 					//	CITY			12	VARCHAR2
		$('spl_prov').value = (json.prov==null) ? "" : json.prov; 					//	PROV			12	VARCHAR2
		$('spl_postal').value = (json.postal_code==null) ? "" : json.postal_code; 			//	POSTAL_CODE		12	VARCHAR2
		$('spl_country').value = (json.country==null) ? "" : json.country; 				//	COUNTRY			12	VARCHAR2
		$('spl_contact_name').value = (json.contact_name==null) ? "" : json.contact_name; 	//  								THERE IS NO COLUMN IN DB
		$('spl_telephone').value = (json.telephone==null) ? "" : json.telephone; 			//	TELEPHONE		12	VARCHAR2
		$('spl_fax').value = (json.fax==null) ? "" : json.fax; 						//	FAX				12	VARCHAR2
		$('spl_email1').href = (json.email1==null) ? "" : "mailto:"+json.email1; 				//	EMAIL			12	VARCHAR2
		$('spl_email1').update( (json.email1==null) ? "" : json.email1 ); 				//	EMAIL			12	VARCHAR2

		$('spl_websites_cell').childElements().invoke('remove');
		$('spl_websites_cell').cleanWhitespace(); 
		getWebsites(json.website).each(function(name, index){ 
			$('spl_websites_cell').insert("<a id='spl_website"+index+"' target='_blank' href='http://"+name+"'>"+name+"</a> ");
	 	});
		
//		$('spl_website').href = (json.website==null) ? "" : "http://"+json.website; 				//	WEBSITE			12	VARCHAR2 
//		$('spl_website').update( (json.website==null) ? "" : json.website ); 				//	WEBSITE			12	VARCHAR2 
		
	//	$('spl_logo').value = (json.logo_file==null) ? "" : json.logo_file;				//  LOGO_FILE 		12	VARCHAR2
		$('spl_editAction').href = "viewOrganization.do?id=" + ((json.id==null) ? "" : json.id);
	}
	
	$('ftMfg').hide();
	$('ftSpl').hide();
}

function getWebsites(websiteString){
	if(websiteString!=null){
	
		var websites=[];
		websiteString.scan( /[^\s,;]+/, 
							function(match){ 
								var w = match[0];
								if(w==null || w=='or' || w=='and' || w=='OR' || w=='AND') return;
								websites.push(w.replace("http://",""))
								
							}); 
		return websites;
	}else{
		return new Array(0);
	}
}

function fillDDFromAjaxResponse(transport)
{
	var json = transport.responseText.evalJSON();

	if(!json) alert("no Json object");
	
	var type = $('changedDDId').value;
	var dd;
	
	if(type == "searchOrganization")
	{
		dd = $('searchArea');
	}
	else if(type == "searchArea")
	{
		dd = $('searchSite');
	}
	else if(type == "searchSite")
	{
		dd = $('searchBuilding');
	}
	else if(type == "searchBuilding")
	{
		dd = $('searchRoom');
	} 
	
	if (dd != null) 
	{
		dd.options.length = 0;
		for (var i = 0; i < json.length; i++) 
		{
			var newEl = document.createElement("option");
			
			newEl.setAttribute('value', json[i].id);
			newEl.setAttribute('label', json[i].namedate);
			newEl.setAttribute('lName', json[i].name);
			newEl.setAttribute('lLastInventory', json[i].lastinventory);
			var txt = document.createTextNode(json[i].name);
			newEl.appendChild(txt);
			
			dd.appendChild(newEl);
		}
	}
	
	enableDisableBtn(1);
}

function displayErrorMessage(){alert('Not OK...')}

function doOnLoadViewLocation()
{
	
	if( $('searchSite') == null ){
		document.getElementsByName('searchArea')[0].disabled = !($('searchOrganization').selectedIndex > 0);
	}else{
		$('greybox_content').select('img').invoke('hide');
		$('greybox_content').select('input').invoke('hide');
		
		// Enable/Disable Lists
		document.getElementsByName('searchArea')[0].disabled = !($('searchOrganization').selectedIndex > 0);
		document.getElementsByName('searchSite')[0].disabled = !($('searchArea').selectedIndex > 0);
		document.getElementsByName('searchBuilding')[0].disabled = !($('searchSite').selectedIndex > 0);
		document.getElementsByName('searchRoom')[0].disabled = !($('searchBuilding').selectedIndex > 0);
		
		// Enable/Disable Buttons
		if (document.getElementsByName('searchRoom')[0].selectedIndex > 0) {
			$('area').select('img').invoke('show');
			$('site').select('img').invoke('show');
			$('building').select('img').invoke('show');
			$('room').select('img').invoke('show');
			$('room_archiveAll').show();
		}else if (document.getElementsByName('searchBuilding')[0].selectedIndex > 0) {
			$('area').select('img').invoke('show');
			$('site').select('img').invoke('show');
			$('building').select('img').invoke('show');
			$('room_add').show();
			$('building_archiveAll').show();
		}else if (document.getElementsByName('searchSite')[0].selectedIndex > 0) {
			$('area').select('img').invoke('show');
			$('site').select('img').invoke('show');
			$('building_add').show();
			$('site_archiveAll').show();
		}else if (document.getElementsByName('searchArea')[0].selectedIndex > 0) {
			$('area').select('img').invoke('show');
			$('site_add').show();
			$('area_archiveAll').show();
		}else if (document.getElementsByName('searchOrganization')[0].selectedIndex > 0) {
			$('area_add').show();
		}else {
			//		alert('ELSE');
		};
	}
}
	
function doOnClickImgBtn(actionType,fieldName,btnName)
{
	$('greybox_content').select('img').invoke('hide');
	$('greybox_content').select('input').invoke('hide');
	
	if(actionType == 0)
	{ 
		$('areaName').value = $('siteName').value = $('buildingName').value = $('roomName').value = '';
		$('areaLastInventory').value = $('siteLastInventory').value = $('buildingLastInventory').value = $('roomLastInventory').value = '';
		$('submitAdd'+btnName).show();
	}
	else
	{
		$('submitUpdate'+btnName).show();
	}
	
	$(fieldName+'Name').show();
	$(fieldName+'LastInventory').show();
	$('img' + btnName + 'LastInventory').show();

	$('submitCancel'+btnName).show();
}

function doSubmitEditOrganization(id){
	
}
		
function doOnClickRemoveImgBtn(formId, sfName)
{
	if (confirm("You are about to delete current location, all sub locations and all bindings of products to that location and sub locations")){
		
		document.getElementsByName('submitRemove'+sfName)[0].value='1';
		$(formId).submit();
	};
}
		
function doOnClickArchiveImgBtn(formId, sfName)
{
	if (confirm("You are about to delete current location, all sub locations and all bindings of products to that location and sub locations. The Products will be archived if it's only location for them."))
	{
		document.getElementsByName('archiveArea')[0].value='0';
		document.getElementsByName('archiveSite')[0].value='0';
		document.getElementsByName('archiveBuilding')[0].value='0';
		document.getElementsByName('archiveRoom')[0].value='0';
		
		document.getElementsByName('archive' + sfName)[0].value='1';
		$(formId).submit();
	};
}

function show_next_home(tab_name,val) {
	// set a hidden input field value
	$("searchTypeHidden").value = val;
	show_next(tab_name);
}

function toggleFields(val){
	if($('advanceSearch') != null){
		if(val!=null && val==7){
			$('advanceSearch').select('input').invoke('disable');
			$('advanceSearch').select('select').invoke('disable');
			$('searchStringMain').enable();
		}else{
			$('advanceSearch').select('input').invoke('enable');
			$('advanceSearch').select('select').invoke('enable');
		}
		if(val!=null && val==8){
			$('reportTypeHD').show();
		}else{
			$('reportTypeHD').hide();
		}
	}
}

function toggleReportDD(){
	if($('reportType')!=null){
		if( $('reportType').value == 1 ){
			$('searchMnfSplId').hide();
			$('reportTypeHD_title').hide();
			$('reportTypeCAS').hide();
//			$('searchCustomerId').show();
		}else if( $('reportType').value == 2 ){
			$('searchMnfSplId').show();
			$('reportTypeHD_title').show();
			$('reportTypeCAS').hide();
	//		$('searchCustomerId').hide();
		}else if( $('reportType').value == 3 ){
			$('searchMnfSplId').hide();
			$('reportTypeHD_title').hide();
			$('reportTypeCAS').show();
	//		$('searchCustomerId').hide();
		}else{
			$('searchMnfSplId').hide();
			$('reportTypeHD_title').hide();
			$('reportTypeCAS').hide();
		//	$('searchCustomerId').hide();
		}
	}
}

function set_active_tab(){
	var id = ($("searchTypeHidden").value < 1) ? 1 : $("searchTypeHidden").value;
	show_next('tab'+id,id);
	toggleFields(id);
}

function init_window()
{
	setFocusField();
//	top.resizeTo(1150,670);
	//top.resizeTo(screen.availWidth,screen.availHeight);
	//top.moveTo(0,0);
	//if( $('searchString') != null ) $('searchString').activate();
}

function setFocusField()
{
	if($('focusField') != null)
	{
		$('focusField').focus();
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function toggleSelectedImage(index)	{
	
	// 	find the corresponsing image and check box
	var chkbox = $('symbol_'+index).select('input').toArray()[0];
	var images = $('symbol_' + index).select('img');
	
	if (chkbox != null) {
		if (chkbox.checked == false) {
			images.invoke("setStyle", { border: '2px solid #0066CC' }); // change the icon border color to show that it is selected. 
			chkbox.checked = true;
		}
		else {
			images.invoke("setStyle", { border: '2px solid #ffffff' }); // blank the icon border color to show that it is UNselected.
			chkbox.checked = false;
		}
	}
	$('messageDataModified').show();
}

// Admin Config - /////////////////////////////////////////////////////////////////////////////////////////////

function setRowStyle(tag,type)
{
	if(type == 'mouseover'){
		tag.style.background = '#eeeeee';
	}else{
		tag.style.background = 'transparent';
	}
}

function setRowBackGround(tag, bgcolor)
{
	tag.style.background = bgcolor;
}

// Admin Config - Edit Images /////////////////////////////////////////////////////////////////////////////////

function selectLabel(index,path)	{
	$('edit_label_area').show();
	
	$('nmId').value = $('id_'+index).value;
	$('nmTdgImageId').value = $('tdgImageId_'+index).value;
	if($('nmLabel') != null){
		$('nmLabel').value = $('label_'+index).value;
	}
	$('nmFileName').value = $('fileName_'+index).value;
	if($('nmNote') != null){
		$('nmNote').value = $('note_'+index).value;
	}
	$('edit_label').select('img').invoke("setStyle", { border: '3px solid #ffffff' });
	$('iconSelected').src = path+$('fileName_'+index).value;
	$('img_' + index).setStyle({ border: '3px solid #0066CC' });
}

function showModifiedMessage(){
	if($('messageDataModified')!=null){
		$('messageDataModified').show();
	}
}

function doOnClickAddBtn(path){

	$('edit_label').select('img').invoke("setStyle", { border: '3px solid #ffffff' });
	$('iconSelected').src = path+"label_blank.gif";

	$('nmId').value = '';
	$('nmTdgImageId').value = '';
	if($('nmLabel') != null) {
		$('nmLabel').value = '';
	}
	$('nmFileName').value = '';
	if($('nmNote') != null) {
		$('nmNote').value = '';
	}
	
	$('isDataChanged').value = 0;
	$('messageDataModified').hide();

	$('edit_label_area').show();
}

function doOnClickDeleteBtn(){
	if(confirm('This image will be removed from all associated products.Are you sure you want to delete this image ?')){
		if ($('nmId').value != null && $('nmId').value > 0) {
			$('submitDelete').value = 1;
			$('acEditImages').submit();
		}
	}
}

function doPopUpSelectFileDialog(){
	alert("select file dilog");
		$('nmFileName').value = 'new file name';
}

// Admin Config - Edit Phrases /////////////////////////////////////////////////////////////////////////////////

function doEditPhrase(index){
	$('nmPhrase').value = $('phrase_'+index).value;
	$('nmId').value = $('id_'+index).value;
	$('nmTextId').value = $('textId_'+index).value;
	$('nmType').value = $('type_'+index).value;
	$('addEditPhraseNote').show();
	$('addEditPhraseNoteFooter').show();
	$('addNoteTitle').hide();
	$('editNoteTitle').show();
	doOnDataNotChanged();
}

function doRemovePhrase(index,formId){
	if(confirm('This phrase will be removed from all associated products.Are you sure you want to delete this phrase ?')){
		$('nmId').value = $('id_'+index).value;
		$('nmType').value = $('type_'+index).value;	
		$('submitDelete').value = 1;
		$(formId).submit();
	}
}

function doAddPhrase(){
	$('nmPhrase').value = '';
	$('nmId').value = '';
	$('nmTextId').value = '';
//	$('nmType').value = '';
	$('addEditPhraseNote').show();
	$('addEditPhraseNoteFooter').show();
	$('editNoteTitle').hide();
	$('addNoteTitle').show();
	doOnDataNotChanged();
}

//function validateEditPhraseForm(){
//	if( $('isDataChanged').value == 1 ) return true;
//	else return false;
//}

function validateQuickSearchForm()
{

		re = /^\(?(\d{3})\)?[\.\-\/ ]?(\d{3})[\.\-\/ ]?(\d{4})$/
		reMemberID = /^\d{3,10}$/
		reCode = /^\w{6,10}$/
		reCountry = /^\w+$/
		rePhoneNumber = /^[\d -\.()]+$/
		reEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
		reString = /\w+/

		errMsg = "";

		if( $('searchType').value <= 0 ){
			errMsg = "You must choose Search Type\n";
		}
		if (!reString.test($('searchString').value)) {
			errMsg = errMsg + "You must type a search string\n";
		}

		if(errMsg != "")
		{
			alert(errMsg)
			return false
		}

		setCursorWait();
		return true
}


// Admin Config - Edit Notes /////////////////////////////////////////////////////////////////////////////////

function doEditNote(index){
	$('nmNote').value = $('note_'+index).value;
	$('nmId').value = $('id_'+index).value;
	$('nmTextId').value = $('textId_'+index).value;
	$('nmType').value = $('type_'+index).value;
	$('nmIsBannedSubstance').checked = ($('isBannedSubstance_'+index).value  == 'true') ? true : false;	
//	alert('asdfasdf:' + $('type_'+index).value);
	$('addEditNote').show();
	$('addEditNoteFooter').show();
	$('addNoteTitle').hide();
	$('editNoteTitle').show();
	doOnDataNotChanged();
}

function doEditHI(index)
{
	$('nmId').value = $('id_'+index).value;
	$('nmName').value = $('name_'+index).value;
	$('nmCas').value = $('cas_'+index).value;

	//$('addEditHI').show();
	//$('addEditHIFooter').show();
	//$('addHITitle').hide();
	//$('editHITitle').show();
	doOnDataNotChanged();
}

function doRemoveNote(index,formId){
	if(confirm('This note will be removed from all associated products.Are you sure you want to delete this note?')){
		$('nmId').value = $('id_'+index).value;
		$('nmType').value = $('type_'+index).value;
		$('submitDelete').value = 1;
		$(formId).submit();
	}
}

function doRemoveHI(index, formId)
{
	if(confirm('This Hazardous Ingredients  will be removed from all associated products.Are you sure you want to delete this Hazardous Ingredients ?')){
			
		$('nmId').value = $('id_'+index).value;
		$('submitDelete').value = 1;
		$(formId).submit();
	}
}

function doAddNote(){
	$('nmNote').value = '';
	$('nmId').value = '';
	$('nmTextId').value = '';
//	$('nmType').value = '';
	$('addEditNote').show();
	$('addEditNoteFooter').show();
	$('editNoteTitle').hide();
	$('addNoteTitle').show();
	doOnDataNotChanged();
}

function doAddHI()
{
	$('nmId').value = '';
	$('nmName').value = '';
	$('nmCas').value = '';

	//$('addEditHI').show();
	//$('addEditHIFooter').show();
	//$('addHITitle').show();
	//$('editHITitle').hide();
	doOnDataNotChanged();
}

function validateForm(){
	if ($('isDataChanged').value == 1) {
		// $('isDataChanged').value = 0;
		return true;
	}
	else {
		alert("There is nothing to save.");
		return false;
	}
}

// Admin Config - Edit Shipping Notes //////////////////////////////////////////////////////////////////////////////


function doEditShippingNote(index)
{
	$('nmId').value = $('id_'+index).value;
	
	$('nmShippingNote').value = $('shippingNote_'+index).value;
	$('nmExemptionNote').value = $('exemptionNote_'+index).value;
	
	$('nmSource').value = $('source_'+ index).value;
	
	$('nmGeneral').checked = ($('general_'+index).value == 'true') ? true : false;
	$('nmTruck').checked = ($('truck_'+index).value  == 'true') ? true : false;
	$('nmPassenger').checked = ($('passenger_'+index).value  == 'true') ? true : false;
	$('nmRail').checked = ($('rail_'+index).value  == 'true') ? true : false;
	$('nmMarine').checked = ($('marine_'+index).value  == 'true') ? true : false;
	$('nmAir').checked = ($('air_'+index).value  == 'true') ? true : false;
	
	$('nmExemption').checked = ($('exemption_'+index).value  == 'true') ? true : false;

	$('addEditShippingNote').show();
	$('addNoteTitle').hide();
	$('editNoteTitle').show();
	doOnDataNotChanged();
}

function doRemoveShippingNote(id)
{
	if(confirm('This shipping note will be removed from all associated products.Are you sure you want to delete this shipping note ?')){
			
		$('nmId').value = id;
		$('submitDelete').value = 1;
		$('acEditShippingNotes').submit();
	}
}

function doAddShippingNote()
{
	$('nmId').value = '';
	
	$('nmShippingNote').value = '';
	$('nmExemptionNote').value = '';
	
	$('nmSource').value = -1;
	
	$('nmGeneral').checked = false;
	$('nmTruck').checked = false;
	$('nmPassenger').checked = false;
	$('nmRail').checked = false;
	$('nmMarine').checked = false;
	$('nmAir').checked = false;
	
	$('nmExemption').checked = false;
	
	$('addEditShippingNote').show();
	$('editNoteTitle').hide();
	$('addNoteTitle').show();
	doOnDataNotChanged();
}

//function validateShippingNoteForm(){
//	if( $('isDataChanged').value == 1 ) return true;
//	else return false;
//}

function fillDDFromJSONArray(jsonArray, controlId) {
	var dd = $(controlId);

	if ( dd !=null ){
		dd.options.length = 0;
		if ( jsonArray.length > 1 ) {
			for (var i = 0; i < jsonArray.length; i++) {
				var newEl = document.createElement("option");
				newEl.setAttribute('value', jsonArray[i].value);
				var txt = document.createTextNode(jsonArray[i].label);
				newEl.appendChild(txt);		
				dd.appendChild(newEl);
			}
		}
	}
}

// Reports //////////////////////////////////////////////////////////////////////////////

function expandDitail(newIndex)
{
	if($('detail_'+newIndex).visible()){
		$('detail_'+newIndex).hide();	
	}else{
		$('detail_'+newIndex).show();		
	}
}

function toggleDitailVisibility(id)
{
	var detail = 'detail'+id;
	var img = 'img'+id;
	if($(detail).visible()){ 
		$(detail).hide(); 
		if($(img) != null) $(img).src = 'images/results_item_arrowdown.gif';
	}else{ 
		$(detail).show();	
		if($(img) != null) $(img).src = 'images/results_item_arrowup.gif';
	}
}

function expandAll(maxIndex){
	for(i=0; i<maxIndex; i++){
		$('detail'+i).show();	
		if($('img'+i) != null) $('img'+i).src = 'images/results_item_arrowup.gif';
	}
}

function collapseAll(maxIndex){
	for(i=0; i<maxIndex; i++){
		$('detail'+i).hide();
		if($('img'+i) != null) $('img'+i).src = 'images/results_item_arrowdown.gif';
	} 	
}

function setCursorWait(){

	if($('isDump')==null || $('isDump').value!=1){
		if($('mainBody')!=null){
			//style="cursor:wait;" 
			$('mainBody').setStyle({ cursor: 'wait' }); 
			$('mainBody').select('input').invoke('setStyle',{ cursor: 'wait' });
		}
	}

	return true;
}


// no longer in use. !!!!!
function doOnClickUnbindConfirm(formId, sfName)
{
	//alert(document.getElementsByName('searchOrganization')[0].value);
	if(sfName == 'Area'){
		if (confirm("You are about to Unbind / Archive all products for Area")){
			 $(formId).submit();
		}else {
			return false;
		}
	}else if(sfName == 'Site'){
		if (confirm("You are about to Unbind / Archive all products for Site")){
			 $(formId).submit();
		}else {
			return false;
		}
	}else if(sfName == 'Building'){
		if (confirm("You are about to Unbind / Archive all products for Building")){
			 $(formId).submit();
		}else {
			return false;
		}
	}else if(sfName == 'Room'){
		if (confirm("You are about to Unbind / Archive all products for Room")){
			 $(formId).submit();
		}else {
			return false;
		}
	}else if(sfName == 'Organization'){
		if (confirm("You are about to Unbind / Archive all products for Organization")){
			 $(formId).submit();
		}else {
			return false;
		}
	}
}

/*
function doSubmit(submitForm,submitName){
	if( $(submitForm)!=null ) {
		setCursorWait();
		$(submitForm).value = 1;
		submitForm.form.submit();
	}
}
*/

//	THE END MSDS.JS
