var strChecked;
var GunModelCode = "";
var HoguePartNumber = "";

var g_strGunExists = "";

function CopyText(strText) {
	if (window.clipboardData)
		window.clipboardData.setData("Text", strText);
	else if (window.netscape) {
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
   		var clip = Components.classes['@mozilla.org/widget/clipboard;1']
			.createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;

		var trans = Components.classes['@mozilla.org/widget/transferable;1']
			.createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;

		trans.addDataFlavor('text/unicode');
		
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"]
			.createInstance(Components.interfaces.nsISupportsString);
		var copytext = strText;
   		str.data = copytext;
		trans.setTransferData("text/unicode", str, copytext.length * 2);
		var clipid=Components.interfaces.nsIClipboard;
		if (!clip) return false;
		clip.setData(trans, null, clipid.kGlobalClipboard);
	}
}

function CheckAll(strName, bValue, bFlagCheck) {
	for(i = 0; i < document.forms.length; i++)
		for (j = 0; j < document.forms[i].elements.length;j++) {
			elm = document.forms[i].elements[j];
			if (elm.type == "checkbox" && elm.name == strName)
				if (!bFlagCheck) elm.checked = bValue;
				else if (bFlagCheck && elm.checked) strChecked += elm.value + " ";
		}
}

function CheckAll2(strName, bValue) {
	var col;
	if (!document.all[strName]) return;
	if (document.all[strName].length) col =document.all[strName];
	else col = Array(document.all[strName]);
	if (col)
		for (i = 0; i < col.length; i++)
			col[i].checked = bValue;
}

function CheckAllCheckBox(bValue) {
	CheckAll("helpchkb", bValue);
	CheckAll("gunchkb", bValue);
	CheckAll("prodchkb", bValue);
	CheckAll("commonchkb", bValue);
}

function SubmitForm(strName, strValueName, bFlag) {
	strChecked = "";
	CheckAll(strName, null, true);
	eval("document.forms['apprform'].elements['" + strValueName + "'].value = '" + strChecked + "'");
	if (!bFlag) document.forms["apprform"].submit();
}

function SubmitAll() {
	SubmitForm('prodchkb', 'pid', true);
	SubmitForm('helpchkb', 'hid', true);
	SubmitForm('gunchkb', 'gid', true);
	document.forms["apprform"].submit();
}

function GM(iID, iManufId, iName, sProducts) {
  this.Id = iID;
  this.ManufId = iManufId;
  this.Name = iName;
  this.prod = sProducts;
}

function AddValue(ASelect,AText,AValue){ 
	n=ASelect.length++;
	ASelect.options[n].text  = AText;
	ASelect.options[n].value = AValue;     
}

function SetCookie(sName, sValue)
{
  document.cookie = sName + "=" + escape(sValue); 
}

function GetCookie(name) 
{ 
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
}

function getModels(what, nFlag) {
	var n = what.selectedIndex;
	var CurManufId = what.form.slManuf.options[n].value;
	var TheControl = what.form.slModel;
	var ModIndex = 0;
	TheControl.length = 0;
	var CountIdx=0;
	for (var i=0; i<aModels.length; i++) {
		if (aModels[i].ManufId == CurManufId) {
			AddValue(TheControl,aModels[i].Name,aModels[i].Id);
			if (aModels[i].Id == GunModelCode && GunModelCode != "")
				ModIndex=CountIdx;
			CountIdx++;  
		}
	}
	TheControl.selectedIndex = ModIndex;
	if (nFlag) getProducts(document.forms[0].slCategory, 1);
}

function InitializeProc(p1, p2) {
	GunModelCode = p1;
	HoguePartNumber = p2;
	var taCols = 92;
	var edAdrSize = 110;
	if (screen.width == 800) {
		taCols = 92;
		edAdrSize = 110;
	}
	if (screen.width == 640) {
		taCols = 70;
		edAdrSize = 90; 
	}
	if (screen.width == 1024) {
		taCols = 120;
		edAdrSize = 130;
	}

	document.frmHelpCall.taProblemNotes.cols = taCols;
	if (document.frmHelpCall.taResNotes) document.frmHelpCall.taResNotes.cols = taCols;
	document.frmHelpCall.edAddress.size = edAdrSize+4;
	document.frmHelpCall.edFirstName.size = edAdrSize/2;
	document.frmHelpCall.edLastName.size = edAdrSize/2;
	getModels(document.frmHelpCall.slManuf, 1);
	getProducts(document.frmHelpCall.slCategory, 1);
}

function GetSrchPage(what) {
	location.href = 'SrchResult.asp?chbGunSearch=Gun&slModel='+what.form.slModel.value+'&slManuf='+what.form.slManuf.value;
}

function GetProdSrchPage(what) {
	location.href = 'SrchResult.asp?chbGunSearch=Product&prodId='+what.form.slProduct.options[what.form.slProduct.selectedIndex].value;
}

function CheckAndSubmit(form) {
	if (document.frmHelpCall.slProduct.length < 1) {
		alert("Set the Gun Model name or Product name before saving the call");
		return;
	}
	var n = document.frmHelpCall.slModel.selectedIndex;
	var _Model = document.frmHelpCall.slModel.options[n].value;

	n = document.frmHelpCall.slProduct.selectedIndex;
	var pitem = document.frmHelpCall.slProduct.options[n];
	var _Product = pitem.value;

	if (_Model == "00" && g_strGunExists.indexOf(","+pitem.value+",") == -1) {
		alert("Set the Gun Model name before saving the call");
		return;
	}
	if (_Product == "0") {
		alert("Set the Product name before saving the call");
		return;
	}
	document.frmHelpCall.product_id.value = _Product;

	var objDest = document.frmHelpCall.HelpCallLinks;
	if (objDest)
		for(i = 0; i < objDest.options.length; i++)
			objDest.options[i].selected = true;
	document.frmHelpCall.submit();
}

function InitializeProc2() {
	getModels(document.HogueSearch.slManuf);
	getProducts(document.HogueSearch.slCategory);
	if (document.ProbSearch) getModels(document.ProbSearch.slManuf);
	ProdToEdit(document.HogueSearch.slProduct);
}

function getProducts_(what) {
	getProducts(what)
	ProdToEdit(document.HogueSearch.slProduct)
}

function ProdToEdit(what) {
	var n = what.selectedIndex;
	if (n != -1) what.form.edProduct.value = what.options[n].text;
	else what.form.edProduct.value = ''; 
}

function AddButtonClick() {
	var objProduct = document.frmHelpCall.slProduct;
	var objModel = document.frmHelpCall.slModel;
	var objDest = document.frmHelpCall.HelpCallLinks;
	
	var pitem = objProduct.options[objProduct.selectedIndex];
	var strModelCode = objModel.options[objModel.selectedIndex].value;

	strModelName = "";
	if (strModelCode == "00" && g_strGunExists.indexOf(","+pitem.value+",") == -1) {
		alert("Set the Gun Model name before saving the call");
		return;
	}
	if (g_strGunExists.indexOf(","+pitem.value+",") == -1 || strModelCode != "00") strModelName = objModel.options[objModel.selectedIndex].text + " - ";
	if (pitem.value == "0") {
		alert("Set the Product name before saving the call");
		return;
	}
	AddValue(objDest, strModelName +pitem.text, strModelCode + "," + pitem.value);
}

function DeleteButtonClick() {
	var objDest = document.frmHelpCall.HelpCallLinks;
	if (objDest.selectedIndex != -1) {
		objDest.options[objDest.selectedIndex] = null;
	}
}

function IsAllowProductID(objModel, nProductID) {
	bResult = (objModel.prod.indexOf("," + nProductID.toString() + ",") != -1)
	return bResult; 
}

function HogueCategory(nCategoryID, strProductID, strProductCode) {
	this.id = nCategoryID;
	this.products_id = strProductID;
	this.products_code = strProductCode;
}

function getProducts(what, nFlag) {
	var n = what.selectedIndex;
	var CurCatId = what.options[n].value;
	var TheControl = document.forms[0].slProduct;
	TheControl.length = 0;
	var ProdIndex = 0;
	var CountIdx=0;
	
	if (nFlag == 1) {
		var obj = document.forms[0].slModel;
		if (obj.options.length == 0) return;
		var e = obj.options[obj.selectedIndex];
		nIndex = -1;
		for(i=0; i < aModels.length;i++)
			if (aModels[i].Id == e.value && aModels[i].ManufId == document.forms[0].slManuf.options[document.forms[0].slManuf.selectedIndex].value) {
				nIndex = i;
				break;
			}
		if (nIndex == -1) return;
	}

	for (i=0;i<g_aCategories.length;i++)
		if (CurCatId == g_aCategories[i].id) {
			var arrPID = g_aCategories[i].products_id.split(",");
			var arrPCode = g_aCategories[i].products_code.split(",");
			for(j=0;j<arrPID.length;j++) {
				nGunExistsFlag = (g_strGunExists.indexOf(","+arrPID[j]+",") == -1) ? 1: 0;
				bResult = (nFlag != 1);
				if (!bResult) bResult = (nGunExistsFlag != 1 || IsAllowProductID(aModels[nIndex], arrPID[j]));
				if (bResult && arrPCode[j] != "") {
					AddValue(TheControl,arrPCode[j],arrPID[j]);
					if (arrPID[j] ==HoguePartNumber && HoguePartNumber != "")
						ProdIndex=CountIdx;
				}
			}
			break;
		}
	TheControl.selectedIndex = ProdIndex;
}