//All scripts ©2002 Clearly Communications & James Haley unless overwise stated

var DOMerror = "Error: Sorry this this option is unavailable\nReason: Browser incompatibility"

//relative scripts

var valueArr = new Array
var textArr = new Array

	function listRelations (relateList, prodList, prodValues, addit, pos, prodId, typeList) {
		if (document.all) {	
		
			if (addit) {
				if (!theProducts.productList.disabled) {
					//Set vars
					newText = prodList[prodList.selectedIndex].text
					newValue = prodList.value
					newPos = prodList.selectedIndex
					
					if (newValue != "") {
						if (newText.indexOf("[added]") == -1) {
							//populate arrays with new values
							textArr.splice(textArr.length, 0, newText)
							valueArr.splice(valueArr.length, 0, newValue)
							
							//populate values field with selected values
							prodValues.value = valueArr
							
							//rite the list of selected products
							strHtml = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">"
								for (i=0; i<textArr.length; i++) {
									strHtml = strHtml + "<tr height=\"18\">"
									strHtml = strHtml + "<td>&nbsp;"
									strHtml = strHtml + textArr[i]
									strHtml = strHtml + "</td><td width=\"100\" align=\"right\"><a href=\"#\" onclick=\"listRelations('', '', '', false, " + i + "); return false;\">Remove&nbsp;</a></td>"
									strHtml = strHtml + "</tr>"						
								}
							strHtml = strHtml + "</table>"
							
							relateList.innerHTML = strHtml
							
							//update the list item to show it has already been added
							prodList[prodList.selectedIndex].text = prodList[prodList.selectedIndex].text + " [added]"
						} else {
							alert('That product has already been added  ')
						}
					} else {
						alert('Please select a valid product  ')
					}
				} else {
					alert('Please select a type  ')
				}
			} else {
				
				//remove selected item from the arrays
				textArr.splice(pos, 1)
				valueArr.splice(pos, 1)
				
				theProducts.relatives.value = valueArr
				
				//write the revised list of selected products
				strHtml = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">"
					for (i=0; i<textArr.length; i++) {
						strHtml = strHtml + "<tr height=\"18\">"
						strHtml = strHtml + "<td>&nbsp;"
						strHtml = strHtml + textArr[i]
						strHtml = strHtml + "</td><td width=\"100\" align=\"right\"><a href=\"#\" onclick=\"listRelations('', '', '', false, " + i + "); return false;\">Remove&nbsp;</a></td>"
						strHtml = strHtml + "</tr>"						
					}
				strHtml = strHtml + "</table>"
				
				if (valueArr.length == 0) {
					window.relatedList.innerHTML = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr height=\"18\"><td>&nbsp;</td></tr></table>"
				} else {
					window.relatedList.innerHTML = strHtml
				}
			}
			if (!theProducts.productList.disabled) {
				popDrop(theProducts.typeList, theProducts.productList, theProducts.relatives);
			}
		
		} else {
			alert(DOMerror)
		}
	}
	
	function clearAll() {
		if (document.all) {
			if (confirm('Are you sure you want to clear the list?')) {
				//Clear all arrays
				valueArr = new Array
				textArr = new Array
				posArr = new Array
			
				//clear and write default msg into list
				window.relatedList.innerHTML = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr height=\"18\"><td>&nbsp;</td></tr></table>"
				
				//refresh dropdown list
				if (!theProducts.productList.disabled) {
					for (j=2; j<theProducts.productList.length; j++) {
						if (theProducts.productList[j].text.indexOf("[added]") > -1) {
							theProducts.productList[j].text = theProducts.productList[j].text.replace(" [added]", "")
						}
					}
				}
				
				theProducts.relatives.value = ""
			}
			if (!theProducts.productList.disabled) {
				popDrop(theProducts.typeList, theProducts.productList, theProducts.relatives);
			}
		} else {
			alert(DOMerror)
		}
	}
	
	function rebuildthelist(lValues, lList) {
		var iCurLen,iIndex,iI=0,iJ=0,iCurLenTmp,arySAdd,sAddV,sAddT;
		iCurLen=aryType.length;
		
		for (iI=0;iI<iCurLen;iI++) {
			iCurLenTmp = aryType[iI].length
				for(iJ=0;iJ<iCurLenTmp;iJ++) {
					arySAdd=aryType[iI][iJ].split(":");
					sAddV=arySAdd[0]
					sAddT=arySAdd[1]
					if(lValues.indexOf(sAddV)>-1){	
						valueArr.splice(valueArr.length, 0, sAddV)
						textArr.splice(textArr.length, 0, sAddT)
					}
				}
		}
		
			strHtml = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">"
			for (i=0; i<textArr.length; i++) {
				strHtml = strHtml + "<tr height=\"18\">"
				strHtml = strHtml + "<td>&nbsp;"
				strHtml = strHtml + textArr[i]
				strHtml = strHtml + "</td><td width=\"100\" align=\"right\"><a href=\"#\" onclick=\"listRelations('', '', '', false, " + i + "); return false;\">Remove&nbsp;</a></td>"
				strHtml = strHtml + "</tr>"			
			}
			strHtml = strHtml + "</table>"
			
			window.relatedList.innerHTML = strHtml
			theProducts.relatives.value = valueArr
	}
	
	
	
	//populate drop boxes
	function popDrop(a,b,c) {
		var oTypeList=a,oTarget=b,iCurLen,iTargetL,iIndex,sAddT,sAddV,aryCurProd,iCurProdLen,iCountProd=0,iCount=0;oTarget.length=0;
		if (oTypeList.value != "") {
			iIndex=oTypeList.value-1;iCurLen=aryType[iIndex].length;aryCurProd=c.value.split(",");
		}
		
		oTarget.disabled=true;
		
		if (oTypeList.value != "") {
			for (iCount=0;iCount<iCurLen;iCount++){
				arySAdd=aryType[iIndex][iCount].split(":");
				sAddV=arySAdd[0]
				sAddT=arySAdd[1]
					for(iCountChk=0;iCountChk<aryCurProd.length;iCountChk++){
						if(aryCurProd[iCountChk] == sAddV){
							sAddT=sAddT+" [added]"
						}
					}
				iTargetL = oTarget.length;
				oTarget.options[iTargetL] = new Option();
				oTarget.options[iTargetL].text = sAddT;
				oTarget.options[iTargetL].value = sAddV;
			}
		
		
		oTarget.disabled=false;
		}
	}	

// end relative scripts

//Images scripts
	
	function previewImg(pane, path, imgName, field) {
		if (document.all) {
			pane.innerHTML = "<img src=" + escape(path) +"/"+ escape(imgName) +">";
			field.value = imgName;
		} else {
			alert(DOMerror)
		}
	}
	
	function clearPic(pane, field) {
		pane.innerHTML = ""
		field.value = "No Picture Selected"
	}
	
//end image scripts


//General scripts

//Open Browser window
	function openBrWindow(theURL,winName,features) {
		window.open(theURL,winName,features);
	}

//update quantities

	function updateQuantities(qFields) {
		//theFields = qFields.split(",")
		thelength = qFields.length
		
		for (i=0; i<thelength; i++) {
			if (qFields[i].value == "") {
				qFields[i].value = 1
			}
		}
	}
	
//Validation (C)2002-3 james haley // ver2
 var errorStr = ""

 var theargs
 function checkregform() {
 
 theargs = checkregform.arguments
 var errorStr = "The following fields are required:   \n\n"
 var errno = 0
 
	for (i=1; i<theargs.length; i++) {
		if (eval(theargs[0]+"."+theargs[i]+".value") == "") {
			errorStr += "  >  "+theargs[i]+"\n"
			errno--
		} else {
			errno++
		}
	}

	if (errno == theargs.length-1) {
		return true
	} else {
		alert(errorStr)
		errorStr=""
		return false
	}
}


//validation2

var errorStr = ""

	var theargs
	function jh_checkform() {
	
	theargs = jh_checkform.arguments;
	var errorStr = "Sorry - Some fields are required  \n\nPlease fill in the highlighted fields before trying to perform that action.    "
	
	var errno = 0
	
		for (i=2; i<theargs.length; i++) {
			if (eval(theargs[0]+"."+theargs[i]+".value") == "") {
				document.getElementById(theargs[i]).style.color = '#FF0000';
				document.getElementById(theargs[i]).style.fontWeight = 'Bold';
				errno--
			} else {
				document.getElementById(theargs[i]).style.color = '#000000';
				document.getElementById(theargs[i]).style.fontWeight = 'normal';
				errno++
			}
		}
	
		if (errno == theargs.length-2) {
			return true
		} else {
			alert(errorStr)
			errorStr=""
			return false
		}
	}


//add dropdown value to querystring

function addToQS(a,b) {
	var oTargetForm=a,oDrop=b,oDrop=eval(oTargetForm+"."+oDrop),bCheck;
	bcheck=(oDrop.value=="")?true:false; if(bcheck){alert('Please select a value from the dropdown'); return bcheck;}
	eval(oTargetForm).action=eval(oTargetForm).action+"&"+oDrop.name+"="+escape(oDrop.value);
	eval(oTargetForm).submit()
}

//switch image pane
function switchIPane(a){
	var oDrop=a; 
	eval("images"+sPrev).style.display='none';
	eval("images"+oDrop.value).style.display='block';
	sPrev = oDrop.value;
}

