function GetRadioCheckedValue(formName, radioName)
{
	//return selected object value
	var radioLength = document.forms[formName][radioName].length;
	for( i = 0; i < radioLength; i++ )
	{
		if(document.forms[formName][radioName][i].checked == true )
			return document.forms[formName][radioName][i].value;		
	}
	
	//no one is selected
	return false;
}

function RemoveWhiteSpace(stringValue)
{
     return stringValue.replace(/\s+/g,'');
}
function Trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function LTrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function RTrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

/*Fitler*/
function FilterAjaxFunction(section)
{
	//get ajax request
	var ajaxRequest = CreateAjaxRequest();
	if(ajaxRequest == null) return false;
	
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function()
	{		
		if(ajaxRequest.readyState == 4)
		{			
			//show result on div
			var ajaxDisplay = document.getElementById("filterResultDIV");
			ajaxDisplay.innerHTML = "";
			
			if(ajaxRequest.responseText)
			{
				//ajaxDisplay.innerHTML = ajaxRequest.responseText;
				//ajaxDisplay.innerHTML = "<div id='divCtrl' title='Close filter result' class=\"divCtrlDIV\"><img id=\"closeDIV\" src=\"'+normal_path+'image/close.jpg\" border=0 onclick=\"document.getElementById('filterCtrlTABLE').style.display = 'block'; document.getElementById('mainResultDIV').style.display = 'none';  document.getElementById('filterResultDIV').style.display = 'none'; document.getElementById('filterResultDIV').innerHTML=''; \"></div>";
				
				var jsonData = eval('(' + ajaxRequest.responseText + ')');
				var length = jsonData.length;
				var limitmsg ='';
				if(length >= 60)
				{
					limitmsg = "(Showing relavant 60 phones)"; 
				}
				//fixing ie & mozila problem
				var txt = "<div id='divCtrl' title='Close filter result' class=\"divCtrlDIV\"><img style=\"cursor:pointer;\" id=\"closeDIV\" src=\""+normal_path+"image/templateImg/close.jpg\" border=0 onclick=\" ";
				if(isIE())
					txt += " document.getElementById('filterCtrlTABLE').style.display = 'block'; ";
				else
					txt += " document.getElementById('filterCtrlTABLE').style.display = 'table'; ";				
				txt += " document.getElementById('filterBtnID').style.display = 'block';document.getElementById('filterREsetBtnID').style.display = 'block';document.getElementById('mainResultDIV').style.display = 'none';  document.getElementById('filterResultDIV').style.display = 'none'; document.getElementById('filterResultDIV').innerHTML=''; \"><span class=\"filterPhonelimitSpan\" >"+limitmsg+"</span></div>";
				

				ajaxDisplay.innerHTML = txt;
				
				for(i=0; i<length; i++)
				{					
					//onclick=\"window.open('" + jsonData[i].bname +"-" + RemoveWhiteSpace(jsonData[i].model) + "-spec-" + jsonData[i].pid + ".html', '_self');
					ajaxDisplay.innerHTML += 	"<div id=\"specPhoneHolder" + jsonData[i].pid + "\" class=\"filterPhoneHolderNormal\" onmouseover=\"this.className='filterPhoneHolderHover'; allPhonesHoverOn(this.id);\" onmouseout=\"this.className='filterPhoneHolderNormal'; allPhonesHoverOff(this.id);\" >"										 	
										  +		"   <a class=\"phnLinkA\" href=\"" + jsonData[i].bname +"-" + RemoveWhiteSpace(jsonData[i].model) + "-spec-" + jsonData[i].pid + ".html\" title=\"" + jsonData[i].bname + " " + jsonData[i].model + "\">"
										  +		"	<img src=\""+normal_path+"image/Brand/simage/" + jsonData[i].pimage + "\" border=0 width=70 height=90> "
										  +		"	<div id=\"specPhoneName" + jsonData[i].pid + "\" class=\"specPhoneNameNormalDIV\">" + jsonData[i].bname + " " + jsonData[i].model + "</div>"
										  +		"   </a>"
										  +		"</div>";
										  
				}
				
				var mainResultDIV = document.getElementById("mainResultDIV");				
				mainResultDIV.className = 'showMainResultDIV';
				ajaxDisplay.className = 'showFilterResultDIV';				
				document.getElementById("filterCtrlTABLE").style.display = 'none';				
				mainResultDIV.style.display = 'block';
				ajaxDisplay.style.display = 'block';				
			}
		}
		else
		{
			var ajaxDisplay = document.getElementById("filterResultDIV");
				ajaxDisplay.style.display = 'block';
				
				ajaxDisplay.innerHTML = '<div style="padding-top:280px;"><img src="image/templateImg/gallery_loader.gif" ></div>';
			
			document.getElementById("filterCtrlTABLE").style.display = 'none';
			document.getElementById("filterBtnID").style.display = 'none';
			document.getElementById("filterREsetBtnID").style.display = 'none';
			
			var mainResultDIV = document.getElementById("mainResultDIV");				
				mainResultDIV.className = 'showMainResultDIV';
				ajaxDisplay.className = 'showFilterResultDIV';				
				mainResultDIV.style.display = 'block';
				
		}
	}
	
	
	/*Get element value & set it as paramiter*/
	var model = document.getElementById("model").value;
	var brand = document.getElementById("brand").value;
	//window.alert(brand);
	var chkGSM850 = document.getElementById("chkGSM850").checked;
	var chkGSM900 = document.getElementById("chkGSM900").checked;
	var chkGSM1800 = document.getElementById("chkGSM1800").checked;
	var chkGSM1900 = document.getElementById("chkGSM1900").checked;
	//window.alert(chkGSM1900);
	var chkUMTS850 = document.getElementById("chkUMTS850").checked;
	var chkUMTS1900 = document.getElementById("chkUMTS1900").checked;
	var chkUMTS2100 = document.getElementById("chkUMTS2100").checked;
	//window.alert(chkUMTS2100);
	var chkHSDPA850 = document.getElementById("chkHSDPA850").checked;
	var chkHSDPA1900 = document.getElementById("chkHSDPA1900").checked;
	var chkHSDPA2100 = document.getElementById("chkHSDPA2100").checked;
	//window.alert(chkHSDPA2100);
	var height = document.getElementById("height").value;
	var heightOperation = GetRadioCheckedValue("filterFrm", "heightOperation");
	var width = document.getElementById("width").value;
	var widthOperation = GetRadioCheckedValue("filterFrm", "widthOperation");
	var thickness = document.getElementById("thickness").value;
	var thicknessOperation = GetRadioCheckedValue("filterFrm", "thicknessOperation");
	//window.alert(thicknessOperation);
	var weight = document.getElementById("weight").value;
	var displayColor = document.getElementById("displayColor").value;
	var camera = document.getElementById("camera").value;
	//window.alert(camera);
	var iMemoroy = document.getElementById("iMemoroy").value;
	var iMemoroyOperation = GetRadioCheckedValue("filterFrm", "iMemoroyOperation");
	var eMemoroy = document.getElementById("eMemoroy").value;
	var eMemoroyOperation = GetRadioCheckedValue("filterFrm", "eMemoroyOperation");
	var standBy = document.getElementById("standBy").value;
	var standByOperation = GetRadioCheckedValue("filterFrm", "standByOperation");
	var talkTime = document.getElementById("talkTime").value;
	var talkTimeOperation = GetRadioCheckedValue("filterFrm", "talkTimeOperation");
	//window.alert(talkTimeOperation);
	var chkGPRS = document.getElementById("chkGPRS").checked;
	var chkEDGE = document.getElementById("chkEDGE").checked;
	var chkWLAN = document.getElementById("chkWLAN").checked;
	var chkGPS = document.getElementById("chkGPS").checked;	
	var chkBluetooth = document.getElementById("chkBluetooth").checked;
	var chkInfrared = document.getElementById("chkInfrared").checked;
	var chkUSB = document.getElementById("chkUSB").checked;
	var chkFullKeyboard = document.getElementById("chkFullKeyboard").checked;
	var chkTouchScreen = document.getElementById("chkTouchScreen").checked;
	var chkVoiceCommand = document.getElementById("chkVoiceCommand").checked;
	var chkRadio = document.getElementById("chkRadio").checked;
	var chkFlashLight = document.getElementById("chkFlashLight").checked;
	//window.alert(chkLouadSpeaker);
	
	if(model != '' || brand != -1 || chkGSM850 != false || chkGSM900 != false || chkGSM1800 != false || chkGSM1900 != false || chkUMTS850 != false  || chkUMTS1900 != false || chkUMTS2100 != false || chkHSDPA850 != false || chkHSDPA1900 != false || chkHSDPA2100 != false || height != 0 || width != 0 || thickness != 0 || weight != 0 || displayColor != 0 || camera != 0 || iMemoroy != 0 || eMemoroy != 0 || standBy != 0 || talkTime != 0 || chkGPRS != false || chkEDGE != false || chkWLAN != false || chkGPS != false || chkBluetooth != false || chkInfrared != false || chkUSB != false || chkFullKeyboard != false || chkTouchScreen != false || chkVoiceCommand != false || chkRadio != false || chkFlashLight != false)
	{
		//now set parameter	
		var queryString = "model=" + model + "&brand=" + brand;
		queryString += "&chkGSM850=" + chkGSM850 + "&chkGSM900=" + chkGSM900 + "&chkGSM1800=" + chkGSM1800 + "&chkGSM1900=" + chkGSM1900;
		queryString += "&chkUMTS850=" + chkUMTS850 + "&chkUMTS1900=" + chkUMTS1900 + "&chkUMTS2100=" + chkUMTS2100;
		queryString += "&chkHSDPA850=" + chkHSDPA850 + "&chkHSDPA1900=" + chkHSDPA1900 + "&chkHSDPA2100=" + chkHSDPA2100;
		queryString += "&height=" + height + "&heightOperation=" + heightOperation;
		queryString += "&width=" + width + "&widthOperation=" + widthOperation;
		queryString += "&thickness=" + thickness + "&thicknessOperation=" + thicknessOperation;
		queryString += "&weight=" + weight + "&displayColor=" + displayColor + "&camera=" + camera;
		queryString += "&iMemoroy=" + iMemoroy + "&iMemoroyOperation=" + iMemoroyOperation;
		queryString += "&eMemoroy=" + eMemoroy + "&eMemoroyOperation=" + eMemoroyOperation;
		queryString += "&standBy=" + standBy + "&standByOperation=" + standByOperation;
		queryString += "&talkTime=" + talkTime + "&talkTimeOperation=" + talkTimeOperation;
		queryString += "&chkGPRS=" + chkGPRS + "&chkEDGE=" + chkEDGE + "&chkWLAN=" + chkWLAN + "&chkGPS=" + chkGPS;	
		queryString += "&chkBluetooth=" + chkBluetooth + "&chkInfrared=" + chkInfrared + "&chkUSB=" + chkUSB + "&chkFullKeyboard=" + chkFullKeyboard;
		queryString += "&chkTouchScreen=" + chkTouchScreen + "&chkVoiceCommand=" + chkVoiceCommand + "&chkRadio=" + chkRadio + "&chkFlashLight=" + chkFlashLight;
		
		ajaxRequest.open("POST", "php/filterAjax.php", true);
		
		//Send the proper header information along with the request
		ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajaxRequest.setRequestHeader("Content-length", queryString.length);
		ajaxRequest.setRequestHeader("Connection", "close");	
		ajaxRequest.send(queryString); 
		return true;
	}
	else
	{
		reportMsgBox("Please select any option.");
	}
}

//toggle filter checkboxes
function toggleFilterCB(box_id)
{
	var cb = document.getElementById(box_id);
	
	if(cb.checked == true)
	{
		cb.checked = false;
	}
	else
	{
		cb.checked = true;
	}
	
}

//handle filter radio button
function handleFilterRB(rb_id)
{
	var rb = document.getElementById(rb_id);
	
	if(rb.checked == false)
	{
		rb.checked = true;
	}
}
