//var selectedShapesCount=0;  
var shapeArr=new Array(10); //Shapes info initialization
for (i=0;i<10;i++)
{
	shapeArr[i]=new Object();
	shapeArr[i].checked = false;
	shapeArr[i].value = i+1;
	switch(i)
	{
		case 0: shapeArr[i].imgId="round";
				shapeArr[i].img = new Image();
				shapeArr[i].img.src ="images/sliders/diamond_04_o.gif";
				break;
		case 1: shapeArr[i].imgId="princess";
				shapeArr[i].img = new Image();
				shapeArr[i].img.src ="images/sliders/diamond_05_o.gif";
				break;
		case 2: shapeArr[i].imgId="emerald";
				shapeArr[i].img = new Image();
				shapeArr[i].img.src ="images/sliders/diamond_06_o.gif";
				break;
		case 3: shapeArr[i].imgId="asscher";
				shapeArr[i].img = new Image();
				shapeArr[i].img.src ="images/sliders/diamond_07_o.gif";
				break;
		case 4: shapeArr[i].imgId="marquise";
				shapeArr[i].img = new Image();
				shapeArr[i].img.src ="images/sliders/diamond_08_o.gif";
				break;
		case 5: shapeArr[i].imgId="oval";
				shapeArr[i].img = new Image();
				shapeArr[i].img.src ="images/sliders/diamond_09_o.gif";
				break;
		case 6: shapeArr[i].imgId="radiant";
				shapeArr[i].img = new Image();
				shapeArr[i].img.src ="images/sliders/diamond_10_o.gif";
				break;
		case 7: shapeArr[i].imgId="pear";
				shapeArr[i].img = new Image();				
				shapeArr[i].img.src ="images/sliders/diamond_11_o.gif";
				break;
		case 8: shapeArr[i].imgId="heart";
				shapeArr[i].img = new Image();
				shapeArr[i].img.src ="images/sliders/diamond_12_o.gif";
				break;
		case 9: shapeArr[i].imgId="cushion";
				shapeArr[i].img = new Image();
				shapeArr[i].img.src ="images/sliders/diamond_13_o.gif";
				break;		
	}
}


function onlyRoundSlected()
{
	var count=0;
	for(var i=0;i<shapeArr.length;i++)
	{
		if (shapeArr[i].checked) count+=1;
	}
	return ( shapeArr[0].checked && count == 1)
}

// Validates if the parameter is numeric  
function IsNumeric(sText)
{	  
       return !isNaN(sText);   
};
	
	
function buildUrl(mincarat, maxcarat, maxcolor, mincolor, maxcut, mincut, maxclarity, minclarity, minprice, maxprice, minpolish, maxpolish, minsymmetry, maxsymmetry, mindepth, maxdepth, mintable, maxtable, minfluorescence, maxfluorescence, minratio, maxratio, sortby, orderby )
{	
	var url = xmlFeed;
	var querylist = new Array();
	var ctr = 0;	
	// generate the url to pass
	for(var sh=0;sh<shapeArr.length;sh++)
	{
		if(shapeArr[sh].checked)
		{
			querylist[ctr]='shape='+shapeArr[sh].value;
			ctr++;
		}
	}
	if(minprice != '')
	{
		querylist[ctr]='lowprice='+minprice;
		ctr++;
	}	
	if(maxprice != '')
	{
		querylist[ctr]='highprice='+maxprice;
		ctr++;
	}	
	if(mincarat != '')
	{
		querylist[ctr]='lowsize='+mincarat;
		ctr++;
	}
	if(maxcarat != '')
	{
		querylist[ctr]='highsize='+maxcarat;
		ctr++;
	}
	for(var i=mincut;i<=maxcut;i++)
	{
		querylist[ctr]='cut='+i;
		ctr++;
	}
	for(var i=mincolor;i<=maxcolor;i++)
	{
		querylist[ctr] = 'color='+i;
		ctr++;
	}	
	for(var i=minclarity;i<=maxclarity;i++)
	{
		querylist[ctr] = 'clarity='+i;
		ctr++;
	}	
	for (var i=0; i<querylist.length; i++)
	{
	    url = url + querylist[i];
		if ( i+1 < querylist.length) url += "&";
	}
     if (minpolish != '' && minpolish != undefined) url = url + "&minpolish=" + minpolish ;
     if (maxpolish != '' && maxpolish != undefined) url = url + "&maxpolish=" + maxpolish ;
	
     if (minsymmetry != '' && minsymmetry !=undefined) url = url + "&minsymmetry=" + minsymmetry ;
     if (minsymmetry != '' && minsymmetry !=undefined) url = url + "&maxsymmetry=" + maxsymmetry ;

     if (mindepth != '' && mindepth != undefined) url = url + "&mindepth=" + mindepth;
     if (maxdepth != '' && maxdepth !=undefined) url = url + "&maxdepth=" + maxdepth;

     if (mintable != '' && mintable != undefined) url = url + "&mintable=" + mintable;
     if (maxtable != '' && maxtable != undefined) url = url + "&maxtable=" + maxtable;

     if (minfluorescence != '' && minfluorescence !=undefined) url = url + '&minfluorescence=' + minfluorescence;
     if (maxfluorescence != '' && maxfluorescence !=undefined) url = url + '&maxfluorescence=' + maxfluorescence;

     if (minratio != '' && minratio !=undefined) url = url + '&minratio=' + minratio;
     if (maxratio != '' && maxratio !=undefined) url = url + '&maxratio=' + maxratio;

     if (sortby != '' && sortby !=undefined) url = url + "&sort_col=" + sortby ;
     if (orderby != '' && orderby !=undefined) url = url + "&sort_dir="+ orderby;
	 if (dreamdesign !='' && dreamdesign != undefined ) url = url + "&dd=" + dreamdesign ;
	return url; 
};   

function formatCurrency(num) 
{
	num = num.toString().replace(/\$|\,|\-/g,'');
	if(isNaN(num))	num = "0";	
	num = (Math.round(num*100)/100).toString();	
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return ( '$' + num );
} 

function initParams(mincarat, maxcarat, mincolor, maxcolor, maxcut, mincut, maxclarity, minclarity, minprice, maxprice)
    {   
        // setting variables for cut
        if(maxcut == '' || IsNumeric(maxcut)!= true) maxcut = 5;
        maxcut = parseInt(maxcut);
        gmaxcut = maxcut;

        if(mincut == '' || IsNumeric(mincut)!= true)
	        mincut = 1;
        mincut = parseInt(mincut);
        gmincut = mincut;
    		
        // setting variables for color
        if(maxcolor == ''  || IsNumeric(maxcolor)!= true)
	        maxcolor = 8;
        maxcolor = parseInt(maxcolor);
        gmaxcolor = maxcolor;

        if(mincolor == ''  || IsNumeric(mincolor)!= true)
	        mincolor = 1;
        mincolor = parseInt(mincolor);
        gmincolor = mincolor;
    	
        // setting variables for clarity
        if(maxclarity == '' || IsNumeric(maxclarity)!= true)
	        maxclarity = 10;
        maxclarity = parseInt(maxclarity);
        gmaxclarity = maxclarity;

        if(minclarity == '' || IsNumeric(minclarity)!= true)
	        minclarity = 1;
        minclarity = parseInt(minclarity);
        gminclarity = minclarity;
     		
        // setting variables for carat
        if(maxcarat == '' || IsNumeric(maxcarat)!= true)
	        maxcarat = maxCarat;
        maxcarat = parseFloat(maxcarat);
        gmaxcarat = maxcarat;

        if(mincarat == ''  || IsNumeric(mincarat)!= true)
	        mincarat = minCarat;
        mincarat = parseFloat(mincarat);
        gmincarat = mincarat;
    	
        if(maxprice == ''  || IsNumeric(maxprice)!= true)
	        maxprice = maxPrice;
        maxprice = parseFloat(maxprice);
        gmaxprice = maxprice;
    	
        if(minprice == ''  || IsNumeric(minprice)!= true)
	        minprice = minPrice;
        minprice = parseFloat(minprice);
        gminprice = minprice;    	
	}

function sortIt(col)
{
	var sortCol='Price' ;
	
	if (col==0 || col==15) return;
	
	sortCol=getColNameById(col);
	if ( col == sortObj.col)  //if the same column is clicked
	{
		if (sortObj.dir=='ASC')
		{
			setSortImage(col,'DESC');			
		}
		else
		{
			setSortImage(col,'ASC');			
		}
	}
	else
	{
		setSortImage(col,'ASC');        
	}
	//initParams(gmincarat, gmaxcarat, gmincolor, gmaxcolor, gmaxcut, gmincut, gmaxclarity, gminclarity, gminprice, gmaxprice);	
	// Save Sorting Order
	Cookie.set('SortOrder',sortObj.col,1);		
	Cookie.set('SortDirection',sortObj.dir,1);		
	var querylist = buildUrl(gmincarat, gmaxcarat, gmaxcolor, gmincolor, gmaxcut, gmincut, gmaxclarity, gminclarity, gminprice, gmaxprice, gminpolish, gmaxpolish, gminsymmetry, gmaxsymmetry, gmindepth, gmaxdepth, gmintable, gmaxtable, gminfluorescence, gmaxfluorescence, gminratio, gmaxratio, sortCol, sortObj.dir);
    mygrid.clearAll(false);
var productElement = document.getElementById("chkGIA");
if (productElement != null)
{
if(productElement.checked)
{
 querylist=querylist+"&Report=GIA"
}
}
var shpElement = document.getElementById("optPshape");
if (shpElement != null)
{
 if(shpElement.value!=''){
   querylist=querylist+'&shape='+shpElement.value;   
  }
}

 //if(document.getElementById("chkGIA").checked ) 
 //       querylist=querylist+"&Report=GIA"
    mygrid.loadXML(querylist);
}


function setSortImage(col, dir)
{
	// Reset image on previously sorted column 
	var prevImg=sortObj.col + 'img';
	$(prevImg).setAttribute('src','images/sliders/arrow_down.gif');	
	// Update search state
	sortObj.col=col;
	sortObj.dir=dir;
	if (dir.toLowerCase() == "asc" ) $(col+'img').setAttribute('src','images/sliders/arrow_up.gif');
		else $(col+'img').setAttribute('src','images/sliders/arrow_down.gif');
}

function getColNameById(id)
{
	var sortCol;
	switch (id)
	{
            case 0: sortCol=''; break;
            case 1: sortCol='shape'; break;
			case 2: sortCol='carats'; break;
			case 3: sortCol='cut'; break;
            case 4: sortCol='color'; break;
            case 5: sortCol='clarity'; break;
            case 6: sortCol='report'; break;
            case 7: sortCol='polish'; break;
            case 8: sortCol='symmetry'; break;
            case 9: sortCol='depth'; break;
            case 10: sortCol='table'; break;
            case 11: sortCol='fluorescence'; break;
            case 12: sortCol='ratio'; break;
            case 13: sortCol='culet'; break;
            case 14: sortCol='price'; break;
            case 15: sortCol=''; break;
	}
	return sortCol;
}

function showGIA(id)
{
 	window.open('diamonddimensions.asp?idProduct='+id,'','toolbar=no,location=no, directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width=400,height=400');
}
  

