var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	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++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month.")
		alert("Please enter a valid date.")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day.")
		alert("Please enter a valid date.")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		alert("Please enter a valid date.")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date.")
		return false
	}
return true
}

function IsValidDate( strDate ){
	if (isDate(strDate)==false){
		return false
	}
    return true
}




function IsEmpty( e )
{
	var temp, re

	re = /\s+/;
	temp = e.value.replace( re, "" )
	if( temp == "" )
		return( true );
	else
		return( false );
}


function SwitchToSubMenuLayer( strLayerNumber ) {
    HideAllSubMenuLayers();
	ShowSubMenuLayer( strLayerNumber );
}

function HideAllSubMenuLayers() {
    document.getElementById( "submenu_layer0" ).style.display = "none";
    document.getElementById( "submenu_layer1" ).style.display = "none";
    document.getElementById( "submenu_layer2" ).style.display = "none";
    document.getElementById( "submenu_layer3" ).style.display = "none";
    document.getElementById( "submenu_layer4" ).style.display = "none";
    document.getElementById( "submenu_layer5" ).style.display = "none";
    document.getElementById( "submenu_layer6" ).style.display = "none";
}

function ShowSubMenuLayer( strLayerNumber ) {
    document.getElementById( "submenu_layer" + strLayerNumber ).style.display = "inline";
}

function LaunchContactUsMap() {
  open( "map.asp", "map", "height=460,width=780,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=1,resizable=1,scrollbars=1,status=0,toolbar=0", "map" );
}

function launch( url )
{
  w = open( url, "AnaSpecPopup", "height=525,width=550,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=1,resizable=1,scrollbars=1,status=0,toolbar=0", false);
  w.focus();
}

function launchWide( url )
{
	w = open( url, "AnaSpecPopup", "height=525,width=800,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=1,resizable=1,scrollbars=1,status=0,toolbar=0", false);
	w.focus();
}

function IsAnySelected( e, msg )
{
	bSel = false;

	if( e != null )
	{
		if( e.checked )
			bSel = true;

		for( i = 0; i < e.length; i++ )
		{
			if( e[i].checked )
				bSel = true;
		}
		if( !bSel )
			alert( msg );
	}

	return( bSel );
}


function SubmitSearch()
{
	document.searchform.submit();
}

function SubmitEmailSignup()
{
	document.emailSignupForm.submit();
}

function CreateAccountSubmit()
{
	if( !CheckEmpty( document.theform.CustomerID, "a customerID" ))
		return( false );
	if( !CheckOneWord( document.theform.CustomerID, "a customerID" ))
		return( false );
	if( !CheckEmpty( document.theform.Password, "a password" ))
		return( false );
	if( !CheckOneWord( document.theform.Password, "a password" ))
		return( false );
	if( !CheckAccountInfo() )
		return( false );
	return( true );
}

function RetrievePassword() {
  open( "password.asp", "password", "height=400,width=350,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0", "password" );
}

function RetrievePasswordSubmit()
{
	if( !CheckEmpty( document.theform.Name, "your name" ))
		return( false );
	if( !CheckEmpty( document.theform.Company, "your company or institution" ))
		return( false );
	if( !CheckEmail( document.theform.Email ))
		return( false );
	return( true );
}


function CreateAccount( strURL )
{
	document.theform.method = "post";
	document.theform.action = strURL;
	document.theform.submit();
}

function UpdateAccountSubmit()
{
	if( !CheckAccountInfo() )
		return( false );
	return( true );
}

function ChangePasswordSubmit()
{
	if( !CheckEmpty( document.theform.oldPassword, "the old password" ))
		return( false );
	if( !CheckEmpty( document.theform.newPassword, "the new password" ))
		return( false );
	if( !CheckEmpty( document.theform.retypePassword, "the new password again" ))
		return( false );

	if( document.theform.retypePassword.value != document.theform.newPassword.value )
	{
		alert( "The new password and the retyped password are not the same, please try again." )
		document.theform.newPassword.focus();
		return( false );
	}

	return( true );
}

function UpdateFavorites( updateType )
{
	document.theform.action = "update.asp";
	document.theform.updateType.value = updateType;
	document.theform.submit();
}
function ShopFavorites( shopType )
{
	var nCheck, i, bNoneChecked;
	nCheck = window.parseInt( document.getElementById( "ncheckboxes" ).value, 10 );

	bNoneChecked = true;
	for( i = 1; i <= nCheck; i++ ) {
		if( document.getElementById( "checkbox_select_"+i ).checked ) {
			bNoneChecked = false;
			break;
		}
	}
	if( bNoneChecked ) {
		window.alert( "Please select products to add to cart." );
		return;
	}
	//return;

	document.theform.action = "shop.asp";
	document.theform.updateType.value = "s";
	//document.theform.updateType.value = "m";
	document.theform.shopType.value = shopType;
	document.theform.submit();
}
function ToggleSelectAllFavorites()
{
	var nCheck, i;
	//window.alert( document.getElementById( "ncheckboxes" ).value );
	nCheck = window.parseInt( document.getElementById( "ncheckboxes" ).value, 10 );
	//window.alert( nCheck );

	for( i = 1; i <= nCheck; i++ ) {
		if (!document.getElementById( "checkbox_select_"+i ).disabled)
		{
			document.getElementById( "checkbox_select_"+i ).checked = document.theform.checkbox_selectall.checked;
		}
	}
}
function ToggleSelectOneFavorite()
{
	var nSelected, nCheck, i;

	nCheck = window.parseInt( document.getElementById( "ncheckboxes" ).value, 10 );

	nSelected = 0;
	for( i = 1; i <= nCheck; i++ ) {
		if( document.getElementById( "checkbox_select_"+i ).checked == true ) {
			nSelected++;
		}
	}
	if( nSelected == nCheck )
		document.theform.checkbox_selectall.checked = true;
	else
		document.theform.checkbox_selectall.checked = false;
}

var isoLogoFadeWait;
var isoLogoFadeInterval;
function FadeInISOLogo() {
    var item = document.getElementById( "iso_logo" );
	if( item != null ) {
	    //while( item.filters.alpha.opacity < 100 ) {
			isoLogoFadeWait = setInterval( "FadeInISOLogoWait()", 2000 );
	    //}
	}
}
function FadeInISOLogoWait() {
	clearInterval( isoLogoFadeWait );
	isoLogoFadeInterval = setInterval( "IncreaseOpacity( 'iso_logo', 4 )", 100 );
}

function IncreaseOpacity( id, increment ) {
    var item = document.getElementById( id );

	if( item != null ) {
		var newOp = item.filters.alpha.opacity + increment;
		if( newOp < 0 ) {
		    newOp = 0;
		}
		if( newOp > 100 ) {
		    newOp = 100;
		}
	    item.filters.alpha.opacity = newOp;
		if( newOp >= 100 ) {
			clearInterval( isoLogoFadeInterval );
		}
	} else {
	    clearInterval( isoLogoFadeInterval );
	}
}

function SwitchToSubMenu( iSubMenu ) {
    //HideAllSubMenus();
	ShowSubMenu( iSubMenu );
}

function ShowSubMenu( iSubMenu ) {
	HideAllSubMenus();

	var item = document.getElementById( "submenu" + iSubMenu + "" );
	if( item != null ) {
		item.style.display = "inline";
	}
}
function HideAllSubMenus() {
	var strAllSubMenuPopups, arr, item;

	strAllSubMenuPopups = document.getElementById( "submenus_all" ).value;
	arr = new Array();
	arr = strAllSubMenuPopups.split( "," );
	for( i=0; i<arr.length; i++ ) {
		item = document.getElementById( "submenu" + arr[ i ]);
		if( item != null ) {
			item.style.display = "none";
		}
	}
}

function AddContentItem() {
    document.theform.mode.value = "a";
    document.theform.submit();
}
function EditContentItem( id ) {
    document.theform.id.value = id;
    document.theform.mode.value = "m";
    document.theform.submit();
}
function DeleteContentItem( id, strType ) {
	DeleteContentItem2( id, strType, -1 )
}
function DeleteContentItem2( id, strType, type ) {
	if( confirm( "Are you sure you want to delete this " + strType + "?" ))
	{
		document.theform.id.value = id;
		if( type > 0 ) {
			document.theform.type.value = type;
		}
		document.theform.mode.value = "d";
		document.theform.submit();
	}
}
function ReorderContentItem( id, increment ) {
	ReorderContentItem2( id, -1, increment );
}
function ReorderContentItem2( id, type, increment ) {
	if( increment == 1 ) {
		document.theform.mode.value = "down";
	} else {
		document.theform.mode.value = "up";
	}
    document.theform.id.value = id;
	if( type > 0 ) {
		document.theform.type.value = type;
	}
    document.theform.submit();
}
function ToggleActiveContentItem( id ) {
	ToggleActiveContentItem2( id, -1 );
}
function ToggleActiveContentItem2( id, type ) {
    document.theform.id.value = id;
	if( type > 0 ) {
		document.theform.type.value = type;
	}
    document.theform.mode.value = "v";
    document.theform.submit();
}
function ToggleHomepageContentItem( id ) {
	ToggleHomepageContentItem2( id, -1 );
}
function ToggleHomepageContentItem2( id, type ) {
    document.theform.id.value = id;
	if( type > 0 ) {
		document.theform.type.value = type;
	}
    document.theform.mode.value = "h";
    document.theform.submit();
}


function HideElementById( strID ) {
	var item = document.getElementById( strID );
	if( item != null ) {
		item.style.display = "none";
	}
}
function ShowElementById( strID ) {
	var item = document.getElementById( strID );
	if( item != null ) {
		item.style.display = "inline";
	}
}
function ColorElementById( strID, strColor ) {
	var item = document.getElementById( strID );
	if( item != null ) {
		item.bgColor = strColor;
	}
}

var nScrollItemStartInterval = 2000;
function DispatchScrollItems( nItems ) {
	var nDelay;
    for( i = 0; i < nItems; i++ ) {
		nDelay = ( i + 1 ) * nScrollItemStartInterval;
		setTimeout( "StartScrollItem( 'scrollitem" + ( i + 1 ) + "' );", nDelay );
    }
}
function StartScrollItem( strID ) {
	var item = document.getElementById( strID );
	if( item != null ) {
		item.scrollAmount = 4;
	}
}

function NewWindow( newURL, strOptions ) {
	newWin = window.open( newURL, "", strOptions );
}

function GetWindowWidth() {
	return document.body.offsetWidth;
}

/*
	1) no catalog #
	2) duplicated catalog #
*/
function CheckQuickOrderInputs( nTotalProducts ) {
	var nFilled = 0;
    for( i = 1; i <= nTotalProducts; i++ ) {
        if( !IsEmpty( document.getElementById( "id_p" + i ))) {
            nFilled++;
        }
    }

	if( nFilled <= 0 ) {
	    window.alert( "Please enter at least 1 Catalog #." );
		document.getElementById( "id_p1" ).focus();
		return false;
	}

	return true;
}

function SubmitQuickOrder( nTotalProducts, strAction ) {
    if( CheckQuickOrderInputs( nTotalProducts )) {
		document.theform.action.value = strAction;
		document.theform.submit();
	}
}

function ToggleDisplayCCBlock( bHide ) {
	return;

	var item = document.getElementById( "cc_block" )
	if( item != null ) {
		if( bHide ) {
		    item.style.display = "none";
		} else {
		    item.style.display = "inline";
		}
	}
}

function ChangeFormAction( strURL ) {
	var form = document.theform;
	if( form != null ) {
		form.action = strURL;
	}
}

function ValidateCreditCardInfo() {
	if( document.theform.cc_name.value == "" ) {
		alert( "Please enter the Cardholder Name." );
		document.theform.cc_name.focus();
		return( false );
	}
	if( document.theform.cc_number.value == "" ) {
		alert( "Please enter the Credit Card #." );
		document.theform.cc_number.focus();
		return( false );
	}
	if( document.theform.cc_cvv2.value == "" ) {
		alert( "Please enter the CVV2 code." );
		document.theform.cc_cvv2.focus();
		return( false );
	}
	if( document.theform.cc_number.value.length < 13 /*|| isNaN( document.theform.cc_number.value )*/) {
		window.alert( "Please enter a valid Credit Card #." );
		document.theform.cc_number.focus();
		return( false );
	}
	if( isNaN( document.theform.cc_cvv2.value )) {
		window.alert( "Please enter a valid CVV2 code." );
		document.theform.cc_cvv2.focus();
		return( false );
	}

	var expM, expY, currDate;
	expM = window.parseInt( document.theform.month.value, 10 );
	expY = window.parseInt( document.theform.year.value, 10 );
	currDate = new Date();
	if( currDate.getFullYear() > expY ) {
		window.alert( "Please enter a valid Expiration Date." );
		document.theform.year.focus();
		return( false );
	}
	if( currDate.getFullYear() == expY ) {
		if( currDate.getMonth() > expM - 1 ) {
			window.alert( "Please enter a valid Expiration Date." );
			document.theform.month.focus();
			return( false );
		}
	}
	return true;
}

function SetSearchFormExactPhraseOn() {
	if( document.searchform.exact.value != "1" ) {
		document.searchform.exact.value = "1";
		document.getElementById( "searchform_type_any_on" ).style.display = "none";
		document.getElementById( "searchform_type_any_off" ).style.display = "inline";
		document.getElementById( "searchform_type_exact_on" ).style.display = "inline";
		document.getElementById( "searchform_type_exact_off" ).style.display = "none";
	}
}
function SetSearchFormExactPhraseOff() {
	if( document.searchform.exact.value != "0" ) {
		document.searchform.exact.value = "0";
		document.getElementById( "searchform_type_any_on" ).style.display = "inline";
		document.getElementById( "searchform_type_any_off" ).style.display = "none";
		document.getElementById( "searchform_type_exact_on" ).style.display = "none";
		document.getElementById( "searchform_type_exact_off" ).style.display = "inline";
	}
}

function PositionGlobalPageFlagStrip() {
  var clientWidth, clientHeight, item_flagstrip, xOff, yOff;

	item_flagstrip = document.getElementById( "flagstrip" );
	if( item_flagstrip != null ) {
		//window.alert( item_flagstrip.offsetWidth + "x" + item_flagstrip.offsetHeight );
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			clientWidth = window.innerWidth;
			clientHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight )) {
			//IE 6+ in 'standards compliant mode'
			clientWidth = document.documentElement.clientWidth;
			clientHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			clientWidth = document.body.clientWidth;
			clientHeight = document.body.clientHeight;
		}
		//window.alert( clientWidth );
		xOff = ( clientWidth - item_flagstrip.offsetWidth ) / 2;
		if( xOff <= 0 ) {
			xOff = 0;
		}
		yOff = ( clientHeight - 450 ) / 2 + 310;
		if( yOff <= 0 ) {
			yOff = 0;
		}
		item_flagstrip.style.left = "" + xOff + " px";
		item_flagstrip.style.top = "" + yOff + " px";
	}
}