// JavaScript Document
var req;
var basePath ='http://mobile-phones.com.pk/';
//var basePath ='http://localhost/cellsindex/';
var path = basePath+'Ajax/';



function loadXMLDoc(option, url) {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		
		
		if(option=='rating')
		{
			
req.onreadystatechange = processReqChange;
		}
		else if(option=='populatemodels')
		{
			
			req.onreadystatechange = processpopulateModels;
		}
		else if(option=='populatemodelsdeal')
		{
			
			req.onreadystatechange = processpopulateModelsDeal;
		}
		else if(option=='ApproveDeals')
		{
			
			req.onreadystatechange = processApproveDeal;
		}
		else if(option=='ApproveOffer')
		{
			
			req.onreadystatechange = processApproveOffer;
		}
		else if(option=='ApproveComment')
		{
			
			req.onreadystatechange = processApproveComment;
		}
		
		else if(option=='ApproveWallpaper')
		{
			
			req.onreadystatechange = processApproveWallpaper;
		}
		else if(option=='insertdeals')
		{
						
			req.onreadystatechange = processInsertDeals;
		}
		else if(option=='approvesoftwares')
		{
						
			req.onreadystatechange = processApprovesoftwares;
		}
			else if(option=='approvethemes')
		{
						
			req.onreadystatechange = processApprovethemes;
		}	
		else if(option=='insertoffer')
		{
						
			req.onreadystatechange = processInsertOffer;
		}
		else if(option=='messagesend')
		{
						
			req.onreadystatechange = processmessageSend;
		}
		
		req.open("GET", url, true);
		req.send("");
	}
}

function processReqChange() {
    // only if req shows "loaded"
	
	var responsediv = document.getElementById('response');
    if (req.readyState == 4)
	{
        // only if "OK"
		
        if (req.status == 200)
		{
            // ...processing statements go here...
			responsediv.innerHTML = ' <div id="respText"><span style="color: #00CC33">'+req.responseText+'</span></div>';

			
        } 
		else 
		{
			
		   responsediv.innerHTML = ' <div id="respText">Error:'+req.statusText+'</div>';
        }
    }
	else if(req.readyState==1)
	{
		
		responsediv.innerHTML = '<div id="img"> <img src="'+basePath+'/images/loader.gif"/> </div> <div id="respText"></div>';
	}
}

function processpopulateModels() {
    // only if req shows "loaded"
	loading = document.getElementById('loading');
	var models = document.getElementById('models');
    if (req.readyState == 4)
	{
        // only if "OK"
		
        if (req.status == 200)
		{
            // ...processing statements go here...
			
			loading.innerHTML=''; 
			models.innerHTML = req.responseText;
        } 
		else 
		{
			
		   loading.innerHTML = ' <div id="respText">Error:'+req.statusText+'</div>';
        }
    }
	else if(req.readyState==1)
	{
		
		loading.innerHTML = '<div id="img"> <img src="'+basePath+'/images/loader.gif"/> </div>';
		
	}
}


function processpopulateModelsDeal() {
    // only if req shows "loaded"
	
	
	loading = document.getElementById('loadingdeal');
	var models = document.getElementById('modelsdeal');
	
    if (req.readyState == 4)
	{
        // only if "OK"
		
        if (req.status == 200)
		{
            // ...processing statements go here...
			
			loading.innerHTML=''; 
			models.innerHTML = req.responseText;
			
			
        } 
		else 
		{
			
		   loading.innerHTML = ' <div id="respText">Error:'+req.statusText+'</div>';
        }
    }
	else if(req.readyState==1)
	{
		
		loading.innerHTML = '<div id="img"> <img src="'+basePath+'/images/loader.gif"/> </div>';
		
		
	}
}
function processInsertDeals() {
    // only if req shows "loaded"
	
	
	loading = document.getElementById('loadingdeal');
	var messagebar = document.getElementById('MessagBar');
    if (req.readyState == 4)
	{
       
		
        if (req.status == 200)
		{
			
            // ...processing statements go here...
			if(req.responseText!='done')
			{
			
		document.getElementById('divform').style.display ='none';
loading.innerHTML='<div id="respText" style="margin:10px;padding:5px; font-weight:bold; background-color:#FFFFCC; font-size:16px; color:#CC0000; border:#993300 solid 1px">'+req.responseText+'</div>'; 
				}
			
			else
			{
			
			loading.innerHTML='<div style="border: 1px solid #eee; margin: 10px; padding: 5px; font-weight: bold; background-color: #f7f7f7; font-size: 16px; color:#0C6;" id="respText" class="r5x">Congratulations! Your mobile Deal has been submitted </div>';
			}
        } 
		else 
		{
			
		   loading.innerHTML = ' <div id="respText">Error:'+req.responseText+'</div>';
        }
    }
	else if(req.readyState==1)
	{
		
		loading.innerHTML = '<div id="img"> <img src="'+basePath+'/images/loader.gif"/></div>';
		
	}
}

function processApprovesoftwares() {
    // only if req shows "loaded"
	
	
	loading = document.getElementById('loadingdetails');
    if (req.readyState == 4)
	{
       
        if (req.status == 200)
		{
            // ...processing statements go here...
			if(req.responseText!='done')
			{
				loading.innerHTML='<div id="respText" style="margin:10px;padding:5px; font-weight:bold; background-color:#FFFFCC; font-size:16px; color:#CC0000; border:#993300 solid 1px">'+req.responseText+'</div>'; 
				}
				else
				{
					loading.innerHTML='<div style="margin:5px;font-size: 15px; font-weight:bold; float:none; font-family: arial; 	color:#009966;">Action taken successfully..</div>';
					}
        } 
		else 
		{
			
		   loading.innerHTML = ' <div id="respText">Error:'+req.responseText+'</div>';
        }
    }
	else if(req.readyState==1)
	{
		
		loading.innerHTML = '<div id="img"> <img src="'+basePath+'/images/loader.gif"/></div>';
		
	}
}

function processApprovethemes() {
    // only if req shows "loaded"
	
	
	loading = document.getElementById('loadingdetails');
    if (req.readyState == 4)
	{
       
        if (req.status == 200)
		{
            // ...processing statements go here...
			if(req.responseText!='done')
			{
				loading.innerHTML='<div id="respText" style="margin:10px;padding:5px; font-weight:bold; background-color:#FFFFCC; font-size:16px; color:#CC0000; border:#993300 solid 1px">'+req.responseText+'</div>'; 
				}
				else
				{
					loading.innerHTML='<div style="margin:5px;font-size: 15px; font-weight:bold; float:none; font-family: arial; 	color:#009966;">Action taken successfully..</div>';
					}
        } 
		else 
		{
			
		   loading.innerHTML = ' <div id="respText">Error:'+req.responseText+'</div>';
        }
    }
	else if(req.readyState==1)
	{
		
		loading.innerHTML = '<div id="img"> <img src="'+basePath+'/images/loader.gif"/></div>';
		
	}
}

function processmessageSend() {
    // only if req shows "loaded"
	
	
	loading = document.getElementById('loadingdetails');
    if (req.readyState == 4)
	{
       
		
        if (req.status == 200)
		{
			
			//alert(req.responseText);
            // ...processing statements go here...
			if(req.responseText!='done')
			{
				
				loading.innerHTML='<div id="respText" style="margin:10px;padding:5px; font-weight:bold; background-color:#FFFFCC; font-size:16px; color:#CC0000; border:#993300 solid 1px">'+req.responseText+'</div>'; 
				}
			
			else
			{
			//location.href='usedmobiles.php';	
			
			loading.innerHTML='<div style="border: 1px solid #eee; margin: 10px; padding: 5px; font-weight: bold; background-color: #f7f7f7; font-size: 16px; color:#0C6;" id="respText" class="r5x">Congratulations! Your message has been submitted </div>';
			}
        } 
		else 
		{
			
		   loading.innerHTML = ' <div id="respText">Error:'+req.responseText+'</div>';
        }
    }
	else if(req.readyState==1)
	{
		
		loading.innerHTML = '<div id="img"> <img src="'+basePath+'/images/loader.gif"/></div>';
		
	}
}


function processInsertOffer() {
    // only if req shows "loaded"
	
	
	loading = document.getElementById('loadingdetails');
	allOffers = document.getElementById('allOffers');
    if (req.readyState == 4)
	{
       

        if (req.status == 200)
		{
            // ...processing statements go here...
			if(req.responseText!='error')
			{
				loading.innerHTML='';
				allOffers.innerHTML =req.responseText;
				disablePopup();
			}
			
			else
			{
			loading.innerHTML='<div id="respText" style="margin:10px;padding:5px; font-weight:bold; background-color:#FFFFCC; font-size:16px; color:#CC0000; border:#993300 solid 1px">'+req.responseText+'</div>'; 
			}
        } 
		else 
		{
			
		   loading.innerHTML = ' <div id="respText">Error:'+req.responseText+'</div>';
        }
    }
	else if(req.readyState==1)
	{
		
		loading.innerHTML = '<div id="img"> <img src="'+basePath+'/images/loader.gif"/><strong></strong></div>';
		
	}
}



function processApproveDeal() {
    // only if req shows "loaded"
	
	
	loading = document.getElementById('loadingdetails');
    if (req.readyState == 4)
	{
       
        if (req.status == 200)
		{
            // ...processing statements go here...
			if(req.responseText!='done')
			{
				loading.innerHTML='<div id="respText" style="margin:10px;padding:5px; font-weight:bold; background-color:#FFFFCC; font-size:16px; color:#CC0000; border:#993300 solid 1px">'+req.responseText+'</div>'; 
				}
				else
				{
					loading.innerHTML='<div style="margin:5px;font-size: 15px; font-weight:bold; float:none; font-family: arial; 	color:#009966;">Action taken successfully..</div>';
					}
        } 
		else 
		{
			
		   loading.innerHTML = ' <div id="respText">Error:'+req.responseText+'</div>';
        }
    }
	else if(req.readyState==1)
	{
		
		loading.innerHTML = '<div id="img"> <img src="'+basePath+'/images/loader.gif"/></div>';
		
	}
}

function processApproveOffer() {
    // only if req shows "loaded"
	
	
	loading = document.getElementById('loadingdetails');
    if (req.readyState == 4)
	{
       
		
        if (req.status == 200)
		{
		
            // ...processing statements go here...
			if(req.responseText!='done')
			{
				loading.innerHTML='<div id="respText" style="margin:10px;padding:5px; font-weight:bold; background-color:#FFFFCC; font-size:16px; color:#CC0000; border:#993300 solid 1px">'+req.responseText+'</div>'; 
				}
				else
				{
					loading.innerHTML='<div style="margin:5px;font-size: 15px; font-weight:bold; float:none; font-family: arial; 	color:#009966;">Action taken successfully..</div>';
					}
			
			
        } 
		else 
		{
			
		   loading.innerHTML = ' <div id="respText">Error:'+req.responseText+'</div>';
        }
    }
	else if(req.readyState==1)
	{
		
		loading.innerHTML = '<div id="img"> <img src="'+basePath+'/images/loader.gif"/></div>';
		
	}
}



function processApproveComment() {
    // only if req shows "loaded"
	
	
	loading = document.getElementById('loadingdetails');
    if (req.readyState == 4)
	{
       
		
        if (req.status == 200)
		{
		
            // ...processing statements go here...
			if(req.responseText!='done')
			{
				loading.innerHTML='<div id="respText" style="margin:10px;padding:5px; font-weight:bold; background-color:#FFFFCC; font-size:16px; color:#CC0000; border:#993300 solid 1px">'+req.responseText+'</div>'; 
				}
				else
				{
					loading.innerHTML='<div style="margin:5px;font-size: 15px; font-weight:bold; float:none; font-family: arial; 	color:#009966;">Action taken successfully..</div>';
					}
			
			
        } 
		else 
		{
			
		   loading.innerHTML = ' <div id="respText">Error:'+req.responseText+'</div>';
        }
    }
	else if(req.readyState==1)
	{
		
		loading.innerHTML = '<div id="img"> <img src="'+basePath+'/images/loader.gif"/></div>';
		
	}
}






function processApproveWallpaper() {
    // only if req shows "loaded"
	
	
	loading = document.getElementById('loadingdetails');
    if (req.readyState == 4)
	{
       
		
        if (req.status == 200)
		{
		
            // ...processing statements go here...
			if(req.responseText!='done')
			{
				loading.innerHTML='<div id="respText" style="margin:10px;padding:5px; font-weight:bold; background-color:#FFFFCC; font-size:16px; color:#CC0000; border:#993300 solid 1px">'+req.responseText+'</div>'; 
				}
				else
				{
					loading.innerHTML='<div style="margin:5px;font-size: 15px; font-weight:bold; float:none; font-family: arial; 	color:#009966;">Action taken successfully..</div>';
					}
			
			
        } 
		else 
		{
			
		   loading.innerHTML = ' <div id="respText">Error:'+req.responseText+'</div>';
        }
    }
	else if(req.readyState==1)
	{
		
		loading.innerHTML = '<div id="img"> <img src="'+basePath+'/images/loader.gif"/></div>';
		
	}
}





function rating(star)
	{
		var modelid = document.getElementById('hiddenModelId');
strurl =path+'responseProcess.php?r='+star.innerHTML+'&m='+modelid.value;

		loadXMLDoc('rating',strurl+'&option=DORATING' );
		
		}
		
		
			
	function populateModels(sel)
	{
		
		var models = document.getElementById('models');
		
		if(sel.value>0)
		{
			models.disabled=false;
			
			loadXMLDoc('populatemodels', path+'responseProcess.php?manufacturer='+sel.value);
			}
			else
			{
				models.innerHTML='<option value=0>Empty</option>';
				models.disabled=true;
				
				}
		
		}
		var globalmanufacture=0;
		var globalmodelname=0;
	function populateModelsDeal(sel)
	{
		
		var models = document.getElementById('modelsdeal');
		globalmanufacture = sel.value;
				
		if(sel.value>0)
		{
			models.disabled=false;
			
			loadXMLDoc('populatemodelsdeal', path+'responseProcess.php?manufacturer='+sel.value);
			}
			else
			{
				models.innerHTML='<option value=0>Empty</option>';
				models.disabled=true;
				
				}
		
		}
	function getModelValue(sel)
	{

		globalmodelname = sel.value;
		}
		
function approvesoftware(chkBox)
	{
		var softwareid = chkBox.id;
		var isapproved= false;
		softwareid = softwareid.substring(3, softwareid.length);
	
		if(chkBox.checked)
		{
			isapproved= true;
		}
		loadXMLDoc('approvesoftwares', path+'responseProcess.php?softwareid='+softwareid+'&isapproved='+isapproved+'&option=APPROVESOFTWARES');
		
	}

function approvethemes(chkBox)
	{
		var softwareid = chkBox.id;
		var isapproved= false;
		softwareid = softwareid.substring(3, softwareid.length);
	
		if(chkBox.checked)
		{
			isapproved= true;
		}
		loadXMLDoc('approvethemes', path+'responseProcess.php?themeid='+softwareid+'&isapproved='+isapproved+'&option=APPROVETHEMES');
		
	}
	
	function insertDeal()
	{
		if(globalmanufacture==0)
		{
			globalmanufacture = $('#hiddenmanufacturerid_deal').val();
		}
			
		if(globalmodelname==0){
				globalmodelname = $('#hiddenmodelid_deal').val();
				
				}
	if(!validateForms()){
	return false;
	}
		var customername = escape(document.getElementById('customername').value.replace(/[^a-zA-Z 0-9]+/g,''));
		var location = escape(document.getElementById('location').value.replace(/[^a-zA-Z 0-9]+/g,''));
		var email = document.getElementById('email').value;
		
		var demand = document.getElementById('demand').value;
		var curruncy = document.getElementById('curruncy').value;
		var condition = document.getElementById('condition').value;
		var warranty = document.getElementById('warranty').value;
		var description = escape(document.getElementById('description').value);
		var recieveemail = document.getElementById('recieveemail').checked;
				
	var DealValue ='customername='+customername +'&location='+location  +'&email='+email +'&manufacturers='+globalmanufacture +'&models='+globalmodelname+'&demand='+demand +'&curruncy='+curruncy   +'&condition='+condition  +'&warranty='+warranty +'&description='+description+'&recieveemail='+recieveemail ;
	
		loadXMLDoc('insertdeals', path+'responseProcess.php?'+DealValue+'&option=INSERTDEAL');
		
		}
		
	
		
		function isApproved(dealid , check)
		{
			var app = 0;
		if(check.checked)
		{
			app=1;
			}
			
		var appval = 'dealid='+dealid +'&app='+app; 
		loadXMLDoc('ApproveDeals', path+'responseProcess.php?'+appval+'&option=APPROVEDEAL');
		}
		function isApprovedOffer(offerid , check)
		{
			var app = 0;
		if(check.checked)
		{
			app=1;
			}
			
		var appval = 'offerid='+offerid +'&app='+app; 
		loadXMLDoc('ApproveOffer', path+'responseProcess.php?'+appval+'&option=APPROVEOFFER');
		}
		
		function isApprovedComment(commentid , check)
		{
			var app = 0;
		if(check.checked)
		{
			app=1;
			}
			
		var appval = 'commentid='+commentid +'&app='+app; 
		loadXMLDoc('ApproveComment', path+'responseProcess.php?'+appval+'&option=APPROVECOMMENT');
		}
		
		
		function isApprovedWallpaper(wallid , check)
		{
			var app = 0;
		if(check.checked)
		{
			app=1;
			}
			
			var appval = 'wallpaperid='+wallid +'&app='+app; 
			
			
		loadXMLDoc('ApproveWallpaper', path+'responseProcess.php?'+appval+'&option=APPROVEWALLPAPER');
		
		}
		
		
		
	function insertOffer(dealid)
	{
		
	if(!validateForms()){
	return false;
	}
		var customername = escape(document.getElementById('customername').value.replace(/[^a-zA-Z 0-9]+/g,''));
		var location = escape(document.getElementById('location').value.replace(/[^a-zA-Z 0-9]+/g,''));
		var offer = document.getElementById('offer').value;
		var description = escape(document.getElementById('description').value);
		var curruncy = document.getElementById('curruncy').value;

	var OfferValue ='customername='+customername +'&location='+location +'&curruncy='+curruncy +'&offer='+offer +'&description='+description+'&id='+dealid;

		loadXMLDoc('insertoffer', path+'responseProcess.php?'+OfferValue+'&option=INSERTOFFER');
		
		}
		
			
	function messageSend()
	{
		
	if(!validateForms()){
	return false;
	}
		var email = document.getElementById('email').value;
		var subject = document.getElementById('subject').value;
		var message = document.getElementById('message').value;

	var MessageValue ='email='+email +'&subject='+subject +'&message='+message;
		loadXMLDoc('messagesend', path+'responseProcess.php?'+MessageValue+'&option=MESSAGESEND');
		
		}



