

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.01
  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 && d.getElementById) x=d.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 changeCase(frmObj) {
	var index;
	var tmpStr;
	var tmpChar;
	var preString;
	var postString;
	var strlen;
	tmpStr = frmObj.value.toLowerCase();
	strLen = tmpStr.length;
	if (strLen > 0)  {
	for (index = 0; index < strLen; index++)  {
	if (index == 0)  {
	tmpChar = tmpStr.substring(0,1).toUpperCase();
	postString = tmpStr.substring(1,strLen);
	tmpStr = tmpChar + postString;
	}
	else {
	tmpChar = tmpStr.substring(index, index+1);
	if (tmpChar == " " && index < (strLen-1))  {
	tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
	preString = tmpStr.substring(0, index+1);
	postString = tmpStr.substring(index+2,strLen);
	tmpStr = preString + tmpChar + postString;
	         }
	      }
	   }
	}
	frmObj.value = tmpStr;
}

//**************************************************************************************************************************
function ignoreSpaces(string,dlmtr) {
	var temp = "";
	string = '' + string;
	splitstring = string.split(dlmtr);
	for(i = 0; i < splitstring.length; i++)
	temp += splitstring[i];
	return temp;
}

//**************************************************************************************************************************
function trimLeadingSpaces(field){
	while(''+field.value.charAt(0)==' ') 		
		field.value=field.value.substring(1,field.value.length);
}



//**************************************************************************************************************************
function checkBrowser(NSvers,NSpass,NSnoPass,IEvers,IEpass,IEnoPass,OBpass,URL,altURL) { //v4.0
  var newURL='', verStr=navigator.appVersion, app=navigator.appName, version = parseFloat(verStr);
  if (app.indexOf('Netscape') != -1) {
    if (version >= NSvers) {if (NSpass>0) newURL=(NSpass==1)?URL:altURL;}
    else {if (NSnoPass>0) newURL=(NSnoPass==1)?URL:altURL;}
  } else if (app.indexOf('Microsoft') != -1) {
    if (version >= IEvers || verStr.indexOf(IEvers) != -1)
     {if (IEpass>0) newURL=(IEpass==1)?URL:altURL;}
    else {if (IEnoPass>0) newURL=(IEnoPass==1)?URL:altURL;}
  } else if (OBpass>0) newURL=(OBpass==1)?URL:altURL;
  if (newURL) { alert(newURL); document.MM_returnValue=false; }
}



//**************************************************************************************************************************
function CharCheck(field) {
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-,'.& ";
  var checkStr = field.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    return (false);
  }
  
  return true;
}


//**************************************************************************************************************************
function NumCheck(field) {
  var checkOK = "0123456789";
  var checkStr = field.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    return (false);
  }
  
  return true;
}






//**************************************************************************************************************************
function emailCheck (emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
		
	if (matchArray==null) {
	//alert("Email address seems incorrect (check @ and .'s)");
	return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
		
		
	for (i=0; i<user.length; i++) {
	if (user.charCodeAt(i)>127) {
	//alert("Ths username in email address contains invalid characters.");
	return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
	if (domain.charCodeAt(i)>127) {
	//alert("Ths domain name in email address contains invalid characters.");
	return false;
	   }
	}
	if (user.match(userPat)==null) {
	//alert("The username in email address doesn't seem to be valid.");
	return false;
	}
		
		
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		
		
	for (var i=1;i<=4;i++) {
	if (IPArray[i]>255) {
	//alert("Destination IP address in email is invalid!");
	return false;
	   }
	}
	return true;
	}
		
		 
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
	if (domArr[i].search(atomPat)==-1) {
	//alert("The domain name in email address does not seem to be valid.");
	return false;
	   }
	}
		
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
	//alert("The email address must end in a well-known domain or two letter " + "country.");
	return false;
	}
		
		
	if (len<2) {
	//alert("The email address is missing a hostname!");
	return false;
	}
		
	return true;
}


//**************************************************************************************************************************
function changeText(bt,over) {
	if (over) 
		bt.style.color="#ff0000";
	else
		bt.style.color="#000080";
}


//**************************************************************************************************************************
function showDlg(winDlg, DlgName, H, W) {
	// Dialog URL
	var strPage = winDlg;
	strAttr = "status:no;dialogWidth:"+ W +"px;dialogHeight:"+ H + "px;help:no";' '

	// run the dialog that implements this type of element
	var strURL = showModalDialog(strPage, DlgName, strAttr);
	strURL.focus();
 
}

//**************************************************************************************************************************
function showPopup(winDlg, DlgName, H, W) {
  var mW = (window.screen.width - W)/2;
  var mH = (window.screen.height - H)/2;
  if (DlgName == 'Img') {
	mH = mH-100;
	}
  
  fs2 = window.open('', DlgName, 'width='+W+',height='+H+',scrollbars=0,location=0,status=0,toolbar=0,resizable=1,titlebar=0,left='+mW+',top='+mH);
  if (fs2 != null) {
    if (fs2.opener == null) {
      fs2.opener = self;
      self.location.href=document.location;
   }
   
   fs2.location.href = winDlg;
   fs2.focus();
  
  }
	fs2.creator=self; 
}


//**************************************************************************************************************************
function button_over(eButton)	{
	eButton.style.backgroundColor = "#B5BDD6";
	eButton.style.border = "darkblue 1px solid";
}

//**************************************************************************************************************************
function button_out(eButton) {
	eButton.style.backgroundColor = "buttonface";
	eButton.style.border = "buttonface 1px solid";
}

//**************************************************************************************************************************
function button_down(eButton) {
	eButton.style.backgroundColor = "#8494B5";
	eButton.style.border = "darkblue 1px solid";
}

//**************************************************************************************************************************
function button_up(eButton) {
	eButton.style.backgroundColor = "#B5BDD6";
	eButton.style.border = "darkblue 1px solid";
	eButton = null; 
}


//**************************************************************************************************************************
function winopen(URL){
	var targeturl=URL;
	newwin=window.open("","MAIN","scrollbars=0, resizable=0,toolbar=0,top=0,left=0")
	if (document.all)	{
		newwin.resizeTo(screen.width,screen.height-27)
	//	newwin.resizeTo(1024, 764)
	}
	newwin.location=targeturl
}



//**************************************************************************************************************************
function FormatPhone (obj) {
	var str = obj.value;
	var strOut;
	if (str.length == 10) {
		if (isNaN(str)) {
		alert('Please enter numeric characters.');
		obj.select();
		obj.focus();
		}
		else {
			strOut = '('+str.substring(0,3)+')'+str.substring(3,6)+'-'+str.substring(6,10);
			obj.value = strOut;
		}
	}
}







//**************************************************************************************************************************
function CheckField(obj) {
	if (obj.value == '') {
		alert(obj.name+' is missing, enter ' + obj.name + '.');
		obj.focus();
		return false;
	}

	return true;
}



//**************************************************************************************************************************
function FormatCurrency(number) {
    var x = (Math.round(number*100))/100;
    return (x == Math.floor(x)) ? x + '.00' : ((x*10 == Math.floor(x*10)) ? x + '0' : x);
}


//**************************************************************************************************************************
function round(number,X) {
// rounds number to X decimal places, defaults to 2
    X = (!X ? 2 : X);
    return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

//**************************************************************************************************************************
function replace(originalString,searchText,replaceText) { 
	var strLength = originalString.length; 
	var txtLength = searchText.length; 
	if ((strLength == 0) || (txtLength == 0)) 
	{ return originalString; } 
	var i = originalString.indexOf(searchText); 
	if ((!i) && (searchText != originalString.substring(0,txtLength))) 
	{ return originalString; } 
	if (i == -1) 
	{ return originalString; } 
	var newstr = originalString.substring(0,i) + replaceText; 
	if (i+txtLength < strLength) 
	{ newstr += replace(originalString.substring(i+txtLength,strLength),searchText,replaceText); } 
	return newstr;
} 



//**************************************************************************************************************************
function isDate(dateStr) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
alert("Please enter date as either mm/dd/yy or mm-dd-yy.");
return false;
}

month = matchArray[1]; // p@rse date into variables
day = matchArray[3];
year = matchArray[5];

if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}

if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn`t have 31 days!")
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("February " + year + " doesn`t have " + day + " days!");
return false;
}
}
return true; // date is valid
}

//**************************************************************************************************************************
function ChangeTab(id, tabs) {
	for (var i=1;i<tabs;i++) {
		eval('tab'+i).style.background = '#cccccc';
		eval('tab'+i).style.fontWeight = 'normal';
		eval('tab'+i).style.color = '#000000';
		eval('frame'+i).style.display = "none";
		
	}
	eval('tab'+id).style.background = '#778899';
	eval('tab'+id).style.fontWeight = 'bold';
	eval('tab'+id).style.color = '#ffffff';
	
	eval('frame'+id).style.display = "block";
	
}

//**************************************************************************************************************************
function btClicked(selcell) {
	for (var i=1;i<10;i++) {
		var obj = document.getElementById('bt'+i);
		if (obj) {
			obj.className = 'btOut';
		}
	}

	for (var i=1;i<32;i++) {
		var obj = document.getElementById('cell'+i);
		if (obj) {
			obj.className = 'cellOut';
		}
	}
	document.frm.sel.value = selcell.id;
	selcell.className = 'cellOver';
}

//**************************************************************************************************************************
function mmClick(selcell, url) {
	for (var i=1;i<10;i++) {
		var obj = document.getElementById('bt'+i);
		if (obj) {
			obj.className = 'btOut';
		}
	}
	document.frm.sel.value = selcell.id;
	selcell.className = 'btOver';
	parent.content.location.href = url;
	
}


//**************************************************************************************************************************
function swap(Cell, Class) {
	var sel = document.frm.sel.value;
	if (sel != Cell.id) {
		//alert (Cell.id);
		Cell.className=Class;
	}
}


//***********************************************/
// functions for displaying larger images...
function getposOffset(overlay, offsettype){
var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
var parentEl=overlay.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function overlay(curobj, subobj, offset){
if (document.getElementById){
var subobj=document.getElementById(subobj)
subobj.style.left=getposOffset(curobj, "left")+offset+"px"
subobj.style.top=getposOffset(curobj, "top")+offset+"px"
subobj.style.display="block"
return false
}
else
return true
}

function overlayclose(subobj){
document.getElementById(subobj).style.display="none"
}
//***********************************************/





//functions for calculing sub total
function Calc(i) {	
	var frm = document.frm;
	var SellPrice = frm.SellPrice[i].value;
	var Qty = frm.Qty[i].value;
	var Instock = parseInt(frm.Instock[i].value);
	var LineAmt = 0;
	
	if ((SellPrice != "") && (Qty != "")) {
		if ((!isNaN(SellPrice)) && (!isNaN(Qty))) {
			if (parseInt(Qty) > Instock) {
				alert('Order quantity is greater than available quantity instock. \nPlease adjust the order quantity entered.');
				frm.LineAmt[i].value = FormatCurrency(SellPrice*Instock);
				frm.Qty[i].value = Instock;
				frm.Qty[i].focus();
				frm.Qty[i].select();
			}
			else {
				LineAmt = SellPrice*(Qty);
				frm.LineAmt[i].value = FormatCurrency(LineAmt);
			}
			CalcTotal();
		}
		else {
			alert ('Please enter numeric characters for the quantity field.');
			frm.Qty[i].value = '';
			frm.Qty[i].focus();
			frm.Qty[i].select();
		}
	}
	else {
		//frm.Qty[i].value = 0;
		frm.LineAmt[i].value = 0;
	}
}

//functions for check field if empty
function Check(i) {	
	var frm = document.frm;

	if (frm.Qty[i].value == "") {
		frm.Qty[i].value = 0;
	}

}




//**************************************************************************************************************************
function CalcTotal() {
	var frm = document.frm;
	var SubTotal = 0;
			
	for (i=0; i < frm.LineAmt.length; i++) {
		if (frm.LineAmt[i].value != "") 
			SubTotal = FormatCurrency((parseFloat(SubTotal) + parseFloat(frm.LineAmt[i].value)));
	}

	frm.SubTotal.value =  FormatCurrency(SubTotal);
}


//**************************************************************************************************************************
function Checkout() {
	var frm = document.frm;
	var SubTotal = frm.SubTotal.value;
	if (SubTotal == '0') {
		alert ('No order quantity was entered. Please enter quantity for the item you want to buy.');
		return false;
	}
	return true;
	
}

