//to get teh x and y poistion of an object
function getposOffset(overlay, offsettype){
  var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
  var parentEl=overlay.offsetParent;
  while (parentEl!=null){
  totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
  parentEl=parentEl.offsetParent;
  }
  return totaloffset;
}
function showsettings(val)
{
		close_other_popups('settings_box_holder');
		document.getElementById('settings_box_holder').innerHTML = "";
		document.getElementById("settings_box_holder").style.display='block';
		// close other open popups
    if(document.getElementById('termandcondition')){
        document.getElementById('termandcondition').style.display='none';
    }
    if(document.getElementById('coming_soon')){
   			document.getElementById('coming_soon').style.display='none';
   	}
   	if(document.getElementById('notdev')){
    		document.getElementById('notdev').style.display='none';
    }

		if(val == 'userdata')
		{
			var url = MP_ROOTPATH+'settings/profiles.php';
			params = '';
		}
		if(val == 'account')
		{
			var url = MP_ROOTPATH+'settings/account.php';
			params = '';
		}	
		if(val == 'notification')
		{
			var url = MP_ROOTPATH+'settings/notification.php';
			params = '';
		}				
		if(val == 'facebook_settings')
		{
			var url = MP_ROOTPATH+'settings/facebook_settings.php';
			params = '';
		}				

		if(window.XMLHttpRequest)
		{
			contactreq = new XMLHttpRequest();
		}
		else if(window.ActiveXObject)
		{
			contactreq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		contactreq.open("POST", url, true);

		contactreq.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
		contactreq.setRequestHeader("Content-length", params.length);
		contactreq.setRequestHeader("Connection", "close");

		contactreq.onreadystatechange = settings_callback;
		contactreq.send(params);
}
function settings_callback()
{
	if(contactreq.readyState == 4)
	{
		if(contactreq.status == 200)
		{
			response = contactreq.responseText;
			document.getElementById('settings_box_holder').innerHTML = response;
		}

	}
}
function closesettings()
{
	document.getElementById("settings_box_holder").style.display='none';
}
function makeDefaultPage(pageid)
{
    var xmlHttpReq6 = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq6 = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq6 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var strURL = MP_ROOTPATH+'settings/profiles.php';
    var param = 'act=makedefault&pageid='+pageid;
    self.xmlHttpReq6.open('POST', strURL, true);
    self.xmlHttpReq6.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq6.onreadystatechange = function() {
        if (self.xmlHttpReq6.readyState == 4) {
            handleMakeDefaultPage(self.xmlHttpReq6.responseText,pageid);
        }
	 }
    self.xmlHttpReq6.send(param);	
}
function handleMakeDefaultPage(str,pageid)
{
 	document.getElementById('settings_box_holder').innerHTML = str;
	show_detail(pageid);
}
function show_detail(sno)
{
	document.getElementById('sprofile_info_'+sno).style.display = 'block';
	document.getElementById('sprofile_action2_'+sno).style.display = 'block';
	document.getElementById('sprofile_action1_'+sno).style.display = 'none';
}
function hide_detail(sno)
{
	document.getElementById('sprofile_info_'+sno).style.display = 'none';
	document.getElementById('sprofile_action2_'+sno).style.display = 'none';
	document.getElementById('sprofile_action1_'+sno).style.display = 'block';
}

function settings_confirm_message(tis,txt,mode)
{
    if(mode=="show")
    {
     var curobj=tis;	 
     var xpos = getposOffset(curobj, "left")+((typeof opt_position!="undefined" && opt_position.indexOf("right")!=-1)? -(subobj.offsetWidth-curobj.offsetWidth) : 0)
     var ypos = getposOffset(curobj, "top")+((typeof opt_position!="undefined" && opt_position.indexOf("bottom")!=-1)? curobj.offsetHeight : 0)
     ypos = ypos;
		 if(ypos > 800)
		 {
			ypos = ypos-750;
		 }
		 else if(ypos > 400 && ypos < 800)
		 {
			ypos = ypos-200;
		 }
		 else if(ypos == 0){
			ypos = 200;
		 }
	     if(xpos > 1000)
	     xpos = xpos-300;
	     if(xpos > 500)
		 {
			xpos = xpos-380;
		 }
		 else if(xpos == 0){
			xpos = 300;
		 }
	     else
		 {
			xpos = xpos-10;
		 }	 
		 // alert(ypos+"===="+xpos);
     document.getElementById('settings_confrm_popup_content').innerHTML ="";
     document.getElementById("settings_confrm_popup").style.display ="block";
     document.getElementById('settings_confrm_popup_btn_div').style.display="block";
     document.getElementById('settings_confrm_popup_content').innerHTML = txt;
     document.getElementById("settings_confrm_popup").style.top = "100px";
     document.getElementById("settings_confrm_popup").style.left = "34%";
   }
   else if(mode=="hide")
   {
        document.getElementById("settings_confrm_popup").style.display ="none";
   }

}

function confirm_page_delete(tis,confirm_str,page_id)
{
    var strURL = MP_ROOTPATH+'settings/account.php';
    param = 'act=check_agenda&page_id='+page_id;
    $.post(strURL, param,
    function(result){
        if(trim(result) != '') 
        {
        	show_cant_delete_profile(result);
        }	
        else
        { 
					 document.getElementById('settings_confrm_btn1').onclick = function()
					 {
					  //when yes button is clicked
					    settings_confirm_message("0","","hide");//hidding the confirmation div 
					    
					  //showing the loader image
					  if(document.getElementById('store_load_img'))
					  {
					   document.getElementById('store_load_img').style.display = 'block' 
					  }       
					  var param = "act=del&page_id="+page_id;
					  var postURL = MP_ROOTPATH+'settings/profiles.php';
					  ajaxSetArray = new ajaxObject(postURL,update_profile_box,'');
					  ajaxSetArray.update(param, 'Post')      
					 };
					 document.getElementById('settings_confrm_btn2').onclick = function()
					 {
					  //when no button is clicked     
					      settings_confirm_message("0","","hide");
					     return false;     
					 };
					 settings_confirm_message(tis,confirm_str,"show");
				}
			}
		);
}
 
function update_profile_box(str)
{	
	if(str.substr(0,8) == 'REDIRECT')
	{
		var path = str.substr(9);
		location.href = path;
	}
	else if(str == 'DELETE_ACCOUNT')
	{
		//location.href = WEBTV_AJAXPATH;
		settings_confirm_message("0","","hide");
		show_cancel_account();  
	}
	else
	{	 	
		document.getElementById('settings_box_holder').innerHTML = str;
		// update profiles list in header my profiles
		var param = "act=update_header";
		var postURL = MP_ROOTPATH+'settings/profiles.php';
		ajaxSetArray = new ajaxObject(postURL,update_header_profilelist,'');
		ajaxSetArray.update(param, 'Post')  	
	 }
}

function update_header_profilelist(str)
{
	document.getElementById('header_member_pages').innerHTML = str;
}

// function to show can't remove all professional roles if any agenda is there for the page
function show_cant_delete_profile(str)
{
		document.getElementById('roles_notify_btn1').onclick = function() {//when yes button is clicked
			notify_message("0","","hide");
		};
		notify_message("0",str,"show");
		document.getElementById("roles_notify_popup").style.zIndex =50000;	
}


//to save the notification set by the user
function save_notification()
{
  var chck_values = getFormHeader('notification_frm');
  var param = "act=notification&"+chck_values;
  document.getElementById('loader_notificationBox').style.display = 'block';
  
  //alert(param)
  var postURL = MP_ROOTPATH+'settings/notification.php';
  ajaxSetArray = new ajaxObject(postURL,response_save_notification);
  ajaxSetArray.update(param, 'Post')    
}

function response_save_notification(responseText,responseStatus,responseXML)
{
	 if (responseStatus==200) 
    { 
        var response = responseText;
		document.getElementById('loader_notificationBox').style.display = 'none';
		document.getElementById('settings_box_holder').innerHTML = "";
		document.getElementById('settings_box_holder').innerHTML = response;
		hide_error_msg('success_msg_notification',100);
		return false;
	}
}

//to show the msg and fade away
function hide_error_msg(div,value1)
{
	if(value1 != 0)
	{
		val = parseInt(value1)-1;
		document.getElementById(div).style.filter='alpha(opacity='+val+')';
		document.getElementById(div).style.mozOpacity='0.'+val;
		document.getElementById(div).style.opacity='.'+val;
		//alert(val+"=="+div)
		window.setTimeout("hide_error_msg('"+div+"','"+val+"')",30);
	}
	else
	{
		document.getElementById(div).style.display='none';
	}
}

function show_change_email()
{
	document.getElementById('change_email').style.display = 'none';
	document.getElementById('save_email').style.display = 'block';
}
function save_account_email()
{
		var email_Format = /^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-zA-Z]{2,6}(\.[a-zA-Z]{2})?$/i;
		if(trim(document.getElementById("account_email").value).search(email_Format)== -1)
		{
				document.getElementById("account_email").style.border = "1px solid #ff0000";
			 	document.getElementById("account_email").focus();
		}
		else
		{				
		    var xmlHttpReq6 = false;
		    var self = this;
		    // Mozilla/Safari
		    if (window.XMLHttpRequest) {
		        self.xmlHttpReq6 = new XMLHttpRequest();
		    }
		    // IE
		    else if (window.ActiveXObject) {
		        self.xmlHttpReq6 = new ActiveXObject("Microsoft.XMLHTTP");
		    }
		    var strURL = MP_ROOTPATH+'settings/validateaccount.php';
		    var param = "act=check_email&email="+document.getElementById("account_email").value;
		    self.xmlHttpReq6.open('POST', strURL, true);
		    self.xmlHttpReq6.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		    self.xmlHttpReq6.onreadystatechange = function() {
		        if (self.xmlHttpReq6.readyState == 4) {
		            update_account_email(self.xmlHttpReq6.responseText);
		        }
			 }
		    self.xmlHttpReq6.send(param);			
		}	
}

function update_account_email(str)
{
	var status = str.substring(0,6);
	var err_msg = str.substring(6);
	if(status == 'ERROR:')
	{
		document.getElementById("account_email").style.border = "1px solid #ff0000";
	 	document.getElementById("account_email").focus();	
	 	if(err_msg != '')
	 	{
	 		document.getElementById('account_email_err').innerHTML = err_msg;
	 		fadein('account_email_err',100);
	 	}	
	}
	else
	{
		document.getElementById('save_email').style.display = 'none';		
		document.getElementById('change_email').style.display = 'block';
		document.getElementById('change_email').innerHTML = str;
	}
}

function change_password()
{
		document.getElementById('loader_accountBox').style.display = 'block';
		if(trim(document.getElementById("old_pwd").value)=="")
		{
				document.getElementById("old_pwd").style.border = "1px solid #ff0000";
			 	document.getElementById("old_pwd").focus();	        
		}
		else if(trim(document.getElementById("new_pwd").value)=="")
		{
				document.getElementById("new_pwd").style.border = "1px solid #ff0000";
			 	document.getElementById("new_pwd").focus();	
		}
		else if(trim(document.getElementById("retype_pwd").value)=="")
		{
				document.getElementById("retype_pwd").style.border = "1px solid #ff0000";
			 	document.getElementById("retype_pwd").focus();	
		}
		else
		{					
		   /*
		    var xmlHttpReq6 = false;
		    var self = this;
		    // Mozilla/Safari
		    if (window.XMLHttpRequest) {
		        self.xmlHttpReq6 = new XMLHttpRequest();
		    }
		    // IE
		    else if (window.ActiveXObject) {
		        self.xmlHttpReq6 = new ActiveXObject("Microsoft.XMLHTTP");
		    }
		    var old_pwd = trim(document.getElementById("old_pwd").value);
		    var new_pwd = trim(document.getElementById("new_pwd").value);
		    var retype_pwd = trim(document.getElementById("retype_pwd").value);
		    
		    var strURL = MP_ROOTPATH+'settings/validateaccount.php';
		    var param = 'act=check_pwd&old_pwd='+old_pwd+'&new_pwd='+new_pwd+'&retype_pwd='+retype_pwd;
		    self.xmlHttpReq6.open('POST', strURL, true);
		    self.xmlHttpReq6.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		    self.xmlHttpReq6.onreadystatechange = function() {
		        if (self.xmlHttpReq6.readyState == 4) {
		            check_account_pwd(self.xmlHttpReq6.responseText);
		        }
			 }
		    self.xmlHttpReq6.send(param);		
		    */
		    var strURL = MP_ROOTPATH+'settings/validateaccount.php';
		    var param = $("#frm_account_info").serialize();	
		    param += '&act=check_pwd';
		    $.post(strURL, param,
		    function(data){
		        
		        check_account_pwd(data);
				document.getElementById('loader_accountBox').style.display = 'none';
		        
		    });
		    
		}		
}
function check_account_pwd(str)
{
	var status = str.split(':');
	if(status[0] == 'ERROR')
	{
		if(status[1] == '1')
		{
			document.getElementById("old_pwd").style.border = "1px solid #ff0000";
		 	document.getElementById("old_pwd").focus();	
			document.getElementById("new_pwd").style.border = "none";
			document.getElementById("retype_pwd").style.border = "none";		 	
		}	
		else if(status[1] == '2')
		{
			document.getElementById("old_pwd").style.border = "none";
			document.getElementById("new_pwd").style.border = "1px solid #ff0000";
			document.getElementById("retype_pwd").style.border = "1px solid #ff0000";
		 	document.getElementById("new_pwd").focus();	
		}		
		else if(status[1] == '12')
		{
			document.getElementById("old_pwd").style.border = "1px solid #ff0000";
		 	document.getElementById("old_pwd").focus();	
			document.getElementById("new_pwd").style.border = "1px solid #ff0000";
			document.getElementById("retype_pwd").style.border = "1px solid #ff0000";		 	
		}				
	}
	else
	{
		document.getElementById('account_cpwd_msg').innerHTML = status[2];
		fadein('account_cpwd_msg',100);
		document.getElementById("old_pwd").style.border = "none";
		document.getElementById("new_pwd").style.border = "none";
		document.getElementById("retype_pwd").style.border = "none";
		document.getElementById("old_pwd").value = "";
		document.getElementById("new_pwd").value = "";
		document.getElementById("retype_pwd").value = "";		
	}	
}

function confirm_cancel_account(tis,confirm_str)
{
    var strURL = MP_ROOTPATH+'settings/account.php';
    param = 'act=check_profile_agenda';
    $.post(strURL, param,
    function(result){
        if(trim(result) != '') 
        {
        	show_cant_delete_profile(result);
        }	
        else
        { 	
					 document.getElementById('settings_confrm_btn1').onclick = function()
					 {
					  //when yes button is clicked
					    settings_confirm_message("0","","hide");//hidding the confirmation div 
					    
					  //showing the loader image
					  if(document.getElementById('store_load_img'))
					  {
					   document.getElementById('store_load_img').style.display = 'block' 
					  }       
						show_cancel_account();    
					 };
					 document.getElementById('settings_confrm_btn2').onclick = function()
					 {
					  //when no button is clicked     
					      settings_confirm_message("0","","hide");
					     return false;     
					 };
					 settings_confirm_message(tis,confirm_str,"show");
				}
			});
}
function show_cancel_account()
{
	var strURL = MP_ROOTPATH+'settings/cancel_account.php';
	var param = '';	
	$.post(strURL, param,
	function(response){
		document.getElementById('cancel_acct_box_content').innerHTML = response;
	});	
	closesettings();
	document.getElementById("cancel_acct_box_holder").style.display = "block";
	document.getElementById("frm_cancel_acct").reset();
}
function close_cancel_account()
{
	document.getElementById("cancel_acct_box_holder").style.display = "none";	
}


//to fade away a div
function fadein(div,duration)
{
		var val = parseInt(duration)-1;
		if(val > 10) 
		{
			document.getElementById(div).style.filter='alpha(opacity='+val+')';
			document.getElementById(div).style.mozOpacity='0.'+val;
			document.getElementById(div).style.opacity='.'+val;
			window.setTimeout("fadein('"+div+"','"+val+"')",30);	
		}
		else
		{
			document.getElementById(div).innerHTML = '';
			window.clearTimeout("fadein");
		}
}

function cancel_my_account(err_msg)
{
	var sel = 0;
	for (var i=0; i < document.frm_cancel_acct.cancel_reason.length; i++)
	{
		if (document.frm_cancel_acct.cancel_reason[i].checked)
	  {
	  	var sel = 1;
	  	/*
	  	var reason = document.frm_cancel_acct.cancel_reason[i].value;
	  	var desc = document.getElementById('cancel_acct_desc').value;
		  var param = "act=cancel_account&reason="+reason+"&desc="+desc;
		  var postURL = MP_ROOTPATH+'settings/account.php';
		  ajaxSetArray = new ajaxObject(postURL,response_cancel_account);
		  ajaxSetArray.update(param, 'Post') 	  	
		  */
		    var strURL = MP_ROOTPATH+'settings/account.php';
		    var param = $("#frm_cancel_acct").serialize();		    
		    
		    param += '&act=cancel_account';
		    $.post(strURL, param,
		    function(data){
		        
		        response_cancel_account(data);
		        
		    });		
	  }
	}	
	if(sel == '0') 
	{
		document.getElementById('cancel_status').innerHTML = err_msg;
		fadein('cancel_status',100);
	}
}
function response_cancel_account(str)
{
	document.getElementById('cancel_status').innerHTML = str;
	fadein('cancel_status',100);
	location.href = WEBTV_AJAXPATH;
}
function show_notification_box()
{
	close_cancel_account();
	showsettings('notification');
}
function show_help_box(tis)
{
	show_not_done_link(tis, 'top');	
}
function confirm_fb_cancel_account(tis,confirm_str)
{
 document.getElementById('settings_confrm_btn1').onclick = function()
 {
  //when yes button is clicked
    settings_confirm_message("0","","hide");//hidding the confirmation div 
    
  //showing the loader image
  if(document.getElementById('store_load_img'))
  {
   document.getElementById('store_load_img').style.display = 'block';
  }       
	fb_cancel_account();    
 };
 document.getElementById('settings_confrm_btn2').onclick = function()
 {
  //when no button is clicked     
      settings_confirm_message("0","","hide");
     return false;     
 };
 settings_confirm_message(tis,confirm_str,"show");	
}
function fb_cancel_account()
{
	//implement code to cancel from facebook account
		var url = MP_ROOTPATH+'settings/revoke_fb_authorization.php';
		$.post(url, { act: "disconnect_fb_account"},
		  function(data){
			if(data == 'SUCESS')
			{
				header_refresh();			
				$("#id_fb_cancel_account").css("color","#124F0F");
				$("#id_fb_cancel_account").html($("#fb_msg_cancel_account").val());
				$("#id_fb_cancel_account").fadeIn("slow");
				$("#id_fb_cancel_account").css("display","none");
			}
		  });	
}

//to save the notification set by the user
function save_fb_settings()
{

  document.getElementById('loader_fbSettingsBox').style.display = 'block';
  var fb_frm_values = getFormHeader('fbSettings_frm');
  var param = "act=fbsettings&"+fb_frm_values;
  //alert(param)
  var postURL = MP_ROOTPATH+'settings/facebook_settings.php';
  ajaxSetArray = new ajaxObject(postURL,response_save_fb_settings);
  ajaxSetArray.update(param, 'Post')    
}

function response_save_fb_settings(responseText,responseStatus,responseXML)
{
	 if (responseStatus==200) 
    { 
        var fbresponse = responseText;
		document.getElementById('settings_box_holder').innerHTML = "";
		document.getElementById('settings_box_holder').innerHTML = fbresponse;
		document.getElementById('loader_fbSettingsBox').style.display = 'none';
		hide_error_msg('success_msg_fbSettings',100);
		return false;
	}
}
//function for showing confirmation msg in divs with OK button only
function notify_message(tis,txt,mode)
{
    if(mode=="show")
    {
     var curobj=tis;	 
     var xpos = getposOffset(curobj, "left")+((typeof opt_position!="undefined" && opt_position.indexOf("right")!=-1)? -(subobj.offsetWidth-curobj.offsetWidth) : 0)
     var ypos = getposOffset(curobj, "top")+((typeof opt_position!="undefined" && opt_position.indexOf("bottom")!=-1)? curobj.offsetHeight : 0)
     ypos = ypos;
	 if(ypos > 800)
	 {
		ypos = ypos-750;
	 }
	 else if(ypos > 400 && ypos < 800)
	 {
		ypos = ypos-200;
	 }
	 else if(ypos == 0){
		ypos = 200;
	 }
     if(xpos > 1000)
     xpos = xpos-300;
     if(xpos > 500)
	 {
		xpos = xpos-380;
	 }
	 else if(xpos == 0){
		xpos = 300;
	 }
     else
	 {
		xpos = xpos-10;
	 }	 
	// alert(ypos+"===="+xpos);
     document.getElementById('roles_notify_popup_content').innerHTML ="";
     document.getElementById("roles_notify_popup").style.display ="block";
     document.getElementById('roles_notify_popup_btn_div').style.display="block";
     document.getElementById('roles_notify_popup_content').innerHTML = txt;
     document.getElementById("roles_notify_popup").style.top = "100px";
     document.getElementById("roles_notify_popup").style.left = "32%";
   }
   else if(mode=="hide")
   {
        document.getElementById("roles_notify_popup").style.display ="none";
   }

}

function makeDefaultNetwork(netid,pageid,rownum)
{
    var xmlHttpReq6 = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq6 = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq6 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var strURL = MP_ROOTPATH+'settings/profiles.php';
    var param = 'act=makeDefaultNetwork&netid='+netid+'&pageid='+pageid;
    self.xmlHttpReq6.open('POST', strURL, true);
    self.xmlHttpReq6.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq6.onreadystatechange = function() {
        if (self.xmlHttpReq6.readyState == 4) {
            handleMakeDefaultNetwork(self.xmlHttpReq6.responseText,pageid);
        }
	 }
    self.xmlHttpReq6.send(param);	
}
function handleMakeDefaultNetwork(str,pageid)
{
 	document.getElementById('networks_'+pageid).innerHTML = str;
}
