
var submitcount=0;

//This changes the state of the mouse to indicate a clicking option
function MouseOver(src, state) {

	var browserName=navigator.appName; 
	if (browserName=="Netscape")
	{
	 	src.style.cursor = (state) ? 'pointer' : 'default';
	}
	else 
	{ 
	  	src.style.cursor = (state) ? 'hand' : 'default';
	}

}

function Redirect(URL){
	window.location = URL;
}

function checkSubmit(form) {

	//alert('fff');
	//alert('form:' + form);
	if (submitcount == 0)
	      {submitcount++;
	      //alert(document.forms[form].name);
		  document.forms[form].submit();
		  }
}

function isEmailAddr(email){
  var result = false;
  if (email.length > 3) {
  	var theStr = new String(email);
  	var index = theStr.indexOf("@");
  	if (index > 0){
    	var pindex = theStr.indexOf(".",index);
    	if ((pindex > index+1) && (theStr.length > pindex+1))
			result = true;
  	}
  }
  return result;
}


function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);





//Matrix functions

function decodeQuote(str){
	var re = new RegExp ('&quot;', 'gi') ;
	return str.replace(re, '"') ;
}

function lTrim(str){
	for (var i=0; str.charAt(i)==" "; i++);
	return str.substring(i,str.length);
}

function rTrim(str){
	for (var i=str.length-1; str.charAt(i)==" "; i--);
	return str.substring(0,i+1);
}

function trim(str){
	return lTrim(rTrim(str));
}

function checkSelection(){
	if(!canContinue())
		return false;
	if(document.SearchData == null)
		return;
	form = 'document.SearchData.elements';
	for(var i = 0; options.length > i; i++){
		selectElement = eval(form + '["' + optionNames[i] + '"]');
		
		if(selectElement == null) 
			return;
		
		formElement = eval(selectElement).options[eval(selectElement).selectedIndex].text;

		if(formElement == options[i]){
			alert("We're sorry, we need more information to add this item to your cart. Please make sure that you select from the available MatrixSelection and then add the item to your cart. Thank You.");
			return false;
		}
	}
	
	return true;
}

function SetProductOptions(optionSelected, optionSelectedIndex, optionValue, optionIndex){
	
	//if select1 is empty then move it to the next selection
	if(document.SearchData.Select1.options[document.SearchData.Select1.selectedIndex].value == " "){
		document.SearchData.Select1.selectedIndex = document.SearchData.Select1.selectedIndex + 1;
	}
	
	if(document.SearchData == null)
		return;
	if(options == null)
		return;
	if(MatrixSelection == null)
		return;
	if(MatrixSelection.length <= 0)
		return;
	
	form = 'document.SearchData.elements';
	if(MatrixSelection.length <= 0 && options != null){
		for(k=0; options.length > k; k++){
			selectElement = eval("document.SearchData.elements" + '["'+ optionNames[k] + '"]');
			eval(selectElement).options.length = 1;
			eval(selectElement).options[0].text = decodeQuote(options[k]);
		}
	return;
	}
	
	if(MatrixSelection == null || MatrixSelection.length <= 0)
		return;
		
	if(options == null || options.length <= 0)
		return;
	
	for(var j=0; MatrixSelection.length > j; j++){
		re = new RegExp ('~', 'gi') ;
		for(i=0; i < options.length; i++){
			
			strChoice = eval("MatrixSelection[" + j + "].option" + i );
			if(strChoice != null){
				strChoice.replace(re, '"') ;
				choiceElement = eval("MatrixSelection[" + j + "].option" + i );
				
				choiceElement = strChoice;
			}
		}
	}
	
	optionIndex = null;
	if(optionSelected != null){
		for(i=0; i < options.length; i++){
			if(optionSelected == optionNames[i]){
				optionIndex = i;
			}
		}
	}

	MatrixSelectionMatched = new Array();
	showMatrixSelection = new Array();
	MatrixSelectionKeyMatched = new Array();
	showMatrixSelectionKey = new Array();
				
	for(var i = 0; options.length > i; i++){
		select = new Array();
		selectKeys = new Array();
		selectPrices = new Array();
		selectHold = new Array();

		select[0] = options[i];
		selectKeys[0] = " ";
		selectHold[0] = " ";
		var l = 1;
		var j = 0;
		var bAdded = false;
	
		if(i == 0){
		
			while(MatrixSelection.length > j){
				
				for(var k=0; select.length > k; k++){
					bAdded = false;

					if(eval("MatrixSelection["+ j + "].option" + i ) == select[k]){
						bAdded = true;
						break;
					}
				}
				
				if(bAdded == false){
					selectHold[l] = eval("MatrixSelection[" + j + "].option" + i );
					
					if (selectHold[l] != null) {
						intIndex = selectHold[l].indexOf("amp;");
						intAmp = selectHold[l].indexOf("&amp;");
					}else{
						intAmp = -1;
					}
					
					
					if (intAmp >= 0) {
						firstSub = selectHold[l].substring(0,intIndex);
						incrementor = selectHold[l].indexOf(";");
						incrementor = incrementor + 1;
						secondSub = selectHold[l].substring(incrementor,selectHold[l].length);
						newString = (firstSub + secondSub);
						select[l] = newString;
						selectKeys[l] = eval("MatrixSelectionKey[" + j + "].option" + i);
					}
					else{
						select[l] = eval("MatrixSelection[" + j + "].option" + i );
						selectKeys[l] = eval("MatrixSelectionKey[" + j + "].option" + i);
							
					}
					
					l++;
				}
				
				j++;
				
			}
			
			
			
		}else{
		
		
									
	//***********************************************
	//return;
	//***********************************************
	




		
			MatrixSelectionMatched = MatrixSelection;
			MatrixSelectionKeyMatched = MatrixSelectionKey;
			
			skipMatch = false;
			for(var j=0; j < i; j++){
				if(j > optionIndex && optionIndex != 0){
					formElement = options[j];
				}else{
					selectElement = eval(form + '["' + optionNames[j] + '"]');
					formElement = eval(selectElement).options[eval(selectElement).selectedIndex].text;
					formElement = trim(formElement);
				}
				
				if(formElement == options[j]){
					skipMatch = true;
				}else{
					skipMatch = false;
				}
				
				var m = 0;
				var n = 0;
				showMatrixSelection = new Array();
				showMatrixSelectionKey = new Array();
				
				while(MatrixSelectionMatched.length > m){
					if(trim(formElement) == decodeQuote(trim(eval("MatrixSelectionMatched[" + m + "].option" + j))) || skipMatch){
						showMatrixSelection[n] = MatrixSelectionMatched[m];
						showMatrixSelectionKey[n] = MatrixSelectionKeyMatched[m];
						n++;
					}
					m++;
				}
				
				MatrixSelectionMatched = showMatrixSelection;
				MatrixSelectionKeyMatched = showMatrixSelectionKey;
			}
			
			m = 0;
			
			while(MatrixSelectionMatched.length > m){
				bAdded = false;
				for(var k=0; select.length > k; k++){
					if(eval("MatrixSelectionMatched["+ m + "]" + ".option" + i) == select[k]){
						bAdded = true;
						break;
					}
				}
				
				if(bAdded == false){
					select[l] = eval("MatrixSelectionMatched[" + m + "]" + ".option" + i);
					selectKeys[l] = eval("MatrixSelectionKeyMatched[" + m + "]" + ".option" + i);
					l++;
				}
				m++;
			}
		}

		for(var j=0; j < selectKeys.length - 1; j++){
			for(var k=j+1; k < selectKeys.length; k++){
				if(selectKeys[k] < selectKeys[j]){
					var tempSelectKey = selectKeys[j];
					selectKeys[j] = selectKeys[k];
					selectKeys[k] = tempSelectKey;
					var tempSelect = select[j];
					select[j] = select[k];
					select[k] = tempSelect;
				}
			}
		}
						

		if(select.length <= 0){
			alert("This product is not available with selected options");
			return;
		}else{
			selectElement = eval("document.SearchData.elements" + '["'+ optionNames[i] + '"]');
			
			curSelectLength = selectElement.length;
			curSelectionValue = selectElement.selectedIndex;
			curSelectionText = eval(selectElement).options[curSelectionValue].text;
			eval(selectElement).options.length = select.length;
			selectElement.options[0].selected = true;
			bEarlierSelectionFound = false;
			
			for(var k=0; select.length > k; k++){
				
				eval(selectElement).options[k].text = decodeQuote(select[k]);
				eval(selectElement).options[k].value = selectKeys[k];
				
				//= strText.replace(re, '') ;
				//eval(selectElement).options[k].text = eval(selectElement).options[k].text.replace("@@","'");
				//eval(selectElement).options[k].value = eval(selectElement).options[k].text;
				
				//alert(select[k] + " | " + selectKeys[k]);
				
				//eval(selectElement).options[k].text = 'eeee';
				
				if(trim(eval(selectElement).options[k].text).length == trim(curSelectionText).length){
					if(trim(eval(selectElement).options[k].text) == trim(curSelectionText)){
						eval(selectElement).options[k].selected = true;
						bEarlierSelectionFound = true;
					}
				}
				
				
			}

					
			//888888888888888888888888888888888888888888888888
			//return;
			//888888888888888888888888888888888888888888888888
			
			
			if((bEarlierSelectionFound == false) && (optionSelected != null) && (i > optionIndex)){
				selectElement.options[0].selected = true;
			}
		}
		
	}
		
	SetProductID();
	
}

function SetOption(optionName){
	var jj= -1;
	for (var ii = 0; ii < options.length; ii++){
		if(optionName == options[ii]){
			jj = ii;
		}
	}
	
	var retvalue = "option"+jj;
	
	return retvalue;
}



//End Matrix functions
