/*
Following section of varibales will be prepopulated at Portal Rendition Layer
  to be accessed in the static pages.
*/
var xmlDoc;
var AudValue = "";
function parseXML(httpresponse){
	if (window.ActiveXObject){
	  var doc=new ActiveXObject("Microsoft.XMLDOM");
	  doc.async="false";
	  doc.loadXML(httpresponse);
	}

	else{
	  var parser=new DOMParser();
	  var doc=parser.parseFromString(httpresponse,"text/xml");
	}
	var x=doc.documentElement;
	
	

	//if(xmlHttp.getResponseHeader('CONTENT-TYPE') == "text/xml"){
    if(xmlHttp.getResponseHeader('CONTENT-TYPE').indexOf("text/xml")!=-1){

		if(x == null){
			document.getElementById("statusBlock").innerHTML="Failure Reason : "+"System currently unavailable. Sorry for the inconvenience caused. Try again later.";
			return false;
		}
	}
	
	//if(xmlHttp.getResponseHeader('CONTENT-TYPE') == "text/xml"){
	if(xmlHttp.getResponseHeader('CONTENT-TYPE').indexOf("text/xml")!=-1){

		//statusMess = document.getElementById("statusBlock").innerHTML=x.childNodes[0].nodeValue;
		statusMess = x.childNodes[0].nodeValue;
		if(statusMess == "Step1-Success"){
			document.location.href = "/SelfServiceWeb/portlets/buyOnline/buyOnlineStep2.do?Step=Step1&Aud="+AudValue;
		}
		if(statusMess == "Step2-Success"){
			document.location.href = "/SelfServiceWeb/portlets/buyOnline/buyOnlineStep3.do?Step=Step2&Aud="+AudValue;
		}
		if(statusMess == "Step3-Success"){
			document.location.href = "/SelfServiceWeb/portlets/buyOnline/buyOnlineStep4.do?Step=Step3&Aud="+AudValue;
		}
		if(statusMess == "Step4-Success"){
			document.location.href = "/SelfServiceWeb/portlets/buyOnline/buyOnlineStep5.do?Step=Step4&Aud="+AudValue;
		}
		if(statusMess == "Home-Success"){
			document.location.href = "/SelfServiceWeb/portlets/buyOnline/Home.do?Step=Step1&Aud="+AudValue;
		}
		if(statusMess == "Edit-Success"){
			document.location.href = "/SelfServiceWeb/portlets/buyOnline/Edit.do?Step=Step1&Aud="+AudValue;
		}
		if(statusMess == "Remove-Success"){
			document.location.href = "/SelfServiceWeb/portlets/buyOnline/Remove.do?Step=Step1&Aud="+AudValue;
		}
		if(statusMess == "EditOrder-Success"){
			document.location.href = "/SelfServiceWeb/portlets/buyOnline/editOrder.do?Step=Step1&Aud="+AudValue;
		}if(statusMess == "System-Failure"){
			document.location.href = "/SelfServiceWeb/portlets/buyOnline/systemFailure.do?Step=Step1&Aud="+AudValue;
		}
		if(statusMess == "Session-Invalid"){
			document.location.href = "/SelfServiceWeb/portlets/buyOnline/sessionInvalid.do?Step=Step1&Aud="+AudValue;
		}
	}
	else{
		document.getElementById("statusBlock").innerHTML=httpresponse;
	}
}

function GetXmlHttpObject()
{ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		
		var responsetext = xmlHttp.responseText;
		parseXML(xmlHttp.responseText);
		
		var originalResponse = xmlHttp.responseText;
		var trimmedResponse = originalResponse.replace(/^\s*|\s*$/g,'');
		if (trimmedResponse.length == 0)
		{
			document.getElementById("statusBlock").style.height="0";
			document.getElementById("statusBlock").style.visibility = "hidden";		
		}else
		{
			document.getElementById("statusBlock").style.visibility = "visible";
		}
	} 
} 

function submitForm(step){
	if(step == "mcRemove"){
		var countryCode = document.getElementById("CountryCode").value;
		var selectedProdId=document.getElementById("mcproductId").value;
		AudValue=document.getElementById("Aud").value;
		var theSendURL = "/SelfServiceWeb/portlets/buyOnline/BuyOnlineController.do?";
		theSendURL = theSendURL + "Step=" + "Remove" +"&";
		theSendURL = theSendURL + "Country=" + countryCode+"&";
		theSendURL = theSendURL + "ProdID=" +selectedProdId+"&";
		
		/*alert(theSendURL);*/
		document.getElementById("statusBlock").innerHTML="<img src='/images/processingnew.gif'> </img>";
		callAjaxControllerToRegister( theSendURL );
	}

	if(step == "scRemove"){
		var countryCode = document.getElementById("CountryCode").value;
		var selectedProdId=document.getElementById("scproductId").value;
		AudValue=document.getElementById("Aud").value;
		var theSendURL = "/SelfServiceWeb/portlets/buyOnline/BuyOnlineController.do?";
		theSendURL = theSendURL + "Country=" + countryCode+"&";
		theSendURL = theSendURL + "Step=" + "Remove" +"&";
		theSendURL = theSendURL + "ProdID=" +selectedProdId+"&";
		//alert(theSendURL);
		document.getElementById("statusBlock").innerHTML="<img src='/images/processingnew.gif'> </img>";
		callAjaxControllerToRegister( theSendURL );
	}

	if(step == "scRemove1"){
		var countryCode = document.getElementById("CountryCode").value;
		var selectedProdId=document.getElementById("scproductId1").value;
		AudValue=document.getElementById("Aud").value;
		var theSendURL = "/SelfServiceWeb/portlets/buyOnline/BuyOnlineController.do?";
		theSendURL = theSendURL + "Country=" + countryCode+"&";
		theSendURL = theSendURL + "Step=" + "Remove" +"&";
		theSendURL = theSendURL + "ProdID=" +selectedProdId+"&";
		//alert(theSendURL);
		document.getElementById("statusBlock").innerHTML="<img src='/images/processingnew.gif'> </img>";
		callAjaxControllerToRegister( theSendURL );
	}
	
	if(step == "Home"){
		var countryCode = document.getElementById("CountryCode").value;
		AudValue=document.getElementById("Aud").value;
		var theSendURL = "/SelfServiceWeb/portlets/buyOnline/BuyOnlineController.do?";
		theSendURL = theSendURL + "Country=" + countryCode+"&";
		theSendURL = theSendURL + "Step=" + "Home" +"&";
		document.getElementById("statusBlock").innerHTML="<img src='/images/processingnew.gif'> </img>";
		callAjaxControllerToRegister( theSendURL );
	}
	
	if(step == "mcEdit"){
		var countryCode = document.getElementById("CountryCode").value;
		var selectedProdId=document.getElementById("mcproductId").value;
		AudValue=document.getElementById("Aud").value;
		var theSendURL = "/SelfServiceWeb/portlets/buyOnline/BuyOnlineController.do?";
		theSendURL = theSendURL + "Step=" + "Edit" +"&";
		theSendURL = theSendURL + "Country=" + countryCode+"&";
		theSendURL = theSendURL + "ProdID=" +selectedProdId+"&";		
		//alert(theSendURL);
		document.getElementById("statusBlock").innerHTML="<img src='/images/processingnew.gif'> </img>";
		callAjaxControllerToRegister( theSendURL );
	}

	if(step == "scEdit"){
		var countryCode = document.getElementById("CountryCode").value;
		var selectedProdId=document.getElementById("scproductId").value;
		AudValue=document.getElementById("Aud").value;
		var theSendURL = "/SelfServiceWeb/portlets/buyOnline/BuyOnlineController.do?";
		theSendURL = theSendURL + "Step=" + "Edit" +"&";
		theSendURL = theSendURL + "Country=" + countryCode+"&";
		theSendURL = theSendURL + "ProdID=" +selectedProdId+"&";
		//alert(theSendURL);
		document.getElementById("statusBlock").innerHTML="<img src='/images/processingnew.gif'> </img>";
		callAjaxControllerToRegister( theSendURL );
	}

	if(step == "scEdit1"){
		var countryCode = document.getElementById("CountryCode").value;
		var selectedProdId=document.getElementById("scproductId1").value;
		AudValue=document.getElementById("Aud").value;
		var theSendURL = "/SelfServiceWeb/portlets/buyOnline/BuyOnlineController.do?";
		theSendURL = theSendURL + "Step=" + "Edit" +"&";
		theSendURL = theSendURL + "Country=" + countryCode+"&";
		theSendURL = theSendURL + "ProdID=" +selectedProdId+"&";	
		//alert(theSendURL);
		document.getElementById("statusBlock").innerHTML="<img src='/images/processingnew.gif'> </img>";
		callAjaxControllerToRegister( theSendURL );
	}
	
	if(step == "Step1"){
		var prodName = document.getElementById("productname").value;
		AudValue=document.getElementById("Aud").value;
		var currency = document.getElementById("Currency_Code").value;
		var productNameDesc = "";
		var countryCode = document.getElementById("CountryCode").value;
		var themeId = document.getElementById("ThemeID").value;
		var oppIdVal = "";
		var scProducts = document.getElementById("scProducts").value;
		var productIdVal="";
		var priceVal="";
		var monthTermVal="";
		var offerURLVal="";
		var buyIDVal="";
		var termVal = "";
		var audioVal = "";
		//For Meeting center Product,find no of hosts,ProductId
		var noOfHosts = "";
		oppIdVal=document.getElementById("OppID").value;
		buyIDVal=document.getElementById("BuyID").value;
		termVal= document.getElementById("term").value;
		audioVal= document.getElementById("audio").value;
		if( prodName == "MeetingCenter"){
			noOfHosts = document.getElementById("hostNum").value;
			//get ProductId
			productIdVal = document.getElementById("mcproductId").value;
			productNameDesc=document.getElementById("mcproductName").value;
			priceVal=document.getElementById("mcPrice").value;
			monthTermVal=document.getElementById("mcmonthTerm").value;
			offerURLVal=document.getElementById("mcofferUrl").value;
		}

		//For Support center Product,find ProductId
		var tempProd1="";
		var tempProd2="";

		var tempPrice1="";
		var tempPrice2="";

		var tempmonthTerm1="";
		var tempmonthTerm2="";

		var tempofferUrl1="";
		var tempofferUrl2="";
				
		if( prodName == "SupportCenter"){
			for (var i=0; i < document.SC.productId.length; i++)
		   {
			   if (document.SC.productId[i].checked)
				  {
				    tempProd1 = document.SC.productId[i].value;
					var tempproductNameDesc1=document.getElementById("scproductName1").value;
					var tempSCPriceIdVal1= 'scPrice'+i;
					var tempMonthTerm1IdVal1= 'scmonthTerm'+i;
					var tempOfferUrl1IdVal1= 'scofferURL'+i;
					tempPrice1 = document.getElementById(tempSCPriceIdVal1).value;
					tempmonthTerm1 =document.getElementById(tempMonthTerm1IdVal1).value;
					tempofferUrl1 = document.getElementById(tempOfferUrl1IdVal1).value;
					//alert(tempofferUrl1);
				  }
		   }
		if(scProducts == "true"){
			for (var k=1; k < document.SC.productId1.length; k++)
				{
				   if (document.SC.productId1[k].checked)
					  {
						tempProd2 = document.SC.productId1[k].value;
						var tempproductNameDesc2=document.getElementById("scproductName2").value;
						var tempSCPriceIdVal2= 'scPrice1'+k;
						var tempMonthTerm1IdVal2= 'scmonthTerm1'+k;
						var tempOfferUrl1IdVal2= 'scofferURL1'+k;
						tempPrice2 = document.getElementById(tempSCPriceIdVal2).value;
						tempmonthTerm2 = document.getElementById(tempMonthTerm1IdVal2).value;
						
						tempofferUrl2 = document.getElementById(tempOfferUrl1IdVal2).value;
					  }
				}
		}
			if(tempProd1 == ""){
				if(tempProd2 == ""){
					productIdVal ="";
					priceVal="";
					monthTermVal="";
					offerURLVal="";
					productNameDesc="";
				}else{
					productIdVal = tempProd2;
					priceVal=tempPrice2;
					monthTermVal=tempmonthTerm2;
					offerURLVal=tempofferUrl2;
					productNameDesc=tempproductNameDesc2;
				}
			}else{
				productIdVal = tempProd1;
				priceVal=tempPrice1;
				monthTermVal=tempmonthTerm1;
				offerURLVal=tempofferUrl1;
				productNameDesc=tempproductNameDesc1;
			}
			
		}
		var theSendURL = "/SelfServiceWeb/portlets/buyOnline/BuyOnlineController.do?";
		theSendURL = theSendURL + "Country=" + countryCode+"&";
		theSendURL = theSendURL + "THEMEID=" + themeId+"&";
		theSendURL = theSendURL + "Step=" + "Step1" +"&";
		theSendURL = theSendURL + "ProdID=" + productIdVal+"&";
		theSendURL = theSendURL + "HostsNumber=" + noOfHosts+"&";
		theSendURL = theSendURL + "ProductName=" + productNameDesc+"&";
		theSendURL = theSendURL + "Price=" + priceVal+"&";
		theSendURL = theSendURL + "Term=" + monthTermVal+"&";
		theSendURL = theSendURL + "OfferURL=" + offerURLVal+"&";
		theSendURL = theSendURL + "buyID=" + buyIDVal+"&";
		theSendURL = theSendURL + "term=" + termVal+"&";
		theSendURL = theSendURL + "audio=" + audioVal+"&";
		theSendURL = theSendURL + "oppID=" + oppIdVal+"&";
		theSendURL = theSendURL + "currency=" +currency+"&";
		
		//alert(theSendURL);
		document.getElementById("statusBlock").innerHTML="<img src='/images/processingnew.gif'> </img>";
		callAjaxControllerToRegister( theSendURL );
	}
	if(step == "Step2"){
		var countryCode = document.getElementById("CountryCode").value;
		AudValue=document.getElementById("Aud").value;
		var theSendURL = "/SelfServiceWeb/portlets/buyOnline/BuyOnlineController.do?";
		theSendURL = theSendURL + "Step=" + "Step2" +"&";
		theSendURL = theSendURL + "Country=" + countryCode+"&";
		//alert(theSendURL);
		document.getElementById("statusBlock").innerHTML="<img src='/images/processingnew.gif'> </img>";
		callAjaxControllerToRegister( theSendURL );
	}
	if(step == "Step3"){
		var countryCode = document.getElementById("CountryCode").value;
		AudValue=document.getElementById("Aud").value;
		var noOfEmptext = "";
		var noOfEmp = "";
		for (var i=0; i < document._mktf.numberemployee.length; i++)
			   {
			   if (document._mktf.numberemployee[i].checked)
				  {
					noOfEmp = document._mktf.numberemployee[i].value;
					var tempNoOfEmptext = 'numberemployeeText'+i;
					noOfEmptext = document.getElementById(tempNoOfEmptext).value;
				  }
			   }
		var isAgree = "";
		if(document.getElementById("agree").checked){
			isAgree = "yes";
		}else{
			isAgree = "no";
		}


		var countryText = "";
		var cardtext = "";
		countryText = document.getElementById("country").options[document.getElementById("country").selectedIndex].text;
		cardtext = document.getElementById("cardtype").options[document.getElementById("cardtype").selectedIndex].text;
		var billingCountryText = "";
		billingCountryText = document.getElementById("billingcountry").options[document.getElementById("billingcountry").selectedIndex].text;
		var companyRevenueText = "";
		companyRevenueText = document.getElementById("companyrevenue").options[document.getElementById("companyrevenue").selectedIndex].text;
		var defaultlanguageText = "";
		defaultlanguageText = document.getElementById("defaultlanguage").options[document.getElementById("defaultlanguage").selectedIndex].text;

		var theSendURL = "/SelfServiceWeb/portlets/buyOnline/BuyOnlineController.do?";
		theSendURL = theSendURL + "Step=" + "Step3" +"&";
		theSendURL = theSendURL + "Country=" + countryCode+"&";
		theSendURL = theSendURL + "firstname=" + document.getElementById("firstname").value+"&";
		theSendURL = theSendURL + "lastname=" + document.getElementById("lastname").value+"&";
		theSendURL = theSendURL + "email=" + document.getElementById("email").value+"&";
		theSendURL = theSendURL + "company=" + document.getElementById("company").value+"&";
		theSendURL = theSendURL + "address1=" + document.getElementById("address1").value+"&";
		theSendURL = theSendURL + "address2=" + document.getElementById("address2").value+"&";
		theSendURL = theSendURL + "city=" + document.getElementById("city").value+"&";
		theSendURL = theSendURL + "state=" + document.getElementById("state").value+"&";
		theSendURL = theSendURL + "zipcode=" + document.getElementById("zipcode").value+"&";
		theSendURL = theSendURL + "country=" + document.getElementById("country").value+"&";
		theSendURL = theSendURL + "countryText=" + countryText+"&";
		theSendURL = theSendURL + "phone=" + document.getElementById("phone").value+"&";
		theSendURL = theSendURL + "cardtype=" + cardtext+"&";
		theSendURL = theSendURL + "cardtext=" + document.getElementById("cardtype").value +"&";
		theSendURL = theSendURL + "cardname=" + document.getElementById("cardname").value+"&";
		theSendURL = theSendURL + "cardno=" + document.getElementById("cardno").value+"&";
		theSendURL = theSendURL + "cvvcode=" + document.getElementById("cvvcode").value+"&";
		theSendURL = theSendURL + "expddmmyy=" + document.getElementById("expddmmyy").value+"&";
		theSendURL = theSendURL + "adddressline1=" + document.getElementById("adddressline1").value+"&";
		theSendURL = theSendURL + "adddressline2=" + document.getElementById("adddressline2").value+"&";
		theSendURL = theSendURL + "billingcity=" + document.getElementById("billingcity").value+"&";
		theSendURL = theSendURL + "billingstate=" + document.getElementById("billingstate").value+"&";
		theSendURL = theSendURL + "billingzip=" + document.getElementById("billingzip").value+"&";
		theSendURL = theSendURL + "billingcountry=" + document.getElementById("billingcountry").value+"&";
		theSendURL = theSendURL + "billingCountryText=" + billingCountryText+"&";
		theSendURL = theSendURL + "billingphone=" + document.getElementById("billingphone").value+"&";
		theSendURL = theSendURL + "firstchoice=" + document.getElementById("firstchoice").value+"&";
		theSendURL = theSendURL + "secondchoice=" + document.getElementById("secondchoice").value+"&";
		theSendURL = theSendURL + "username=" + document.getElementById("username").value+"&";
		theSendURL = theSendURL + "password=" + document.getElementById("password").value+"&";
		theSendURL = theSendURL + "defaultlanguage=" + document.getElementById("defaultlanguage").value+"&"; 
		theSendURL = theSendURL + "companyrevenue=" + document.getElementById("companyrevenue").value+"&";
		theSendURL = theSendURL + "companyrevenueText=" + companyRevenueText+"&";
		theSendURL = theSendURL + "defaultlanguageText=" + defaultlanguageText+"&";
		theSendURL = theSendURL + "numberemployee=" + noOfEmp+"&";
		theSendURL = theSendURL + "NoOfEmptext=" + noOfEmptext+"&";
		theSendURL = theSendURL + "agree=" + isAgree+"&";
		//alert(theSendURL);
		//Handling '#' character
		var out = "#"; // replace this
		var add = "%23"; // with this
		var temp = "" + theSendURL; // temporary holder
		var pos;
		while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add +temp.substring((pos + out.length), temp.length));
		}
		var finalUrl = temp;
		//alert(temp);
		document.getElementById("statusBlock").innerHTML="<img src='/images/processingnew.gif'> </img>";
		callAjaxControllerToRegister( finalUrl );
	}
	if(step == "Step4"){
		var countryCode = document.getElementById("CountryCode").value;
		AudValue=document.getElementById("Aud").value;

		//Set default Track id's if value is empty
		var trackIDVal = document._mktf.TrackID.value;
		if(trackIDVal == null || trackIDVal =="" || trackIDVal== undefined){
			//Set it to default value based on Country
			if(countryCode == "US"){
				trackIDVal = "1004686";
			}
			if(countryCode == "FR"){
				trackIDVal = "1004686";
			}
			if(countryCode == "AU"){
				trackIDVal = "1003722";
			}
			if(countryCode == "UK"){
				trackIDVal = "1003226";
			}
			if(countryCode == "DE"){
				trackIDVal = "1004686";
			}
		}
		var theSendURL = "/SelfServiceWeb/portlets/buyOnline/BuyOnlineController.do?";
		theSendURL = theSendURL + "Country=" + countryCode+"&";
		theSendURL = theSendURL + "Step=" + "Step4" +"&";
		theSendURL = theSendURL + "TrackID=" + trackIDVal+"&";
		theSendURL = theSendURL + "TrackID2=" + document._mktf.TrackID2.value+"&";
		theSendURL = theSendURL + "OrigTrackID=" + document._mktf.OrigTrackID.value+"&";
		//alert(theSendURL);
		document.getElementById("statusBlock").innerHTML="<img src='/images/processingnew.gif'> </img>";
		callAjaxControllerToRegister( theSendURL );


	}
}

    
function callAjaxControllerToRegister(str){
	theSendURL = str;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support XML HTTP Request");
		return;
	} 
	
	xmlHttp.onreadystatechange=stateChanged ;
	xmlHttp.open("POST",theSendURL,true);
	xmlHttp.send(null);
}


