/* 
	File:		/js/scripts.js
	Author:		Mindcast Software
	Copyright:	© 1996-2005  The GCRH Corporation  All rights reserved.
	Notes:		generic functions used for window management etc.
*/

//\
//\ Generic Javascript Functions
//\


// UTILITY FUNCTION TO REPLACE A STRING WITH ANOTHER STRING
function replaceString(oldS,newS,fullS) 
{
// Replaces oldS with newS in the string fullS   
	for (var i=0; i<fullS.length; i++) {      
		if (fullS.substring(i,i+oldS.length) == oldS) {
		         fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length)
		}
	}
	   
	return fullS;
}

//\ Checks if enter was pressed on a text field and executes the specified action
function CheckEnter(iKeyCode, sAction)
{
	if (iKeyCode == 13) 
		{ 
			eval(sAction);
		}
}


//\ Updates a dropdown box with a given value
function update_dropdown(name, value)
{
	for (var i = 0; i < eval(name + '.options.length'); i++) 
	{
		if (eval(name + '.options(' + i + ').value') == value)
		{
			eval(name + '.options(' + i + ').selected = true');
		}
	}
}



//\ 
//\ /EntityAdmin/Rights Section
//\ 

function confirm_exit(url, message)
{
//	if ( window.confirm(message) )
	if ( 1 )
	{
		document.URL = url;			
	}
}

function confirm_action(url, message)
{
	if ( confirm(message) )
//	if ( 1 )
	{
		document.URL = url;			
	}
}

function tool_tip(tipID, x, y)
{
	var url;
	url = '/Popup/Tips/Tips.asp?tipID=' + tipID;
	var name;
	name = 'Info';
	popupWindow = null;
	//alert(url);
	popupWindow = open(url, name, 'toolbar=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=250, height=250');	
	//alert(x);
	//alert(y);
	popupWindow.moveTo(x, y);
}

//\
//\ Generic Material Code Selector
//\
function popupMaterialCodeSelector(allowNew, FormDotElementName, UpdateList)
{
	var url
	url = '/Popup/MaterialCode/materialCodeSelector.asp?allowNew=' + allowNew + '&FormDotElementName=' + FormDotElementName + '&UpdateList=' + UpdateList;
	
	var name;
	name = 'materialCodeSelector'
	popupWindow = null;
	popupWindow = open(url, name, 'toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=440, height=450');
	popupWindow.moveTo(10, 10);
}

//\
//\ Generic Classification Code Selector
//\
function popupClassificationCodeSelector(allowNew, FormDotElementName, UpdateList, ClassificationID, strCodeLabel, strDescLabel)
{
	var url
	url = '/Popup/ClassificationCode/ClassificationSelector.asp?allowNew=' + allowNew + '&FormDotElementName=' + FormDotElementName + '&UpdateList=' + UpdateList + '&ClassificationID=' + ClassificationID + '&CodeLabel=' + strCodeLabel + '&DescLabel=' + strDescLabel;
	
	var name;
	name = 'ClassificationCodeSelector'
	popupWindow = null;
	popupWindow = open(url, name, 'toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=440, height=450');
	popupWindow.moveTo(10, 10);
}

//\
//\ Generic Code Selector
//\
function popupGenericCodeSelector(allowNew, allowMulti, FormDotElementName, FormDotElementID, UpdateList, GroupID, strCodeLabel, strDescLabel, Value_TableName, Group_uKeyName, Value_uKeyName, SelectedValues, Value_FieldName, Value_FieldDescName)
{
	var url
	url = '/Popup/GenericCode/GenericSelector.asp?allowNew=' + allowNew + '&allowMulti=' + allowMulti + '&FormDotElementName=' + FormDotElementName + '&FormDotElementID=' + FormDotElementID + '&UpdateList=' + UpdateList + '&GroupID=' + GroupID + '&CodeLabel=' + strCodeLabel + '&DescLabel=' + strDescLabel + '&Value_TableName=' + Value_TableName + '&Group_uKeyName=' + Group_uKeyName + '&Value_uKeyName=' + Value_uKeyName + '&SelectedValues=' + SelectedValues + '&Value_FieldName=' + Value_FieldName + '&Value_FieldDescName=' + Value_FieldDescName;
	//alert(url);
	var name;
	name = 'GenericCodeSelector'
	popupWindow = null;
	popupWindow = open(url, name, 'toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=440, height=510');
	popupWindow.moveTo(10, 10);
	
}

function popupGenericCodeSelectorSelectAll(allowNew, allowMulti, FormDotElementName, FormDotElementID, UpdateList, GroupID, strCodeLabel, strDescLabel, Value_TableName, Group_uKeyName, Value_uKeyName, SelectedValues, Value_FieldName, Value_FieldDescName)
{
	var url
	url = '/Popup/GenericCode/GenericSelector.asp?selectall=1&allowNew=' + allowNew + '&allowMulti=' + allowMulti + '&FormDotElementName=' + FormDotElementName + '&FormDotElementID=' + FormDotElementID + '&UpdateList=' + UpdateList + '&GroupID=' + GroupID + '&CodeLabel=' + strCodeLabel + '&DescLabel=' + strDescLabel + '&Value_TableName=' + Value_TableName + '&Group_uKeyName=' + Group_uKeyName + '&Value_uKeyName=' + Value_uKeyName + '&SelectedValues=' + SelectedValues + '&Value_FieldName=' + Value_FieldName + '&Value_FieldDescName=' + Value_FieldDescName;
	
	var name;
	name = 'GenericCodeSelector'
	popupWindow = null;
	popupWindow = open(url, name, 'toolbar=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=1, height=1');
	
}




//\
//\ Generic Code Selector WITH Query Filter
//\
function popupGenericCodeSelectorWithFilter(Filter, allowNew, allowMulti, FormDotElementName, FormDotElementID, UpdateList, GroupID, strCodeLabel, strDescLabel, Value_TableName, Group_uKeyName, Value_uKeyName, SelectedValues, Value_FieldName, Value_FieldDescName)
{
	var url
	url = '/Popup/GenericCode/GenericSelector.asp?Value_Filter=' + Filter + '&allowNew=' + allowNew + '&allowMulti=' + allowMulti + '&FormDotElementName=' + FormDotElementName + '&FormDotElementID=' + FormDotElementID + '&UpdateList=' + UpdateList + '&GroupID=' + GroupID + '&CodeLabel=' + strCodeLabel + '&DescLabel=' + strDescLabel + '&Value_TableName=' + Value_TableName + '&Group_uKeyName=' + Group_uKeyName + '&Value_uKeyName=' + Value_uKeyName + '&SelectedValues=' + SelectedValues + '&Value_FieldName=' + Value_FieldName + '&Value_FieldDescName=' + Value_FieldDescName;
	
	var name;
	name = 'GenericCodeSelector'
	popupWindow = null;
	popupWindow = open(url, name, 'toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=440, height=450');
	popupWindow.moveTo(10, 10);
}
//\
//\ Generic Code Selector with DocID
//\
function popupGenericCodeSelectorWithDocID(docID, allowNew, allowMulti, FormDotElementName, FormDotElementID, UpdateList, GroupID, strCodeLabel, strDescLabel, Value_TableName, Group_uKeyName, Value_uKeyName, SelectedValues, Value_FieldName, Value_FieldDescName)
{
	var url
	url = '/Popup/GenericCode/GenericSelector.asp?docID=' + docID + '&allowNew=' + allowNew + '&allowMulti=' + allowMulti + '&FormDotElementName=' + FormDotElementName + '&FormDotElementID=' + FormDotElementID + '&UpdateList=' + UpdateList + '&GroupID=' + GroupID + '&CodeLabel=' + strCodeLabel + '&DescLabel=' + strDescLabel + '&Value_TableName=' + Value_TableName + '&Group_uKeyName=' + Group_uKeyName + '&Value_uKeyName=' + Value_uKeyName + '&SelectedValues=' + SelectedValues + '&Value_FieldName=' + Value_FieldName + '&Value_FieldDescName=' + Value_FieldDescName;
	
	var name;
	name = 'GenericCodeSelector'
	popupWindow = null;
	popupWindow = open(url, name, 'toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=440, height=450');
	popupWindow.moveTo(10, 10);
}

//\
//\ Generic Manufacturer Selector
//\
function popupManufacturerSelector(allowNew, FormDotElementName, FormDotElementID)
{
	var url
	url = '/Popup/Manufacturer/manufacturerSelector.asp?allowNew=' + allowNew + '&FormDotElementName=' + FormDotElementName + '&FormDotElementID=' + FormDotElementID;
	
	var name;
	name = 'manufacturerSelector'
	popupWindow = null;
	popupWindow = open(url, name, 'toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=440, height=450');
	popupWindow.moveTo(10, 10);
}



//\
//\  Document Section Sections
//\


function document_preview_fmp(docid)
{
//\ Submit the form to another window since target should be set to 'Preview'
//\
	//previewWindow = window.open('/Documents/Preview/index.asp?docid='+docid, 'Preview', 'scrollbars=yes, resizable=yes');
	 a = openwindow('/Documents/Preview/index.asp?docid='+docid, 'Preview', 0,0,0,0,1,1);
}

function document_preview_unicode_selectlang(docid,returnpage)
{
//\ This preview is used for all documents that go through the eurasian server documents.
//\ Submit the form to another window since target should be set to 'Preview'
	//previewWindow = window.open('/Documents/Preview/index.asp?docid='+docid+'&type=unicode_selectlang&return=' + returnpage, 'Preview', 'directories=no, menubar=no, status=no, toolbars=no, scrollbars=yes, resizable=yes');
    a = openwindow('/Documents/Preview/index.asp?docid='+docid+'&type=unicode_selectlang&return=' + returnpage, 'Preview', 0,0,0,0,1,1);

}

function document_preview_unicode(docid, returnpage)
{
//\ This preview is used for Asian documents.
//\ Submit the form to another window since target should be set to 'Preview'

a = openwindow('/Documents/Preview/index.asp?docid='+docid+'&type=unicode&return=' + returnpage, 'Preview', 0,0,0,0,1,1);

}

function openwindow(url, name, dirs, menu, status, toolbar,scroll, resize)
{
var params = '';
params = '"directories=' + dirs;
params += ',menubar=' + menu;
params += ',status=' + status;
params += ',toolbars=' + toolbar;
params += ',scrollbars=' + scroll;
params += ',resizable=' + resize;
params += ',height=' + (screen.availHeight-60);
params += ',width=' + (screen.availWidth-60);
params += ',top=30,left=30"';

if (typeof window.previewWindow != 'undefined')
			{
				previewWindow.close();
			}
			//alert(params);
previewWindow = window.open(url, name, params);
return;
}

function document_preview_msds(docid)
{
//\ This preview is used for MSDS documents.
	//previewWindow = window.open('/Documents/Preview/index.asp?docid='+docid+'&type=MSDS', 'Preview', 'directories=no, menubar=yes, status=yes, toolbars=yes, scrollbars=yes, resizable=yes');
	if (typeof window.previewWindow != 'undefined')
			{
				previewWindow.close();
			}
	a = openwindow('/Documents/Preview/index.asp?docid='+docid+'&type=MSDS', 'Preview', 0,1,1,1,1,1);
	
}

function document_preview_msds_hsr(docid)
{
//\ This preview is used for MSDS documents.
//\ Submit the form to another window since target should be set to 'Preview'
	//previewWindow = window.open('/Documents/Preview/edoc.asp?docid='+docid+'&type=MSDS_HSR', 'Preview', 'directories=no, menubar=no, status=no, toolbars=no, scrollbars=yes, resizable=yes');
	a = openwindow('/Documents/Preview/edoc.asp?docid='+docid+'&type=MSDS_HSR', 'Preview', 0,0,0,0,1,1);
}

function document_generate_msds_hsr(docid)
{
//\ This generate is used for MSDS - HSR documents only.
//\ Submit the form to another window since target should be set to 'Preview'
	//previewWindow = window.open('/Documents/Preview/index.asp?docid='+docid+'&type=MSDS_HSR', 'Preview', 'directories=no, menubar=no, status=no, toolbars=no, scrollbars=yes, resizable=yes');
	a = openwindow('/Documents/Preview/index.asp?docid='+docid+'&type=MSDS_HSR', 'Preview', 0,0,0,0,1,1);
}

function document_preview_edoc(docid)
{
//\ This preview is used for Asian documents.
//\ Submit the form to another window since target should be set to 'Preview'
	//previewWindow = window.open('/Documents/Preview/edoc.asp?docid='+docid, 'Preview', 'menubar=no, scrollbars=yes, resizable=yes');
	a = openwindow('/Documents/Preview/edoc.asp?docid='+docid, 'Preview', 0,0,0,0,1,1);
}
function catalog_preview_edoc(docid)
{
	//previewWindow = window.open('/Catalog/Preview/edoc.asp?docid='+docid, 'Preview', 'menubar=no, scrollbars=yes, resizable=yes');
	a = openwindow('/Catalog/Preview/edoc.asp?docid='+docid, 'Preview', 0,0,0,0,1,1);
}
function catalog_preview_fmp(docid, urltxt)
{
//\ Submit the form to another window since target should be set to 'Preview'
//\
	//previewWindow = window.open('/Catalog/Preview/index.asp?docid='+docid+urltxt, 'Preview', 'scrollbars=yes, resizable=yes');
	a = openwindow('/Catalog/Preview/index.asp?docid='+docid+urltxt, 'Preview', 0,0,0,0,1,1);
}

function catalog_preview_unicode(docid)
{
//\ This preview is used for Asian documents.
//\ Submit the form to another window since target should be set to 'Preview'
	//previewWindow = window.open('/Catalog/Preview/index.asp?docid='+docid+'&type=unicode', 'Preview', 'directories=no, menubar=no, status=no, toolbars=no, scrollbars=yes, resizable=yes');
	a = openwindow('/Catalog/Preview/index.asp?docid='+docid+'&type=unicode', 'Preview', 0,0,0,0,1,1);
}

function catalog_preview_batch(docid, layout)
{
	//previewWindow = window.open('/Catalog/Production/Preview/index.asp?docid='+docid+'&layout='+layout, 'Preview', 'directories=no, menubar=no, status=no, toolbars=no, scrollbars=yes, resizable=yes');
	a = openwindow('/Catalog/Production/Preview/index.asp?docid='+docid+'&layout='+layout, 'Preview', 0,0,0,0,1,1);
}

function archive_print_status(docid, printid, mode)
{
//\ Submit the form to another window since target should be set to 'Preview'
	printid = parseInt(printid);
	if(printid != '0' ){
		//alert(printid);
		//previewWindow = window.open('/Archive/index.asp?docid='+docid+'&mode='+mode+'&printid='+printid, 'Preview', 'menubar=no, scrollbars=yes, resizable=yes');
		a = openwindow('/Archive/index.asp?docid='+docid+'&mode='+mode+'&printid='+printid, 'Preview', 0,0,0,0,1,1);
	}
	else{
		//alert("no printid");
		//previewWindow = window.open('/Archive/index.asp?docid='+docid+'&mode='+mode, 'Preview', 'menubar=no, scrollbars=yes, resizable=yes');
		a = openwindow('/Archive/index.asp?docid='+docid+'&mode='+mode, 'Preview', 0,0,0,0,1,1);
	}
}

///////////////////////////////////////////////////////////////////////////////




var pId;
var DivBlock = document.getElementsByTagName("div");

///////////////////////////////////////////////////////////////////////////////


function Section_Properties_click(id)
{
//\
	//var DivBlock = document.getElementsByTagName("div");
	
	// Hide or Display this section
	with(document.getElementById("Section_Properties_" + id).style)
		display == "none" ? display = "block" : display = "none"
}


function LaunchNotesWinWithID(Note_Type, Note_User,Note_ID)
{
//\
	var url
	
	url = '/Documents/notes.asp?note_user=' + Note_User + '&note_id=' + Note_ID + '&note_type=' + Note_Type + '&Note_DocId=' + document.Edit.docid.value;
	
	popupWindow = open(url, '', 'toolbar=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, width=450, height=200');
	popupWindow.moveTo(100, 100);
}

function PopupContributorClose()
{
//	window.opener.location.reload();
	window.close();
}


function PopupContributors(docid)
{
//\
	url = '/Popup/ContributorMatrix/Edit.asp?docid=' + docid;
	popupWindow = window.open(url, 'Preview', 'directories=no, menubar=no, status=no, toolbars=no, scrollbars=yes, resizable=yes, width=600, height=400');
}

function PopupCatalogPreview(docid)
{
//\
	url = '/Catalog/Preview/index.asp?docid=' + docid;
	previewWindow = window.open(url, 'CatalogPreview', 'directories=no, menubar=no, status=no, toolbars=no, scrollbars=yes, resizable=yes, width=700, height=900');
}


function document_SubmitFormSimple(iForm)
{
//\ Submit the form to another window since target should be set to 'Preview'
	previewWindow = window.open('', 'Preview', 'directories=no, menubar=no, status=no, toolbars=no, scrollbars=yes, resizable=yes');
	eval ('document.' + iForm + '.submit()');
}






//
// Catalog
//

function catalog_options_submit()
{
	// Submit the form to another window since target should be set to 'Preview'
	previewWindow = window.open('', 'Preview', 'directories=no, menubar=no, status=no, toolbars=no, scrollbars=yes, resizable=yes');
	document.Edit.submit();
}


function catalog_options_click(id)
{
	var DivBlock = document.getElementsByTagName("div");
	
	// Hide all of the other sections
/* - no need for this anymore...
	for (var i = 1; i <= 9; i++)
		if (i !== id)
			if (DivBlock["Catalog_Options_" + i])
				DivBlock["Catalog_Options_" + i].style.display = "none";
*/
	// Hide or Display this section
	with(DivBlock["Catalog_Options_" + id].style)
		display == "none" ? display = "block" : display = "none"
}


function catalog_options_sn_qty_blur()
{
	catalog_options_SetPkgEnd();
	catalog_options_SetUniqueValues();
}


function catalog_options_SetPkgEnd()
{
	if (document.Edit.SN_PkgBegin && document.Edit.SN_PkgQty)
		if (document.Edit.SN_PkgBegin.value !== '' && document.Edit.SN_PkgQty.value !== '')
			document.Edit.SN_PkgEnd.value = parseInt(document.Edit.SN_PkgBegin.value) + parseInt(document.Edit.SN_PkgQty.value) - 1;
}


function catalog_options_SetPkgQty()
{
	var myBegin = document.Edit.SN_PkgBegin.value;
	var myEnd = document.Edit.SN_PkgEnd.value;
	
	if (myBegin !== '' && myEnd !== '')
	{
		
		if (isNaN(myBegin) == true) {
			document.Edit.SN_PkgBegin.value = 1;
		}
		if (isNaN(myEnd) == true) {
			document.Edit.SN_PkgEnd.value = document.Edit.SN_PkgBegin.value;
		}		
		document.Edit.SN_PkgQty.value = parseInt(document.Edit.SN_PkgEnd.value) - parseInt(document.Edit.SN_PkgBegin.value) + 1;
		catalog_options_SetUniqueValues();
	}
}

function catalog_options_SetUniqueValues()
{
	if (document.Edit.SN_UnqBegin)
		if (document.Edit.SN_UnqBegin.value !== '' && document.Edit.SN_PkgQty.value !== '')
		{
			// Set the Unique Qty & End Values
			document.Edit.SN_UnqQty.value = document.Edit.SN_PkgQty.value;
			document.Edit.SN_UnqEnd.value = parseInt(document.Edit.SN_UnqBegin.value) + parseInt(document.Edit.SN_UnqQty.value) - 1;
		}
}

function SubmitForm(iForm, iVariable, iValue)
{	
	eval ('document.' + iForm + '.' + iVariable + '.value = iValue');	
	eval ('document.' + iForm + '.submit()');
}

function singleQuoteEscape(str)
{
	var retVal;
	retVal = str.replace('\"', '\\\"');
	retVal = retVal.replace('\'', '\\\'');
	return retVal;
}

function Section_Addend_click(id, maxid)
{
//\
	var DivBlock = document.getElementsByTagName("div");
	
	// Hide all of the other sections
	for (var i = 1; i <= maxid; i++)
		if (i !== id)
			if (DivBlock["Section_Addend_" + i]) {
			DivBlock["Section_Addend_" + i].style.display = "none";}

	// Hide or Display this section
	with(DivBlock["Section_Addend_" + id].style)
		display == "none" ? display = "block" : display = "none"
}

function ValiDate(Mydate){
	var errs = "";
	
	//It would be nice to trim the string, but that's not an easy task in js.
	//var theDate = f.theDate.value;
	var theDate = Mydate;
	
	//Basic sanity check. Skip the more rigorous tests if there's nothing there.
	if( theDate=="" ){
		errs += "  -The Date you entered was blank.\n";
		return ShowAlert(errs);
	}
	//Pattern looks for 1-2 digits at the start of the string, followed by '/', followed by 1-2 digits,
	//followed by /, followed by exactly 4 digits, followed by the end of the string
	var datePattern = /^\d{1,2}\/\d{1,2}\/\d{4}$/;

	if(!theDate.match(datePattern)){
		errs += "  -The Date you entered was not in the format 'MM/DD/YYYY'.\n";
		return ShowAlert(errs);
	}
	//That assures that it's in the proper numerical format, but not that the numbers are sane.
	//Using Date.parse() doesn't really help, since it accepts pretty wacky values and converts them to dates
	//when it sometimes shouldn't (like '34/84/5555'). Instead, it's slightly more reasonable to handle
	//the values individually. This accepts odd dates like "Feb. 31", but I think we can live with that for now.
	//Here goes with the string manipulation.
	var dateParts = theDate.split("/");
	if(!(dateParts[0]>=1 && dateParts[0]<=12)){
		errs += "  -The Month you entered was not in a valid range.\n";
	}
	if(!(dateParts[1]>=1 && dateParts[1]<=31)){
		errs += "  -The Day you entered was not in a valid range.\n";
	}
	if(!(dateParts[2]>=1970 && dateParts[2]<=2100)){
		errs += "  -The Year you entered was not in the valid range between 1970 and 2100.\n";
	}
	if(errs) {
		return ShowAlert(errs);
	} else {
		return true;
	}
}

function ShowAlert(errs){
		var msg = "Your form contained the following error(s).\n";
		msg += errs;
		msg += "Please fix these issues and resubmit.";
		alert(msg);
		return false;
}

