var gCorpURL = "http://www.torridromance.com/";
var gCorpSURL = "https://www.torridromance.com/";
var gCorpCartURL = gCorpURL + "basket.php";
var gCorpCheckOutURL = gCorpSURL + "checkout.php";
var gCorpProductExcerptURL = gCorpURL + "excerpt.php";

// component: contact_us.html

function ContactUsObj(firstName, lastName, eMail, subject, body) {
    this.fFirstName = firstName;
    this.fLastName = lastName;
    this.fEMail = eMail;
    this.fSubject = subject;
    this.fBody = body;

    this.Valid = CuValid;
}

function CuValid()
{
	if ((this.fFirstName.value == "") || (this.fLastName.value == "") ||
		(this.fSubject.value == "") || (this.fBody.value == ""))
	{
		alert("Please fill-in all form fields");
		return false;
	}
		
	if (!eMailValid(this.fEMail.value))
	{
		this.fEMail.focus();
		return false;
	}

	return true;
}


// component: book_shelf.html

function BookObj(sku, title, author, price, pages, dimensions, description) {
	this.fSKU = sku;
    this.fTitle = title;
    this.fAuthor = author;
    this.fPrice = price;
    this.fPages = pages;
    this.fDimensions = dimensions;
    this.fDescription = description;
}

var gBooks = new Array();

gBooks["book_allure_of_the_cowboy"] = new BookObj("003-000001-00", "Allure of the Cowboy", "Chaucer, Taylor", 44.99, 183, "4 1/8\" x 6 3/4\"",
		"\"Another best-selling example of Taylor Chaucer’s talent for enormously engaging stories with extra steamy love scenes.\"\n\n" +
		"The idea was simple – she’d travel to the little western town (population: next-to-nothing), claim the land she inherited and return to civilization in less than twenty-four hours.  But what she never expected was the provision in the will that required her to reside on the property for six months before she could sell it.  And she never imagined she’d live with the ruggedly handsome cowboy whose mere presence exuded a dangerous, unpredictable and totally male sensuality that tantalized her fantasies and teased the heightened awareness of her body.\n\n" +
		"Although he reasoned their union didn’t make sense, he could not deny the burning passion that simmered just below the surface.  For she was the perfect combination of smart-mouthed feistiness and wicked temptation and her sinful curves were impossible to resist.  When their worlds collide with a stunning force neither had expected but both soon craved, they are forced to question whether they can overcome their differences, one dizzying kiss at a time."
	);

gBooks["book_knights_of_passion"] = new BookObj("003-000002-00", "Knights of Passion", "Chaucer, Taylor", 44.99, 188, "4 1/8\" x 6 3/4\"",
		"He Vowed To Defend Her Even If It Meant Losing His Heart\n\n" +
		"The Warrior Beauty\n" +
		"When an evil warlord strips her of her castle and people, the Lady of the Sea must turn to the only man who can help her – the man who once betrayed her.\n\n" +
		"The Champion\n" +
		"He is known as The Champion – a protector of the people, a fierce knight and legendary lover.   When he finds the unconscious lady inadvertently injured by his own blade, the Lord of Hawkcrest is shocked to discover the gorgeous woman from his past.\n\n" +
		"The Alliance\n" +
		"Now, compelled by duty and inspired by the fire that burns between them, both must overcome their fears to fight a battle against a common enemy.   She must place the lives of her people in the hands of the man whose searing kisses she can’t ignore.  And he must succeed in defending the one woman whose curves and sparkling eyes tempt his steely resolve and whose sensuality speaks to him in ways none has ever done before."
	);

gBooks["book_strangers_in_paris"] = new BookObj("003-000003-00", "Strangers in Paris", "Chaucer, Taylor", 44.99, 193, "4 1/8\" x 6 3/4\"",
		"LET YOURSELF GO…\n\n" +
		"What Happens When a Beautiful Woman Goes On Vacation...\n\n" +
		"Overworked, under-appreciated and stressed beyond belief, she is looking forward to a calm vacation in a charming French cottage.   But when she meets a strikingly handsome stranger with a body that tempts her beyond reason, and her common sense whispers caution, she does the only logical thing – give into the wanton impulses he inspires and dive head first into a forbidden romance of scintillating passion.\n\n" +
		"And Meets the Man of Her Dreams...\n\n" +
		"Even in his wildest fantasies, he never dreamed he’d meet someone as gorgeous and fascinating as the woman who resides in the room next to his at the French cottage.   With soft, womanly curves and lips so luscious, he rues the day their nights of unbridled romance must end with the close of their unforgettable vacation.\n\n" +
		"In The Most Romantic City on Earth?\n\n" +
		"But even more threatening than their impending separation, another force intervenes to jeopardize the exhilaration and ecstasy of their union.  When they become pawns in a game of deceit and murder, they must rely on one another and trust their blooming love to not only save their own lives, but to tend the smoldering embers of their burning desires."
	);

gBooks["book_taming_the_tycoon"] = new BookObj("003-000004-00", "Taming the Tycoon", "Chaucer, Taylor", 44.99, 196, "4 1/8\" x 6 3/4\"",
		"Only She Could Tame His Restless Heart\n\n" +
		"Voted as one of most eligible bachelors and a playboy of the most notorious sort, the last thing this dangerously handsome and successful businessman wanted was a relationship to keep him from enjoying his vast riches and indiscriminate affairs.  He was determined to never let anyone distract him from his ambitions.  Until he met her...\n\n" +
		"She was the only woman worthy of the love he’s vowed never to surrender.  Gorgeous, clever and sexy as hell, she was the one woman whose fascination with him had nothing to do with his money.  With her, he surrendered to the fiery passion she stirred within him.\n\n" +
		"But can she trust that he – an old friend once separated by the trivialities of life and now reunited by Fate – is capable of surrendering the past she knew too well?  For he must soon choose between the life he once knew and the love of a woman who has changed his heart forever."
	);

gBooks["book_beauty_and_the_bodyguard"] = new BookObj("003-000005-00", "Beauty and the Bodyguard", "Chaucer, Taylor", 44.99, 202, "4 1/8\" x 6 3/4\"",
		"From romance writer extraordinaire, Taylor Chaucer, comes a novel in which a disciplined man meets the one temptation that threatens to destroy his unbreakable resolve when he experiences a sensuality he never knew existed...\n\n" +
		"As one of the most in-demand bodyguards, his mantra is simple – never get involved with a client. Yet, thoughts of one woman and her sinfully perfect body consume him day and night, threatening his guarded heart. With his professionalism – and her protection – at stake, he must struggle to resist the desire she awakens in him.\n\n" +
		"Gorgeous and devastatingly sexy, she is the world's most coveted supermodel - loved and admired by all but stalked by one – a crazed fan who will stop at nothing to possess her. Although hesitant at first, she reluctantly agrees to his protection. He's stubborn, irritatingly smug and she swears she'd hate him if he didn't ooze such lethal amounts of masculinity.  Now forced to live together, she must fight her attraction for a man whose magnetism proves far more dangerous than the stalker pursuing her."
	);


function BookShelfObj()
{
	this.fPromptVisible = true;
	this.fSelBook = "";
	this.fSelBookImgURL = ["",""];
	this.fCurBookImgURL = ["",""];

	this.onMouseOver = onBsMouseOver;
	this.onClick = onBsClick;
	this.onMouseOut = onBsMouseOut;
	
	this.ShowExcerpt = BsViewExcerpt;
}

var gBookShelf = new BookShelfObj();

function onBsMouseOver()
{
	if (this.fSelBook != event.srcElement.name)
	{		
		this.fCurBookImgURL[0] = event.srcElement.src;	
		this.fCurBookImgURL[1] = event.srcElement.src.replace(/.jpg$/, "_lg.jpg");
		event.srcElement.src = this.fCurBookImgURL[1];
	}
}

function _HideObj(anObj)
{
	with (anObj.style)
	{
		visibility='hidden';
		position='absolute';
		z_index=1;
		overflow='hidden';
		height='1px';
	}
}

function _ShowObj(anObj)
{
	with (anObj.style)
	{
		height='auto';
		overflow='auto';
		z_index = "auto";					
		position = "static";
		visibility = "inherit";
	}
}

function onBsClick()
{
	if (this.fPromptVisible)
	{
		promptDiv.className = "hidden";

		_ShowObj(detailsDiv);

		this.fPromptVisible = false;
	}
	
	if (this.fSelBook != event.srcElement.name)
	{
		if (this.fSelBook != "")
		{
			document.images[this.fSelBook].src = this.fSelBookImgURL[0];
		}

		this.fSelBook = event.srcElement.name;
		this.fSelBookImgURL[0] = this.fCurBookImgURL[0];
		this.fSelBookImgURL[1] = this.fCurBookImgURL[1];

		idBookSKU.innerText = gBooks[this.fSelBook].fSKU;
		idBookTitle.innerText = gBooks[this.fSelBook].fTitle;
		idBookAuthor.innerText = gBooks[this.fSelBook].fAuthor;
		idBookPrice.innerText = gBooks[this.fSelBook].fPrice;
		idBookPages.innerText = gBooks[this.fSelBook].fPages;
		idBookDimensions.innerText = gBooks[this.fSelBook].fDimensions;
		idBookDescription.innerText = gBooks[this.fSelBook].fDescription;								
	}
}

function onBsMouseOut()
{
	if (this.fSelBook != event.srcElement.name)
	{
		event.srcElement.src = this.fCurBookImgURL[0];
	}
}

function BsViewExcerpt()
{
	var excerptWidth  = 675;
	var excerptHeight = 425;
	var parentWidth   = 770;
	var parentHeight  = 550;

	if (document.layers) {
	    parentWidth = window.innerWidth;
	    parentHeight = window.innerHeight;
	}
	else if (document.all) {
	    parentWidth = document.body.clientWidth;
	    parentHeight = document.body.clientHeight;
	}

	excerptWinOpts = "width=" + excerptWidth + "," +
					 "height=" + excerptHeight + "," +
					 "left=" + (window.screenLeft + (parentWidth-excerptWidth)/2) + "," +		//ie
					 "top=" + (window.screenTop + (parentHeight-excerptHeight)/6) + "," +	  	//ie
//					 "screenX=400," +	//netscape
//					 "screenY=400," +	//netscape
//					 "scrollbars=yes," +
					 "resizable=yes," +
					 "menubar=no," +
					 "status=no," +
					 "location=no";

	var excerptWin = open(gCorpProductExcerptURL+"?item="+gBooks[this.fSelBook].fSKU,"excerptWin", excerptWinOpts);
	excerptWin.focus();
}

// component: excerpt.html

function BookExcerptObj()
{
	this.ToggleForm = BeToggleForm;
	this.ToggleOther = BeToggleOther;
	this.SetHint = BeSetHint;
	this.InitForm = BeInitForm;
	
	this._FillInExcerpt = _BeFillInExcerpt;
	this._FillInSubst = _BeFillInSubst;
}

var gBookExcerpt = new BookExcerptObj();

function BeInitForm()
{
	for (var i = 0; i < document.forms.length; i++)
	{
		var aForm = document.forms[i];
		
		for (var j = 0; j < aForm.length; j++)
		{
			if (aForm.elements[j].value == "other")
				aForm.elements[j+1].style.visibility = "inherit";
		}
	}
	document.forms[0].elements[0].focus();
}

function _BeFillInSubst(formData, srcText, fieldMarker)
{
	var substText = srcText;
	
	for (var i = 0; i < formData.length; i++)
	{
		var key = new RegExp(fieldMarker + formData.elements[i].name + fieldMarker, "g");
		var value = formData.elements[i].value;
		
		if (value == "other")
			value = formData.elements[i+1].value;

		substText = substText.replace(key, "<b>" + value + "</b>");
	}
	
	return substText;
}

function _BeFillInExcerpt()
{
	var substText = this._FillInSubst(herInfo, excerptTemplate, "\\$");
	substText = this._FillInSubst(hisInfo, substText, "\\#");
	
	excerptText.innerHTML = substText;
}

function BeToggleForm()
{
	if (personalizeForm.style.visibility == 'hidden')
	{
		_HideObj(excerptDash);
		_HideObj(excerptDiv);
		_ShowObj(personalizeDash);
		_ShowObj(personalizeForm);
		
		with (personalizeForm.style)
		{
			height='100%';
		}
	}
	else
	{
		_HideObj(personalizeDash);
		_HideObj(personalizeForm);
			
		this._FillInExcerpt();
		
		_ShowObj(excerptDash);
		_ShowObj(excerptDiv);

		with (excerptDiv.style)
		{
			height='100%';
		}
	}
}

function BeToggleOther()
{
	if (event.srcElement.value == "other")
	{
		event.srcElement.form[event.srcElement.name+"_other"].style.visibility = 'inherit';
		event.srcElement.form[event.srcElement.name+"_other"].focus();
	}
	else
	{
		event.srcElement.form[event.srcElement.name+"_other"].style.visibility = 'hidden';
	}
}

function BeSetHint()
{
	var hintKey = event.srcElement.name;
	var hintStr = excerptHintStrs[excerptHints[hintKey]];
	
	if (hintStr == null)
		hintStr = excerptHintStrs[excerptHints[hintKey.replace(/_other$/, "")]];
		
	if (event.srcElement.form.id == "herInfo")
	{
		herHintText.innerText = "Instructions: " + hintStr;
		hisHintText.innerText = "Instructions:";
	}
	else
	{
		hisHintText.innerText = "Instructions: " + hintStr;
		herHintText.innerText = "Instructions:";
	}
}

// component: _customize.php

function CustomFormObj()
{
	this.ToggleOther = CfToggleOther;
	this.SetHint = CfSetHint;
}

var gCustomForm = new CustomFormObj();

function CfToggleOther()
{
	var otherName = event.srcElement.name;
	
	otherName = otherName.replace(/\]/, '_other]');
	
	if (event.srcElement.value == "other")
	{
		event.srcElement.form[otherName].style.visibility = 'inherit';
		event.srcElement.form[otherName].focus();
	}
	else
	{
		event.srcElement.form[otherName].style.visibility = 'hidden';
	}
}

function CfSetHint()
{
	var hintKey = event.srcElement.name.match(/(.+)\[(.+)\]/);
	hintSect = RegExp.$1;
	hintKey = RegExp.$2;

	var hintStr = excerptHintStrs[excerptHints[hintKey]];
	
	if (hintStr == null)
		hintStr = excerptHintStrs[excerptHints[hintKey.replace(/_other$/, "")]];
		
	if (hintSect == "her")
	{
		herHintText.innerText = "Instructions: " + hintStr;
		hisHintText.innerText = "Instructions:";
		if (self.miscHintText) miscHintText.innerText = "Instructions:";
	}
	else if (hintSect == "his")
	{
		hisHintText.innerText = "Instructions: " + hintStr;
		herHintText.innerText = "Instructions:";
		if (self.miscHintText) miscHintText.innerText = "Instructions:";
	}
	else
	{
		if (self.miscHintText) miscHintText.innerText = "Instructions: " + hintStr;
		hisHintText.innerText = "Instructions:";
		herHintText.innerText = "Instructions:";
	}
}

// component: shopping_basket.php

function ShoppingCartObj()
{
	this._SaveLocation = _ScSaveLocation;
	this.Add = ScAdd;
	this.Remove = ScRemove;
	this.Update = ScUpdate;
	this.Continue = ScContinue;
	this.CheckOut = ScCheckOut;
}

var gShoppingCart = new ShoppingCartObj();

function _ScSaveLocation()
{
	var expires = new Date();
	expires.setTime(expires.getTime() + 3600*24*7*1000);

	setCookie("aisle",location.href,expires, "/");
}

function ScContinue()
{
	location.href = getCookie("aisle") ? getCookie("aisle") : gCorpURL;
}

function ScAdd(item, qty)
{
	this._SaveLocation();
	location.href = gCorpCartURL + "?cmd=add&item=" + item + "," + qty;	
}

function ScRemove(item, qty)
{
	location.href = gCorpCartURL + "?cmd=remove&item=" + item + "," + qty;
}

function ScUpdate(formID)
{
	formID.action = gCorpCartURL + "?cmd=update";
}

function ScCheckOut()
{
	location.href = gCorpCheckOutURL;
}

// system: run-time library

function eMailValid(emailStr)
{
	/* The following pattern is used to check if the entered e-mail address
	   fits the user@domain format.  It also is used to separate the username
	   from the domain. */
	var emailPat=/^(.+)@(.+)$/
	/* The following string represents the pattern for matching all special
	   characters.  We don't want to allow special characters in the address. 
	   These characters include ( ) < > @ , ; : \ " . [ ]    */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	/* The following string represents the range of characters allowed in a 
	   username or domainname.  It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]"
	/* The following pattern applies if the "user" is a quoted string (in
	   which case, there are no rules about which characters are allowed
	   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	   is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")"
	/* The following pattern applies for domains that are IP addresses,
	   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	   e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* The following string represents an atom (basically a series of
	   non-special characters.) */
	var atom=validChars + '+'
	/* The following string represents one word in the typical username.
	   For example, in john.doe@somewhere.com, john and doe are words.
	   Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	   domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	/* Finally, let's start trying to figure out if the supplied address is
		valid. */

	/* Begin with the coarse pattern to simply break up user@domain into
		different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null)
	{
	/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		alert("Please provide accurate and complete information.  The e-mail address seems incorrect (check @ and .'s)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	// See if "user" is valid 
	if (user.match(userPat)==null)
	{
	    // user is not valid
	    alert("The e-mail address contains a user name that seems to be invalid")
	    return false
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	   host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	
	if (IPArray!=null)
	{
	    // this is an IP address
		for (var i=1;i<=4;i++)
		{
			if (IPArray[i]>255)
			{
				alert("The e-mail address contains an invalid destination IP address")
				return false
		    }
	    }
		return true
	}
	
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	
	if (domainArray==null)
	{
		alert("The e-mail address contains a domain name that seems to be invalid")
	    return false
	}
		
	/* domain name seems valid, but now make sure that it ends in a
	   three-letter word (like com, edu, gov) or a two-letter word,
	   representing country (uk, nl), and that there's a hostname preceding 
	   the domain or country. */
	
	/* Now we need to break up the domain to get a count of how many atoms
	   it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3)
	{
	   // the address must end in a two letter or three letter word.
	   alert("The e-mail address must end in a three-letter domain (i.e. com, org, edu, et al), or a two letter country code")
	   return false
	}

	// Make sure there's a host name preceding the domain.
	if (len<2)
	{
	   alert("The e-mail address must end in a three-letter domain (i.e. com, org, edu, et al), or a two letter country code")
	   return false
	}

	// If we've gotten this far, everything's valid!
	return true;
}

function getCookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function setCookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}