var path="";

//Global variables
var intBlind=1;
var spchars ="`()\\~!@^&*+\"|:=,<>-?'#/"; 


function homeOver(obj)
{
	obj.src=path+"/img/home_h.gif";
}

function homeOut(obj)
{
		obj.src=path+"/img/Home.gif";
}

function comOver(obj)
{
	obj.src=path+"/img/complaint_h.gif";
}

function comOut(obj)
{
		obj.src=path+"/img/complaint.gif";
}

function accountOver(obj)
{
	obj.src=path+"/img/account_h.gif";
}

function accountOut(obj)
{
		obj.src=path+"/img/account.gif";
}

function helpOver(obj)
{
	obj.src=path+"/img/help_h.gif";
}

function helpOut(obj)
{
		obj.src=path+"/img/help.gif";
}


function logOver(obj)
{
	obj.src=path+"/img/logout_h.gif";
}

function logOut(obj)
{
		obj.src=path+"/img/logout.gif";
}

function RptOver(obj)
{
	obj.src=path+"/img/report_h.gif";
}

function RptOut(obj)
{
		obj.src=path+"/img/report.gif";
}

function PerOver(obj)
{
	obj.src=path+"/img/permission_h.gif";
}

function PerOut(obj)
{
		obj.src=path+"/img/permission.gif";
}



//This function shows complaints
function showComplaints(Dvid)
{
	$('parCom').style.display="none";
	var intID=Dvid.substring(2);
	var valCompl=$F('hdn'+intID);

	$('parCom').update(valCompl);
	//Effect.Appear('parCom', { duration: 2.0 });
	$('parCom').appear();
}

//This function show and hide address container
function controlAddress()
{
	if(intBlind==0)
	{
		
		Effect.BlindDown('hidedivs');
		$('imgSymb').src="../img/arrow.gif";
		intBlind=1;

	}
	else
	{
		Effect.BlindUp('hidedivs');
		$('imgSymb').src="../img/arrowdown.gif";
		intBlind=0;
	}
}

/****************************************************************************************************************************************/
//Open popup
function popup()
 {
	 document.getElementById('shad').style.height=document.body.clientHeight+180+"px";
	 document.getElementById('shad').style.width=document.body.clientWidth+30+"px";
	 
	 //Get middle position
	 var intX=Number(document.body.clientHeight)/2-25;
	 var intY=Number(document.body.clientWidth)/2-150;
	 document.getElementById('mes').style.top=intX+"px";
	 document.getElementById('mes').style.left=intY+"px";
	 document.getElementById('shad').style.display="inline";
	 document.getElementById('mes').style.display="inline";
	 new Effect.Opacity('mes', { from: 0, to: 1,duration: 2});
 }

/****************************************************************************************************************************************/
//open update popup
function Updpopup(userid)
 {
	 $('hdnupUser').value=userid;
	  //Load campaign
	 getCampaign(userid)
	 document.getElementById('shad').style.height=document.body.clientHeight+180+"px";
	 document.getElementById('shad').style.width=document.body.clientWidth+30+"px";
	 
	 //Get middle position
	 var intX=Number(document.body.clientHeight)/2-25;
	 var intY=Number(document.body.clientWidth)/2-150;
	 document.getElementById('mes1').style.top=intX+"px";
	 document.getElementById('mes1').style.left=intY+"px";
	 document.getElementById('shad').style.display="inline";
	 document.getElementById('mes1').style.display="inline";
	
	 new Effect.Opacity('mes1', { from: 0, to: 1,duration: 2});
 }


/****************************************************************************************************************************************/
 //Close popup
 function closepop()
 {
	 	 
      Effect.DropOut('mes');
	  //Effect.DropOut('shad');
	  $('shad').style.display="none";
	  
 }
 /****************************************************************************************************************************************/
 //Close update popup
 function closepopupd()
 {
	 	 
      Effect.DropOut('mes1');
	  $('shad').style.display="none";
	  
 }
/****************************************************************************************************************************************/
//Open popup
function popupRes()
 {
	 document.getElementById('shad').style.height=document.body.clientHeight+180+"px";
	 document.getElementById('shad').style.width=document.body.clientWidth+30+"px";
	 
	 //Get middle position
	 var intX=Number(document.body.clientHeight)/2-135;
	 var intY=Number(document.body.clientWidth)/2-200;
	 Effect.ScrollTo('article_top');
	 document.getElementById('popupsectionRes').style.top=intX+"px";
	 document.getElementById('popupsectionRes').style.left=intY+"px";
	 document.getElementById('shad').style.display="inline";
	 document.getElementById('popupsectionRes').style.display="inline";
	 new Effect.Opacity('popupsectionRes', { from: 0, to: 1,duration: 2});
	 
     
 }


/****************************************************************************************************************************************/
 //Close popup
 function closepopRes()
 {
	 	 
      Effect.DropOut('popupsectionRes');
	  //Effect.DropOut('shad');
	  $('shad').style.display="none";
	  
 }


//Get response Description 
 function resText()
 {
	
	$('txtRespDecp').innerHTML=$F('selResponse');
 }
 
 //Validate on enter key
 function Go(event)
 {
	 if(event.keyCode==13)
	 {
		 //key code for enter
		 loginValidate();
	 }
 }
 
 
 
/****************************************************************************************************************************************/

/* Ajax */

//Insert new user

function insertUser()
{
	 var strFirstName=encodeURIComponent($F('txtFirstName'));
	 var strLastName=encodeURIComponent($F('txtLastName'));
	 var strUser=$F('txtUser');
	 var strPass=encodeURIComponent($F('txtPass'));
	 var strEmail=$F('txtEmail');
	 var strCompany=encodeURIComponent($F('txtCompany'));
	 var strPhone=$F('txtPhone');
	 var strStreet1=encodeURIComponent($F('txtStreet1'));
	 var strStreet2=encodeURIComponent($F('txtStreet2'));
	 var strCity=encodeURIComponent($F('txtCity'));
	 var strState=encodeURIComponent($F('txtState'));
	 var strZip=encodeURIComponent($F('txtZip'));
	 var strCountry=encodeURIComponent($F('selCountry'));
	 var strAct=$F('selAct');
	 
	 if($F('txtStreet2'))
	 {
	 	strStreet2=$F('txtStreet2');
	 }
	 
	 if(strFirstName!="" && strLastName!="" && strUser!="" && strPass!="" && strEmail!="" && strCompany!="" && strPhone!="" && strCountry!="" && strCity!="" && strZip!="" && strStreet1!="" && strState != "")
	 {
		if(!echeck(strEmail)){
			$('txtEmail').focus();
			return false;
		}else if(!checkInternationalPhone(strPhone)){
			alert('Please enter valid phone number(xxx-xxx-xxxx).');
			$('txtPhone').focus();
			return false;
		}else if(!checkzipnum(strZip)){
			alert('Please enter five digit zip number.');
			$('txtZip').focus();
			return false;
		}else {
			//Insert via ajax
			
		new Ajax.Request('signup/',
			{   
		
				method: 'get',
				parameters: "fn="+strFirstName+"&ln="+strLastName+"&un="+strUser+"&pwd="+strPass+"&em="+strEmail+"&com="+strCompany+"&ph="+strPhone+"&con="+strCountry+"&ct="+strCity+"&zp="+strZip+"&st1="+strStreet1+"&st2="+strStreet2+"&st="+strState+"&act="+strAct,
				contentType:'application/json; charset=utf-8',
				onSuccess: insertUserSuccess,
				onFailure: insertUserFailure
			});
		
	
		}
	 }
	 else
	 {
		 alert("All fields are mandatory");
	 }
	
}

function insertUserSuccess(result)
{
	location.href= path+'/users/loginpage';	
}

function insertUserFailure(result)
{
	alert(result.responseText);
}

/****************************************************************************************************************************************/

function getCities()
{
	var strCountry=encodeURIComponent($F('selCountry'));
	$('imgLoading').style.display="block";
	new Ajax.Request('getCities',
			{   
	
	
							method: 'post',
							parameters:'contCode='+strCountry,
							onSuccess: getCitiesSuccess,
							onFailure: getCitiesFailure
			});
	
}

function getCitiesSuccess(result)
{
	
	$('imgLoading').style.display="none";
	$('dvCity').update(result.responseText);
}

function getCitiesFailure(result)
{
	alert('Ajax Error to get cities');
}

/****************************************************************************************************************************************/

function updateUser()
{
	var strFirstName=encodeURIComponent($F('txtFirstName'));
	 var strLastName=encodeURIComponent($F('txtLastName'));
	 var strEmail=encodeURIComponent($F('txtEmail'));
	 var strPhone=$F('txtPhone');
	 var strStreet1=encodeURIComponent($F('txtStreet1'));
	 var strStreet2=encodeURIComponent($F('txtStreet2'));
	 var strCity=encodeURIComponent($F('txtCity'));
	 var strState=encodeURIComponent($F('txtState'));
	 var strZip=encodeURIComponent($F('txtZip'));
	 var strCountry=encodeURIComponent($F('selCountry'));
	 
	 if(strFirstName!="" && strLastName!=""  && strEmail!="" && strPhone!="" && strCountry!="" && strCity!="" && strZip!="" && strStreet1!="" && strState != "")
	 {
			$('AdminLogMes1').style.display = 'block';
			$('AdminLogMes1').innerHTML = 'User updated successfully.';
			//Insert via ajax
			if(strCountry == 'USA' && !$('usstate').checked){
				alert('please select different country');
			}else {
				$('imgUpdRes').style.display="block";
				document.getElementById('hdnState').value= strState;
				new Ajax.Request(path+'/users/UpdateProfile',
				{   
					method: 'post',
					parameters:'fname='+strFirstName+'&lname='+strLastName+'&email='+strEmail+'&phone='+strPhone+'&country='+strCountry+'&state='+strState+'&city='+strCity+'&zip='+strZip+'&street1='+strStreet1+'&street2='+strStreet2,
					onSuccess: updateUserSuccess,
					onFailure: updatetUserFailure
				});
			}
	 }
	 else
	 {
		 alert("All fields are mandatory");
	 }
	
}

function updateUserSuccess(result)
{
	
    $('imgUpdRes').hide();
	setTimeout("displaynone('AdminLogMes1')",3000);
}

function updatetUserFailure(result)
{
	alert(result.responseText);
}

/****************************************************************************************************************************************/

//Add Response and their text
function InsertResponse()
{
	var txtName=encodeURIComponent($F('txtResName'));
    var txtDecp=encodeURIComponent($F('txtDescrip'));
	
	//Modified on 27 july 2010
	var originalDescription=$F('txtDescrip');
	//Get text length
	var descriptionLength=originalDescription.length;
		
	if(txtName!="" && txtDecp!="")
	{
		
			if(descriptionLength<=1000)
			{
				$('AdminLogMes').style.display = 'block';
				$('AdminLogMes').innerHTML = "<br/>Response added successfully.";
					
				//Insert via ajax
				new Ajax.Request('InsertResponse',
				{   
		
		
					method: 'post',
					parameters:'name='+txtName+'&response='+txtDecp,
					onSuccess: InsertResponseSuccess,
					onFailure: InsertResponseFailure
				});
				
				//Close popup
				closepop();
			
		
			}
			else
			{
				alert('Response Description can not exceed 1000 characters');
			}
	
		
			
	}
	else
	{
		 alert("All fields are mandatory");
	}
	
	
}
//Get selected response Text
function GetResText()
{
		 var resid=$F('selResonse')

			//Insert via ajax
			new Ajax.Request(path+'/users/GetResText',
			{   
				method: 'post',
				parameters:'resid='+resid,
				onSuccess: GetResTextSuccess
				
			});
}

function GetResTextSuccess(result)
{
	//update div
	$('txtResText').innerHTML=result.responseText;
}


function InsertResponseSuccess(result)
{
	   $('addResLoading').style.display="none";
	   
	    if(result.responseText=="")
		{
			//Reload responses
			getResponses();
			
		}
		else
		{
			alert(result.responseText);
		}
}

function InsertResponseFailure(result)
{
		
		alert("Ajax Error");
}

/****************************************************************************************************************************************/

//Bind all the response

function getResponses()
{
		//Geting values
		new Ajax.Request('GetResponses',
			{   
	
	
							method: 'post', contentType:'application/json; charset=utf-8',
							onSuccess: getResponsesSuccess,
							onFailure: getResponsesFailure
			});
}

function getResponsesSuccess(result)
{
	
	$('dvResponse').update(result.responseText);
	$('addResLoading').style.display="none";
	$('AdminLogMes').style.display = 'block';
	setTimeout("displaynone('AdminLogMes')",3000);
	GetResText();
}

function getResponsesFailure(result)
{
	alert("Ajax Error");
}

/****************************************************************************************************************************************/

function getResponseText()
{
	var intResponseID=$F('selResonse');
	
	$('rhtUpdImg').style.display="block";
	
	if(intResponseID!="")
	{
			//Getting value via ajax
			new Ajax.Request('GetResponseText/'+intResponseID,
			{   
	
	
							method: 'post', contentType:'application/json; charset=utf-8',
							onSuccess: getResponseTextSuccess,
							onFailure: getResponseTextFailure
			});
	}
}

function getResponseTextSuccess(result)
{
	$('rhtUpdImg').hide();
	//alert(result.responseText);
	$('txtResText').innerHTML=result.responseText;
}

function getResponseTextFailure(result)
{
	alert('Ajax Error');
}

/****************************************************************************************************************************************/
//Update Response Template
function UpdateResponse()
{
	var intResponseID=$F('selResonse');
		
	var response=encodeURIComponent($F('txtResText'));
	
	//Modified on 27 july 2010
	var orginalResponse=$F('txtResText');
	var responseLength=orginalResponse.length;
	
    if(responseLength <= 1000)	
	{
		
		$('rhtUpdImg').style.display="block";
		
			//Ajax
			if(intResponseID!="")
			{
					$('AdminLogMes').style.display = 'block';
					$('AdminLogMes').innerHTML = "<br/>Response updated successfully.";
					//updating value via ajax
					new Ajax.Request('UpdateResponse',
					{   
			
			
									method: 'post',
									parameters: 'resID='+intResponseID+'&resText='+response,
									onSuccess: UpdateResponseSuccess,
									onFailure: UpdateResponseFailure
					});
			}
	}
	else
	{
		alert('Response cannot exceed 1000 characters');
	}
	
	
}


function UpdateResponseSuccess(result)
{
	if(result.responseText=="")
	{
		$('rhtUpdImg').hide();
		setTimeout("displaynone('AdminLogMes')",3000);
	}
	else
	{
		alert(result.responseText);
	}
}

function UpdateResponseFailure(result)
{
	alert("Ajax Error");
}

/****************************************************************************************************************************************/
var currRes="";
    
function SaveResponse()
{
	var compID=$F('hdnId');
	var compg=$F('hdnComgId');
	var email=$F('hdnEmail')
       var response=encodeURIComponent($F('txtRespDecp'));
	$('cusResLod').style.display="block";
	currRes=response;
	
	
			//updating value via ajax
	new Ajax.Request(path+'/Complaints/saveResponse/'+compID+'/na/na',
	{
		method: 'post',		
		parameters:'compg='+compg+'&compID='+compID+'&email='+email+'&response='+response,
		onSuccess: SaveResponseSuccess,
		contentType:'application/x-www-form-urlencoded',  
        encoding:'UTF-8',  
		onFailure: SaveResponseFailure
	});
	
}


function SaveResponseSuccess(result)
{
	if(result.responseText)
	{
		$('AdminLogMes').style.display = 'block';
		if(currRes){
		 $('res').update(currRes);
		 $('txtRespDecp').innerHTML=currRes;
		}else{
		$('res').update("None");
		 $('txtRespDecp').innerHTML= "None";
		}	
		$('cusResLod').hide();
		//alert('update successfully');
	}
	else
	{
		alert(result.responseText);
	}
}

function SaveResponseFailure(result)
{
	alert("Ajax Error");
}

/****************************************************************************************************************************************/

//Search complaints
function SearchComp()
{
	var custName="na";
	var compID="na";
	var backDate="na";
	var startDate="na";
	var endDate="na";
	var tag="na"
	var key="na";
	var comwres="na";
	
	if($F('txtCons'))
	{
		 custName=encodeURIComponent($F('txtCons'));
	}
	
	if($F('txtComp'))
	{
		 compID=encodeURIComponent($F('txtComp'));
	}
	
	if($F('selBacDate'))
	{
		 backDate=encodeURIComponent($F('selBacDate'));
	}
	
	if($F('startDate'))
	{
		 startDate=encodeURIComponent($F('startDate'));
	}
	
	if($F('endDate'))
	{
		 endDate=encodeURIComponent($F('endDate'));
	}
	
	if($F('txtTag'))
	{
		 tag=encodeURIComponent($F('txtTag'));
	}
	
	if($F('txtKey'))
	{
		key=encodeURIComponent($F('txtKey'));
	}
	if($F('selComWRep'))
	{
		comwres=encodeURIComponent($F('selComWRep'));
	}
	
	$('complaintssmall').hide();
	$('containercomp').hide();
	$('srchLoad').style.display="block";
	//search complaints via ajax
	new Ajax.Request(path+'/Complaints/Search',
	{
		method: 'post',
		parameters:'consumerName='+custName+'&complID='+compID+'&backDate='+backDate+'&startDate='+startDate+'&endDate='+endDate+'&tag='+tag+'&key='+key+'&comwres='+comwres,
		onSuccess: searchSuccess,
		onFailure: searchFailure
	});
}


function searchSuccess(result)
{
	$('srchLoad').style.display="none";
	if(result.responseText.indexOf('Warning')>=0)
	{
		$('smlConnt').update('No Data found.Improve you search criteria.');
	}
	else
	{
		$('smlConnt').update(result.responseText);
	}
	
	$('complaintssmall').appear();
	$('containercomp').appear();
}


function searchFailure(result)
{
	alert("Ajax Error");
}

/****************************************************************************************************************************************/
var strPathPath = '';
function loginValidate(strPath)
{
	var user=encodeURIComponent($F('username'))
	var pwd=encodeURIComponent($F('password'))
	
	if(user!="" && pwd!="")
	{
		strPathPath = strPath;
		$('dvLogLoad').style.display="block";
		$('loginErrMes').style.display="none";
		
		$('imgPwd').src=path+"/img/icon_valid.gif";
		$('imgUser').src=path+"/img/icon_valid.gif";
		
		new Ajax.Request(path+'/users/login',
		{   
	
			method: 'post',
			parameters:'userName='+user+'&pwd='+pwd,
			onSuccess: loginSuccess,
			onFailure: loginFailure
		});
		
	}
	else
	{
		if(user=="")
		{
			
			$('imgUser').src=path+"/img/icon_warning.gif";
			
		}
		else
		{
			$('imgUser').src=path+"/img/icon_valid.gif";
		}
		
		if(pwd=="")
		{
			
			$('imgPwd').src=path+"/img/icon_warning.gif";
		}
		else
		{
			$('imgPwd').src=path+"/img/icon_valid.gif";
		}
		
	}
}


function loginSuccess(result)
{
	if(result.responseText=="")
	{
		alert(result.responseText);
				
	}
	else
	{
		$('dvLogLoad').hide();
		
		if(result.responseText=="selcamp")
		{
			window.location.href=path+"/users/campaign";
		}
		else if(result.responseText=="home")
		{
			window.location.href=path+"/users/home";
		}
		else 
		{
			$('loginErrMes').innerHTML=result.responseText;
			//Effect.Pulsate('loginErrMes')
			$('loginErrMes').style.display="block";
		}
	}
}

function loginFailure()
{
}

/****************************************************************************************************************************************/

function updateadmindata(){
	new Ajax.Request(path+'/users/admindata/0',
	{
		method: 'post', contentType:'application/json; charset=utf-8',
		onSuccess: updateadmindataSuccess,
		onFailure: updateadmindataFailure
	});
}

function updateadmindataSuccess(res){
	if(res.responseText != ""){
		$('divadmindata').innerHTML = res.responseText;
		applyshadow(0);
	}
}

function updateadmindataFailure(res){
}

//Activate user
 function doActive(id)
 {
	
	 var userid=id.substring(3);
	 var selid="sel"+userid;
	
	 var campID=$F('hdn'+userid);
	 var getID="em"+userid;
	 var email=$(getID).innerHTML;
	 
	 if(campID!="")
	 {
		$('adminLoading').style.display="block";
		$('AdminLogMes').style.display="none";
		applyshadow(1);
		 new Ajax.Request(path+'/users/doActive',
			{
				method: 'post', 
				parameters:'userid'+userid+'&campid='+campID+'&email='+email,
				onSuccess: doActiveSuccess,
				onFailure: doActiveFailure
			});
	 }
	 else
	 {
		 alert("Please select campaign to activate");
	 }
 }
 
 function doActiveSuccess(result)
 {
	 if(result.responseText=="")
	 {
		//success
		updateadmindata();
		$('adminLoading').style.display="none";
   		$('AdminLogMes').innerHTML = "User has been activated successfully.";
   		$('AdminLogMes').style.display="block";
		setTimeout("displaynone('AdminLogMes')",3000);
	 }
	 else
	 {
		 //Failure
		 $('adminLoading').style.display="none";
   		$('AdminLogMes').innerHTML = result.responseText;
   		$('AdminLogMes').style.display="block";
		setTimeout("displaynone('AdminLogMes')",3000);
	 }
	applyshadow(0);
 }
 
 function doActiveFailure(result)
 {
 }
 
 
 /****************************************************************************************************************************************/
 
 //Jump to page
function jumpTo()
{
	var custName="na";
	var compID="na";
	var backDate="na";
	var startDate="na";
	var endDate="na";
	var tag="na"
	var key="na";
	var comwres="na";
    
	var pageIndex=$F('selPage');
		
	if($F('txtCons'))
	{
		 custName=encodeURIComponent($F('txtCons'));
	}
	
	if($F('txtComp'))
	{
		 compID=encodeURIComponent($F('txtComp'));
	}
	
	if($F('selBacDate'))
	{
		 backDate=encodeURIComponent($F('selBacDate'));
	}
	
	if($F('startDate'))
	{
		 startDate=encodeURIComponent($F('startDate'));
	}
	
	if($F('endDate'))
	{
		 endDate=encodeURIComponent($F('endDate'));
	}
	
	if($F('txtTag'))
	{
		 tag=encodeURIComponent($F('txtTag'));
	}
	
	if($F('txtKey'))
	{
		key=encodeURIComponent($F('txtKey'));
	}
	if($F('selComWRep'))
	{
		comwres=encodeURIComponent($F('selComWRep'));
	}
	
	$('complaintssmall').hide();
	$('containercomp').hide();
	$('srchLoad').style.display="block";
	
	//search complaints via ajax
	new Ajax.Request(path+'/Complaints/Jump',
			{   
		
							method: 'post',
							parameters:'consumerName='+custName+'&complID='+compID+'&backDate='+backDate+'&startDate='+startDate+'&endDate='+endDate+'&tag='+tag+'&key='+key+'&comwres='+comwres+'&pageIndex='+pageIndex,
							onSuccess: jumpToSuccess,
							onFailure: jumpToFailure
			});
}


function jumpToSuccess(result)
{
	
	$('srchLoad').style.display="none";
	$('onlData').update(result.responseText);
	$('complaintssmall').appear();
	$('containercomp').appear();
}


function jumpToFailure(result)
{
	alert("Ajax Error");
}

/****************************************************************************************************************************************/

//Send password to user
function sendPassword()
{
	var Email=$F('txtEmail');
	
		if(Email!="")
		{
			//send via ajax
			new Ajax.Request('mailSend',
					{   
				
									method: 'post',
									parameters:'mailAdd='+Email,
									onSuccess: sendSuccess,
									onFailure: sendFailure
					});
		}
		else
		{
			alert("All fields are mandatory");
		}
}

function sendSuccess(result)
{
	
	
	if(result.responseText=="")
	{
		//Success message
		//Effect.Pulsate('sendPassMes');
		$('sendPassMes').style.display="block";
	}
	else
	{
		alert(result.responseText);
	}
	
}

function sendFailure(result)
{
	alert("Ajax Error.");
}



//Deactivate User
function deactivateuser(userid){
	$('adminLoading').style.display="block";
	applyshadow(1);
	new Ajax.Request(path+'/users/deactivate/'+userid,
	{
		method: 'post', contentType:'application/json; charset=utf-8',
		onSuccess: deactiveSuccess,
		onFailure: deactiveFailure
	});
}

function deactiveSuccess(res){
	if(res.responseText == ""){
		updateadmindata();
		$('adminLoading').style.display="none";
		$('AdminLogMes').innerHTML = "User has been deactivated successfully.";
   		$('AdminLogMes').style.display="block";
		setTimeout("displaynone('AdminLogMes')",3000);
	}
	//else
		//alert('Success'+res.responseText);
		
	applyshadow(0);
}

function deactiveFailure(res){
}

//Deactivate User
function updateuser(userid){
	$('adminLoading').style.display="block";
	applyshadow(1);
	
	new Ajax.Request('updateUser/'+userid,
	{
		method: 'post', contentType:'application/json; charset=utf-8',
		onSuccess: updateSuccess,
		onFailure: deactiveFailure
	});
}

function updateSuccess(res){
	if(res.responseText == ""){
		updateadmindata();
		$('adminLoading').style.display="none";
   		$('AdminLogMes').innerHTML = "User has been updated successfully.";
   		$('AdminLogMes').style.display="block";
		setTimeout("displaynone('AdminLogMes')",3000);
	}
	else
		alert('Success'+res.responseText);
		
	applyshadow(0);
}

function deactiveFailure(res){
}

function selUsOrNonUs(){
	var objstate = document.getElementsByName('nmstate');
	var objdivState = document.getElementById('dvState');
	var objimgload = document.getElementById('imgLoading1');
	if(objstate != null){
		for(var i=0;i<objstate.length;i++){
			if(objstate[i].checked == true){
				if(objstate[i].value == 1){
					objimgload.style.display = 'block';
					new Ajax.Request('usState/'+objstate[i].value,
					{
						method: 'post', contentType:'application/json; charset=utf-8',
						onSuccess: updatestate,
						onFailure: deactiveFailure
					});
					
				}
				else if(objstate[i].value == 0){
					var str = "<input id='txtState' type='text' />";
					str += "<img id='imgLoading1' style='display:none;float:left;' src='../img/loading.gif' />";
					objdivState.innerHTML = str;
					document.getElementById('imgLoading1').style.display = 'none';
					$('selCountry').selectedIndex = 0;
				}
			}
		}
	}
}

function updatestate(res){
	var objdivState = document.getElementById('dvState');
	var objcountry = document.getElementById('selCountry');
	if(res.responseText != ''){
		objdivState.innerHTML = res.responseText;
		document.getElementById('imgLoading1').style.display = 'none';
		for(var i=0;i<objcountry.length;i++){
			if(objcountry[i].value == 'USA')
				objcountry.selectedIndex = i;
		}
	}
}

function selUsOrNonUsAccount(){
	var objstate = document.getElementsByName('nmstate');
	var objdivState = document.getElementById('dvState');
	var objimgload = document.getElementById('imgLoading1');
	if(objstate != null){
		for(var i=0;i<objstate.length;i++){
			if(objstate[i].checked == true){
				if(objstate[i].value == 1){
					objimgload.style.display = 'block';
					new Ajax.Request('usState/'+objstate[i].value,
					{
						method: 'post', contentType:'application/json; charset=utf-8',
						onSuccess: updatestateAccount,
						onFailure: deactiveFailure
					});
				}
				else if(objstate[i].value == 0){
					var str = "<input id='txtState' type='text' style='width:100px;' />";
					str += "<img id='imgLoading1' style='display:none;float:left;' src='../img/loading.gif' />";
					objdivState.innerHTML = "<p>&nbsp;</p>"+str;
					document.getElementById('imgLoading1').style.display = 'none';
					document.getElementById('selCountry').selectedIndex = 0;
					document.getElementById('selCountry').disabled = false;
				}
			}
		}
	}
}

function updatestateAccount(res){
	var objdivState = document.getElementById('dvState');
	var objcountry = document.getElementById('selCountry');
	if(res.responseText != ''){
		objdivState.innerHTML = "<p>&nbsp;</p>"+res.responseText;
		document.getElementById('imgLoading1').style.display = 'none';
		
		var objhdnState = document.getElementById('hdnState');
		var objselState = document.getElementById('txtState');
		for(var i=0;i<objselState.length;i++){
			if(objselState[i].value == objhdnState.value)
				objselState.selectedIndex = i;
		}
		var objcountry = document.getElementById('selCountry');
		for(var i=0;i<objcountry.length;i++){
			if(objcountry[i].value == 'USA')
				objcountry.selectedIndex = i;
		}
		document.getElementById('selCountry').disabled = true;
	}
}

function returnstate(){
	new Ajax.Request('usState/1',
	{
		method: 'post', contentType:'application/json; charset=utf-8',
		onSuccess: returnstaterply,
		onFailure: deactiveFailure
	});
}

function returnstaterply(res){
	var objdivState = document.getElementById('dvState');
	var objhdnState = document.getElementById('hdnState');
	
	if(res.responseText != ''){
		objdivState.innerHTML = "<p>&nbsp;</p>"+res.responseText;
	}
	var objselState = document.getElementById('txtState');
	for(var i=0;i<objselState.length;i++){
		if(objselState[i].value == objhdnState.value)
			objselState.selectedIndex = i;
	}
}

function displaynone(Iddiv){
	var objdivmsg = document.getElementById(Iddiv);
	objdivmsg.style.display = 'none';
}

function applyshadow(val){
	if(val == 1){
		var objbody = document.getElementsByTagName('body')[0];
		var objshadow = document.createElement('div');
		objshadow.setAttribute('id','divShadow');
		objshadow.style.position = 'absolute';
		objshadow.style.top = '0px';
		objshadow.style.left = '0px';
		objshadow.style.width = '100%';
		objshadow.style.height = '800px';
		objshadow.style.backgroundColor = '#000000';
		objshadow.style.opacity = '0.5';
		objshadow.style.filter = 'alpha(opacity=50)';
		objbody.appendChild(objshadow);
		displayselect(1);
	}else if(val == 0){
		var objbody = document.getElementsByTagName('body')[0];
		var objshadow = document.getElementById('divShadow');
		objbody.removeChild(objshadow);
		displayselect(0);
	}
}

function displayselect(val){
	if(val == 1){
		var objbody = document.getElementsByTagName('body')[0];
		var objsel = objbody.getElementsByTagName('select');
		for(var i=0;i<objsel.length;i++){
			objsel[i].style.display = 'none';
		}
	}else if(val == 0){
		var objbody = document.getElementsByTagName('body')[0];
		var objsel = objbody.getElementsByTagName('select');
		for(var i=0;i<objsel.length;i++){
			objsel[i].style.display = 'block';
		}
	}
}

function changeadminuserpage(objthis){
	var page = objthis.value-1;
	window.location.href = path+'/users/admin/'+page+'/0';
}

function changeadminuserpage1(objthis){
	var page = objthis.value-1;
	window.location.href = path+'/users/admin/0/'+page;
}

//Check Email
/**
 * DHTML email validation script.
 */

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("Invalid E-mail ID")
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		alert("Invalid E-mail ID")
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("Invalid E-mail ID")
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		alert("Invalid E-mail ID")
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert("Invalid E-mail ID")
		return false
	 }

	 return true					
}

//Check Phone Number
/**
 * DHTML phone number validation script. 
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function checknum(){
	if(isNaN($('#zipCode').val()) == true || $('#zipCode').val().length < 5)
		alert(isNaN($('#zipCode').val()));
}

function checkzipnum(strzip){
	if(!isNaN(strzip) && strzip.length == 5){
		return true;
	}
	return false;
}

function getstate(){
	new Ajax.Request('usState/1',
	{
		method: 'post', contentType:'application/json; charset=utf-8',
		onSuccess: getusstate,
		onFailure: getusFailure
	});
}

function getusstate(res){
	$('dvState').innerHTML = res.responseText;
}

function getusFailure(){
}


//Delete User
function deleteuser(userid){
	
	if(confirm('Are you sure you want to delete the user?')){
		$('adminLoading').style.display="block";
	    applyshadow(1);
	new Ajax.Request(path+'/users/deleteuser/'+userid,
	{
		method: 'post', contentType:'application/json; charset=utf-8',
		onSuccess: function(res){
			updateadmindata();
			$('adminLoading').style.display="none";
			$('AdminLogMes').innerHTML = "User has been deleted successfully.";
			$('AdminLogMes').style.display="block";
			setTimeout("displaynone('AdminLogMes')",3000);

			applyshadow(0);
		},
		onFailure: function(){
			alert('Ajax error');
		}
	});
	}
}

/****************************************************************************************************************************************/
//set clicked userid
function setCurUser(userid)
{
	$('curUser').value=userid;
	
}

/****************************************************************************************************************************************/
function mapCamp()
{
	//current user
	var id='hdn'+$('curUser').value;
	$(id).value=$F('listMap');
}

/****************************************************************************************************************************************/
//select campaign
function selCamp()
{
	//Get Campaign
	var camp=$F('selCamp');
	
	new Ajax.Request('SetCampaign',
	{
		method: 'post', 
		parameters:'campid='+camp,
		onSuccess: selCampSucess
		
	});
	
}

function selCampSucess()
{
	window.location.href=path+"/users/home";
}



//switch campaign
function switchCamp()
{
	//Get Campaign
	var camp=$F('swtCamp');
	
	new Ajax.Request('SetCampaign',
	{
		method: 'post', 
		parameters: 'campid='+camp,
		onSuccess: selCampSucess
		
	});
	
}

/****************************************************************************************************************************************/

var adcampid='';
//select campaign
function seladmCamp()
{
	//Get Campaign
	var camp=$F('selCamp');
	
	//set global
	adcampid=camp;
	new Ajax.Request('SetCampaign/'+camp,
	{
		method: 'post', contentType:'application/json; charset=utf-8',
		onSuccess: selAdmCampSucess
		
	});
	
}

function selAdmCampSucess(result)
{
	window.location.href=path+"/users/summary/0/"+adcampid;
}


/****************************************************************************************************************************************/

//Load Campaign by userid
function getCampaign(userid)
{
	//Get Campaign
		
	new Ajax.Request('LoadCampaignByid/'+userid,
	{
		method: 'post', contentType:'application/json; charset=utf-8',
		onSuccess: getCampaignSucess
		
	});
}

function getCampaignSucess(result)
{
	
	$('updCampaign').update(result.responseText);
}

/****************************************************************************************************************************************/

//Update user's campaign
function updateCampaign()
{
	var userid=$F('hdnupUser');
	var campid=$F('listMapUd');

	if(campid!='')
	{
		//Update Campaigns
		new Ajax.Request('updateCampaign/'+userid+'/'+campid,
		{
			method: 'post', contentType:'application/json; charset=utf-8'
			//onSuccess: updateCampaignSucess,
			
		});
	}
	else
	{
		alert('Select atleast one campaign to update.');
	}
}


/****************************************************************************************************************************************/

//unselected options
function clearSelected(obj)
{
	var objElement=$(obj);
	for(var intCount=0;intCount<objElement.options.length;intCount++)
	{
		//Unselected all the options
		objElement.options[intCount].selected=false;
	}
}

//Show user's reports
function showReport(flag)
{
	var userid=$F('selUser');
	var startDate=$F('startDate');
	var endDate=$F('endDate');
	
	
		if(startDate !="")
		{
			
			if(flag==0)
			{
				new Ajax.Request('UsersReport/'+userid+'/'+startDate+'/'+endDate,
					{
						method: 'post', contentType:'application/json; charset=utf-8',
						onSuccess: showReportSucess
					});
			}
			else
			{
				//For all the Reports
				window.location.href=path+"/users/AllReports/"+startDate+"/"+endDate;
			}
			
		}
		else
		{
			alert("Select Start date.");
		}
	
	
}

function showReportSucess(result)
{
	var resultstr=result.responseText;
	var arrRes=resultstr.split(',');
	
	//Set Name
	$('name').innerHTML=arrRes[0];
	
	$('lastlog').update(arrRes[1]);
		
	$('totCom').update(arrRes[2]);
		
	$('totRes').update(arrRes[3]);
		
	$('totCamp').update(arrRes[4]);
	
	$('timespnt').update(arrRes[5]);
	
	document.getElementById('mainacc').style.display="block";
	
}

var intNewsIndex=0; 

function GetLatestNews()
{
	//Set news index
	intNewsIndex+=1;
	
	if(intNewsIndex>4)
	{
		intNewsIndex=0
	}
	
	new Ajax.Request('Complaints/GetNews',
	{
				method: 'post', 
				parameters: 'intIndex='+intNewsIndex,
				onSuccess: newsSuccess
	});
	
	
}

function newsSuccess(result)
{
	
	var news=result.responseText
	
	news=news.split('`^')
	
		
	$('ltHd').update("Someone has complaints on "+news[0]);
	
	$('ltNews').update(news[1]);
	
	$('ltDate').update(news[2]);
	
	
	 new Effect.Opacity('ltHd', { from: 0, to: 1,duration: 3});
	 new Effect.Opacity('ltNews', { from: 0, to: 1,duration: 3});
	 new Effect.Opacity('ltDate', { from: 0, to: 1,duration: 3});
	
	setTimeout("GetLatestNews();",7000); 
}


//Delete Campaign

function deleteCampaign(campid)
{
	//Get userid
	var userid=$F('hdnUserid');
	
	if(confirm('Do you want to delete campaign?'))
	{
			
			new Ajax.Request(path+'/users/DeleteCampaign/'+userid+'/'+campid,
			{
						method: 'post', contentType:'application/json; charset=utf-8',
						onSuccess: delCampSuccess
			});
	}
}

function delCampSuccess(result)
{
	applyshadow(1);
	
	//Refresh campaign data
	refreshCampData();
}

function addCampaign()
{
	//Get userid
	var userid=$F('hdnUserid');
	
	//Get campaignid
	var campid=$F('selCamp');
	
	new Ajax.Request(path+'/users/AddCampaign/'+userid+'/'+campid,
			{
						method: 'post', contentType:'application/json; charset=utf-8',
						onSuccess: addCampSuccess
			});
}



function addCampSuccess()
{
	applyshadow(1);
	
	//Refresh campaign data
	refreshCampData();
}

function refreshCampData()
{
	//Get userid
	var userid=$F('hdnUserid');
	
	new Ajax.Request(path+'/users/CampaignData/'+userid,
			{
						method: 'post', contentType:'application/json; charset=utf-8',
						onSuccess: CampDataSuccess
			});
}

function CampDataSuccess(result)
{
	
	$('dvCamp').update(result.responseText);
	applyshadow(0);
}


//Update Permissions
function updatePermm(val)
{
	$('hdnActive').value=val;
	
	applyshadow(1);
	$('FrmPerm').request({
 	 	onComplete: updatePermSuccess
	});
}

function updatePermSuccess(result)
{
	applyshadow(0);
	$('upmess').style.display="block";
}



 //Activate user
 function ActivateUser(userid)
 {
	 //Get table row length
	var rowlength=$('headtable').rows.length;
	
	 if(rowlength > 1)
	 {
		applyshadow(1);
		
		new Ajax.Request(path+'/users/doActive/'+userid,
			{
				method: 'post', contentType:'application/json; charset=utf-8',
				onSuccess: ActivateUserSuccess
				
			});
		
	 }
	 else
	 {
		 alert("Please select campaign to activate");
	 }
 }
 
 
 //Activate success
 function ActivateUserSuccess(result)
 {
	 window.location.href=path+"/users/home";
 }	
 
 
//Check Special character in string
function checkSpecChar(str)
{
	var blnResult=new Boolean(true);
	
	for(var intCount=0; intCount<str.length;intCount++)
	{
		for(var jValue=0;jValue<spchars.length;jValue++)
		{
			
			if(str.charAt(intCount)==spchars.charAt(jValue))
			{
				blnResult=false;
				break;
			}
			
		}
	}
		
	return blnResult;
}


//Check maximum length

function CheckMaxLenngth()
{
	var blnResult=new Boolean(false);
	var response=$F('txtRespDecp');
	
	//Get text Length
	var txtLength=response.length;
	
		
	if(txtLength <=1000)
	{
		
		blnResult=true;
		
	}
	else
	{
		alert('Response text cannot exceed 1000 characters')
		blnResult=false;
	}
	
	return blnResult;
}