
var agt=navigator.userAgent.toLowerCase();
var is_ie =((agt.indexOf("msie 5.5\; windows nt 5.0")!= -1) || (agt.indexOf("msie 5.01\; windows nt 5.0")!= -1));  

function str_replace (c1, c2, s)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c == c1) { 
         	returnString += c2;
        } else {
        	returnString += c;
        }
    }

    return returnString;
}


function win2000() {
	if (is_ie == true) {
		window.open('/security.php3','newin','toolbar=yes,location=top,directories=0,status=0,resizable=yes,menubar=yes,scrollbars=yes,width=350,height=200');
	}
}

var selected_option;
function changeMenu(menuNum,subMenuName,formName,productNum,itemNum) {

	//mysubMenu = eval('document.' + formName + '.' + subMenuName);
	mysubMenu = document[formName][subMenuName];

	//alert (subMenuName);
	for (i=mysubMenu.options.length-1;i>0;i--){
		mysubMenu.options[i]=null
		}
		
	for (i=0;i<eval('menu'+productNum)[menuNum].length;i++){
		mysubMenu.options[i]=new Option(eval('menu'+productNum)[menuNum][i].text,eval('menu'+productNum)[menuNum][i].value)
		}
		
	mysubMenu.selectedIndex=0
	
}

function mymail(inaddr) {
	outaddr = inaddr.replace(/--at--/g,'@');
	outaddr = outaddr.replace(/--dot--/g,'.');
	document.location.href = 'mailto:'+outaddr;
}
	
function selectmenu(mySelect){
	
	//alert ('2selected option is ' + selected_option);
	formName = mySelect.form.name;
	//alert ('2 formname is '+formName);
	str_params = mySelect.options[mySelect.selectedIndex].value;
	params = str_params.split(',');
	selected_option = params[4].substr(0,params[4].indexOf(')'));
	if (mySelect.options[mySelect.selectedIndex].value == "null"){
		alert('Select a topMenu');
		}
	else{
		window.top.location.href = mySelect.options[mySelect.selectedIndex].value;
		mySelect.options[mySelect.selectedIndex].value = selected_option;
	}
		
	return formName;
}

function check_options(thisform) {

	var error_message = '';
	
	//alert("checking options for " + thisform);
	
	theForm = document.forms[thisform];
	
	for (formElementCounter = 0; formElementCounter < theForm.elements.length; formElementCounter++)
	{
		theElement = theForm.elements[formElementCounter];
		elementName = new String(theElement.name);
		elementID = new String(theElement.id);
		//alert("checking " + elementName);
		if (theElement.name.indexOf("*") == 0) { //a required attribute
			if (theElement.type == "select-one"){
				thisvalue = theElement.options[theElement.selectedIndex].value;
			} else {
				thisvalue = theElement.value;
			}
			
			//alert(elementName + "is required " + theElement.type + " value is *" +thisvalue+ "*");
			//alert (where_change);
			if ((thisvalue == '') || (thisvalue == 'null')){
				if ((elementID != '') && (elementID != 'undefined')) {
					error_message += elementID +"\n";
				} else {
					error_message += str_replace("_"," ",elementName)+"\n";
				}
				//alert(error_message);
			}
			
		}
	}
	
	if (error_message != '') {
		alert("Please select the following required options:"+"\n"+error_message);
		return false;
	} else {
		return true;
	}

}

function returnObjById( id ){
    if (document.getElementById)
        var returnVar = document.getElementById(id);
    else if (document.all)
        var returnVar = document.all[id];
    else if (document.layers)
        var returnVar = document.layers[id];
    return returnVar;
}

function getRadioLength( thisform ,radio_name){
   len = 0;
   theForm = thisform;

   for (formElementCounter = 0;formElementCounter < theForm.elements.length;formElementCounter++){
      theElement = theForm.elements[formElementCounter];
      if (theElement.type == "radio" && theElement.name == radio_name) { //a required attribute
          len++;
      }
   }
//   alert("radio length = "+len);
   return len;
}  

function getCheckboxLength( thisform ,checkbox_name){
//   alert("get Checkbox Length");
   len = 0;
   theForm = thisform;

   for (formElementCounter = 0;formElementCounter < theForm.elements.length;formElementCounter++){
      theElement = theForm.elements[formElementCounter];
      if (theElement.type == "checkbox" && theElement.name == checkbox_name) { //a required attribute
          len++;
      }
   }
   return len;
}


function getRadioPosition(thisform, radio_name){
//   alert("get Radio Position");
   radio_pos = 0;
   theForm = thisform;
   
   for (formElementCounter = 0;formElementCounter < theForm.elements.length;formElementCounter++){
      theElement = theForm.elements[formElementCounter];
      if (theElement.type == "radio" && theElement.name == radio_name) { //a required attribute
          radio_pos = formElementCounter;
      }
   }
//   alert("radio position = "+radio_pos);
   return radio_pos;
}

function getCheckboxPosition(thisform, checkbox_name){
//   alert("get Checkbox Position");
   checkbox_pos = 0;
   theForm = thisform;

   for (formElementCounter = 0;formElementCounter < theForm.elements.length;formElementCounter++){
      theElement = theForm.elements[formElementCounter];
      if (theElement.type == "checkbox" && theElement.name == checkbox_name) { //a required attribute
          checkbox_pos = formElementCounter;
      }
   }
   return checkbox_pos;
}


function getAllDistinctId(thisform){
   var myId = new Array();
   theForm = thisform;
  
   i=0;
   for (formElementCounter = 0;formElementCounter < theForm.elements.length;formElementCounter++){
      theElement = theForm.elements[formElementCounter];
      if (theElement.id.indexOf("*") == 0) { //a required attribute
	 myId[i] = theElement.id	
         i++;
      }
   }

   var myTempId = new Array();
   for(c in myId){
      myTempId[myId[c]] = myId[c];
   }

   return myTempId; 
} 

function setColor(control){
   if(control.style){
      if(control.value != control.defaultValue){
         control.style.backgroundColor = '#ffffcc';
         control.style.Color = '#ffffcc';
      }
   }
}


function isString(a) {
    return typeof a == 'string';
}

function validate_form(thisform){
   if(isString(thisform)){
      theForm = document.forms[thisform];
   }
   else{
      theForm = thisform;
   }   

   var error_message = '';

//   alert("Checking the required");
//   theForm = document.forms[thisform];

   theRequiredElement = theForm.elements['required'];

   if(theRequiredElement){
//      alert(theRequiredElement.value);
      requiredElt = theRequiredElement.value;
      var reqElement = requiredElt.split(",");
      for(i=0;i<reqElement.length;i++){
         theElement = theForm.elements[reqElement[i]];
         //alert("theElement = "+theElement.name);
		if (theElement) {
         //want to check the type of the element
         if(theElement.type == "checkbox"){
            checkboxid = theElement.id;
            checkboxname = theElement.name;
            checkboxlen = getCheckboxLength(theForm,checkboxname);
            checkboxpos = getCheckboxPosition(theForm,checkboxname);
            checkboxpos = checkboxpos-checkboxlen+1;

            ischeck = false;
            for(j=0; j<checkboxlen; j++){
               if(theForm.elements[checkboxpos].checked){
//                  alert("checkbox button at " +checkboxpos+ " is checked");
                  ischeck = true;
               }
               checkboxpos++;
            }
            if(ischeck == false){
               msg = checkboxid;
               if (!msg)
               	msg = checkboxname;
               msg = msg.replace("_"," ");
               error_message += msg + "\n";
               setColor(theElement);
            }
         }
         else if(theElement.type == "radio"){
            radioid = theElement.id;
            
            radioname = theElement.name;
//            alert("radio name"+radioname);
            radiolen = getRadioLength(theForm,radioname);
            radiopos = getRadioPosition(theForm,radioname);
            radiopos = radiopos-radiolen+1;
   //         alert("radio pos ="+radiopos);

            ischeck = false;
            for(j=0; j<radiolen; j++){
               if(theForm.elements[radiopos].checked){
//                  alert("radio button at " +radiopos+ " is checked");
                  ischeck = true;
               }
               radiopos++;
            }
            if(ischeck == false){
               msg = radioid;
               msg = msg.replace("_"," ");
               error_message += msg + "\n";
               setColor(theElement);
            }

         } else{	
				if(!theElement.value){
				   msg = theElement.id;
				   if (!msg)
					msg = theElement.name;
				   msg = msg.replace("_"," ");
				   error_message += msg + "\n";
	   //          setColor(theElement);
				}
			 }
         } 
      }

      if(error_message != ''){
         alert('Please enter the following required fields:\n'+error_message);
         return false;
      } else {
		mysession = theForm.elements['captcha'].value;
		if (mysession) {
			//alert('http://'+ document.domain + '/images-mm/mmprocess.php?ajax=1&captcha=' + mysession);
			response = loadXMLDoc('http://' + document.domain + '/images-mm/mmprocess.php?ajax=1&captcha=' + mysession );
		} else
			response = '';
		//alert('response' + response);
		if (response) {
			alert(response);
			return false;
		} else {
			return true;
		}
      }

   }
   else{
      return check_options2(theForm);
   }
}

      
function check_options2(thisform){
   var error_message = '';

//   alert("check_options2("+thisform+")");

   var DistinctId = new Array();
   DistinctId = getAllDistinctId(thisform);

   for(c in DistinctId){
      //alert("DistinctId = "+DistinctId[c]);
      theElement = returnObjById(DistinctId[c]);
      //alert("theElement = "+theElement.id);

      //want to check the type of the element
      if(theElement.type == "checkbox"){
	 	 checkboxid = theElement.id;
         checkboxname = theElement.name;
         checkboxlen = getCheckboxLength(thisform,checkboxname);
         checkboxpos = getCheckboxPosition(thisform,checkboxname);
         checkboxpos = checkboxpos-checkboxlen+1;

         ischeck = false;
         for(j=0; j<checkboxlen; j++){
            if(thisform.elements[checkboxpos].checked){
//               alert("checkbox button at " +checkboxpos+ " is checked");
               ischeck = true;
            }
            checkboxpos++;
         }
         if(ischeck == false){
            msg = checkboxid;
            if (!msg)
            	msg = checkboxname;
	    msg = msg.replace("*","");
            msg = msg.replace("_"," ");
            error_message += msg + "\n";
 	    setColor(theElement);
         }
      }
      else if(theElement.type == "radio"){
	 	 radioid = theElement.id;
         radioname = theElement.name;
         radiolen = getRadioLength(thisform,radioname);
         radiopos = getRadioPosition(thisform,radioname);
         radiopos = radiopos-radiolen+1;
//         alert("radio pos ="+radiopos);      

         ischeck = false;
         for(j=0; j<radiolen; j++){
            if(thisform.elements[radiopos].checked){
//               alert("radio button at " +radiopos+ " is checked");
               ischeck = true;
            }
            radiopos++;
         }
         if(ischeck == false){
            msg = radioid;
            if (!msg)
            	msg = radioname;
            msg = msg.replace("*","");
            msg = msg.replace("_"," ");
            error_message += msg + "\n";
	    setColor(theElement);
         }

      } else{
         if(!theElement.value){
            msg = theElement.id;
            if (!msg)
            	msg = theElement.name;
            msg = msg.replace("*","");
            msg = msg.replace("_"," ");
            error_message += msg + "\n";
//	    setColor(theElement);
         }
      }
   }
 
   if(error_message != ''){
      alert("required fields:\n"+error_message);
      return false;
   } else {
     return true;
   }

}

function toggleDiv(whichLayer, action) {

	if (document.getElementById) {
	 	//this is the way the standards work
		var divtag = document.getElementById(whichLayer);
	} else 
	if (document.all) {
		// this is the way old msie versions work
		var divtag = document.all[whichLayer];
	} else if (document.layers) {
		// this is the way nn4 works
		var divtag = document.layers[whichLayer];
	}
	
	style2 = divtag.style;

	if (!action) { //just toggle
		if (style2.display == "block") {
			//alert("1 style2.display "+style2.display);
			style2.display = "none";
		} else {
			//alert("2 style2.display "+style2.display);
			style2.display = "block";
		}
	} else { //show or hide
		if (action == 'show')
			style2.display = "block";
		else
			style2.display = "none";
	}
	
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function displayWindow(url, width, height) {
  var Win = window.open(url,"popupWindow",'width=' + width + ',height=' + height + ',resizable=1,scrollbars=yes');
  window.name='parent';

}

function updatecart_js(code,form) {
	
	_uacct=code;
	_udn="none";
	_ulink=1;
	__utmLinkPost(form);
	
}

function checkout_test(zip,country) {
	
	okay = true;
	if (check_simple_options()) { 
		if ((zip == '') || (country == '')) { 
			alert ('Please enter your Zip/Postal Code and Country Code before proceeding.'); 
			okay = false;
		}
	} else {
		okay = false;
	}
	
	return okay;
}

function check_simple_options() {

	okay = 1;
	
	count = 0;
	option1 = document.getElementById("option1_"+count);
	while (option1){
		value = option1[option1.selectedIndex].value;
		if (!value) {
			okay = 0;
			break;
		}
		count++;
		option1 = document.getElementById("option1_"+count);
	}

	if (okay) {
		count = 0;
		option2 = document.getElementById("option2_"+count);
		while (option2){
			value = option2[option2.selectedIndex].value;
			if (!value) {
				okay = 0;
				break;
			}
			count++;
			option2 = document.getElementById("option2_"+count);
		}
	}
	
	if (okay) {
		count = 0;
		option3 = document.getElementById("option3_"+count);
		while (option3){
			value = option3[option3.selectedIndex].value;
			if (!value) {
				okay = 0;
				break;
			}
			count++;
			option3 = document.getElementById("option3_"+count);
		}
	}
	
	if (okay) {
		return true;
	} else {
		alert("Some items have options that you have not selected. Please select options for all items.");
		return false;
	}
		
}

function loadXMLDoc(url) {
// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		//req.onreadystatechange = processReqChange;
		req.open("GET", url, false);
		req.send(null);
		return req.responseText;
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		isIE = true;
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			//req.onreadystatechange = processReqChange;
			req.open("GET", url, false);
			req.send(null);
     		return req.responseText;
		}
	}
}

function processReqChange() {
// only if req shows "loaded"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			//alert('okay');
			if (req.responseText) {
				alert('processreqchange');
				alert(req.responseText); // this is the returned data
				return false;
			} else {
				return true;
			}
		} else {
			//alert ("There was an error");
			return true;
		}
	}
}