function showpopupmagic(){
	openShadowbox('/util/popup.php', 'iframe', 'B2BBloggers','832','390');
}

function startpopupmagictimer(){
	setTimeout("showpopupmagic()",30000); // EVERY MINUTE, REFRESH STUFF
}

function doIntervalTwitterSearch(url){
	var url = twittersearchurl;
	jQuery.ajax({
		url: url,
		cache: false,
		success: function(html){
			handleTwitterSearchB(html);
		},
		error: function(o,m,e){
			//alert( "Load Data NOT Saved: " + m + " : " + e );
		}
	});
}

function doTwitterSearch(url){
	jQuery.ajax({
		url: url,
		cache: false,
		success: function(html){
			handleTwitterSearchB(html);
		},
		error: function(o,m,e){
			//alert( "Load Data NOT Saved: " + m + " : " + e );
		}
	});
}

function closeFromBox(){
	var Shadowbox = window.parent.Shadowbox;
	Shadowbox.close();
}

function openShadowbox(content, player, title, width, height){
    Shadowbox.open({
        content:    content,
        player:     player,
        title:      title,
        width: width,
        height: height
    });
}

function closePopUp(){

}

var gl_html = '';
function display_popup_stuff(surl,id){
	//alert(surl);
	jQuery.ajax({
  		url: surl,
  		cache: false,
  		success: function(html){
  			//jQuery.("#"+id).empty();
  			//jQuery.("#"+id).append(html);
  			if(document.getElementById(id)){
				document.getElementById(id).innerHTML = html;
			}  			
  		},
  		error: function(o,m,e){
     		//alert( "Cant get the page content: " + m + " : " + e );
   		}
   	});
}

var loadingIMG = "<div style='text-align:center;margin:10px;'><img src='/images/loadinfotransparent24.gif' style='margin-bottom:5px;' /><br />Please Wait...</div>";
function doJoin(fname,lname,email,listid){
	var url = "/util/forms/do_mail_list_join.php?email=" + email + "&fname=" + fname + "&lname=" + lname;
	
	if(document.getElementById('loadingIMG')){
		document.getElementById('loadingIMG').innerHTML = loadingIMG;	
	}
	if(document.getElementById('mailFORM')){
		document.getElementById('mailFORM').style.display = 'none';	
	}
	
  	jQuery.ajax({
    	url: url,
    	cache: false,
    	success: function(html) {    	
    		//alert(html);
    		if( html == '1' ){
    			displayOKJoin(email);
     			//logOKJoin(email);
    		} else if(html == '214'){
    			//alert(email + " is already subscribe to our newsletter");
    			if(document.getElementById('mailFORM')){
					document.getElementById('mailFORM').style.display = 'block';	
				}
    			addError("email_id",email + " is already subscribed to our newsletter");
        		erct = 1;
    		} else {
    			alert("There was an error entering your subscription.  Please try again.");
    			if(document.getElementById('mailFORM')){
					document.getElementById('mailFORM').style.display = 'block';	
				}
    		}		
    		if(document.getElementById('loadingIMG')){
				document.getElementById('loadingIMG').innerHTML = '';	
			}	
		},
		error: function(o,m,e){
     		//alert( "Error: " + m + " : " + e + " : " + o.responseText );
     		alert("There was an error entering your subscription.  Please try again.");
     		if(document.getElementById('loadingIMG')){
				document.getElementById('loadingIMG').innerHTML = '';	
			}
			if(document.getElementById('mailFORM')){
				document.getElementById('mailFORM').style.display = 'block';	
			}
   		}
    });
	return 1;
}


function doJoinOLD(fname,lname,email,listid){
	var apikey = '7d11f5ab060410ff1f891363c0a71a97-us1';
	var listID = '6828b296df';
	var url = "http://btobbloggers.us1.list-manage1.com/subscribe/post?u=cb8b064638e9b1e39a40d417c&id=6828b296df";
	var url2 = "http://api.mailchimp.com/1.2/?method=listSubscribe&apikey=" + apikey + "&id=" + listID + "&email_address=" + email + "&merge_vars[MMERGE2]=" + fname + "&merge_vars[MMERGE3]=" + lname + "&output=json";
			
  	jQuery.ajax({
    	url: url2,
    	cache: false,
    	success: function(html) {
			//alert(html);
			//var st = o.status;
			//var rt = o.responseText;
			//var rs = o.readyState;
			//alert(st + " : " + rt + " : " + rs + " : " + html);
			//display_popup_stuff("","");
			displayOKJoin(email);
     		//logOKJoin(email);
		},
		error: function(o,m,e){
     		//alert( "Error: " + m + " : " + e + " : " + o.responseText );
     		if( o.readyState == 4 && !e ){
     			displayOKJoin(email);
     			//logOKJoin(email);
     		}
   		}
    });
	return 1;
}

function displayOKJoin(email){
	display_popup_stuff("/util/forms/mail_list_success.php","mail_list_column");	
}

function logOKJoin(email){
	var url = "/util/forms/mail_list_log.php?email=" + email;
	jQuery.ajax({
  		url: url,
  		cache: false,
  		success: function(html){
 	
  		},
  		error: function(o,m,e){
     		//alert( "Cant get the page content: " + m + " : " + e );
   		}
   	});
}



function displayErrorJoin(e){
	alert('There was an error signing up: ' + e);
}

function doNewsletterSignup(){
	if( !validateNewsletterSignup() ){
		return 0;
	}
	doJoin(document.getElementById('firstName').value,document.getElementById('lastName').value,document.getElementById('email').value);
}

var ppwin=0;
function doRSSSignup(){
	if( !validateRSSSignup() ){
		if(ppwin){
			//ppwin.close();
		}
		return 0;
	}
	doRSSJoin(document.getElementById('email').value);
	//document.rssform.submit();
	
	return 1;
}

function doRSSJoin(email){
	var url = "/util/forms/rss_list_log.php?em=" + email;
  	jQuery.ajax({
    	url: url,
    	cache: false,
    	success: function(html) {
			displayOKRSSJoin(email);
		},
		error: function(o,m,e){
     		//alert( "Error: " + m + " : " + e + " : " + o.responseText );
     		if( o.readyState == 4 && !e ){
     			displayOKRSSJoin(email);
     		}
   		}
    });
	return 1;
}

function displayOKRSSJoin(email){
	//display_popup_stuff("/util/forms/rss_list_success.php","rss_list_column");	
}

/************* SIGNUP VALIDATION FUNCTIONS *********************/
var er_img = "<img src='/images/dialog-warning.gif' width='16' height='16' align='' style='margin:0 5px 0 5px;' />"
function removeError(id){
	var el = document.getElementById(id);
	var el2 = document.getElementById(id + "_error");
	if(!el || !el2){
		return;
	}
	el.className = "";
    el2.innerHTML = "";
    el2.style.display='none';
}
function addError(id,msg){
	var el = document.getElementById(id);
	var el2 = document.getElementById(id + "_error");
	if(!el || !el2){
		return;
	}
	el.className = "error_msg";
    el2.innerHTML = er_img + msg;
    el2.style.display='block';
}
function removeNonDigits(str){
	var digits = str.replace(/[^0-9]/ig, ''); 
	return digits;
}
function is_valid_email(){
	return true;
}

function validateEmail(addr,man,db) {
if (addr == '' && man) {
   if (db) alert('email address is mandatory');
   return false;
}
if (addr == '') return true;
var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
for (i=0; i<invalidChars.length; i++) {
   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
      if (db) alert('email address contains invalid characters');
      return false;
   }
}
for (i=0; i<addr.length; i++) {
   if (addr.charCodeAt(i)>127) {
      if (db) alert("email address contains non ascii characters.");
      return false;
   }
}

var atPos = addr.indexOf('@',0);
if (atPos == -1) {
   if (db) alert('email address must contain an @');
   return false;
}
if (atPos == 0) {
   if (db) alert('email address must not start with @');
   return false;
}
if (addr.indexOf('@', atPos + 1) > - 1) {
   if (db) alert('email address must contain only one @');
   return false;
}
if (addr.indexOf('.', atPos) == -1) {
   if (db) alert('email address must contain a period in the domain name');
   return false;
}
if (addr.indexOf('@.',0) != -1) {
   if (db) alert('period must not immediately follow @ in email address');
   return false;
}
if (addr.indexOf('.@',0) != -1){
   if (db) alert('period must not immediately precede @ in email address');
   return false;
}
if (addr.indexOf('..',0) != -1) {
   if (db) alert('two periods must not be adjacent in email address');
   return false;
}
var suffix = addr.substring(addr.lastIndexOf('.')+1);
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
   if (db) alert('invalid primary domain in email address');
   return false;
}
return true;
}

function validateNewsletterSignup(zform){
	//return true;
	removeRSSErrors();
    var erct = 0;    
    if(document.getElementById('firstName') && document.getElementById('firstName').value==""){
         addError("first_name_id","Your first name is required");
         erct = 1;
    } else {
    	removeError("first_name_id"); 
    }
    if(document.getElementById('lastName') && document.getElementById('lastName').value==""){
         addError("last_name_id","Your last name is required");
         erct = 1;
    } else {
    	removeError("last_name_id"); 
    }
    
    if(document.getElementById('email') && document.getElementById('email').value==""){
        addError("email_id","Please enter your email address");
        erct = 1;
    } else if(document.getElementById('email') && !validateEmail(document.getElementById('email').value,1,0)){
        addError("email_id","Please enter a valid email address");
        erct = 1;
    } else {
    	removeError("email_id"); 
    }            
  
  
    if(erct){
    	//display_newsletter_error_display();
        return false;
    }
    return true;
}

function validateRSSSignup(zform){
	removeNewsletterErrors();
	var erct = 0;
	if(document.getElementById('rssemail') && document.getElementById('rssemail').value==""){
         addError("rss_email_id","Please enter a valid email address");
         erct = 1;
    } else if(document.getElementById('rssemail') && !validateEmail(document.getElementById('rssemail').value,1,0)){
        addError("rss_email_id","Please enter a valid email address");
        erct = 1;
    } else {
    	removeError("rss_email_id"); 
    }
   
    if(erct){
    	display_rss_error_display();
        return false;
    }
    return true;
}

function removeRSSErrors(){
	removeError("rss_email_id"); 
}

function removeNewsletterErrors(){
	removeError("first_name_id");
	removeError("last_name_id");
	removeError("email_id");
}

function display_newsletter_error_display(){
	if(document.getElementById('newsletter_error_display')){
		document.getElementById('newsletter_error_display').style.display='block';
	}
	remove_rss_error_display();
}
function display_rss_error_display(){
	if(document.getElementById('rss_error_display')){
		document.getElementById('rss_error_display').style.display='block';
	}
	remove_newsletter_error_display();
}
function remove_newsletter_error_display(){
	if(document.getElementById('newsletter_error_display')){
		document.getElementById('newsletter_error_display').style.display='none';
	}
}
function remove_rss_error_display(){
	if(document.getElementById('rss_error_display')){
		document.getElementById('rss_error_display').style.display='none';
	}
}