// Assuming JQuery is already loaded before loading this
///Rate Offers Function 

function loadXMLString(txt) {
	try{ //Internet Explorer
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(txt);
		return(xmlDoc);  
	}catch(e){
		try {//Firefox, Mozilla, Opera, etc.
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(txt,"text/xml");
			return(xmlDoc);
		}catch(e) {alert(e.message)}
	}
	return(null);
}



function rate_up(){
	category_id = '';
	brand_id = '';				 
	customer_id= '';
	offer_id = '';
	client_id='';
	off_name='';
	querystring = '';
	with(document.frmOffersRate){	
		querystring="&task=rating&cat_id="+category_id.value+"&brand_id="+brand_id.value+	
					"&oID="+oID.value+"&client_id="+clt_id.value+"&off_name="+off_name.value+"&rate="+rateValue.value;
	}
	$.ajax({ 	method: "get",
				url: AJAX_SITE_REQUEST_OFFERS_URL+"offers_rate.php",
				data: querystring ,
				beforeSend: function(){ }, //show loading just when link is clicked
				complete: function(){ },  
				success: function(html){ //so, if data is retrieved, store it in html
					xmlDoc=loadXMLString(html);
					x=xmlDoc.documentElement.childNodes;
					rate_result = 'no'; errors =''; total_errors=0; 
					updated_ups=0;	updated_downs=0;
					for (i=0;i<x.length;i++){
						switch(x[i].nodeName){
							case 'state' :			rate_result = x[i].childNodes[0].nodeValue; break;
							case 'total_ups' :		updated_ups= x[i].childNodes[0].nodeValue; break;
							case 'total_downs' :	updated_downs= x[i].childNodes[0].nodeValue; break;
							case 'errors' :			errors = x[i].childNodes[0].nodeValue; break;
							case 'errors_count' : 	total_errors = x[i].childNodes[0].nodeValue;break;
						}
					}
					if(rate_result=='yes'){	// Means Successfult done Rating 
						$("#up_count").html("(" + updated_ups + ")");
						$("#down_count").html("(" + updated_downs + ")");
						$("#up_down_msg").hide();
						$("#up_down_msg").html(errors);
						$("#up_down_msg").slideDown("slow");
						$("#up_down_msg").addClass("BgDarkYellow Black");
					}else{
						$("#up_down_msg").hide();
						$("#up_down_msg").html(errors);
						$("#up_down_msg").slideDown("slow");
						$("#up_down_msg").addClass("BgLightRed Black");
					}
					/*$("#doRateProcessing").html("");
					$("#doRateProcessing").slideUp();
					$("#doRateProcessing").html(html);
					$("#doRateProcessing").slideDown("slow");*/

				}
	});	
}	


function do_offer_rate(frm){
	$.ajax({	method: "get",
				url: AJAX_SITE_REQUEST_USERS_URL+"check_login.php",
				data: '',
				beforeSend: function(){}, 
				complete: function(){ }, 
				success: function(html){ //so, if data is retrieved, store it in html
					is_login=html;
					if(is_login=='yes'){
						var val=frm.rateValue.value;
						/*if(val=='1'){
						rate_up();
						}else{*/
						rate_up();
						return true;
					}else{
						var t = null;
						var a = AJAX_SITE_REQUEST_USERS_URL + "short_login_register.php?jsfunc=do_offer_rate&jsfunc_frmref="+frm.name+"&popup&keepThis=true&TB_iframe=true&height=288&width=790";
						var g = false;
						tb_show(t,a,g);
						return false;
					}
				}
	});
	return false;
}


//Offers CheckBoxes
function checkNSetAlert(whichElement, whichform, mainForm){
	var frm =  mainForm;
	val = whichElement.value;
	orderid=mainForm.changei.value;
	result_element = "td_"+val+"_"+orderid;
	processing_icon = whichElement.name + "_processing";
	checkbox_element = whichElement.name ;
	with(frm){
		alert_for.value=val;
		queryString = "&oID="+oID.value+"&alert_for="+alert_for.value+
					  "&category_id="+category_id.value+
					  "&brand_id="+brand_id.value+
					  "&customer_id="+customer_id.value+
					  "&expire_on="+expire_on.value+
					  "&offer_alert=do_offer_alert";

	}
	//alert(queryString);
	$.ajax({	method: "get",
				url: AJAX_SITE_REQUEST_OFFERS_URL+"do_offer_alert.php",
				data: queryString ,
				beforeSend: function(){ $("#"+checkbox_element).hide("fast");	$("#"+processing_icon).show("fast");}, //show loading just when link is clicked
				complete: function(){ $("#"+processing_icon).hide("fast");}, //stop showing loading when the process is complete
				success: function(html){ //so, if data is retrieved, store it in html
				xmlDoc=loadXMLString(html);
				x=xmlDoc.documentElement.childNodes;
				can_register = 'no'; errors =''; total_errors=0;
				for (i=0;i<x.length;i++){
					switch(x[i].nodeName){
						case 'state' :			join_result = x[i].childNodes[0].nodeValue; break;
						case 'errors' :			errors = x[i].childNodes[0].nodeValue; break;
						case 'errors_count' : 	total_errors = x[i].childNodes[0].nodeValue;break;
					}
				}
				if(join_result=='yes'){
					$("#"+result_element).show("slow");
					$("#"+result_element).html("<span class='Green'>Notification Enabled.</span>"); //show the html inside .content div
				}else{
					while(errors.indexOf("~")>=0){		errors = errors.replace(/~/,"<br />");	}
						$("#"+result_element).html("<span class='Red'>"+errors+"</span>"); //show the html inside .content div
					}
				}
	});
}
 function do_offers_check(frm){
	 $.ajax({	method: "get",
				url: AJAX_SITE_REQUEST_USERS_URL+"check_login.php",
				data: '',
				beforeSend: function(){}, 
				complete: function(){ }, 
				success: function(html){ //so, if data is retrieved, store it in html
					is_login=html;
					if(is_login=='yes'){
						whichElement="";
						for(i=1;i<=4;i++){
							
							chkFlag = eval("frm.alert"+i+".checked");
							chkFlag1=true;
							elementName= eval("frm.alert"+i+".name");
							if($("#"+elementName)){
								if( $("#"+elementName).css('display')=='none' || $("#"+elementName).css('display')==undefined ){		chkFlag1= false; }
							}else{
								chkFlag1= false;
							}
							
							if(chkFlag && chkFlag1){
								whichElement=eval("frm.alert"+i);	break;
							}
						}
						//alert("#"+elementName+"\r\n"+chkFlag1 + "\r\n" + $("#"+elementName).css("display") );
						whichForm=frm;
						mainForm=frm;
						//alert("whichElement : " + whichElement + "\r\n whichForm : " + whichForm + "\r\nmainForm" + mainForm);
						//return false;
						 //checkNSetAlert(this,this.form,document.frmAlertss1)
						checkNSetAlert(whichElement, whichForm, mainForm);
						return true;
					}else{
						var t = null;
						var a = AJAX_SITE_REQUEST_USERS_URL + "short_login_register.php?jsfunc=do_offers_check&jsfunc_frmref="+frm.name+"&popup&keepThis=true&TB_iframe=true&height=288&width=790";
						var g = false;
						tb_show(t,a,g);
						return false;
					}
				}
	});
	return false;
	 
 }


//Offers CheckBoxes
function checkCAlert(whichElement,whichform){
						//return false;
		val = whichElement.value;
		orderid=whichform.oID.value;
		result_element = "td_"+val+"_"+orderid;
		processing_icon = whichElement.name + "_processing"+"_"+orderid;
		checkbox_element = whichElement.name+"_"+orderid;
		with(whichform){
			alert_for.value=val;
			queryString = "&oID="+oID.value+"&alert_for="+alert_for.value+
							"&category_id="+category_id.value+
							"&brand_id="+brand_id.value+
							"&customer_id="+customer_id.value+
							"&expire_on="+expire_on.value+
							"&offer_alert=do_offer_alert";
						//	alert(queryString);return false;
		}
		//alert(queryString);
		//return false;
		$.ajax({	method: "get",
					url: AJAX_SITE_REQUEST_OFFERS_URL+"do_offer_alert.php",
					data: queryString ,
					beforeSend: function(){ $("#"+checkbox_element).hide("fast");	$("#"+processing_icon).show("fast");}, //show loading just when link is clicked
					complete: function(){ $("#"+processing_icon).hide("fast");}, //stop showing loading when the process is complete
					success: function(html){ //so, if data is retrieved, store it in html
					
									xmlDoc=loadXMLString(html);
									x=xmlDoc.documentElement.childNodes;
									can_register = 'no'; errors =''; total_errors=0;
									for (i=0;i<x.length;i++){
										switch(x[i].nodeName){
											case 'state' :			join_result = x[i].childNodes[0].nodeValue; break;
											case 'errors' :			errors = x[i].childNodes[0].nodeValue; break;
											case 'errors_count' : 	total_errors = x[i].childNodes[0].nodeValue;break;
										}
									}
									if(join_result=='yes'){
										$("#"+result_element).show("slow");
										$("#"+result_element).html("<span class='Green'>Notification Enabled.</span>"); //show the html inside .content div
									}else{
										while(errors.indexOf("~")>=0){		errors = errors.replace(/~/,"<br />");	}
											$("#"+result_element).html('<span class="Red">'+errors+'</span>'); //show the html inside .content div
										}
									}
				});
	}
	
	
 function do_catoffers_check(frm,obj){
	 $.ajax({	method: "get",
				url: AJAX_SITE_REQUEST_USERS_URL+"check_login.php",
				data: '',
				beforeSend: function(){}, 
				complete: function(){ }, 
				success: function(html){ //so, if data is retrieved, store it in html
					is_login=html;
					if(is_login=='yes'){
						whichElement="";
					
						//for(i=1;i<=4;i++){
							//chkFlag = eval("frm.alert"+i+".checked");
							chkFlag = obj.checked;
							if(chkFlag){								
								//whichElement=eval("frm.alert"+i);	
								whichElement=obj;	
							}else{
								return false;
							}
						//}
//						return false;
						whichForm=frm;
						//alert("whichElement : " + whichElement + "\r\n whichForm : " + whichForm);
						//return false;
						 //checkNSetAlert(this,this.form,document.frmAlertss1)
						checkCAlert(whichElement, whichForm);
						return true;
					}else{
						var t = null;
						var a = AJAX_SITE_REQUEST_USERS_URL + "short_login_register.php?jsfunc=do_catoffers_check&jsfunc_frmref="+frm.name+"&popup&keepThis=true&TB_iframe=true&height=288&width=790";
						var g = false;
						tb_show(t,a,g);
						return false;
					}
				}
	});
	return false;
	 
 }



