var isAjaxcall = false;
var isviewType = false;

function sortResults(paramName,paramValue,targetDIV) {
	targetDIV =  checktargetDIV(targetDIV);
	isAjaxcall = true;
	var page = "";
	if (paramName == 'viewType') {
		isviewType = true;
		page = prepareParameter(null,null);
		page += "&"+ paramName + "=" + paramValue;
		isviewType = false;
	} else if (paramName == 'hstart') {
		page = prepareParameter(null,null);
		page += "&"+ paramName + "=" + paramValue;
	} else {
		page = prepareParameter(null,null);
	}
	page += "&isAjax=true";
	isAjaxcall = false;
	searchResults(page,targetDIV);
}

function checktargetDIV(targetDIV) {
	if(document.getElementById(targetDIV) == null ) {
		if(document.getElementById("searchResultDisplay") != null) { // check if the target div exists?
			targetDIV = "searchResultDisplay";
		} else {								// else load results of complete Layout body.
			targetDIV = "searchLayoutBody";  
		}
	}
	return targetDIV;
}
function narrowSearch(targetDIV) 
{
	//alert('Sandeep');
	targetDIV = checktargetDIV(targetDIV);
	isAjaxcall = true;
	page = prepareParameter(null,null);
//	window.location = page;
//	return false;
	page = page + "&isAjax=true";
	isAjaxcall = false;
	
	prodIDs = "";  // removing all checked products if narrow search is called
	objIDs = "";
	cataIDs = "";
	//alert('categoryFilter:: page====' + page);
	searchResults(page,targetDIV);
}
function getPreparedURL () {
	isAjaxcall = true;
	page = prepareParameter(null,null);
	page = page + "&isAjax=true";
	isAjaxcall = false;
	return page;
}
function prepareParameter(paramName,paramValue)
{
	
	var endPrice="";
	var startPrice="";
	

	var sortBy= '';
	var viewType= '';

	  //////////// Temp cludge////////////
	 priorityForm = 'searchLayoutForm';
	  
	  ////////////////////////////////////
	  
	  for(k=0; k<document.forms.length; k++)
	  {
	  	form = document.forms[k];
	  	var elems = form.elements;
		
	  	for( var i=0; i<elems.length; i++) 
	  	{
	  		if (elems[i].type == 'text')
			{
				
				if(elems[i].id.indexOf(':dct')!= -1){
					elems[i].value=getSellerIds();
				}
				if( elems[i].id.indexOf("slider" + ':startPrice') != -1 ) {
					if(elems[i].value != 0)
						startPrice = elems[i].value;
				} 
				if( elems[i].id.indexOf("slider" + ':endPrice') != -1 ){
					if(elems[i].value != 0)
						endPrice = elems[i].value;
				}
				if(endPrice !="" && startPrice !="")
				{
					priceRange=startPrice+"-"+endPrice;
				}
				else if (endPrice =="" && startPrice !="")
				{
					priceRange=startPrice+"-"+99999999;
				}
					else if (endPrice !="" && startPrice =="")
				{
					priceRange="0"+"-"+endPrice;
				}
			else
					priceRange="";
			}

		
	    }
	  }
	 var additionalParams = "";
    s_search_letter=document.getElementById("search_letter").value;
	 additionalParams = 'startYear='+startPrice+'&endYear='+endPrice+'&search_t='+s_search_letter;
				 
//alert(additionalParams);
	if(typeof current_page == "undefined") {
		current_page='searchResult.php';
	}
	if(typeof path == "undefined") {
		path = '';
	}

	page = current_page + '?search=y&'+ additionalParams;
	return page;
}


function CheckKeys()
{
  if((event.keyCode<=46)||(event.keyCode>57)||(event.keyCode==47) )
  {
	event.keyCode=0;
  }
}
/**
*This sets the tip value
*in given id
*@param
*/		
function setValue(spanid)
{
	var minPrice ="" ;
	var maxPrice = "" ;
	var oElements = document.forms[0].elements;
	for(var  i=0; i < oElements.length; i++)
	{
		var curr = oElements[i];
		if(curr.type=='text')
		{
			if(curr.id.indexOf("startPrice") != -1){
				if(curr.value.length<9)
					minPrice =curr.value;
				else{
					alert("You can not enter more than 8 digit");
					curr.value="";
					minPrice=curr.value;
				}
			}
			if(curr.id.indexOf("endPrice") != -1){
				if(curr.value.length<9)
					maxPrice =curr.value;
				else{
					alert("You can not enter more than 8 digit");
					curr.value="";
					maxPrice=curr.value ;
				}
			}
		}
	}

	var span = document.getElementsByTagName("SPAN");
	var msgStr="(Available from Rs." + minPrice + "/- to Rs." + maxPrice+ "/-)";
	for(var i=0 ; i< span.length; i++)
	{
		var currentSpan=span[i];
		if(currentSpan.id.indexOf(spanid) != -1)
				currentSpan.innerText = msgStr;
	}
}

function checkvalue()
{
	var minPrice = "";
	var maxPrice = "99999999" ;
	var oElements = document.forms[0].elements;
	for(var  i=0; i < oElements.length; i++)
	{
		var curr = oElements[i];
		if(curr.type=='text')
		{
			if(curr.id.indexOf("startPrice") != -1){
				if(curr.value.length>0)
					minPrice =curr.value;
			}
			if(curr.id.indexOf("endPrice") != -1){
				if(curr.value.length>0)
					maxPrice =curr.value;
			}
		}
	}
	if(minPrice.length>8){
		alert("You can not enter more than 8 digit");
		return false;
	}
	if(maxPrice.length>8){
		alert("You can not enter more than 8 digit");
		return false;
	}
	if( maxPrice/minPrice<1){
		alert("Second prize value should be greater than 1st one");
		return false;
	}
}

/**
*This is the helper method that 
*returns comma separated id
*@param whichChkbox
*			-this is the id for which we are collecting Ids
*/



//// slider.js ---- START

var xMax = 0;
var xMin = 0;
var swap = new Boolean(false);
displayObj = "";
displayObj_ref = "";

function carpeGetElementById(element)
{
	if (document.getElementById) element = document.getElementById(element);
	else if (document.all) element = document.all[element];
	else element = null;
	return element;
}
// carpeLeft: Cross-browser version of "element.style.left"
function carpeLeft(elmnt, pos)
{
	if (!(elmnt = carpeGetElementById(elmnt))) return 0;
	if (elmnt.style && (typeof(elmnt.style.left) == 'string')) {
		if (typeof(pos) == 'number') elmnt.style.left = pos + 'px';
		else {
			pos = parseInt(elmnt.style.left);
			if (isNaN(pos)) pos = 0;
		}
	}
	else if (elmnt.style && elmnt.style.pixelLeft) {
		if (typeof(pos) == 'number') elmnt.style.pixelLeft = pos;
		else pos = elmnt.style.pixelLeft;
	}
	return pos;
}
// carpeTop: Cross-browser version of "element.style.top"
function carpeTop(elmnt, pos)
{
	if (!(elmnt = carpeGetElementById(elmnt))) return 0;
	if (elmnt.style && (typeof(elmnt.style.top) == 'string')) {
		if (typeof(pos) == 'number') elmnt.style.top = pos + 'px';
		else {
			pos = parseInt(elmnt.style.top);
			if (isNaN(pos)) pos = 0;
		}
	}
	else if (elmnt.style && elmnt.style.pixelTop) {
		if (typeof(pos) == 'number') elmnt.style.pixelTop = pos;
		else pos = elmnt.style.pixelTop;
	}
	return pos;
}
// moveSlider: Handles slider and display while dragging
count = 0;
function moveSlider(evnt)
{
	mousemove =true;
	var evnt = (!evnt) ? window.event : evnt; 	// The mousemove event
	if (mouseover) { 				// Only if slider is dragged
		x = pxLeft + evnt.screenX - xCoord 	// Horizontal mouse position relative to allowed slider positions
		y = pxTop + evnt.screenY - yCoord 	// Horizontal mouse position relative to allowed slider positions
		
		if (x > xMax) x = xMax  	// Limit horizontal movement
		if (x < xMin) x = xMin 		// Limit horizontal movement
		if (y > yMax) y = yMax 		// Limit vertical movement
		if (y < 0) y = 0 		// Limit vertical movement
		
		displayObj_A = carpeGetElementById('slider:startPrice') ;
		displayObj_B = carpeGetElementById('slider:endPrice') ;
		price_A = parseInt (displayObj_A.value) ;
		price_B = parseInt (displayObj_B.value) ;
		if(price_A > price_B) {
			swapElements();
			(swap == true) ? swap = false : swap = true ;
		}
		carpeLeft(sliderObj.id, x)  	// move slider to new horizontal position
		carpeTop(sliderObj.id, y) 	// move slider to new vertical position
		sliderVal = x + y 		// pixel value of slider regardless of orientation
		sliderPos = (sliderObj.pxLen / sliderObj.valCount) * Math.round(sliderObj.valCount * sliderVal / sliderObj.pxLen)
		v = Math.round((sliderPos * sliderObj.scale + sliderObj.fromVal) * // calculate display value
		Math.pow(10, displayObj.dec)) / Math.pow(10, displayObj.dec)
		displayObj.value = v 		// put the new value in the slider display element
		return false
	}
	return
}

function swapElements() {
	var temp_ref = displayObj;
	displayObj = displayObj_ref;
	displayObj_ref = temp_ref;		
}

// moveSlider: Handles the start of a slider move.
function slide(evnt,slider_id,slider_ref_id, orientation, length, from, to, count, decimals, display, display_ref)
{
	//alert('22222222            ' +from + '        ' + to);
	if (!evnt) evnt = window.event;
	sliderObj = (evnt.target) ? evnt.target : evnt.srcElement; // Get the activated slider element.
	sliderObj.pxLen = length // The allowed slider movement in pixels.
	sliderObj.valCount = count ? count - 1 : length // Allowed number of values in the interval.
	sliderObj_ref = carpeGetElementById(slider_ref_id);
	
	displayObj = carpeGetElementById(display) // Get the associated display element.
	displayObj_ref = carpeGetElementById(display_ref) // Get the second display element so that range can be determined.

	if(swap == true) {	//swapping the references if the sliders cross each other.
		swapElements();
	}
	
	displayObj.dec = decimals // Number of decimals to be displayed.
	sliderObj.scale = (to - from) / length // Slider-display scale [value-change per pixel of movement].
	if (orientation == 'horizontal') { // Set limits for horizontal sliders.
		sliderObj.fromVal = from
		xMax = length;
		xMin = 0; 
		yMax = 0
	}
	if (orientation == 'vertical') { // Set limits and scale for vertical sliders.
		sliderObj.fromVal = to
		xMax = 0
		yMax = length
		sliderObj.scale = -sliderObj.scale // Invert scale for vertical sliders. "Higher is more."
	}
	pxLeft = carpeLeft(sliderObj.id) // Sliders horizontal position at start of slide.
	pxTop  = carpeTop(sliderObj.id) // Sliders vertical position at start of slide.
	xCoord = evnt.screenX // Horizontal mouse position at start of slide.
	yCoord = evnt.screenY // Vertical mouse position at start of slide.
	mouseover = true
	mousemove = false;
	document.onmousemove = moveSlider // Start the action if the mouse is dragged.
//	document.onmouseout = sliderMouseUp // Stop sliding.
	document.onmouseup = sliderMouseUp // Stop sliding.
}
// sliderMouseup: Handles the mouseup event after moving a slider.
// Snaps the slider position to allowed/displayed value. 
function sliderMouseUp()
{

	v = (displayObj.value) ? displayObj.value : 0 // Find last display value.
	pos = (v - sliderObj.fromVal)/(sliderObj.scale) // Calculate slider position (regardless of orientation).
	if (yMax == 0) carpeLeft(sliderObj.id, pos) // Snap horizontal slider to corresponding display position.
	if (xMax == 0) carpeTop(sliderObj.id, pos) // Snap vertical slider to corresponding display position.
	try {
		if (document.removeEventListener) { // Remove event listeners from 'document' (Moz&co).
			document.removeEventListener('mousemove', moveSlider)
			document.removeEventListener('mouseup', sliderMouseUp)
		} else if (document.detachEvent) { // Remove event listeners from 'document' (IE&co).
			document.detachEvent('onmousemove', moveSlider)
			document.detachEvent('onmouseup', sliderMouseUp)
		}
	} catch(e) {}
	if(mouseover && mousemove) narrowSearch('searchResultDisplay');
	mouseover = false // Stop the sliding.	
	mousemove = false;
}

//// slider.js ---- END

//narow Search Javascipt start
function showOpts(obj,id) {
document.getElementById(obj).style.display =
(document.getElementById(obj).style.display=='block') ? 'none':'block';
	swapImage(id);
}

/* This will change the = & - image*/
function swapImage(componentId)
{
	swapableImd = document.getElementById(''+componentId);
	var imgSrc = swapableImd.src;
	var lstIndx = imgSrc.lastIndexOf('/');
	var startSrc = imgSrc.substring(0,lstIndx);
	var lastSrc = imgSrc.substring(lstIndx+1);
	var imgUrl;

	if(lastSrc == 'plus.jpg')
		imgUrl = 'minus.jpg'
	else
		imgUrl = 'plus.jpg'

	swapableImd.src =startSrc + '/' +imgUrl;
}
//narow Search Javascipt End
