//Checks browser 


var ie4 = (document.all.details) ? true : false;
var ns4 = (document.layers) ? true : false;
var ns6 = (document.getElementById && !document.all) ? true : false;

/* This is for preloading images, used in the preloadimages function */
var myimages = new Array();

var ie4 = (document.all) ? true : false;
var ns4 = (document.layers) ? true : false;
var ns6 = (document.getElementById && !document.all) ? true : false;


function hidelayer(lay) {
	if (ie4) {document.all[lay].style.visibility = "hidden";}
	if (ns4) {document.layers[lay].visibility = "hide";}
	if (ns6) {document.getElementById([lay]).style.display = "none";}
}

function showlayer(lay) {
	if (ie4) {document.all[lay].style.visibility = "visible";}
	if (ns4) {document.layers[lay].visibility = "show";}
	if (ns6) {document.getElementById([lay]).style.display = "block";}
}

function writetolayer(lay,txt) {
	if (ie4) {
		document.all[lay].innerHTML = txt;
	}
	if (ns4) {
		document[lay].document.write(txt);
		document[lay].document.close();
	}
	if (ns6) {
		over = document.getElementById([lay]);
		range = document.createRange();
		range.setStartBefore(over);
		domfrag = range.createContextualFragment(txt);
		while (over.hasChildNodes()) {
			over.removeChild(over.lastChild);
		}
		over.appendChild(domfrag);
   }
}
//this is a function for initializing stuff when the page loads
function init() {

}


function WM_netscapeCssFix() {
	if (document.WM.WM_netscapeCssFix.initWindowWidth != window.innerWidth || document.WM.WM_netscapeCssFix.initWindowHeight != window.innerHeight) {
		document.location = document.location;
  	}
}

function WM_netscapeCssFixCheckIn() {
	if ((navigator.appName == 'Netscape') && (parseInt(navigator.appVersion) == 4)) {
    	if (typeof document.WM == 'undefined'){
      		document.WM = new Object;
    	}
    	if (typeof document.WM.WM_scaleFont == 'undefined') {
      		document.WM.WM_netscapeCssFix = new Object;
      		document.WM.WM_netscapeCssFix.initWindowWidth = window.innerWidth;
      		document.WM.WM_netscapeCssFix.initWindowHeight = window.innerHeight;
    	}
    	window.onresize = WM_netscapeCssFix;
  	}
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function



// setting a cookie.
function setCookie(name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) +
  	((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  	((path == null) ? "" : "; path=" + path) +
  	((domain == null) ? "" : "; domain=" + domain) +
  	((secure == null) ? "" : "; secure");
}

// deleting a cookie.
function delCookie (name,path,domain) {
	if (getCookie(name)) {
    	document.cookie = name + "=" +
    	((path == null) ? "" : "; path=" + path) +
    	((domain == null) ? "" : "; domain=" + domain) + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function openWindow(url, name, w, h) {
	popupWin = window.open(url, name, 'scrollbars,menubar,width=' + w + ',height=' + h);
}


function ValidateExtraData(thisObject)
{
    var ThisElement, CompleteErrorString = "";
    var SelectElements = new Enumerator(thisObject.getElementsByTagName("SELECT"));
    for (;!SelectElements.atEnd();SelectElements.moveNext()) {
	    ThisElement = SelectElements.item();
        if (ThisElement.getAttribute("ErrorString")) {
        	if(ThisElement.options(ThisElement.selectedIndex).value == "") {
            	CompleteErrorString = CompleteErrorString + ThisElement.getAttribute("ErrorString") + "\r\n"; 
			}
        }
    }
    if (CompleteErrorString) {
    	alert(CompleteErrorString);
    	window.event.returnValue = false;
    }
}

function popupVideo(url, name, w, h) {
	popupWin = window.open("", name, 'width=' + w + ',height=' + h);
	popupWin.focus();
	popupWin.document.open();
	popupWin.document.write('<center><img src=\"'+url+'\"><P><form><input type=button value=Close onClick=\"javascript:self.close();\"></form>');
	popupWin.document.close();
}


function preloadimages(){
	for (i=0;i<preloadimages.arguments.length;i++){
		myimages[i] = new Image();
		myimages[i].src=preloadimages.arguments[i];
	}
}

	
function getCookieVal(name) {

	var nameLen = name.length;
	var cookieLen = document.cookie.length;
	var i = 0;
	var cookieEnd;
	
	while(i < cookieLen) {
		var j = i + nameLen;
		if(document.cookie.substring(i, j) == name) {
			cookieEnd = document.cookie.indexOf(";",j);
			if(cookieEnd == -1) {
				cookieEnd = document.cookie.length;
			}
			return unescape(document.cookie.substring(j, cookieEnd));
		}
		i++;
	}
	return "";
}


/* this fuction checks to see if the user is logged in or not. If not it will display */
/* a login box */

function checkLogin() {

    var username = String(getCookieVal('mashead_username')).substring(1,String(getCookieVal('mashead_username')).length);
    

    if(getCookieVal('mas_password') == "" || getCookieVal('mas_password') == '=') {
	html = '<table width="130" border=0 cellspacing=0 cellpadding=0>' +
  		  '<tr><td colspan="3"><img src="/images/login_top.gif" width="130" height="12"></td></tr>' +
  		  '<tr>' +
    		    '<TD bgcolor="#999999" width="1"><img src="/images/spacer.gif" height="15" width="1"></TD>' +
		    '<td width="128">' +
		    	'<form method="post" action="/cgi-bin/auth.cgi">' +
			'<img src="/images/spacer.gif" width="15" height="1"><b>Log in:</b><BR>' +
			'<img src="/images/spacer.gif" width="15" height="1"><font size="1">Username:</font><BR>' +
	    		'<img src="/images/spacer.gif" width="15" height="1"><input type="text" size="12" name="username" value="' + username + '"><BR>' +
	    		'<img src="/images/spacer.gif" width="15" height="1"><font size="1">Password:</font><br>' +
	    		'<img src="/images/spacer.gif" width="15" height="1"><input type="password" size="12" name="password" value=""><BR>' +
	    		'<img src="/images/spacer.gif" width="15" height="1"><input type="Submit" name="" value="Login"></form>' +
	    		'<div align="center"><a href="https://www.laserquipt.com/cgi-bin/adduser.cgi" class="leftbar">New Account</a></div></td>' +
    		    '<td bgcolor="#999999" width="1"><img src="/images/spacer.gif" width="1" height="20"></td>' +
  		  '</tr>' +
  		  '<tr><td colspan=3><img src="/images/login_bottom.gif" height="12" width="130"></td></tr>' +
		'</table>';
				
	document.write(html);
    }

}

function check_email(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	    return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	if (str.indexOf(at,(lat+1))!=-1){
	    return false
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	}

	if (str.indexOf(dot,(lat+2))==-1){
	    return false
	}
	
	if (str.indexOf(" ")!=-1){
	    return false
	}
    return true
}

//  check for valid numeric strings	
function isNumeric(strString) {
    var strValidChars = "0123456789.-";
    var strChar;
    var blnResult = true;

    if (strString.length == 0) return false;

    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++) {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1) {
            blnResult = false;
        }
    }
    return blnResult;
}

function item_rebate(rebate, storeprice, sku) {
    if(isNumeric(rebate)) {
        var final_price = 0;
	    final_price = storeprice - rebate;
	    document.write("<div align=left><font size=1>* <a target=new href=\"/cgi-bin/rebatecenter.cgi?sku=" +sku + "\" class=stda><font color=black size=1>Rebate details</font></a></font></div>");
	} else {
	    return 0;
	}
} 

function item_final_price(rebate, storeprice, sku) {
    if(isNumeric(rebate)) {
        var final_price = 0;
	    final_price = storeprice - rebate;
		final_price = CurrConv(final_price);
        document.write("<font face=Arial size=2><strike>" + storeprice + "</strike><BR> <a href=\"/cgi-bin/rebatecenter.cgi?sku=" + sku + "\" target=new class=stda>REBATE</a>: -$" + rebate + "<BR>Final Price: $" + final_price + "</font>");
	} else {
	    document.write(storeprice);
	}
}

function thumb_final_price(rebate, storeprice, sku) {
    if(isNumeric(rebate)) {
	    var final_price = 0;
		final_price = storeprice - rebate;
        final_price = CurrConv(final_price);
		document.write("<font face=Arial size=2><strike>Price: " + storeprice + "</strike><br><a target=new href=\"/cgi-bin/rebatecenter.cgi?sku=" + sku + "\" class=stda>REBATE</a>: -$" + rebate + "<BR>Final Price: <font size=3 color=#cc0000>$" + final_price + "</font></font>");
	} else {
	    document.write(storeprice);
	}
}

function clean_string(strString) {
    var strInvalidChars = "\n\r";
	var strChar = '';
	var strReturnString = "";
    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length; i++) {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1) {
            ;
		} else {
            strReturnString = strReturnString + strChar;
        }
    }
}

/* Converts string to currency values */
function CurrConv(num) {
    var con = ((Math.round(num*100)/100).toString()+'.00').split('.')
    return (con[0] + '.' + con[1] + (con[1].length < 2 ? '0' : '') )
}

// Pulls up a window with compatible printers in it //
function compats(strSKU) {
    var strURL = "/cgi-bin/compats.cgi?" + strSKU;
    window.open(strURL, '', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=375,height=300,left=200,top=200');
    return 1;
}

/* Resizes window to w and h */
function resizeOuterTo(w,h) {
 if (parseInt(navigator.appVersion)>3) {
   if (navigator.appName=="Netscape") {
    top.outerWidth=w;
    top.outerHeight=h;
   }
   else top.resizeTo(w,h);
 }
}


function waitPreloadPage() {
    if (document.getElementById) {
        document.getElementById('prepage').style.visibility='hidden';
    } else {
        if (document.layers) {
            document.prepage.visibility = 'hidden';
        } else { //ie4
            document.all.prepage.style.visibility = 'hidden';
        }
    }
}


