// JavaScript Document

function popupBox( sCigarID ){

var w = 800, h = 600;

if (document.all || document.layers) {
   w = screen.availWidth;
   h = screen.availHeight;
}

var popW = 700, popH = 600;

var leftPos = (w-popW)/2, topPos = (h-popH)/2;

var oWin;
	oWin = window.open('cigarBox.asp?cigar=' + sCigarID, 'cigarBox','width=' + popW + ',height=' + popH + ',resizable=no,toolbar=no,scrollbars=yes,status=no,directories=no,menubar=no,,top=' + topPos + ',left=' + leftPos + ',screenX=' + topPos + ',screenY=' + leftPos);
	
	if( oWin != void(0) ) oWin.focus();
}

function popupSize( sCigarID, iSize ){

var w = 800, h = 600;

if (document.all || document.layers) {
   w = screen.availWidth;
   h = screen.availHeight;
}

var popW = ( iSize == '' ? 700 : 440 ), popH = 470;

var leftPos = (w-popW)/2, topPos = (h-popH)/2;

var oWin;
	oWin = window.open('cigarSize.asp?cigar=' + sCigarID + '&size=' + iSize, 'cigarBox','width=' + popW + ',height=' + popH + ',resizable=no,toolbar=no,scrollbars=yes,status=no,directories=no,menubar=no,,top=' + topPos + ',left=' + leftPos + ',screenX=' + topPos + ',screenY=' + leftPos);
	
	if( oWin != void(0) ) oWin.focus();
}

function popupAccessory( sAccessoryID ){

var w = 800, h = 600;

if (document.all || document.layers) {
   w = screen.availWidth;
   h = screen.availHeight;
}

var popW = 700, popH = 600;

var leftPos = (w-popW)/2, topPos = (h-popH)/2;

var oWin;
	oWin = window.open('popupAccessory.asp?accessory=' + sAccessoryID, 'Accessory','width=' + popW + ',height=' + popH + ',resizable=no,toolbar=no,scrollbars=yes,status=no,directories=no,menubar=no,,top=' + topPos + ',left=' + leftPos + ',screenX=' + topPos + ',screenY=' + leftPos);
	
	if( oWin != void(0) ) oWin.focus();
}

function popupItem( sID, sItem ){

var w = 800, h = 600;

if (document.all || document.layers) {
   w = screen.availWidth;
   h = screen.availHeight;
}

var popW = ( sItem == '' ? 700 : 440 ), popH = 450;

var leftPos = (w-popW)/2, topPos = (h-popH)/2;

var oWin;
	oWin = window.open('accessory_item.asp?accessory=' + sID + '&item=' + sItem, 'cigarBox','width=' + popW + ',height=' + popH + ',resizable=no,toolbar=no,scrollbars=yes,status=no,directories=no,menubar=no,,top=' + topPos + ',left=' + leftPos + ',screenX=' + topPos + ',screenY=' + leftPos);
	
	if( oWin != void(0) ) oWin.focus();
}

Math.formatDecimals = function (num, digits) {
        //if no decimal places needed, we're done
        if (digits <= 0) {
                return Math.round(num); 
        } 
        //round the number to specified decimal places
        //e.g. 12.3456 to 3 digits (12.346) -> mult. by 1000, round, div. by 1000
        var tenToPower = Math.pow(10, digits);
        var cropped = String(Math.round(num * tenToPower) / tenToPower);

        //add decimal point if missing
        if (cropped.indexOf(".") == -1) {
                cropped += ".0";  //e.g. 5 -> 5.0 (at least one zero is needed)
        }

        //finally, force correct number of zeroes; add some if necessary
        var halves = cropped.split("."); //grab numbers to the right of the decimal
        //compare digits in right half of string to digits wanted
        var zerosNeeded = digits - halves[1].length; //number of zeros to add
        for (var i=1; i <= zerosNeeded; i++) {
                cropped += "0";
        }
        return(cropped);
}

Date.parseDMY = function( vsStr ){
		var laDate = ( "" + vsStr ).split( "/" );
		return ( laDate.length == 3
				 ? new Date( Date.parse( laDate[1] + "/" + laDate[0] + "/" + laDate[2] ))
				 : null );
	}

String.prototype.isEmail = function(){
	if( this == "" ) return false;
	
	var indexOfAt	= this.indexOf("@");
	var indexOfDot	= this.indexOf(".");
	
	var foundAt		= ( indexOfAt > 0 );
	var foundDot	= ( indexOfDot > 0 && indexOfDot < ( this.length - 1 ));
	return ( foundAt && foundDot );
}

function sortColumn( iValue ){
	document.forms[0].hdSort.value = iValue;
	document.forms[0].submit();
}

function jumpPage( iPage ){
	document.all.txtPage.value = iPage;
	goPage();
}

function goPage( iDirection ){
	with( document.forms[0] ){
		var iGoPage = txtPage.value;
			
		if( iGoPage == "" || isNaN( iGoPage ) || parseInt( iGoPage ) <= 0 ){
			alert( "Please enter positive number for page to go." );
			txtPage.value = 1;
			txtPage.select();
			return;
		}
		
		if( !iDirection ) 
			iGoPage = parseInt( iGoPage );
		else
			iGoPage = parseInt( iGoPage ) + iDirection;
				
		txtPage.value = iGoPage;
			
		submit();
	}
}
function sortColumn( iValue ){
	document.forms[0].hdSort.value = iValue;
	document.forms[0].submit();
}

function isWaste( oElement, oLable, sMessage ){
	if( oElement.value == "" || oElement.value == 0 ){
		alert( sMessage );
		oLable.className = "clsError";
		oElement.focus();
		return true;
	} else {
		oLable.className = "clsField";
		return false;
	}
}

function isChecked( oElement, oLabel, sMessage ){
		var bChecked = false;
		if( typeof(oElement)  != 'undefined' ){
			if( !oElement.length ){
				bChecked = oElement.checked;
			} else {
				for( var i =0; i< oElement.length;i++){
					if( oElement[i].checked ){
						bChecked = true;
						break;
					}
				}
			}
			
			if( !bChecked ){
				alert(sMessage);
				if( oLabel != null ) oLabel.className = "clsError";
				return false;
			} else{
				if( oLabel != null ) oLabel.className = "clsField";
				return true;
			} 
		}
}

function isValidDate( iDay, iMonth, iYear ){
	var sDate = iMonth + "/" + iDay + "/" + iYear;

	var oDate=new Date(sDate);
	if(oDate.getMonth()+1==iMonth){
		return true;
	} 
	else{
		return false;
	}
}

function captureKeyPress( oElement, sFunction ){
	if (navigator.appName == 'Netscape') {
		window.captureEvents(Event.KEYPRESS);
		window.onKeyPress = function( e ) { if( e.which == 13 ) eval( sFunction ); };
	} else {
		document.body.onkeypress = function(){ 
			if( window.event.keyCode == 13 ) {
				if( window.event.srcElement.tagName != "TEXTAREA" ) eval( sFunction ); 
			}
		};
	}
	
	try { 
		oElement.focus();
	} catch( e ) {}
}

function changeLanguage( sLang ){
	document.frmLanguage.lang.value = sLang;
	document.frmLanguage.submit();
}

function focusElement(){
	with( document.forms[0] ) {
		var objElement = null;
		
		for( var index = 0; index < length; index++ )
			if( elements[ index ].tagName == "SELECT" ) {
				objElement = elements[ index ];
				break;
			} else {
				if( elements[ index ].tagName == "INPUT" && 
					elements[ index ].type != "hidden" ) {
					objElement = elements[ index ];
					break;
				}
			}

		if( objElement != null )
			objElement.focus();
			
		return;
	}
}

function isEmpty( oText, oLabel, sMessage ){
	if( oText.value == "" ){
		oLabel.className = 'clsError';
		alert( sMessage );
		oText.focus();
		return true;
	} else {
		oLabel.className = 'clsField';
		return false;
	}
}

function isChecked( oElement, oLabel, sMessage ){
		var bChecked = false;
		if( typeof(oElement)  != 'undefined' ){
			if( !oElement.length ){
				bChecked = oElement.checked;
			} else {
				for( var i =0; i< oElement.length;i++){
					if( oElement[i].checked ){
						bChecked = true;
						break;
					}
				}
			}
			
			if( !bChecked ){
				alert(sMessage);
				if( oLabel != null ) oLabel.className = "clsError";
				return false;
			} else{
				if( oLabel != null ) oLabel.className = "clsField";
				return true;
			} 
		}
}

	function checkNumber( oInput ){
		if( isNaN( oInput.value ) ){
			alert("Please enter numberic for order quantity.");
			oInput.select();
		} 
	}

//Remove the $ sign if you wish the parse number to NOT include it
var prefix=""
var wd
function formatNumber( mNumber ){
	mNumber = String( mNumber );
	
	wd="w"
	var tempnum=mNumber
	
	for (i=0;i<tempnum.length;i++){
		if (tempnum.charAt(i)=="."){
			wd="d"
			break
		}
	}
	if (wd=="w")
		return prefix+tempnum+".00"
	else{
		if (tempnum.charAt(tempnum.length-2)=="."){
			return prefix+tempnum+"0"
		} else{
			tempnum=Math.round(tempnum*100)/100
			return prefix+tempnum
		}
	}
}

