var regtext='';
var regtext2='';

function showJoin() {
	var iframes = document.getElementsByTagName('iframe');
	var objects = document.getElementsByTagName('object');

	iframes[0].style.visibility = 'hidden';
	if(iframes[1]) { iframes[1].style.visibility = 'hidden'; }
	if(iframes[2]) { iframes[2].style.visibility = 'hidden'; }
	if(iframes[3]) { iframes[3].style.visibility = 'hidden'; }

	if(objects[0]) { objects[0].style.visibility = 'hidden'; }
	if(objects[1]) { objects[1].style.visibility = 'hidden'; }
	if(objects[2]) { objects[2].style.visibility = 'hidden'; }


	if (parseInt(navigator.appVersion)>3) {
	 if (navigator.appName=="Netscape") {
	  winW = window.innerWidth;
	  winH = window.innerHeight;
	 }
	 if (navigator.appName.indexOf("Microsoft")!=-1) {
	  winW = document.body.offsetWidth;
	  winH = document.body.offsetHeight;
	 }	
	}

	var joinDivBG = document.getElementById('joinDivBG');
	joinDivBG.style.height  = winH+"px";
	joinDivBG.style.width   = winW+"px";
	joinDivBG.style.display = "block";

	var leftPos = (winW/2)-300;
	var topPos  = document.body.scrollTop+50;

	var joinDiv = document.getElementById('joinDiv');
	joinDiv.style.left    = leftPos+"px";
	joinDiv.style.top     = topPos+"px";
	joinDiv.style.display = "block";
	grabRegisterData('/index.php/?option=com_comprofiler&task=registers','option=com_comprofiler&task=registers');

}
function hideJoin() {
	var iframes = document.getElementsByTagName('iframe');
	var objects = document.getElementsByTagName('object');

	iframes[0].style.visibility = 'visible';
	if(iframes[1]) { iframes[1].style.visibility = 'visible'; }
	if(iframes[2]) { iframes[2].style.visibility = 'visible'; }
	if(iframes[3]) { iframes[3].style.visibility = 'visible'; }

	if(objects[0]) { objects[0].style.visibility = 'visible'; }
	if(objects[1]) { objects[1].style.visibility = 'visible'; }
	if(objects[2]) { objects[2].style.visibility = 'visible'; }


	var joinDiv = document.getElementById('joinDiv');
	joinDiv.style.display = "none";

	var joinDivBG = document.getElementById('joinDivBG');
	joinDivBG.style.display = "none";
}

function ajaxUpdate() {
	data = window.regtext;
	var cbsecurityg1 = getCBSecurity(data);
	var id = getID(data);
	var cbnewsletter       = document.getElementById('cbnewsletter').value;
	var cbtos              = document.getElementById('cbtos').value;
	var cbexpecting        = document.getElementById('cbexpecting').value;
	var cbduedate          = document.getElementById('cbduedate').value;
	var cbbirthday         = document.getElementById('cbbirthday').value;
	var cbkidsnewborns     = document.getElementById('cbkidsnewborns').value;
	var cbkidsonethree     = document.getElementById('cbkidsonethree').value;
	var cbkidsfourseven    = document.getElementById('cbkidsfourseven').value;
	var cbkidseightfifteen = document.getElementById('cbkidseightfifteen').value;
	var cbkidsfifteenmore  = document.getElementById('cbkidsfifteenmore').value;
	
	var qs = "cb_newsletter="+encodeURI(cbnewsletter)+"&cb_tos="+encodeURI(cbtos)+"&cb_expecting="+encodeURI(cbexpecting)+"&cb_duedate="+encodeURI(cbduedate)+"&cb_birthday="+encodeURI(cbbirthday)+"&cb_kidsnewborns="+encodeURI(cbkidsnewborns)+"&cb_kidsonethree="+encodeURI(cbkidsonethree)+"&cb_kidsfourseven="+encodeURI(cbkidsfourseven)+"&cb_kidseightfifteen="+encodeURI(cbkidseightfifteen)+"&cb_kidsfifteenmore="+encodeURI(cbkidsfifteenmore)+"&id="+encodeURI(id)+"&task=saveUserEdit&cbsecurityg1="+encodeURI(cbsecurityg1);
	
	var post = "http://www.realsavvymoms.com/index.php?option=com_comprofiler&Itemid=41";
	
	submitRegisterData(post,qs);
}

function getID(data) {
	return getSingleMatch('id" value="','"',data);
}

function ajaxJoin() {
	var popup = document.getElementById('joinDiv');
	var old = popup.innerHTML;
	var username     = document.getElementById('usernamePopup').value;
	var name         = document.getElementById('namePopup').value;
	var email        = document.getElementById('emailPopup').value;
	var password     = document.getElementById('passwordPopup').value;
	var passwordconf = document.getElementById('confirmpasswordPopup').value;
	popup.innerHTML = "<center><br><br><br><img src='/rsm/images/registerLoading.gif'></center>";
	data = window.regtext;
	var cbsecurityg1 = getCBSecurity(data);
	var cbrasitway   = getCBRasitway(data);

	if(password!=passwordconf) {
		alert('Your Password and Password Confirmation Do Not Match');
		popup.innerHTML = old;				
	} else if (name=='name') {
		alert('Please Use Your Real Name');
		popup.innerHTML = old;
	} else if (email=='email') {
		alert('Please Use A Real Email Address');
		popup.innerHTML = old;
	} else if (username=='username') {
		alert('Please Use A Real Username');
		popup.innerHTML = old;
	} else if (password=='password') {
		alert('Please Use A More Unique Password');
		popup.innerHTML = old;
	} else {
		var qs = "name="+encodeURI(name)+"&username="+encodeURI(username)+"&email="+encodeURI(email)+"&password="+encodeURI(password)+"&verifyPass="+encodeURI(password)+"&id=0&gid=0&emailpass=0&option=com_comprofiler&task=saveregisters&cbsecurityg1="+cbsecurityg1+"&cbrasitway="+cbrasitway;
		submitRegisterData("/index.php/?option=com_comprofiler",qs);

	}

}

function returnResult(res) { 

	var popup = document.getElementById('joinDiv');
	var arr = res.split('password already in use');
	if(arr.length>1) { 
		alert('The username you chose is already in use. Please choose another username and try again');
		location.reload(true);

	} else {

		// CHECK TO SEE IF EMAIL IS ALREADY BEING USED
		var arr2 = res.split('is already registered');

		if(arr2.length>1) { 
			alert('The email address you signed up with is already registered');
			location.reload(true);

		} else {

			// CHECK TO SEE IF YOU ARE ALREADY A FULLY REGISTERED USER
			var arr3 = res.split('already registered with this');
			if(arr3.length>1) {
				popup.innerHTML = "<center><br><br><br><h3>You have already registered with this Username and Password.</h3> Go ahead and log in!<br /><a style='cursor:pointer;cursor:hand;text-decoration:underline;' onClick='hideJoin()'>close</a></center>";
		
			} else {
				popup.innerHTML = "<center><br><br><br><h3>Congratulations. Your Registration was Successful! </h3>An email has been dispatched to the email address with which you registered. It includes instructions to complete your free membership! <br /><a style='cursor:pointer;cursor:hand;text-decoration:underline;' onClick='hideJoin()'>close</a></center>";
			}
		}	


	}
}

function getCBSecurity(data) {
	var arr1 = data.split('cbsecurityg1" value="');
	var str = arr1[1];
	var arr2 = str.split('"');
	return arr2[0];
}

function getCBRasitway(data) {
	var arr1 = data.split('cbrasitway" value="');
	var str = arr1[1];
	var arr2 = str.split('"');
	return arr2[0];
}

function getSingleMatch(start,end,content) {
	var arr1 = content.split(start);
	var arr2 = arr1[1].split(end);
	return arr2[0];
}


function grabRegisterData(strURL,qs) {

    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
	if(self.xmlHttpReq.readyState == 4) {
		window.regtext = self.xmlHttpReq.responseText;
	}
    }
    self.xmlHttpReq.send(qs);
}

function submitRegisterData(strURL,qs) {

    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
	if(self.xmlHttpReq.readyState == 4) {
		window.regtext2 = self.xmlHttpReq.responseText;
		returnResult(self.xmlHttpReq.responseText);
	}
    }
    self.xmlHttpReq.send(qs);
}



function register()
{ 
    document.getElementById('joinForm').innerHTML = "<center><img style='margin-top:120px;margin-left:-50px;' src='http://www.realsavvymoms.com/rsm/images/ajax-loader.gif'>";
    document.getElementById('joinForm').style.background = "url(http://www.realsavvymoms.com/rsm/images/join-bg-loader.jpg)";	
    var name            = document.getElementById('name').value;	
    var email           = document.getElementById('email').value;
    var username        = document.getElementById('username').value;
    var password        = document.getElementById('password').value;
    var confirmpassword = document.getElementById('confirmpassword').value;
    var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
  
    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200) 
                  document.getElementById('joinForm').innerHTML = "<center><br><br><b><span style='font-size:14px;font-family:verdana;'>Thank you for joining the Real Savvy Moms family. Our Absolutely Free website is yours to enjoy.<br ><br> You may not have known this, but we can actually customize the site to your particular needs. Just fill out the additional form below to get the specific motherhood advice you need!</span></center>"; 
              else 
                 document.ajax.dyn="Error code " + xhr.status;
         }
    }; 

   xhr.open(POST, "/index.php/",  true); 
   xhr.send("id=0&gid=0&useractivation=1&option=com_registration&task=saveRegistration&ajaxBypass=1&name="+encodeURI(name)+"email="+encodeURI(email)+"&username="+encodeURI(username)+"&password="+encodeURI(password)+"&password2="+encodeURI(password)); 
}
function register2()
{ 
    var name2            = document.getElementById('name2').value;    	
    var email2           = document.getElementById('email2').value;
    var username2        = document.getElementById('username2').value;
    var password2        = document.getElementById('password2').value;
    var confirmpassword2 = document.getElementById('confirmpassword2').value;
    var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
  
    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200) 
                  document.ajax.dyn="Received:"  + xhr.responseText; 
              else 
                 document.ajax.dyn="Error code " + xhr.status;
         }
    }; 

	

   xhr.open(POST, "/index.php/",  true); 
   xhr.send("id=0&gid=0&useractivation=1&option=com_registration&task=saveRegistration&ajaxBypass=1&name="+encodeURI(name2)+"email="+encodeURI(email2)+"&username="+encodeURI(username2)+"&password="+encodeURI(password2)+"&password2="+encodeURI(password2)); 
} 
