/*
* This file contains common java script function of handbook builder aplication.
* @author : kuldeep singh
* @dependency : JQuery
*/
//Costants for banner
var BANNER_COUNT = 4;
var START_BANNER_INDEX = 1;
var ROTATION_SPEED = 8000; //in milliseconds
var g_currentBannerIndex = START_BANNER_INDEX;
/**
* This method read browser cookie for given name
* @param name -name of the cookie 
*/
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;
}
/**
* This method create cookie for given name and value.
* these cookies will be expired after given days.
* @param name - name of cookie
* @param value - value of cookie
* @param days - expiry period in days.
*/
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 = "";
        document.cookie = name+"="+value+expires+"; path=/; domain=thompson.com";
}

/**
 * This method display dynamic banners
 */
function showBanner()
{
	jQuery("#banner-"+g_currentBannerIndex).hide();
	//Fetch next index
	if(g_currentBannerIndex < BANNER_COUNT)
	{
		g_currentBannerIndex = g_currentBannerIndex + 1;
	}
	else
	{
		g_currentBannerIndex = START_BANNER_INDEX;
	}
	jQuery("#banner-"+g_currentBannerIndex).fadeIn(1000) ;
}
/**
* this method redirects to home page of BILD website
*/
function redirectToHome()
{
	window.location = "/index.html";
}

/**
* this method submits the form whose name is passed in first argument
*/
function submitForm(formName, formAction)
{
	var argc = submitForm.arguments.length;
	if(argc == 2)
	{
		document.forms[formName].action = formAction;
	}
	document.forms[formName].submit();
}

/**
* this method toogles the view when "explain this"/"return back" link on a page is clicked.
* If explain this clicked it hides the current content content and shows the explain this section.
* On clicking the return back link the user is shown the previous content.
*/
function toggleExplainThis()
{
	if(document.getElementById("explainThisContent").style.display == 'none')
	{
		document.getElementById("explainThisContent").style.display = '';
		document.getElementById("returnBackLink").style.display = '';
		document.getElementById("explainThisLink").style.display = 'none';
		document.getElementById("pageContent").style.display = 'none';
	}
	else
	{
		document.getElementById("explainThisContent").style.display = 'none';
		document.getElementById("returnBackLink").style.display = 'none';
		document.getElementById("explainThisLink").style.display = '';
		document.getElementById("pageContent").style.display = '';
	}
}
/**
* This method setups cookie in browser to calculate price for handbook.
* @param hbCode - value of code.
*/
function setWebCode(hbCode)
{
	var query = window.location.search.substring(1);
	//Read handbook code
	var cook = readCookie("hbcode");
	if (query) 
	{ 
   		if (query.indexOf("&") > 1) 
   		{ 
   			query=query.substring(0, query.indexOf("&")); 
   		}   
   		createCookie("hbcode", query, 1000);
	}
	else if (cook == null) 
	{ 
   		createCookie("hbcode", hbCode, 1000);
	}
}

function redirectToUrl(redirectUrl, isSecure )
{
	if(!isSecure)
	{
		window.location = redirectUrl ;
		return true;
	}
	else
	{
		window.location = "https://"+ window.location.hostname +redirectUrl;
		return true;
	}	
}
