function check_form(frm) {
	var error_found = false;
	var error_message = "";
	var error_field;
	var radio_checked = false;
	var cbox_checked = false;
	var price_from = document.diamond_search.pfrom.value;
	var price_to = document.diamond_search.pto.value;
	var carat_from = document.diamond_search.carat_from.value;
	var carat_to = document.diamond_search.carat_to.value;
	var carat_min_value = document.diamond_search.carat_min_value.value;
	var carat_max_value = document.diamond_search.carat_max_value.value;
	var price_min_value = document.diamond_search.price_min_value.value;
	var price_max_value = document.diamond_search.price_max_value.value;
	var elems = frm.elements;

	var error_search_price_from = document.getElementById('error_search_price_from').value;
	var error_search_price_to = document.getElementById('error_search_price_to').value;
	var error_search_carat_from = document.getElementById('error_search_carat_from').value;
	var error_search_carat_to = document.getElementById('error_search_carat_to').value.value;


	//To make atleast one shape to select by default if none is selected by the user.
	for (i=0; i<elems.length; i++) {
		if (elems[i].type == "radio" && elems[i].checked) {
			radio_checked = true;
		}
		else if (elems[i].type == "checkbox" && elems[i].checked) {
			cbox_checked = true;
		}
	}

	if (!cbox_checked) {
		for (i=0; i<elems.length; i++) {
			if (elems[i].type == "checkbox" && elems[i].name == "cbox[0]") {
				elems[i].checked = true;
			}
		}
	}

	if (!radio_checked) {
		for (i=0; i<elems.length; i++) {
			if (elems[i].type == "radio" && elems[i].value == 1) {
				elems[i].checked = true;
			}
		}
	}

	//To check whether the Price From field contains only numeric values.
	if (price_from.length > 0) {
		if (isNaN (price_from) || price_from<0) {
			error_message = error_message + "*" + error_search_price_from+"\n";
			error_found = true;
			error_field = document.diamond_search.pfrom;
		}
/*		if (Number(price_from)<Number(price_min_value)) {
			error_message = error_message + "*Price From must be greater than "+price_min_value+"\n";
			error_found = true;
			error_field = document.diamond_search.pfrom;
		}
		if (Number(price_from)>Number(price_max_value)) {
			error_message = error_message + "*Price From must be less than "+(price_max_value)+"\n";
			error_found = true;
			error_field = document.diamond_search.pfrom;
		}*/
	}
	//To check whether the Price To field contains only numeric values.
	if (price_to.length > 0) {
		if (isNaN (price_to) || price_to<0) {
			error_message = error_message + "*" + error_search_price_to+"\n";
			error_found = true;
			error_field = document.diamond_search.pto;
		}
/*		if (Number(price_to)>Number(price_max_value)) {
			error_message = error_message + "*Price To must be less than "+Number(price_max_value)+"\n";
			error_found = true;
			error_field = document.diamond_search.pto;
		}
		if (Number(price_to)<Number(price_min_value)) {
			error_message = error_message + "*Price To must be greater than "+Number(price_min_value)+"\n";
			error_found = true;
			error_field = document.diamond_search.pto;
		}*/
	}

	//if Price From bigger than Price To then change the values of the Price From and To
	if ((Number(price_to) < Number(price_from)) && (price_from.length > 0) && (price_to.length > 0)){
		document.diamond_search.pfrom.value = price_to;
		document.diamond_search.pto.value = price_from;
	}

	//To check whether the Carat From field contains only numeric values.
	if (carat_from.length > 0) {
		if (isNaN (carat_from) || carat_from<0) {
			error_message = error_message + "*" + error_search_carat_from+"\n";
			error_found = true;
			error_field = document.diamond_search.carat_from;
		}
/*		else if (Number(carat_from) < Number(carat_min_value)) {
			error_message = error_message + "*Carat From value must be greater than "+Number(carat_min_value)+"\n";
			error_found = true;
			error_field = document.diamond_search.carat_from;

		}
		else if (Number(carat_from) > Number(carat_max_value)) {
			error_message = error_message + "*Carat From value must be lesser than "+(Number(carat_max_value))+"\n";
			error_found = true;
			error_field = document.diamond_search.carat_from;
		}*/
	}
	//To check whether the Carat To field contains only numeric values.
	if (carat_to.length > 0) {
		if (isNaN (carat_to) || carat_to<0) {
			error_message = error_message + "*" + "Carat To must be a number"+"\n";
			error_found = true;
			error_field = document.diamond_search.carat_to;
		}
/*		else if (Number(carat_to) < Number(carat_min_value)) {
			error_message = error_message + "*Carat To value must be greater than "+Number(carat_min_value)+"\n";
			error_found = true;
			error_field = document.diamond_search.carat_to;
		}
		else if (Number(carat_to) > Number(carat_max_value)) {
			error_message = error_message + "*Carat To value must be lesser than "+Number(carat_max_value)+"\n";
			error_found = true;
			error_field = document.diamond_search.carat_to;
		}*/
	}

	//if Carat From bigger than Carat To then change the values of the Carat From and To
	if ((Number(carat_to) < Number(carat_from)) && (carat_from.length > 0) && (carat_to.length > 0)){
		document.diamond_search.carat_from.value = carat_to;
		document.diamond_search.carat_to.value = carat_from;
	}

	if (error_found == true) {
		alert (error_message);
		error_field.focus();
		return false;
	}
}

function show_more_search_options(round_shape) {
	if (document.getElementById('more_search_options').style.display == "none") {
		document.getElementById('lowspacer').style.display = "none";
		document.getElementById('more_search_options').style.display = "block";
	}
	else {
		document.getElementById('lowspacer').style.display = "block";
		document.getElementById('more_search_options').style.display = "none";
	}
	if (document.getElementById('more_search_options').innerHTML.indexOf("Loading...")!=-1)
	{
		var xmlHttp;
		xmlHttp=GetXmlHttpObject();

		if (xmlHttp==null)  {
		  alert ("Your browser does not support AJAX!");
		  return;
		} 
		var url="landing_adv.php";
		var urlargs="";
		urlargs = "round_shape="+round_shape;
		/*alert(urlargs);
		xmlHttp.onreadystatechange=advstatecha;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(urlargs);*/

		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", urlargs.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.onreadystatechange=function(){
				if (xmlHttp.readyState==4) { 
					document.getElementById('more_search_options').innerHTML=xmlHttp.responseText;
				}
			};
		xmlHttp.send(urlargs);
	}

}


function chkObject (theVal) {
	if (document.getElementById(theVal) != null) {
	  return true;
	}
	else {
	   return false;
	}
}

function image_rollover(div_val,count) {
//hide divs
for(i=1; i<=count;i++) {
	if(chkObject('div'+i)) {
		document.getElementById('div'+i).style.display='none';
	}
}
//show divs
	if(chkObject('div'+div_val)) {
		document.getElementById('div'+div_val).style.display='block';
	}
}

function image_rollovers(div_val,count) {
//hide divs
for(i=1; i<=count;i++) {
	if(chkObject('div_sd'+i)) {
		document.getElementById('div_sd'+i).style.display='none';
	}
}
//show divs
	if(chkObject('div_sd'+div_val)) {
		document.getElementById('div_sd'+div_val).style.display='block';
	}
}

//To set default shape as round when the form gets loaded
function set_default_shape() {
	var elems = document.diamond_search.elements;
	for (i=0; i<elems.length; i++) {
		if (elems[i].type == "radio" && elems[i].value == 1) {
			elems[i].checked = true;
		}
	}
}

//To change the Cut image and Cut drop down value in selection of the shape
function search_cut_display(frm, selectedVal) {
	//To get total diamond information based on the shape selection.
	cutstart=100;
	inpelem=document.getElementsByTagName("input");
	for (i=0; i<inpelem.length; i++)
	{
		curelem = inpelem[i];
		if (curelem.type=='checkbox' && curelem.checked==true && curelem.name.substr(0,3) == 'cut' )
		{
			cutstart=curelem.value;
			break;
		}
	}
	showTotalDiamond(selectedVal, cutstart);
}



function GetXmlHttpObject() {
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
function showTotalDiamond(shape_id, cut_val) {
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)  {
	  alert ("Your browser does not support AJAX!");
	  return;
	} 
	
	var url="get_total_diamond.php";
	url = url + "?shape_id="+ shape_id;
	url = url + "&cut_val="+ cut_val;
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4) { 
			retval=xmlHttp.responseText.split("#$#");
			document.getElementById("total_diamond_info").innerHTML=retval[0];
			document.diamond_search.price_min_value.value=retval[1];
			document.diamond_search.price_max_value.value=retval[2];
		}
	};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function expandDiv() {
	document.getElementById('magnifier').style.display="none";
	document.getElementById('printreport').style.display="none";
	document.getElementById('expander').style.display="block";
}

function reduceDiv() {
	document.getElementById('magnifier').style.display="block";
	document.getElementById('printreport').style.display="block";
	document.getElementById('expander').style.display="none";
}

function printReport(imgval) {
	window.open("diamond_certificate_details.php?display_image="+imgval,"gradient_info", "location=0, scrollbars=1, resizable=0");
}

function callConfirm(type, item_id, cat_id, nod, search_id) {
	var input;
	if (type == "diamond") {
		input = confirm("Changing your diamond will remove\nyour current diamond.\n\nAre you sure want to do this?");
		if (input) {
			if(!cat_id > 0) {
				window.location.href = "diamond_search.php?basic"+"&search_id="+search_id+"&clear=1";
			}
			else {
				window.location.href = "diamond_search.php?basic&clear=1"+"&search_id="+search_id+"&cat_id="+cat_id+"&nod="+nod;
			}
		}
	}
	else {
		input = confirm("Changing your style will remove\nyour current style.\n\nAre you sure want to do this?");
		if (input) {
			if (!cat_id > 0) {
				window.location.href = "build_ring.php?ring_landing&clear=1";
			}
			else {
				window.location.href = "build_ring.php?ring_landing&clear=1"+"&cat_id="+cat_id+"&nod="+nod;
			}
		}
	}
}

function checkfun(form, control, ordid, checked)
{
	valstr = "";
	valstr1="";
	conlen=control.length;
	inpelem=document.getElementsByTagName("input");
	if (checked==true)
	{
		for (i=0; i<inpelem.length; i++)
		{
			curelem = inpelem[i];
			if (curelem.type=='checkbox' && curelem.checked==true && curelem.name.substr(0,conlen) == control )
			{
				valstr=valstr+curelem.name.substr(conlen)+",";
			}
		}

		valstr1= valstr.substr(0,valstr.length-1);
		parts=valstr1.split(",");
		total=parts[parts.length-1]-parts[0];
		if (total>=2)  
		{
			for (j=parseInt(parts[0])+1;j<parseInt(parts[parts.length-1]);j++)
			{
				eval("document."+form.name+"."+control+j+".checked=true;");
				eval("document."+form.name+"."+control+j+".disabled=true;");
			}
		}
	}
	else
	{
		// uncheck
		for (i=0; i<inpelem.length; i++)
		{
			curelem = inpelem[i];
			if (curelem.type=='checkbox' && curelem.checked==true && curelem.name.substr(0,conlen) == control && curelem.disabled==false )
			{
				valstr=valstr+curelem.name.substr(conlen)+",";
			}
		}

		valstr1= valstr.substr(0,valstr.length-1);
		parts=valstr1.split(",");
		total=parts[parts.length-1]-parts[0];
		if (total<=1)
		{
			minlim = parseInt(parts[0])+1;
			maxlim = parseInt(ordid);
			if (minlim<=maxlim)
			{
				for (j=minlim;j<maxlim;j++)
				{
					eval("document."+form.name+"."+control+j+".disabled=false;");
					eval("document."+form.name+"."+control+j+".checked=false;");
				}
			}
			else
			{
				for (j=minlim-2;j>maxlim;j--)
				{
					eval("document."+form.name+"."+control+j+".disabled=false;");
					eval("document."+form.name+"."+control+j+".checked=false;");
				}
			}
		}
	}
}

function switchto(control)
{
	ctl=document.getElementById(control).style.backgroundImage;
	l=ctl.length;
	if (ctl.search('_a')!=-1) ctl=ctl.replace('_a', '_d');
	else ctl=ctl.replace('_d', '_a');
	document.getElementById(control).style.backgroundImage=ctl;
}

function checkshape(td)
{
	chkd=td.className;
	if (chkd=="deselected")
	{
		td.className="selected";
	}
	else
	{
		td.className="deselected";
	}
	selstr="";
	tdsel = td.parentNode.getElementsByTagName('td');
	for(i = 0; i < tdsel.length; i++)
	{
		if(tdsel[i].className=="selected")
		{
			selstr +=  (i+1)+",";
		}
	}
	document.diamond_basic_search.shapesel.value= selstr.substr(0,selstr.length-1);
	subdata();
}

function swapbut(hand)
{
	if (hand.src.indexOf("searchup")!=-1)
	{
		hand.src = hand.src.replace("searchup", "searchdown");
		document.getElementById('subm1').style.display="block";
	}
	else
	{
		hand.src = hand.src.replace("searchdown", "searchup");
		document.getElementById('subm1').style.display="none";
	}
	return true;
}


function changeico(hand, stat)
{
	if (stat==1)
	{
		hand.src = hand.src.replace("grey-icon", "red-icon");
	}
	else
	{
		hand.src = hand.src.replace("red-icon", "grey-icon");
	}
}