/* 
 * emailbulletin.js
 * 
 * collection of scripts to manage and display of email bulletin pop-up prompt
 * 
 */
 
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	var basedomain = "";
	var tempdomainparts = new Array(); 	
	tempdomainparts = document.domain.split('.');
	if (tempdomainparts.length >= 2) {
		basedomain = tempdomainparts[tempdomainparts.length - 2] + '.' + tempdomainparts[tempdomainparts.length - 1] ;
		//window.alert('domain='+basedomain); 
		document.cookie = name+"="+value+expires+"; path=/ ; domain="+basedomain;
	}
	else {
		document.cookie = name+"="+value+expires+"; path=/";
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function ShowBulletinPopup(elementId, cookieName) {
	//alert("ShowBulletinPopup elementId " + elementId); 
	//alert("ShowBulletinPopup cookieName " + cookieName); 
	if (cookieName != null) {
		createCookie(cookieName, '1', 90);
	}
	var BbsBulletinPopBasic1 = document.getElementById(elementId);
	BbsBulletinPopBasic1.style.visibility = "visible";
}

function CloseBulletinPopup(elementId, cookieName) {
	//alert("elementId " + elementId); 
	if (cookieName != null) {
		createCookie(cookieName, '1', 90);
	}
	var BbsBulletinPopBasic1 = document.getElementById(elementId);
	BbsBulletinPopBasic1.style.visibility = "hidden";
	BbsBulletinPopBasic1.style.display = "none";
	
}

function checkEmail(emailAddr) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailAddr)){
		return (true)
	}
	return (false)
}

function processStateChange() {			
	if (onLoadReq.readyState == 4) { // Complete
	  if (onLoadReq.status == 200) { // OK response					
		//alert("Ajax response:"+req.responseText);									
	  } else {
		//alert("Problem with server response:\n " + req.statusText);
	  }
	}
}

function OptInBBS(form, elementId, cookieName, sid) {			
	var submitEmail = false;
	var szEmail = ""; 
	var szList = '0'; 
	
	if (form.Semail != null && form.Semail.value.length > 0 && checkEmail(form.Semail.value)) {
		submitEmail = true;
		szEmail = form.Semail.value;
		//alert("Email address" + szEmail)
	} else {
	  submitAd = false;
	  alert("Valid email address required")
	  form.Semail.focus();
	}
	
	//JMR added ability to pass listing id in - used in fsbo manager
	if (form.Slist != null && form.Slist.value.length > 0) {
		szList = form.Slist.value;
		//alert("ListNumber " + szList)
	} 
		
	
	if (submitEmail == true) {				
		window.alert("Thank you for signing up for the BizBuySell Bulletin."); 
		CloseBulletinPopup(elementId, cookieName);
		if ( cookieName != null ) {
			createCookie('BbsBulletinOpted', '1', 1825);			// 5 years
		}
		
		url="/cgi-bin/ajaxbulletin?sid="+sid+"&bulletin=bbs&email="+szEmail+"&list="+szList;
		
		//alert(url); 
							
		//Do the Ajax call
		if (window.XMLHttpRequest) { // Non-IE browsers
		  onLoadReq = new XMLHttpRequest();
		  onLoadReq.onreadystatechange = processStateChange;
		  try {
			onLoadReq.open("GET", url, true); //was get
		  } catch (e) {
			alert("Problem Communicating with Server\n"+e);
		  }
		  onLoadReq.send(null);
		} 
		else if (window.ActiveXObject) { // IE					  
		  onLoadReq = new ActiveXObject("Microsoft.XMLHTTP");
		  if (onLoadReq) {
			//alert("update onLoadReq"); 
			onLoadReq.onreadystatechange = processStateChange;
			onLoadReq.open("GET", url, true);
			onLoadReq.send();
		  }
		}
	}
}

function OptInBW(form, elementId, cookieName, sid) {			
	var submitEmail = false;
	var szEmail = ""; 
	var szAccount = '0'; 
	
	if (form.Semail != null && form.Semail.value.length > 0 && checkEmail(form.Semail.value)) {
		submitEmail = true;
		szEmail = form.Semail.value;
		//alert("Email address" + szEmail)
	} else {
	  submitAd = false;
	  alert("Valid email address required")
	  form.Semail.focus();
	}
	if (form.Saccount != null && form.Saccount.value.length > 0) {
		szAccount = form.Saccount.value;
		//alert("Account " + szAccount)
	} 
	
	if (submitEmail == true) {				
		window.alert("Thank you for signing up for the BizBuySell BrokerWorks Bulletin."); 
		CloseBulletinPopup(elementId, cookieName);
		if ( cookieName != null ) {
			createCookie('BwBulletinOpted', '1', 1825); // 5 years
		}
		
		url="/cgi-bin/ajaxbulletin?sid="+sid+"&bulletin=bw&email="+szEmail+"&acct="+szAccount;
		
		//alert(url); 
							
		//Do the Ajax call
		if (window.XMLHttpRequest) { // Non-IE browsers
		  onLoadReq = new XMLHttpRequest();
		  onLoadReq.onreadystatechange = processStateChange;
		  try {
			onLoadReq.open("GET", url, true); //was get
		  } catch (e) {
			alert("Problem Communicating with Server\n"+e);
		  }
		  onLoadReq.send(null);
		} 
		else if (window.ActiveXObject) { // IE					  
		  onLoadReq = new ActiveXObject("Microsoft.XMLHTTP");
		  if (onLoadReq) {
			//alert("update onLoadReq"); 
			onLoadReq.onreadystatechange = processStateChange;
			onLoadReq.open("GET", url, true);
			onLoadReq.send();
		  }
		}
	}
}

function OptInFD(form, elementId, cookieName, sid) {			
	var submitEmail = false;
	var szEmail = ""; 
	
	if (form.Semail != null && form.Semail.value.length > 0 && checkEmail(form.Semail.value)) {
		submitEmail = true;
		szEmail = form.Semail.value;
		//alert("Email address" + szEmail)
	} else {
	  submitAd = false;
	  alert("Valid email address required")
	  form.Semail.focus();
	}
	
	if (submitEmail == true) {				
		window.alert("Thank you for signing up for the BizBuySell Franchise Bulletin."); 
		CloseBulletinPopup(elementId, cookieName);
		if ( cookieName != null ) {
			createCookie('FdBulletinOpted', '1', 1825);			// 5 years
		}
		
		url="/cgi-bin/ajaxbulletin?sid="+sid+"&bulletin=fd&email="+szEmail;
		
		//alert(url); 
							
		//Do the Ajax call
		if (window.XMLHttpRequest) { // Non-IE browsers
		  onLoadReq = new XMLHttpRequest();
		  onLoadReq.onreadystatechange = processStateChange;
		  try {
			onLoadReq.open("GET", url, true); //was get
		  } catch (e) {
			alert("Problem Communicating with Server\n"+e);
		  }
		  onLoadReq.send(null);
		} 
		else if (window.ActiveXObject) { // IE					  
		  onLoadReq = new ActiveXObject("Microsoft.XMLHTTP");
		  if (onLoadReq) {
			//alert("update onLoadReq"); 
			onLoadReq.onreadystatechange = processStateChange;
			onLoadReq.open("GET", url, true);
			onLoadReq.send();
		  }
		}
	}
}