var price_to = '';
var price_from = '';
var carat_to = '';
var carat_from = '';
var cut = '';
var cut_to = '';
var cut_from = '';
var color_to='';
var color_from='';
var color = '';
var purity = '';
var purity_to = '';
var purity_from = '';
var certi_no = '';
var page_records = '';
var l = window.location.hash;// for getting the shape of the diamond from home page
var shape= l.slice(1);
if(shape==''){
	shape='ROUND';	
}

var urlPath = 'http://www.kathana.in/search_result.php?price_to=' + price_to + '&price_from=' + price_from + '&carat_to=' + carat_to + '&carat_from=' + carat_from + '&cut_to=' + cut_to + '&cut_from=' + cut_from + '&color_to=' + color_to + '&color_from=' + color_from + '&purity_to=' + purity_to + '&purity_from=' + purity_from + '&shape='+shape+'&certi_no='+certi_no+'&page_records='+page_records+'';

//var urlPath = 'http://localhost/kathana/search_result.php?price_to=' + price_to + '&price_from=' + price_from + '&carat_to=' + carat_to + '&carat_from=' + carat_from + '&cut=' + cut + '&color=' + color + '&purity=' + purity + '&shape='+shape+'&certi_no='+certi_no+'';

//var urlPath = 'http://weblancer.in/kathana/search_result.php?price_to='+price_to+'&price_from='+price_from+'&carat_to='+carat_to+'&carat_from='+carat_from+'&cut='+cut+'&color='+color+'&purity='+purity+'&shape='+shape+'&certi_no='+certi_no+'';

$(window).load(function(){
	$("iframe#grid").attr('src',urlPath); // use urlPath in place of html page when deploying						
});
var test = 0;
// for storing the sliders position
function store(){
	$.cookie('store', storeVal);
}
//
$(document).ready(function() {
					
	if(shape!='ROUND')
	{
		$("#cut").hide('slow'); // hide cut when selected fancy diamonds
		$(".cut").hide('slow');
	}
	else
	{
		$("#cut").show('slow'); // show cut when selected round diamonds
		$(".cut").show('slow');
	}							
	// for setting the required ahape on page load
	if ($('div.shape input:checked').val() != 'ROUND'){
			$('div.shape input:checked').removeAttr('checked');// by default round is checked
	}
	var s = 'input[value='+shape+']';
	$('div.shape').find(s).attr('checked','checked');
	//
	//
    $('div.shape input').click(function(){ // for reloading the grid with the new shape.
		shape = $(this).val();
		
		if(shape!='ROUND')
		{
			$("#cut").hide('slow'); // hide cut when selected fancy diamonds
			$(".cut").hide('slow');
		}
		else
		{
			$("#cut").show('slow'); // show cut when selected round diamonds
			$(".cut").show('slow');
		}
		
		$('#gosearch').click(function(){
			$("#gosearch").attr('href','diamondSearch.php#'+shape+'');	// for home page diamond selected and show it on diamond search page				  
		});	
		
		refreshGrid();
	});
	$('#go').click(function(){// for reloading the grid with the new shape.
		certi_no = $('#certi_no').val();
		refreshGrid();
	});
	$('#numRecord').change(function(){// for reloading the grid with the new per page records.
		page_records = $('#numRecord').val();
		refreshGrid();
	});
	$("#carat").slider({
        orientation: 'horizontal',
        range: true,
        min: 0.01,
        max: 10,
        step: 0.01,
        values: [0.01, 10],
        animate: true,
        slide: function (event, ui) {
            $("#caratArea strong.brown").html(ui.values[0] + ' - ' + ui.values[1]);
        },
        change: function (event, ui) {
            carat_to = ui.values[0];
            carat_from = ui.values[1];
            refreshGrid();
        }
    });
    $("#colour").slider({
        orientation: 'horizontal',
        range: true,
        min: 1,
        max: 10,
        step: 1,
        values: [1, 10],
        animate: true,
        change: function (event, ui) { //$("#amount").html('Rs.' + ui.values[0] + ' - Rs.' + ui.values[1]);
            //refreshGrid();
            colorCode1 = ui.values[0];
            colorCode2 = ui.values[1];
            switch (colorCode1) {
            case 1:
                color_to = 'D';
                break;
            case 2:
                color_to = 'E';
                break;
            case 3:
                color_to = 'F';
                break;
            case 4:
                color_to = 'G';
                break;
            case 5:
                color_to = 'H';
                break;
            case 6:
                color_to = 'I';
                break;
            case 7:
                color_to = 'J';
                break;
			case 8:
                color_to = 'K';
                break;
			case 9:
                color_to = 'L';
                break;
			case 10:
                color_to = 'M';
                break;
			default:
                color_to = 'D';
                break;
            }
            switch (colorCode2) {
            case 1:
                color_from = 'D';
                break;
            case 2:
                color_from = 'E';
                break;
            case 3:
                color_from = 'F';
                break;
            case 4:
                color_from = 'G';
                break;
            case 5:
                color_from = 'H';
                break;
            case 6:
                color_from = 'I';
                break;
            case 7:
                color_from = 'J';
                break;
			case 8:
                color_from = 'K';
                break;
			case 9:
                color_from = 'L';
                break;
			case 10:
                color_from = 'M';
                break;
			default:
                color_from = 'D';
                break;
            } refreshGrid();
        }
    });
    $("#cut").slider({
        orientation: 'horizontal',
        range: true,
        min: 1,
        max: 5,
        step: 1,
        values: [1, 5],
        animate: true,
        change: function (event, ui) {
			cut_to = ui.values[0];
            cut_from = ui.values[1];
            
			/*ct = ui.values[0];
            cf = ui.values[1];
            switch (ct) {
            case 1:
                cut_to = 'F';
                break;
            case 2:
                cut_to = 'G';
                break;
            case 3:
                cut_to = 'VG';
                break;
            case 4:
                cut_to = 'I';
                break;
            case 5:
                cut_to = 'SI';
                break;
            default:
                cut_to = 'SI';
                break;
            }
            switch (cf) {
            case 1:
                cut_from = 'F';
                break;
            case 2:
                cut_from = 'G';
                break;
            case 3:
               cut_from = 'VG';
                break;
            case 4:
                cut_from = 'I';
                break;
            case 5:
                cut_from = 'SI';
                break;
            default:
                cut_from = 'F';
                break;
            }*/
			refreshGrid();
        }
    });
	
	//
    $("#clarity").slider({
        orientation: 'horizontal',
        range: true,
        min: 1,
        max: 11,
        step: 1,
        values: [1, 11],
        animate: true,
        change: function (event, ui) { 
			purity_to = ui.values[0];
            purity_from = ui.values[1];
            
			/*pt = ui.values[0];
            pf = ui.values[1];
            switch (pt) {
            case 1:
                purity_to = 'SI2';
                break;
            case 2:
                purity_to = 'SI1';
                break;
            case 3:
                purity_to = 'VS2';
                break;
            case 4:
                purity_to = 'VS1';
                break;
            case 5:
                purity_to = 'VVS2';
                break;
            case 6:
                purity_to = 'VVS1';
                break;
            case 7:
                purity_to = 'IF';
                break;
			case 8:
                purity_to = 'FL';
                break;
            default:
                purity_to = 'FL';
                break;
            }
            switch (pf) {
            case 1:
                purity_from = 'SI2';
                break;
            case 2:
                purity_from = 'SI1';
                break;
            case 3:
                purity_from = 'VS2';
                break;
            case 4:
                purity_from = 'VS1';
                break;
            case 5:
                purity_from = 'VVS2';
                break;
            case 6:
                purity_from = 'VVS1';
                break;
            case 7:
                purity_from = 'IF';
                break;
			case 8:
                purity_from = 'FL';
                break;
            default:
                purity_from = 'SI2';
                break;
            }*/refreshGrid();
        }
    });
	//
    $("#price").slider({
        orientation: 'horizontal',
        range: true,
        min: 0,
        max: 10000000,
        values: [0, 10000000],
        animate: true,
        slide: function (event, ui) {
            $("#amount strong.brown").html('Rs.' + intToFormat(ui.values[0]) + ' - Rs.' + intToFormat(ui.values[1]));
        },
        change: function (event, ui) {
            price_to = ui.values[0];
            price_from = ui.values[1];
            refreshGrid();
        }
    });refreshGrid();
});

function refreshGrid() { 
	$('div#toolTip').css('display','none');

//diamondSearch.url='http://weblancer.in/kathana/search_result.php?price_to='+price_to+'&price_from='+price_from+'&carat_to='+carat_to+'&carat_from='+carat_from+'&cut='+cut+'&color='+color+'&purity='+purity+ '&shape='+shape+'&certi_no='+certi_no+'';

 //var url = 'http://localhost/kathana/search_result.php?price_to=' + price_to + '&price_from=' + price_from + '&carat_to=' + carat_to + '&carat_from=' + carat_from + '&cut_to=' + cut_to + '&cut_from=' + cut_from + '&color_to=' + color_to + '&color_from=' + color_from + '&purity_to=' + purity_to + '&purity_from=' + purity_from + '&shape='+shape+'&certi_no='+certi_no+'';

 var url = 'http://www.kathana.in/search_result.php?price_to=' + price_to + '&price_from=' + price_from + '&carat_to=' + carat_to + '&carat_from=' + carat_from + '&cut_to=' + cut_to + '&cut_from=' + cut_from + '&color_to=' + color_to + '&color_from=' + color_from + '&purity_to=' + purity_to + '&purity_from=' + purity_from + '&shape='+shape+'&certi_no='+certi_no+'&page_records='+page_records+'';

    $("iframe#grid").attr('src',url);
}


//function for converting string into indian currency format

function intToFormat(nStr)
    {
     nStr += '';
     x = nStr.split('.'); 
	 
	 if(x[1] >= 50)
	 {
		x[0] = parseInt(x[0]) + 1;	 
	 }
     x1 = String(x[0]);
     x2 = x.length > 1 ? '.' + x[1] : ''; 
     var rgx = /(\d+)(\d{3})/;
     var z = 0;
     var len = String(x1).length;
     var num = parseInt((len/2)-1);
 
      while (rgx.test(x1))
      {
        if(z > 0)
        {
          x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        else
        {
          x1 = x1.replace(rgx, '$1' + ',' + '$2');
          rgx = /(\d+)(\d{2})/;
        }
        z++;
        num--;
        if(num == 0)
        {
          break;
        }
      }
     return x1;
    }

