//Utilities
function replaceAll (streng, soeg, erstat)
{ 
	var st = streng;
	if (soeg.length == 0)
	 return st;
	var idx = st.indexOf(soeg);
	while (idx >= 0)        
	{  st = st.substring(0,idx) + erstat + st.substr(idx+soeg.length);
	 idx = st.indexOf(soeg);
	}
	return st;
}

function emailCheck (emailStr) { 
var checkTLD=0; 
var knownDomsPat=/ ^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/; 
var emailPat=/^(.+)@(.+)$/; 
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]"; 
var validChars="\[^\\s" + specialChars + "\]"; 
var quotedUser="(\"[^\"]*\")"; 
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/; 
var atom=validChars + '+'; 
var word="(" + atom + "|" + quotedUser + ")"; 
var userPat=new RegExp("^" + word + "(\\." + word + ")*$"); 
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$"); 
var matchArray=emailStr.match(emailPat); 
if (matchArray==null) { 
//alert("The Email Address Is Invalid"); 
return false; 
} 
var user=matchArray[1]; 
var domain=matchArray[2]; 
for (i=0; i<user.length; i++) { 
if (user.charCodeAt(i)>127) { 
//alert("The Username Contains Invalid Characters."); 
return false; 
} 
} 
for (i=0; i<domain.length; i++) { 
if (domain.charCodeAt(i)>127) { 
//alert("Ths Domain Name Contains Invalid Characters."); 
return false; 
} 
} 
if (user.match(userPat)==null) { 
//alert("The Username Is Invalid."); 
return false; 
} 
var IPArray=domain.match(ipDomainPat); 
if (IPArray!=null) { 
for (var i=1;i<=4;i++) { 
if (IPArray>255) { 
//alert("The Destination IP Address Is Invalid."); 
return false; 
} 
} 
return true; 
} 
var atomPat=new RegExp("^" + atom + "$"); 
var domArr=domain.split("."); 
var len=domArr.length; 
for (i=0;i<len;i++) { 
if (domArr[i].search(atomPat)==-1) { 
//alert("The Domain Name Is Invalid."); 
return false; 
} 
} 
if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) { 
//alert("The Domain Name Extension Is Invalid"); 
return false; 
} 
if (len<2) { 
//alert("The Address Is Missing A Hostname."); 
return false; 
}
return true;
} 

//email form
function submitForm()
{
	document.frmMain.submit();
}

//email newsletter form
function submitNewsletterForm()
{
	document.frmNewsletter.submit();
}

//tell a friend form
function submitFriendForm()
{
	document.frmFriend.submit();
}

//functions to build HTML for navigation links
var strPath = unescape(location.href);
var strPage = strPath.substring(strPath.lastIndexOf('/') + 1);

function buildLinkHTML(strURL,strDisplay,strLinkClass,blnUseParentURL)
{
	var strReturn;
	strReturn = buildLinkHTML(strURL,strDisplay,strLinkClass,blnUseParentURL,"nav_text_current");
	document.write (strReturn);
}

function buildLinkHTML(strURL,strDisplay,strLinkClass,blnUseParentURL,strVisitedClass)
{
	var strReturn;
	var strPageName;
	var strURLName;
	var strURLIndexName;
	
	if (blnUseParentURL == 'T')
	{
		strPageName = strPage.replace('.html','');
		strURLName = strURL.replace('.html','');
		strURLIndexName = 'index';
	}
	else
	{
		strPageName = strPage;
		strURLName = strURL;
		strURLIndexName = 'index.html';
	}

	//alert("strURLName = " + strURLName + " : strPageName = " + strPageName);
	
	if (strPageName.indexOf(strURLName) >= 0 || (strPageName == "" && strURLName == "index")) 
	{
		strReturn = "<a href=" + strURL + " class=" + strLinkClass + " target='_top'><span class=" + strVisitedClass+ ">" + strDisplay + "</span></a>";
	}
	else
	{
		strReturn = "<a href=" + strURL + " class=" + strLinkClass + " target='_top'>" + strDisplay + "</a>";
	}
	//alert(strReturn);
	document.write (strReturn);

}

function buildSpacerHTML()
{
	document.write ("&nbsp;&nbsp|&nbsp;&nbsp");
}

function buildSpacerImgHTML()
{
	//document.write ("&nbsp;&nbsp<img class='xxsmall' src='images/flower2.gif'>&nbsp;&nbsp");
	//buildSpacerImgPathHTML("images/flower2.gif","xxsmall");
	buildSpacerImgPathHTML("images/icon_dot_purple.gif","xxsmall");
}

function buildSpacerImgPathHTML(strImg, strClass)
{
	document.write ("&nbsp;&nbsp<img class='" + strClass + "' src='" + strImg + "'>&nbsp;&nbsp");
}

function buildNewLineHTML()
{
	document.write ("<br>");
}

//functions to parse querystring
function QueryString(key)
{
	var value = "";
	for (var i=0;i<QueryString.keys.length;i++)
	{
		if (QueryString.keys[i]==key)
		{
			value = QueryString.values[i];
			break;
		}
	}
	return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse()
{
	var query = window.location.search.substring(1);
	var pairs = query.split("&");
	
	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			value = replaceAll(value,"+"," ");
			value = replaceAll(value,"%3A",":");
			value = replaceAll(value,"%2F","/");
			value = replaceAll(value,"%29",")");
			value = replaceAll(value,"%28","(");
			value = replaceAll(value,"%26","&");

			QueryString.keys[QueryString.keys.length] = argname;
			QueryString.values[QueryString.values.length] = value;		
		}
	}
}

QueryString_Parse();

//Function to insert tell a friend feature.
function tellFriendHTML(url,product)
{
	document.write('<tr><td align=right colspan=5>');
	document.write('<a class=tellfriend href="javascript:submitForm();"><img src="images/envelope.bmp">&nbsp;Tell a friend</a>');
	//document.write('<a class=tell href="javascript:submitForm();"><img src="images/btn_emailafriend.gif"></a>');
	document.write('<form name=frmMain method=get action="tellafriend.html">');
	document.write('<input type=hidden name=url value="');
	document.write(url);
	document.write('"></input>');
	document.write('<input type=hidden name=product value="');
	document.write(product);
	document.write('"></input>');
	document.write('</form></td></tr>');
}

function getCurrentURL(staticURL)
{
	if (staticURL != '')
	{
		return staticURL;
	}
	else
	{
		return window.document.location.toString();
	}
}

function sendConfirm()
{
	alert("in here");
	//alert(document.frmMain.recipient.value());
	document.frmConfirm.recipient.value = "order@thebabyloft.com";
	document.frmConfirm.submit();
}

//All cookie functions
function Get_Cookie(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 Set_Cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function Delete_Cookie(name,path,domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

var today = new Date();
var zero_date = new Date(0,0,0);
today.setTime(today.getTime() - zero_date.getTime());

var todays_date = new Date(today.getYear(),today.getMonth(),today.getDate(),0,0,0);
var expires_date = new Date(todays_date.getTime() + (8 * 7 * 86400000));

function storeMasterCookie() {
    if (!Get_Cookie('MasterCookie'))
        Set_Cookie('MasterCookie','MasterCookie');
}

function storeIntelligentCookie(name,value) {
    if (Get_Cookie('MasterCookie')) {
        var IntelligentCookie = Get_Cookie(name);
        if ((!IntelligentCookie) || (IntelligentCookie != value)) {
            Set_Cookie(name,value,expires_date);
            var IntelligentCookie = Get_Cookie(name);
            if ((!IntelligentCookie) || (IntelligentCookie != value))
                Delete_Cookie('MasterCookie');
        }
    }
}

//Resizing DIV

function getPixelsNumeric(strHTMLPixelValue)
{
	var strReturn;
	var lngStrLen;
	
	strReturn = strHTMLPixelValue + "";
	lngStrLen = strReturn.length;
	
	if (strReturn.substr(lngStrLen - 2, 2) == "px")
	{
		strReturn = strReturn.substr(0, lngStrLen - 2);
	}
	
	return strReturn;
}
/*
function resize()
{
	var lngBodyHeight = getPixelsNumeric(document.body.offsetHeight) - 78;
	if (lngBodyHeight < 300)
	{
		lngBodyHeight = 300;
	}

	document.all["mainDiv"].style.height = lngBodyHeight;
}

function window.onload()
{
	resize();	
}						

function window.onresize()
{
	resize();
}
*/
//For Slings
function buyNow()
{
	alert("Please select one of the ranges to access order information");
}

function changePriceVcd(price)
{
	if (document.order.ADDITIONALINFO2.value=='With Demo VCD')
	{
		document.order.PRICE.value = price;
	}
	else
	{
		document.order.PRICE.value = price-5;
	}
}

function changePriceSleepSack(price)
{
	if (document.order.ADDITIONALINFO2.value=='No Embroidery')
	{
		document.order.PRICE.value = price;
		babyname.style.visibility = "hidden"; 
		document.order.ADDITIONALTEXTINFO.value="";
	}
	else
	{
		document.order.PRICE.value = price+8;
		babyname.style.visibility = "visible"; 
	}	
}

function setGiftCertPrice()
{
	document.order.PRICE.value=document.order.ADDITIONALINFO.value;	
}

function changePriceSleepSackSize()
{
	if (document.order.ADDITIONALINFO2.value=='0-6 months')
	{
		document.order.PRICE.value = 125.90;
	}
	else if (document.order.ADDITIONALINFO2.value=='6-12 months')
	{
		document.order.PRICE.value = 132.90; 
	}		
	else if (document.order.ADDITIONALINFO2.value=='12-24 months')
	{
		document.order.PRICE.value = 142.90; 
	}	
	else if (document.order.ADDITIONALINFO2.value=='24-36 months')
	{
		document.order.PRICE.value = 155.90; 
	}	
}
