/**
 Populate the Error Message from the XML response if the Status is 0
 Else redirect the user to Thank you Page
 Note: This function is specific for Non IE Browsers like Mozialla Opera.
 */
function nonMSPopulate(req) {

    xmlDoc = document.implementation.createDocument("", "", null);

    var resp = req.responseText;

	if(resp.indexOf("An error occurred while accessing") > 0)
	{
		displayServerError(getErrorMessage('400',''),true);
	}
	else
	{
		var parser = new DOMParser();
		var dom = parser.parseFromString(resp, "text/xml");
		parseXML(dom);
	}
}
/**
 Populate the Error Message from the XML response if the Status is 0
 Else redirect the user to Thank you Page
 Note: This function is specific for IE Browsers.
 */

function msPopulate(req) {
  
    var resp = req.responseText;
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

	if(resp.indexOf("An error occurred while accessing") > 0)
	{
		displayServerError(getErrorMessage('400',''),true);
	}
	else
	{
		xmlDoc.async = "false";
		xmlDoc.loadXML(resp);
		parseXML(xmlDoc);
	}
}

function parseXML(dom)
{
	//alert("Inside parseXML"+document.getElementById('email').value);
    var code ;
    var codeValue ;
    var message ;
    var messageValue ;
	var AEPADownFlg = "false";

	var statusValue = dom.getElementsByTagName("status")[0].firstChild.nodeValue;		

	if (statusValue == "0") { // If status in xml is bad
		// Iterate over all the responses and display the error Messages from the server
		var response = dom.getElementsByTagName('response');
		var errorDiv = document.getElementById("errorDiv");
		var innerHtml = "";

		for (var i = 0; i < response.length; i++) {
			code = response[i].getElementsByTagName("code");
			message = response[i].getElementsByTagName("message");
			codeValue = code[0].firstChild.nodeValue;
			messageValue = message[0].firstChild.nodeValue;
			if(codeValue == "400" || codeValue == "999")
			{
				AEPADownFlg = "true";
				break;
			}

			innerHtml = innerHtml + "<li>"+getErrorMessage(codeValue,messageValue) + "</li>";
		}
		if(AEPADownFlg == "true")
		{
			displayServerError(getErrorMessage('400',''),true);
		}
		else
		{
			displayServerError(innerHtml,false);
		}
	} 
	else
	{
		// Status is good So Redirect the user to thankyou page
		// Get the link from the hidden Page
		var thankyoulink =  document.getElementById("thankyoulink").value;

		if(thankyoulink =='')
		{
			document.thankyou.action="http://www.sirius.com/";
			document.thankyou.submit();
		}
		else
		{			
			//alert("calling thank you page:"+document.getElementById('email').value);

			//alert("link:"+thankyoulink);
			document.thankyou.action=thankyoulink;			
			//document.thankyou.action="http://webstage.sirius.corp/freetrial/thankyou?email=testtestemail";
			
			document.thankyou.EMAIL_ADDRESS.value=document.getElementById('email').value;
			document.thankyou.FNAME.value=document.getElementById('firstName').value;
			document.thankyou.LNAME.value=document.getElementById('lastName').value;
			//document.thankyou.email.value="testtestemail";
			document.thankyou.submit();
			//document.thankyou.action=thankyoulink+'&EMAIL_ADDRESS='+document.getElementById('email').value+'&FNAME='+document.getElementById('firstName').value+'&LNAME='+document.getElementById('lastName').value;
			//document.thankyou.submit();
		}
	}
}

/**
 This is a CallBack function
 which call the appropriate populate function to process the response.
*/

function processResponse(req) {
	if (req.readyState == 4) {
		if (req.status == 200) {
			// V.Imp Note:This Condition is checked first because it causes error in IE
			if (window.ActiveXObject) {
				msPopulate(req);
			}
			else if (window.XMLHttpRequest) {
				nonMSPopulate(req);
            		}
        	}
		else
		{
			displayServerError(getErrorMessage('400',''),true);
		}
	}
}



function getErrorMessage(cd,msg){
	switch(cd){
		case "105": 	return 'We are unable to register the email address you have provided. We apologize for the inconvenience.';
				break;
		case "108":	return 'Campaign is invalid or expired';
				break;
		case "200":	var temp = "Sorry, your account already exists. ";
				temp += "<a href='http://www.sirius.com/sirius/servlet/MediaPlayer?activity=forgotPassword' ";
				temp += "onclick= \"window.open('http://www.sirius.com/sirius/servlet/MediaPlayer?activity=forgotPassword',";
				temp += "'FullMediaPlayer','menubar=no,locationbar=no,status=no,resizable=yes,height=575,width=650,screenX=10,screenY=10,left=10,top=10'); ";
				temp += "return false;\"";
				temp += ">Have you forgotten your password?</a>";
				return temp;
		case "400": 
		case "999":	return "Our system is currently unavailable, please try again later.";
		default	  :	return msg;
	}
}

function displayServerError(msg,addLi)
{
	var errorDiv = document.getElementById("errorDiv");
	var innerHtml = "";
	if(addLi)
		innerHtml = innerHtml + "<li>"+msg + "</li>";
	else
		innerHtml = innerHtml + msg;

	innerHtml = "<ul>" + innerHtml + "</ul>" ;       
	errorDiv.style.display = 'block';
	errorDiv.innerHTML = innerHtml;
}
