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_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_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 NewWindow( mypage, myname, w, h, scroll ) 
{
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	
	window.opener = this.window;
	
	var mywin = window.open(mypage, myname, winprops); 
	mywin.focus() 
}

function MainPagePopup( mypage, myname, w, h, scroll ) 
{
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    
	winprops = 'height='+h+',width='+w+',top=100,left=100,scrollbars='+scroll+',resizable'
	
	window.opener = this.window;
	
	var mywin = window.open(mypage, myname, winprops); 
	mywin.focus() 
}

function ValidateSearchForm( curr_form )
{   
    var price_from = curr_form.minprice;
    var price_to   = curr_form.maxprice;
    var mincarat = curr_form.mincarat;
    var maxcarat = curr_form.maxcarat;
    var stock_num = curr_form.stock_num;
    var is_couples = curr_form.IsCouples;
    
    // no need to validate if stock number is prsent
    if( stock_num.value  )
    {
        if( stock_num.value.search( /^[0-9]+$/ ) == -1 )
        {
            alert( "Stock number must be only numbers.  No letters or spaces can be entered." );
            stock_num.focus();
            return( false );
        }
        
        return( true );
    }
        
    // Either price range or carat range is required
    if( price_to.value == "" && price_from.value == "" && mincarat.value == "" && maxcarat.value == "" )
    {
        alert( "Please choose either a price range or a carat size range to begin search." );
        return( false );
    }
    
    // If price range is entered, validate prices
    if( price_to.value != "" || price_from.value != "" ) 
    {
        if( price_from.value == "" || isNaN( parseFloat( price_from.value ) ) || price_from.value.indexOf( "/" ) != -1)
        {
            alert( "Please enter a valid starting price." );
            price_from.focus();
            return( false );
        }
        
        if( price_to.value == "" || isNaN( parseFloat( price_to.value ) ) || price_to.value.indexOf( "/" ) != -1)
        {
            alert( "Please enter a valid ending price." );
            price_to.focus();
            return( false );
        }
        
        if( parseFloat( price_to.value ) < parseFloat( price_from.value ) )
        {
            alert( "Starting price must be less than ending price." );
            price_to.focus();
            return( false );
        }
    }
    
    // If carat range is entered, validate carat sizes
    if( mincarat.value != "" || maxcarat.value != "" ) 
    {
        if( mincarat.value == "" || isNaN( parseFloat( mincarat.value ) ) || mincarat.value.indexOf( "/" ) > -1 )
        {
            alert( "Please enter a valid minimum carat size.\nIf you are using fractions, use decimals instead." );
            mincarat.focus();
            return( false );
        }
        
        if( maxcarat.value == "" || isNaN( parseFloat( maxcarat.value )  )|| maxcarat.value.indexOf( "/" ) > -1 )
        {
            alert( "Please enter a valid maximum carat size.\nIf you are using fractions, use decimals instead." );
            maxcarat.focus();
            return( false );
        }
        
        if( parseFloat( maxcarat.value ) < parseFloat( mincarat.value ) )
        {
            alert( "Minimum carat size must be less than maximum carat size." );
            maxcarat.focus();
            return( false );
        }
    }
/*
    if( is_couples.value == 1 )
    {
        if( Math.abs( maxcarat.value - mincarat.value ) > 1 )
        {
            alert( "Please limit your search to a range of 1 carat or less." );
            return( false );
        }
        
        if( Math.abs( price_from.value - price_to.value ) > 1000 )
        {
            alert( "Please limit your search to a range of $1000.00 or less." );
            return( false );
        }
    }
*/    
        
    return( true );
}

function SearchJump( StartNum )
{
    document.refine_search.startnum.value = StartNum;
    document.refine_search.ispagejump.value = "1";
    document.refine_search.submit();
}

function ResetOrderResults( NewOrder )
{
    document.refine_search.OrderBy.value = NewOrder;
    document.refine_search.submit();
}

function ToggleCertAll( IsChecked )
{
    if( IsChecked )
    {
        document.diamond_search.grading_GIA.disabled = true;
        document.diamond_search.grading_AGS.disabled = true;
        document.diamond_search.grading_EGL.disabled = true;
        document.diamond_search.grading_IGI.disabled = true;
        document.diamond_search.grading_HRD.disabled = true;
    }
    else
    {        
        document.diamond_search.grading_GIA.disabled = false;
        document.diamond_search.grading_AGS.disabled = false;   
        document.diamond_search.grading_EGL.disabled = false;   
        document.diamond_search.grading_IGI.disabled = false;   
        document.diamond_search.grading_HRD.disabled = false;
    }
}

function UpdateSettingsView( curr_view )
{
    switch( curr_view )
    {
        case "all": location.href = "settings.asp?view=all"; break;
        case "solitaire": location.href = "settings.asp?view=solitaire"; break;
        case "side_stones": location.href = "settings.asp?view=side_stones"; break;
        case "wedding_bands": location.href = "wedding_bands.asp?view=wedding_bands"; break;
        case "ladies_wedding_bands": location.href = "wedding_bands_dyn.asp?view=ladies_wedding_bands"; break;
        case "mens_wedding_bands": location.href = "wedding_bands_dyn.asp?view=mens_wedding_bands"; break;
        case "matching_bands": location.href = "settings.asp?view=matching_bands"; break;
        case "pre_set": location.href = "settings.asp?view=pre_set"; break;
        default: location.href = "settings.asp?view=all"; break;
    }
}

function UpdateWeddingBandView( curr_view )
{
    switch( curr_view )
    {
        case "all": location.href = "wedding_bands.asp?view=all"; break;
        case "ladies_wedding_bands": location.href = "wedding_bands_dyn.asp?view=ladies_wedding_bands"; break;
        case "mens_wedding_bands": location.href = "wedding_bands_dyn.asp?view=mens_wedding_bands"; break;
        default: location.href = "wedding_bands.asp?view=all"; break;
    }
}

function AddItemToCart()
{
    document.add_item_cart.submit();
}

function AddItemToRequestList( stock_num )
{
    location.href='http://www.abazias.com/request/SubmitItem.asp?stock=' + stock_num;
}

function OpenCouplesSearch()
{
    document.couples_search.Couples.value = 1;
    document.couples_search.submit();
}


function undo(pageURL)
{
	var tempStr="";
	for (i=0;i<pageURL.length;i++)
	{
		tempStr = tempStr + String.fromCharCode(pageURL.charCodeAt(i)-((i+1) % 6));	
	}
	
	return tempStr;
}

function SendCert( stock_num )
{
    NewWindow( "/database/SendCert.asp?stock=" + stock_num, 'cert_win', 430, 435, 'no' );
}

function ShowCert( stock_num )
{
    NewWindow( "/database/CertWin.asp?stock=" + stock_num, 'cert_win', 700, 500, 'yes' );
}

function ShowDiamondImages( stock_num, num_pics )
{
    if( stock_num == "N/A" )
    {
        Message = "There are no images currently available for this diamond in\n"
	            + "online format.\n\n"
	            + "Please call our offices at 1.800.603.9940 and we will be happy\n"
	            + "to provide you with images of this diamond if available."
	            
        alert( Message );
    }
    else
    {
        if( num_pics > 2 && num_pics != "" ) 
        {
            SetHeight = 620;
        }
        else
        {
            SetHeight = 500;
        }
        
        window.open( "/database/DiamondImageViewer.asp?stock=" + stock_num, 'diamond_image_win', 'width=550,height=' + SetHeight + ',scrollbars=yes,resizable' );
    }
}

function FindSimilarDiamonds()
{
    document.find_similar.submit();
}

function ValidateRegisteredCustomerRequest( curr_form )
{
    var EmailRegex = "^[a-zA-Z0-9\-\_\.]+\@[a-zA-Z0-9\-\_\.]+\.[a-zA-Z0-9]+$";
    if( curr_form.email_address.value.search( EmailRegex ) == -1 ) 
    {
        alert( "The email address you have entered is invalid.  Please try again." );
        curr_form.email_address.focus();
        return( false );
    }
    
    if( curr_form.password.value == "" ) 
    {
        alert( "Please enter in you account password in the area provided." );
        curr_form.password.focus();
        return( false );
    }
    return( true );
}

function ValidateNewCustomerRequest( curr_form )
{
    var EmailRegex = "^[a-zA-Z0-9\-\_\.]+\@[a-zA-Z0-9\-\_\.]+\.[a-zA-Z0-9]+$";
    
    if( curr_form.nc_first_name.value == "" ) 
    {
        alert( "Please enter in you first name in the area provided." );
        curr_form.nc_first_name.focus();
        return( false );
    }
    
    if( curr_form.nc_last_name.value == "" ) 
    {
        alert( "Please enter in you last name in the area provided." );
        curr_form.nc_last_name.focus();
        return( false );
    }
    
    if( curr_form.nc_email_address.value.search( EmailRegex ) == -1 ) 
    {
        alert( "The email address you have entered is invalid.  Please try again." );
        curr_form.nc_email_address.focus();
        return( false );
    }
    
    if( curr_form.nc_phone_number.value == "<optional>" )
    {
        curr_form.nc_phone_number.value = "";
    }
    
    if( curr_form.nc_comments.value == "<optional>" )
    {
        curr_form.nc_comments.value = "";
    }
    var refCookie = getCookie( "RefSite" );
    curr_form.jsRefString.value = refCookie;
    return( true );
}

function ClearBox( curr_box )
{
    if( document.getElementById( curr_box ).value == "<optional>" )
    {
        document.getElementById( curr_box ).value = "";
    }
}

function ResetBox( curr_box, new_value )
{
    if( document.getElementById( curr_box ).value == "" )
    {
        document.getElementById( curr_box ).value = new_value;
    }
}    

////////////////////////////////////////////////////////////////////////////////////
// SEARCH FUNCTIONS
function SubmitBasicRefine()
{
    var error = 0;
    
     // Set price and carat
    minprice = document.getElementById( "minprice_textbox" );
    maxprice = document.getElementById( "maxprice_textbox" );
    mincarat = document.getElementById( "mincarat_textbox" );
    maxcarat = document.getElementById( "maxcarat_textbox" );
    
    // Either price range or carat range is required
    if( minprice.value == "" && price_from.value == "" && mincarat.value == "" && maxcarat.value == "" )
    {
        alert( "Please choose either a price range or a carat size range to begin search." );
        error = 1;
    }
    
    // If price range is entered, validate prices
    if( maxprice != "" || minprice != "" ) 
    {
        if( minprice.value == "" || isNaN( parseFloat( minprice.value ) ) )
        {
            alert( "Please enter a valid starting price." );
            minprice.focus();
            error = 1;
        }
        
        if( maxprice.value == "" || isNaN( parseFloat( maxprice.value ) ) )
        {
            alert( "Please enter a valid ending price." );
            maxprice.focus();
            error = 1;
        }
        
        if( parseFloat( maxprice.value ) < parseFloat( minprice.value ) )
        {
            alert( "Starting price must be less than ending price." );
            minprice.focus();
            error = 1;
        }
    }
    
    // If carat range is entered, validate carat sizes
    if( mincarat.value != "" || maxcarat.value != "" ) 
    {
        if( mincarat.value == "" || isNaN( parseFloat( mincarat.value ) ) )
        {
            alert( "Please enter a valid minimum carat size." );
            mincarat.focus();
            error = 1;
        }
        
        if( maxcarat.value == "" || isNaN( parseFloat( maxcarat.value ) ) )
        {
            alert( "Please enter a valid maximum carat size." );
            maxcarat.focus();
            error = 1;
        }
        
        if( parseFloat( maxcarat.value ) < parseFloat( mincarat.value ) )
        {
            alert( "Minimum carat size must be less than maximum carat size." );
            mincarat.focus();
            error = 1;
        }
    }
    
    if( error == 0 ) {
        TranslateBasicSearchData();
    }
}

function TranslateBasicSearchData()
{   
    // Set price and carat
    minprice = document.getElementById( "minprice_textbox" );
    maxprice = document.getElementById( "maxprice_textbox" );
    mincarat = document.getElementById( "mincarat_textbox" );
    maxcarat = document.getElementById( "maxcarat_textbox" );
    
    document.getElementById( "minprice" ).value = minprice.value;
    document.getElementById( "maxprice" ).value = maxprice.value;
    document.getElementById( "mincarat" ).value = mincarat.value;
    document.getElementById( "maxcarat" ).value = maxcarat.value;
    
    // Set color possible ranges
    var colorNumericValue = new Array();
    colorNumericValue[ "D" ] = 1;
    colorNumericValue[ "E" ] = 2;
    colorNumericValue[ "F" ] = 3;
    colorNumericValue[ "G" ] = 4;
    colorNumericValue[ "H" ] = 5;
    colorNumericValue[ "I" ] = 6;
    colorNumericValue[ "J" ] = 7;
    colorNumericValue[ "K" ] = 8;
    var startColor = document.getElementById( "mincolor_select" ).value;
    var endColor = document.getElementById( "maxcolor_select" ).value;
    document.getElementById( "mincolor" ).value = startColor;
    document.getElementById( "maxcolor" ).value = endColor;
    
    // If min color is greater than max color, switch them
    if( colorNumericValue[ startColor ] > colorNumericValue[ endColor ] )
    {
        startColor = document.getElementById( "maxcolor_select" ).value;
        endColor = document.getElementById( "mincolor_select" ).value;
    }
    
    var colorString = "color_D,color_E,color_F,color_G,color_H,color_I,color_J,color_K";
    var colorArray = colorString.split( "," );
    for( var i = 0; i < colorArray.length; i++ )
    {
        document.getElementById( colorArray[ i ] ).value = "";
        if( colorArray[i].search( "color_" + startColor ) != -1 ) {
            startIndex = i;
        }
        
        if( colorArray[i].search( "color_" + endColor ) != -1 ) {
            endIndex = i;
        }
    }
    
    for( var i = startIndex; i <= endIndex; i++ )
    {
        document.getElementById( colorArray[ i ] ).value = "1";
    }
    
    
    // Set clarity possible ranges
    var startClarity = document.getElementById( "minclarity_select" ).value;
    var endClarity = document.getElementById( "maxclarity_select" ).value;
    document.getElementById( "minclarity" ).value = startClarity;
    document.getElementById( "maxclarity" ).value = endClarity;
    
    // Set color possible ranges
    var clarityNumericValue = new Array();
    clarityNumericValue[ "IF" ] = 1;
    clarityNumericValue[ "VVS1" ] = 2;
    clarityNumericValue[ "VVS2" ] = 3;
    clarityNumericValue[ "VS1" ] = 4;
    clarityNumericValue[ "VS2" ] = 5;
    clarityNumericValue[ "SI1" ] = 6;
    clarityNumericValue[ "SI2" ] = 7;
    clarityNumericValue[ "I1" ] = 8;
    clarityNumericValue[ "I2" ] = 9;
    clarityNumericValue[ "I3" ] = 10;
    
    // If min color is greater than max color, switch them
    if( clarityNumericValue[ startClarity ] > clarityNumericValue[ endClarity ] )
    {
        var startClarity = document.getElementById( "maxclarity_select" ).value;
        var endClarity = document.getElementById( "minclarity_select" ).value;
    }
    
    var clarityString = "clarity_IF,clarity_VVS1,clarity_VVS2,clarity_VS1,clarity_VS2,clarity_SI1,clarity_SI2,clarity_I1,clarity_I2,clarity_I3";
    var clarityArray = clarityString.split( "," );
    for( var i = 0; i < clarityArray.length; i++ )
    {
        document.getElementById( clarityArray[ i ] ).value = "";
        if( clarityArray[i].search( "clarity_" + startClarity ) != -1 ) {
            startIndex = i;
        }
        
        if( clarityArray[i].search( "clarity_" + endClarity ) != -1 ) {
            endIndex = i;
        }
    }
    
    for( var i = startIndex; i <= endIndex; i++ )
    {
        document.getElementById( clarityArray[ i ] ).value = "1";
    }
    
    document.refine_search.submit();
}

function UpdateSortView(FormName)
{

	FormName.submit();
}

function SetViewAll(FormName)
{
	FormName.ShowItems.value = "999";
	FormName.submit();
}

function View3dMovie( currStock )
{
    var agt = navigator.userAgent.toLowerCase();
    var is_win = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
    var viewerWin = null;
    viewerWin = NewWindow( '/database/3dRingViewer.asp?stock=' + currStock, 'viewerWin', '680', '620', 'no' );
}

function NewWindow_v2( mypage, myname, w, h, scroll ) 
{
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	
	window.opener = this.window;
	
	var mywin = window.open(mypage, myname, winprops); 
	mywin.focus();
	return( mywin);
}

function OpenJewelryPopup_v2( currStock ) 
{   
    var myname = currStock;
    var scroll = "no";
 
    var w = "555";
    var h = "415";
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
       
    mypage = "/database/multi_popup.asp?stock=" + currStock;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	window.opener = this.window;
	
	var mywin = window.open(mypage, "popup_win", winprops); 
	mywin.focus() 
}


function ValidateNewCustomerRequestCertWin( curr_form )
{
    var EmailRegex = "^[a-zA-Z0-9\-\_\.]+\@[a-zA-Z0-9\-\_\.]+\.[a-zA-Z0-9]+$";
    
    if( curr_form.nc_first_name.value == "" ) 
    {
        alert( "Please enter in your name in the area provided." );
        curr_form.nc_first_name.focus();
        return( false );
    }
     if( curr_form.nc_first_name.value == "Name" ) 
    {
        alert( "Please enter in your name in the area provided." );
        curr_form.nc_first_name.focus();
        return( false );
    }
   
    if( curr_form.nc_last_name.value == "" ) 
    {
        alert( "Please enter in you last name in the area provided." );
        curr_form.nc_last_name.focus();
        return( false );
    }
    
    if( curr_form.nc_email_address.value.search( EmailRegex ) == -1 ) 
    {
        alert( "The email address you have entered is invalid.  Please try again." );
        curr_form.nc_email_address.focus();
        return( false );
    }
    
    if( curr_form.nc_phone_number.value == "Phone (Optional)" )
    {
        curr_form.nc_phone_number.value = "";
    }
    

    var refCookie = getCookie( "RefSite" );
    curr_form.jsRefString.value = refCookie;
    return( true );
}
