
/************************************************** 
***************      OMNITURE TRACK       ****************
***************************************************/

function doOmnitureTrackPage(prop, val) {
	log("Omniture Tracking: " + prop + " = " + val);
	
	s[prop] = val.stripTags();
	s.t();
}

function doOmnitureTrackData(data) {
	log("Omniture Data Tracking: " + data);
	
/*	var keys = $A(Object.keys(data));
	s = s_gi('wbrostheatricaldomesticdvd');
	for (var i = 0; i < keys.length; i++) s[keys[i]] = data[keys[i]];
	s.tl(this,'o','Detected Values');
*/
	var keys = $A(Object.keys(data));
	for (var i = 0; i < keys.length; i++) {
		log(keys[i] + ' = ' + data[keys[i]]);
		s[keys[i]] = data[keys[i]];
	}
//	s.t();
	void(s.t());
}


/************************************************** 
***************       XML          ****************
***************************************************/

function getParam(name)
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

/************************************************** 
***************       XML          ****************
***************************************************/

/**
*	Retrieve node value
*/
function getNodeValue(obj,tag) {	
	try 
	{
		
		/*if(tag=='Collection')
		{
			if(obj.getElementsByTagName(tag)[0] != undefined )
				
				return obj.getElementsByTagName(tag)[0].firstChild.nodeValue; 
				
		}
		*/
		//else{
			if(obj.getElementsByTagName(tag)[0] !=undefined)
			{
				if (obj.getElementsByTagName(tag)[0].hasChildNodes()) {
					return obj.getElementsByTagName(tag)[0].firstChild.nodeValue; 
				
				} 
				else {
					return "";
						}
			}
		//}//end else
	} catch(e) {
		log('error parsing: ' + tag + ', error name: ' + e.name + ', error message: ' + e.message);

//		return "error";		
	} finally {
		// do nothing
	
	}

}	


/************************************************** 
***************      LOADING POPUP       ****************
***************************************************/
/**
*	Create the loading popup
*/
function buildLoadingPopup(msg) {
	log('**build loading popup');
	
	if($('loading-popup') != null) return;

	// hide contents of document body
	$('bounding-area').setOpacity(0.3);
//	$('csform').hide();
	
	// create the popup div
	loadingPopup = new Element('div', {id: 'loading-popup'}).update(msg);
	loadingPopup.insert(new Element('img', {src: 'images/common/loadingbar.gif'}));
	$('popup-holder').insert(loadingPopup);
	
	var tmp = document.viewport.getDimensions();
	var _top = String(245)+"px";
	var _left = String((tmp.width/2)-100)+"px";
	$('popup-holder').setStyle({position:'absolute',top:_top, left:_left});
	
}


/**
*	Remove the loading popup
*/
function removeLoadingPopup() {


	if($('loading-popup') != null)
		$('loading-popup').remove();

	// show document body
	$('bounding-area').setOpacity(1);
	if($('csform') != null)
		$('csform').show();
}

function popWin(url,name)
{
	
	window.open(url,name);
}

/************************************************** 
*************	    SHOW/HIDE DIVS	   *****************
***************************************************/


/**
*	Displays and hides the FAQ sections
*/
function showHideSection(event) {
	var el = event.element();
	var ancestors = el.ancestors();
	var next = ancestors[0].next();
//	var categories = 0;
	if (next.hasClassName('hidden') == true) {
		
		// hide all faq-panel divs
		$A($(document.body).getElementsByClassName("faq-panel")).each(function(f) {
			f.addClassName('hidden');
		});

		next.removeClassName('hidden');
//		$A($(document.body).getElementsByClassName("category-header")).each(function(c){
//			categories++;
//		});
//		$('faq-panel').style.height = $('middle').style.height - (categories*15);
		
	} else {
		next.addClassName('hidden');
		
		// [todo] close all the 'answer' in this container
	}
}


/**
*	Displays and hides the answer in the FAQs
*/
function showHideAnswer(event,id) {
	log('ID: '+id);
	var el = event.element();
	var next = el.next('.answer');
	console.log("Event"+event);
	console.log("showHideAnswer.next: %o ", next);
	console.log("showHideAnswer: el.parentNode.next('.answer'): %o ", el.parentNode.next('.answer'));
	if (next == null) next = el.parentNode.next('.answer');
	if (next == null) return;

	next.toggleClassName('hidden');
}


/************************************************** 
***************      COOKIE        ****************
***************************************************/

/**
*	Retrieve cookie value
*/
function getCookie(cookieName) { 
	log('get cookie: ' + cookieName);
	
	if (document.cookie.length > 0) { 
		begin = document.cookie.indexOf(cookieName+"=");
		if (begin != -1) { 
			begin += cookieName.length+1;
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end)); 
		}
	}
	return null;
}

/**
*	Set cookie value
*/
function setCookie(cookieName, value, expiredays) { 
	log('set cookie: ' + cookieName + ' = '  + value);
	
	var expireDate = new Date ();
	expireDate.setTime(expireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	document.cookie = cookieName + "=" + escape(value) +	((expiredays == null) ? "" : "; expires=" + expireDate.toGMTString());
}


/**
*	Delete cookie value
*/
function delCookie (cookieName) { 
	if (getCookie(cookieName)) {
		document.cookie = cookieName + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}

}

function checkHash(){
	//alert("Selected title" +strmy);
	/*if(BrowserDetect.browser == "Explorer")
	{
		var iframeElem = parent.document.getElementById("dummyiframe");
		if(iframeElem.src != parent.document.location)
		{
			var ifrmLoc = iframeElem.src.split("#");
			document.location.hash = ifrmLoc[1];
		}
	}*/
	
	if(readyToLoad == true)
	{
	if(document.location.hash){
		// get ha sh value
		var HashLocationName = document.location.hash;
		HashLocationName = HashLocationName.replace("#","");
		/***
		 * inApp specific support
		 */
		if(HashLocationName.include("inapp=true") && HashLocationName.include("&code") )
			{	
				    var g = HashLocationName.split("&");
					for(var j=0;j<g.length;j++)
					{   
						if(g[j]!=undefined && g[j].include("code"))
						{
						 var tmp =  g[j].split("=");
						 authorizationCode = tmp[1];
						 
						}
						
					}

			}
		
		if(HashLocationName == 'feedback-content' && selectedSection != 'Feedback' || (HashLocationName.include('feedback-content&inapp=true') && selectedSection != 'Feedback') )
			buildFeedback();
			else if(HashLocationName.include('privacy-content') && selectedSection != 'Privacy')
			{
				var location = HashLocationName.split('&');
				var country = ''; 
				if(location.length>1)
				{
					country = location[1].split('=');
					selectedLang = LandingPage.getlanguageID(country[1]);
				}
				
				if(HashLocationName.include('inapp'))
					selectedTitle=selectedTitle;
				else selectedTitle = "";
				//selectedSource = "";
				
				buildPrivacy(country[1]);
			}
			else if(HashLocationName.include('terms-content') && selectedSection != 'Terms')
			{
				var location = HashLocationName.split('&');
				var country = ''; 
				if(location.length>1)
				{
					country = location[1].split('=');
					selectedLang = LandingPage.getlanguageID(country[1]);
				}
				
				if(HashLocationName.include('inapp'))
					selectedTitle=selectedTitle;
				else selectedTitle = "";
				//selectedSource = "";
				buildTerms(country[1]);
			}	
			else if((HashLocationName.include('WhatIsDigitalCopy') || HashLocationName.include('HowItWorks') || HashLocationName.include('home-content')) && selectedSection != 'Home')
			{
				var location = HashLocationName.split('&');
				var langParam = getParamValue('lang');
				var country = '';
				var discid = '';
				if(location.length >= 3)
				{
					if(location.length <6)
					{
							var a = location[1].split('=');
							var b = location[2].split('=');
							if(a[0] == 'country')
							{
								country = a[1];
								discid = b[1];
							}	
							else
							{
								discid = a[1];
								country = b[1];
							}
					}
					else if(location.length == 6)
					{  //extract all variables
						for(b=1 ; b<location.length;b++)
						{
							var tmpstr = location[b].split('=');
							//alert(tmpstr[0] + "__________"+ tmpstr[1]);
							if(tmpstr[0] == 'discid' )
							{
								setTitle(tmpstr[1]);
								selectedTitle=tmpstr[1];
								discid= tmpstr[1];
							}	
							else if(tmpstr[0]=='inapp')
							{
								
							}
							else if(tmpstr[0]=='code')
							{
								inAppAuthCode=tmpstr[1];
							}
							else if(tmpstr[0]=='country')
							{
								setCountry(tmpstr[1].toLowerCase());
								country=tmpstr[1].toLowerCase();
							}	
							else if(tmpstr[0]=='lang')
							{
								setLanguage(tmpstr[1]);
							}
						}
						
					
					
					
					}
					
					setTitle(discid);
					setCountry(country.toLowerCase());
					setSource(getDiscSource(selectedTitle));
					if(langParam == undefined)
						selectedLang = LandingPage.getlanguageID(selectedCountry);
					else
						selectedLang = langParam
					setLanguage(selectedLang);	
					var title = getDiscTitle(selectedTitle); 
					if(selectedCountry != getDiscCountry(selectedTitle,selectedCountry) || title == "")	
						{buildLandingPage();}
					else
					{	
						selectedSection = "Home";
						buildHome();
						}
				}
				else
					{buildLandingPage();}
				
			}	
			else if((HashLocationName.include('gettingstarted-content') || HashLocationName.include('GetStarted')) && selectedSection != 'GettingStarted'){
				var location = HashLocationName.split('&');
				var langParam = getParamValue('lang');
				var country = '';
				var discid = '';
				
				if(location.length <= 3)
				{
					var a = location[1].split('=');
					var b = location[2].split('=');
					if(a[0] == 'country')
					{
						country = a[1];
						discid = b[1];
					}	
					else
					{
						discid = a[1];
						country = b[1];
					}
					setTitle(discid);
					setCountry(country.toLowerCase());
					setSource(getDiscSource(selectedTitle));
					if(langParam == undefined)
						selectedLang = LandingPage.getlanguageID(selectedCountry);
					else
						selectedLang = langParam
						
					setLanguage(selectedLang);
					var title = getDiscTitle(selectedTitle); 
					if(selectedCountry != getDiscCountry(selectedTitle,selectedCountry) || title == "")	
						{buildLandingPage();} 
					else	
						buildGettingStarted();
				}
				else if(location.length == 6)
				{//extract all variables
					
					for(a=1 ; a<location.length;a++)
					{
						var tmpstr = location[a].split('=');
						//alert(tmpstr[0] + "__________"+ tmpstr[1]);
						if(tmpstr[0] == 'discid' )
						{
							//title=tmpstr[1];
							setTitle(tmpstr[1]);
							selectedTitle=tmpstr[1];
						}	
						else if(tmpstr[0]=='inapp')
						{
							
						}
						else if(tmpstr[0]=='code')
						{
							inAppAuthCode=tmpstr[1];
						}
						else if(tmpstr[0]=='country')
						{
							setCountry(tmpstr[1].toLowerCase());
							
						}	
						else if(tmpstr[0]=='lang')
						{
							setLanguage(tmpstr[1]);
						}
					}
				  
					//alert(selectedCountry + "<  >" + getDiscCountry(selectedTitle,selectedCountry) + "title ="+title );
					if(selectedCountry != getDiscCountry(selectedTitle,selectedCountry) || title == "")	
					{buildLandingPage();
						
					} 
					else{	
						selectedSource=getDiscSource(selectedTitle);
						buildGettingStarted();
					
					}
				
				
				}
				else
					{
					
					buildLandingPage();
					}
				
			}
			else if(HashLocationName.include('support-content') && selectedSection != 'Support')
			{   
				var location = HashLocationName.split('&');
				var langParam = getParamValue('lang');
				var country = '';
				var discid = '';
				if(location.length >= 3)
				{
					if(location.length <6)
					{	
							var a = location[1].split('=');
							var b = location[2].split('=');
							var c = '';
							var supportID = "";
							if(location.length >=4)
							{
								c = location[3].split('=');
								if(c[0].include('category'))
								{
									selectedFAQCategory = c[1];
									selectedTopSupportQue = "";
									supportID = selectedFAQCategory;
								}else if(c[0].include('faq')){
									selectedTopSupportQue = c[1];
									selectedFAQCategory = "";
									supportID = selectedTopSupportQue;
								}else{
									selectedTopSupportQue = "";
									selectedFAQCategory = "";
								}
								
							}
							
							if(a[0] == 'country')
							{
								country = a[1];
								discid = b[1];
							}	
							else
							{   
								discid = a[1];
								country = b[1];
							}
							selectedTitle=discid;
					}
					if(location.length >=6)
					{//extract all variables
						for(b=1 ; b<location.length;b++)
						{
							var tmpstr = location[b].split('=');
							//alert(tmpstr[0] + "__________"+ tmpstr[1]);
							if(tmpstr[0] == 'discid' )
							{
								setTitle(tmpstr[1]);
								selectedTitle=tmpstr[1];
								discid= tmpstr[1];
							}	
							else if(tmpstr[0]=='inapp')
							{
								
							}
							else if(tmpstr[0]=='code')
							{
								inAppAuthCode=tmpstr[1];
							}
							else if(tmpstr[0]=='country')
							{
								setCountry(tmpstr[1].toLowerCase());
								country=tmpstr[1].toLowerCase();
							}	
							else if(tmpstr[0]=='lang')
							{
								setLanguage(tmpstr[1]);
							}
							else if(tmpstr[0]=='categoryid')
							{
								selectedFAQCategory =tmpstr[1];
							}
							else if(tmpstr[0]=='faqid')
							{
							selectedTopSupportQue =tmpstr[1];
							}
						}
					}
					
					
					selectedSource = getDiscSource(selectedTitle);
					setTitle(discid);
					setCountry(country.toLowerCase());
					setSource(selectedSource);
					
					if(langParam == undefined)
						{selectedLang = LandingPage.getlanguageID(selectedCountry);}
					else
						{selectedLang = langParam}
						
					setLanguage(selectedLang);	
					
					var title = getDiscTitle(selectedTitle); 
					//alert("selectedCountry= "+selectedCountry +" getdisccournt= "+ getDiscCountry(selectedTitle,selectedCountry))
					//alert("selectedCountry="+selectedCountry+" Selected Title="+selectedTitle + " title="+ title);
					if(selectedCountry != getDiscCountry(selectedTitle,selectedCountry) || title == "")
					{   
						buildLandingPage();
					}
					else{	
						//alert("support id="+ supportID);
						selectedSection = "Support";
						
						buildSupport(supportID);
					}
				}
				else
					{ 
					buildLandingPage();}
			}
			else if(HashLocationName == 'support-form-content' && selectedSection != 'SupportForm')
				buildECSupportForm();
			else if(HashLocationName.include('supportform-content') && selectedSection != 'SupportForm')
			{
				
				var langParam = getParamValue('lang');
				var location =HashLocationName.split('&');
				
				if(location.length >= 3)
				{
					var a = location[1].split('=');
					var b = location[2].split('=');
					var country = "";
					var format = "";
					var code = '';
					var err = '';
					
					
					inApp = b[1];
					
					if(getDiscTitle(a[1]) != "")
					{
						setTitle(a[1]);
						setSource(getDiscSource(selectedTitle));
					}
					else
						err = 'true';
					
//					setFormat(getDiscFormat(selectedTitle));
//					setCountry(getDiscCountry(selectedTitle));
					if(langParam == undefined)
						setLanguage("en");
					else
						setLanguage(langParam)
				
					if(location.length == 4)
					{
						var c = location[3].split('=');
						if(c[0].include('code'))
							inAppAuthCode = c[1];
						else if(c[0].include('format'))
							format = c[1];
						else if(c[0].include('country'))
							country = c[1];
					}
					else if(location.length == 5)
					{
						var c = location[3].split('=');
						var d = location[4].split('=');
						
						if(c[0].include('code'))
							inAppAuthCode = c[1];
						else if(c[0].include('format'))
							format = c[1];
						else if(c[0].include('country'))
							country = c[1];
						
						if(d[0].include('code'))
							inAppAuthCode = d[1];
						else if(d[0].include('format'))
							format = d[1];
						else if(d[0].include('country'))
							country = d[1];
					}
					else if(location.length == 6)
					{   //extract all variables
						for(b=1 ; b<location.length;b++)
						{
							var tmpstr = location[b].split('=');
							//alert(tmpstr[0] + "__________"+ tmpstr[1]);
							if(tmpstr[0] == 'discid' )
							{
								setTitle(tmpstr[1]);
							}	
							else if(tmpstr[0]=='inapp')
							{
								
							}
							else if(tmpstr[0]=='code')
							{
								inAppAuthCode=tmpstr[1];
							}
							else if(tmpstr[0]=='country')
							{
								setCountry(tmpstr[1].toLowerCase());
								country=tmpstr[1].toLowerCase();
							}	
							else if(tmpstr[0]=='lang')
							{
								setLanguage(tmpstr[1]);
							}
						}
						/*var c = location[3].split('=');
						var d = location[4].split('=');
						var e = location[5].split('=');
						
						if(c[0].include('code'))
							inAppAuthCode = c[1];
						else if(c[0].include('format'))
							format = c[1];
						else if(c[0].include('country'))
							country = c[1];
						
						if(d[0].include('code'))
							inAppAuthCode = d[1];
						else if(d[0].include('format'))
							format = d[1];
						else if(d[0].include('country'))
							country = d[1];
						
						if(e[0].include('code'))
							inAppAuthCode = e[1];
						else if(e[0].include('format'))
							format = e[1];
						else if(e[0].include('country'))
							country = e[1];
						 */
					
					}
					authorizationCode = inAppAuthCode;
					selectedSection = "SupportForm";
					if(err == 'true')
					{   
						//error = 'true'
						buildThankYouForFeedback(4);
					}
					else{
						//alert(selectedTitle +" "+ country);
						//alert(getDiscCountry(selectedTitle,country));
						//alert(getDiscCountry('24A1947E-2789-46b2-9188-FB998A97CE0E','us')+"This is the disc country");
						if(getDiscCountry(selectedTitle,country) != "")
							setCountry(getDiscCountry(selectedTitle,country));
						else if(country == "" && getDiscCountry(selectedTitle) != "")
							setCountry(getDiscCountry(selectedTitle));
						else
							{err = 'true';}
						if(getDiscFormat(selectedTitle,format) != "")
							setFormat(getDiscFormat(selectedTitle,format));
						else if(format == "" && getDiscFormat(selectedTitle) != "")
							setFormat(getDiscFormat(selectedTitle));
						else
							{err = 'true';}
						
						if(err != 'true')
							buildSupportForm1();
						else
							{buildThankYouForFeedback(4);}
						
						if(HashLocationName.include('inapp') && HashLocationName.include('&country') )
						{
						        var g = HashLocationName.split("&");
								for(var j=0;j<g.length;j++)
								{   //alert(g[j]);
									if(g[j]!=undefined && g[j].include("country"))
									{
									 var tmp =  g[j].split("=");
									setCountry(tmp[1].toLowerCase());
									//toLowerCase()
									}
									
								}
						
						}
						
						
						}
				}	
				else
					buildSupportForm1();
				
			}
			else if(HashLocationName.include('landing-content') && selectedSection != 'LandingPage')
			{	
				buildLandingPage();
			}
	//		document.getElementById(selectedSection+"-content").style.display = (selectedSection+"-content" == HashLocationName) ? 'block' : 'none';
	}
	}
	
}

function buildSection()
{
	if(window.location.include('?'))
	{
		var location=window.location.search;
		var section=new Array();
		section = location.split('?');
		if(section.length>1)
		{
			var sectionName = section[1].split('=');
			if(sectionName[1] == 'SupportForm')
			{
				if(selectedSource.include('DC'))
					buildSupportForm();
				else if(selectedSource == 'EC')
				{
					buildECSupportForm();
				}
			}
		}
	}
	else
	{
		buildLandingPage();
	}
}

function buildHash(section)
{
	document.location.hash = section;
/*	if(BrowserDetect.browser == "Explorer")
	{
		var iframeElem = parent.document.getElementById("dummyiframe");
		/*var el = iframeElem.cloneNode(true);
		el.style.position = "absolute";
        el.style.visibility = "hidden";
        el.id = "";
        iframeElem.parentNode.replaceChild(el, iframeElem);
        el.id = iframeElem.id;
        el.src = document.location;
        document.body.appendChild(el);
        alert(el.src);*/
	/*	iframeElem.src =  "http://www.google.com";
		var doc = iframeElem.contentDocument;
		
		if (doc == undefined) { // Internet Explorer
			doc = iframeElem.contentWindow.document;
		}
		doc.open();
		doc.write(document.location);
		doc.close();
	}*/

}

/************************************************** 
***************       DEBUG        ****************
***************************************************/

function log(msg) {
	if (BrowserDetect.browser == "Firefox") console.log(msg);
}

