var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}

//-------------------------------------------------------------------------------
//AJAX FUNCTIONS START HERE
var divid;
var actionstr;

var showobj;
		var pageURL;
		function makeRequest(url,arg,obj) {
			//alert(url);
			pageURL=url;	
			arg=getargs(arg);
			showobj=obj;
			var http_request = false;
			if (window.XMLHttpRequest) { // Mozilla, Safari, ...
				http_request = new XMLHttpRequest();
				if (http_request.overrideMimeType) {
					http_request.overrideMimeType('text/xml');
				}
			} else if (window.ActiveXObject) { // IE
				try {
					http_request = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try {
						http_request = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {}
				}
			}
			if (!http_request) {
				alert('Giving up :( Cannot create an XMLHTTP instance');
				return false;
			}
		
			http_request.onreadystatechange = function() { handleRespon(http_request); };
			http_request.open('POST', url, true);
			http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			http_request.setRequestHeader("Content-Length", arg.length);
			http_request.send(arg);
		}
			
		
		function handleRespon(http_request) {
			if (http_request.readyState == 4) {
				//alert(http_request.responseText);
				if (http_request.status == 200) {
					//alert(showobj);
					//alert(http_request.responseText);
					document.getElementById(showobj).innerHTML =  http_request.responseText;
					
					var checkvalue = http_request.responseText;	
					
					//alert(checkvalue);
					
					//document.getElementById('pb1').style.display='none';
					
				} else {
					alert('Your request can\'t process.\nThere is some problem with the request.');
				}
			}
		}
		
		function getargs(arg){
			var val;
			var timestamp = Date.parse(new Date()); //document.lastModified;
			if (typeof arg != 'undefined'){	val = "sess="+timestamp+"&"+arg;	}
			else{	val = "sess="+timestamp; }
			return val;
		}


//AJAX FUNCTION ENDS HERE
//-------------------------------------------------------------------------------



//-------------------------------------------------------------------------------
//show Sub-Tabs starts here

function shw_subtab(id,holder)
	{
		//alert(id+"------"+holder);
		//return false;
		var FileName 	=	 "subtabs.php";
		var arg 		=	 "ban_tabid="+id;
		makeRequest(FileName,arg,holder);
	}

//show Sub-Tabs  ends here
//-------------------------------------------------------------------------------
function refreshNewdiv(action, page)
{
	 $.ajax({
	   type: "POST",
	   url: 'includes/operations/refreshNewdiv_process.php',
	   data: 'action='+action+'&page='+page,
	   
	   success: function(msg)
	   {
			document.getElementById('results').innerHTML = msg;
			$('a[rel*=facebox]').facebox()
	   },
	   error: function(){ //so, if data is retrieved, store it in html 
			//alert('error')
			//$("#"+resultDiv).html('Error Loading Script'); //show the html inside .content div 
	   }
	 }); //refreshNewdiv_process.php
}

//-----------------------------------------------------------------------------//

function refreshApprovediv(action, page)
{
	 $.ajax({
	   type: "POST",
	   url: 'includes/operations/refreshApprovediv_process.php',
	   data: 'action='+action+'&page='+page,
	   
	   success: function(msg)
	   {
			document.getElementById('results').innerHTML = msg;
			$('a[rel*=facebox]').facebox()
	   },
	   error: function(){ //so, if data is retrieved, store it in html 
			//alert('error')
			//$("#"+resultDiv).html('Error Loading Script'); //show the html inside .content div 
	   }
	 });
}

//-----------------------------------------------------------------------------//

function refreshRejecteddiv(action, page)
{
	 $.ajax({
	   type: "POST",
	   url: 'includes/operations/refreshRejecteddiv_process.php',
	   data: 'action='+action+'&page='+page,
	   
	   success: function(msg)
	   {
			document.getElementById('results').innerHTML = msg;
			$('a[rel*=facebox]').facebox()
	   },
	   error: function(){ //so, if data is retrieved, store it in html 
			//alert('error')
			//$("#"+resultDiv).html('Error Loading Script'); //show the html inside .content div 
	   }
	 });
}

//-----------------------------------------------------------------------------//


function send_email(appid, action, page, s)
{
	  $.ajax({
	   type: "POST",
	   url: 'includes/operations/sendemail_process.php',
	   data: 'appid='+appid+'&action='+action+'&page='+page,
	   
	   success: function(msg)
	   {
		   var myarray=new Array();
		   myarray=msg.split("-separator-");
		   
		   if(myarray[0] == 'done')
		   {
			   document.getElementById('output').innerHTML = myarray[1];
			   //document.getElementById('output').style.display = '';
			   jQuery('#output').fadeIn(500);
			   setTimeout("$(document).trigger(\'close.facebox\')", 2000);
			   
			   //----------------//
			 
			   if(s == 'new')
			   {
				   setTimeout("refreshNewdiv('"+action+"','"+page+"')", 2002);
			   }
			   else if(s == 'approved')
			   {
				   setTimeout("refreshApprovediv('"+action+"','"+page+"')", 2002);
			   }
			   else if(s == 'rejected')
			   {
				   setTimeout("refreshRejecteddiv('"+action+"','"+page+"')", 2002);
			   }
			   //----------------//
			   
		   }
		},
		error: function(){ //so, if data is retrieved, store it in html 
			//alert('error')
			//$("#"+resultDiv).html('Error Loading Script'); //show the html inside .content div 
		}
	 });
}


//--------------------------------------------------------------------------------//

function re_send_email(appid)
{
	  var msgbody;
	  msgbody = document.getElementById('msgbody').value;
	  
	  $.ajax({
	   type: "POST",
	   url: 'includes/operations/sendemail_process.php',
	   data: 'appid='+appid+'&action=resend&msgbody='+msgbody,
	   
	   success: function(msg)
	   {
		   var myarray=new Array();
		   myarray=msg.split("-separator-");
		   
		   if(myarray[0] == 'done')
		   {
			   document.getElementById('output').innerHTML = myarray[1];
			   //document.getElementById('output').style.display = '';
			   jQuery('#output').fadeIn(500);
			   setTimeout("$(document).trigger(\'close.facebox\')", 2000);
		   }
		},
		error: function(){ //so, if data is retrieved, store it in html 
			//alert('error')
			//$("#"+resultDiv).html('Error Loading Script'); //show the html inside .content div 
		}
	 });
}

//--------------------------------------------------------------------------------//
function gotohomepage(url)
{
	window.location = url;
}

//--------------------------------------------------------------------------------//
function signupsubmit()
{
	$('#forma').unbind('submit');
		var options = {
		target: '#signupoutup', // target element(s) to be updated with server response
		beforeSubmit: showsignupreqRequest, // pre-submit callback
		success: showsignupreqtResponse, // post-submit callback
		url: 'process/signup_process.php' //override for form's 'action' attribute //type: type // 'get' or 'post', override for form's 'method' attribute
	};

	// bind to the form's submit event
	$('#forma').submit(function() {
	$(this).ajaxSubmit(options);
	
	return false;
	});
}
function showsignupreqRequest(formData, jqForm, options)
{
	var queryString = $.param(formData);
	return true;
}

function showsignupreqtResponse(responseText, statusText)
{
	if(responseText == 'done')
	{
		jQuery('#signupoutup').hide();
		
		window.location='survey.php';
	}
	else
	{
		jQuery('#signupoutup').show();
	}
}

function loginsubmit()
{
	
	$('#login').unbind('submit');
		var options = {
		target: '#loginerror', // target element(s) to be updated with server response
		beforeSubmit: loginRequest, // pre-submit callback
		success: showloginResponse, // post-submit callback
		url: 'process/login_process.php' //override for form's 'action' attribute //type: type // 'get' or 'post', override for form's 'method' attribute
	};

	// bind to the form's submit event
	$('#login').submit(function() {
								
	$(this).ajaxSubmit(options);
	
	return false;
	});
}

// pre-submit callback
function loginRequest(formData, jqForm, options)
{
	var queryString = $.param(formData);
	return true;
}

function showloginResponse(responseText, statusText)
{//   alert(responseText);
	if(responseText == 'done')
	{
		jQuery('#loginerror').hide();
		window.location='survey.php';
	}
	else
	{
		jQuery('#loginerror').show();
	}
}

function updatesubmit()
{  
	$('#updateform').unbind('submit');
		var options = {
		target: '#updatebutton', // target element(s) to be updated with server response
		beforeSubmit: updateRequest, // pre-submit callback
		success: showupdateResponse, // post-submit callback
		url: 'process/update_process.php' //override for form's 'action' attribute //type: type // 'get' or 'post', override for form's 'method' attribute
	};

	// bind to the form's submit event
	$('#updateform').submit(function() {
	$(this).ajaxSubmit(options);
	
	return false;
	});
}
function updateRequest(formData, jqForm, options)
{
	var queryString = $.param(formData);
	return true;
}

function showupdateResponse(responseText, statusText)
{

	if(responseText == 'done')
	{
		jQuery('#updatebutton').hide();
		window.location='surveys.php';
	}
	else
	{
		jQuery('#updatebutton').show();
	}
}


//--------------------------------------------------------------------------------//
function addnewlistuser()
{
	$('#addnewlistform').unbind('submit');
		var options = {
		target: '#output', // target element(s) to be updated with server response
		beforeSubmit: showaddlistuserRequest, // pre-submit callback
		success: showaddlistuserResponse, // post-submit callback
		url: 'includes/operations/addnewlist_user.php' //override for form's 'action' attribute //type: type // 'get' or 'post', override for form's 'method' attribute
	};

	// bind to the form's submit event
	$('#addnewlistform').submit(function() {
	$(this).ajaxSubmit(options);
	
	return false;
	});
}

// pre-submit callback
function showaddlistuserRequest(formData, jqForm, options)
{
	var queryString = $.param(formData);
	return true;
}

function confirmuserdel(user_id)
{
	var res=confirm("Are you sure to delete this?");
	if (res){
		window.location = 'deleteuser.php?user_id='+user_id;
		return true ;
	}
	else
	return false ;
}

function showaddlistuserResponse(responseText, statusText)
{
	var myarray=new Array();
	myarray=responseText.split("-separator-");
	
	if(myarray[0] == 'done')
	{
		document.getElementById('output').innerHTML = '<span style="color:#6BAC01">User Save Successfully!</span>';
		jQuery('#output').fadeIn(800);
		jQuery('#output').fadeOut(3000);
		
		setTimeout('gotohomepage(\''+myarray[1]+'\')', 3005);
	}
	else
	{
		jQuery('#output').fadeIn(800);
		jQuery('#output').fadeOut(3000);
	}
}

