function fixWin(){
	// This fix requires the following in the body tag... style="width:100%"
	if (document.body.style.width) {
//		alert((document.body.style.width))
		document.body.style.width=parseFloat(document.body.style.width) - 0.001 + "%";
	}
	//document.body.style.height=parseFloat(document.body.style.Height) - 0.001 + "%";
}

function expandCollapse(section) {
	if(document.all) {
		if(document.all[section].style.display == "none" || document.all[section].style.display == "") {
			if(navigator.appName.indexOf("Microsoft") > -1){
				document.all[section].style.display = "block";
			} else {
				document.all[section].style.display = "table-row";
			}
		} else {
			document.all[section].style.display = "none";
		}
	} else {
		if(document.getElementById(section).style.display == "none" || document.getElementById(section).style.display == "") {
			if(navigator.appName.indexOf("Microsoft") > -1){
				document.getElementById(section).style.display = "block";
			} else {
				document.getElementById(section).style.display = "table-row";
			}
		} else {
			document.getElementById(section).style.display = "none";
		}
	}
	fixWin();
}

function expand(section) {
	if(document.all) {
		if(navigator.appName.indexOf("Microsoft") > -1){
			document.all[section].style.display = "block";
		} else {
			document.all[section].style.display = "table-row";
		}
	} else {
		if(navigator.appName.indexOf("Microsoft") > -1){
			document.getElementById(section).style.display = "block";
		} else {
			document.getElementById(section).style.display = "table-row";
		}
	}
	fixWin();
}

function collapse(section) {
	if(document.all) {
		document.all[section].style.display = "none";
	} else {
		document.getElementById(section).style.display = "none";
	}
	fixWin();
}

function divShow(section) {
	if(document.all) {
		document.all[section].style.visibility = "inherit";
	} else {
		document.getElementById(section).style.visibility = "inherit";
	}
}

function divHide(section) {
	if(document.all) {
		document.all[section].style.visibility = "hidden";
	} else {
		document.getElementById(section).style.visibility = "hidden";
	}
}

function sendToFriend(theBook) {
	window.location.href = "priv_send-to-friend.asp?b=" + theBook
}

function pageLoaded() {
	MM_preloadImages(	'images/butt_contacts_over.gif',	'images/butt_download_over.gif',	'images/butt_why_over.gif',	'images/butt_faqs_over.gif',	'images/butt_gallery_over.gif',	'images/butt_header_login_over.gif',	'images/butt_header_logout_over.gif',	'images/butt_header_register_over.gif',	'images/butt_home_over.gif',	'images/butt_pricing_over.gif',	'images/butt_priv_logout_over.gif',	'images/butt_priv_upload_over.gif',	'images/butt_priv_your-books_over.gif',	'images/butt_priv_your-items_over.gif',	'images/butt_priv_your-profile_over.gif',	'images/butt_register_over.gif',	'images/butt_resellers_over.gif',	'images/butt_tips_over.gif')
}

function signup() {
	signupWindow=window.open('signup_popup.asp','signupWindow','width=340,height=260,toolbar=no,scrollbars=no,directories=no,location=no,menubar=no,status=no,resizable=yes');
	signupWindow.focus()
}

function openLegals() {
	legalsWindow=window.open('terms_site_privacy.asp','legalsWindow','width=600,height=600,toolbar=no,scrollbars=yes,directories=no,location=no,menubar=no,status=no,resizable=yes');
	legalsWindow.focus()
}

function openPrintLegals() {
	legalsWindow=window.open('terms_print.asp','legalsWindow','width=600,height=600,toolbar=no,scrollbars=yes,directories=no,location=no,menubar=no,status=no,resizable=yes');
	legalsWindow.focus()
}

function openCompetitionLegals() {
	legalsWindow=window.open('terms_competition.asp','legalsWindow','width=600,height=600,toolbar=no,scrollbars=yes,directories=no,location=no,menubar=no,status=no,resizable=yes');
	legalsWindow.focus()
}

function openResellerLegals() {
	legalsWindow=window.open('terms_reseller.asp','legalsWindow','width=600,height=600,toolbar=no,scrollbars=yes,directories=no,location=no,menubar=no,status=no,resizable=yes');
	legalsWindow.focus()
}

function previewBook(books_id, doctype) {
	if(doctype=='Calendar'){
		alert('Preview not currently available for Calendars')
	}else if(doctype=='GiftCard'){
		alert('Preview not currently available for Gift Cards')
	}else{
		previewWindow = window.open('priv_preview.asp?b=' + books_id,'previewWindow','width=680,height=340,toolbar=no,scrollbars=no,directories=no,location=no,menubar=no,status=no,resizable=yes')
		previewWindow.focus()
	}
}

function previewBookPublic(books_id, doctype) {
	if(doctype=='Calendar'){
		alert('Preview not currently available for Calendars')
	}else if(doctype=='GiftCard'){
		alert('Preview not currently available for Gift Cards')
	}else{
		previewWindow = window.open('public_preview.asp?b=' + books_id,'previewWindow','width=680,height=340,toolbar=no,scrollbars=no,directories=no,location=no,menubar=no,status=no,resizable=yes')
		previewWindow.focus()
	}
}

function openPreviewColour() {
	colourSelect=window.open('hardcover_colours.asp','colourSelect','width=680,height=530,toolbar=no,scrollbars=yes,directories=no,location=no,menubar=no,status=no,resizable=yes');colourSelect.focus()
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
  if (n <= 0)
     return "";
  else if (n > String(str).length)
     return str;
  else {
     var iLen = String(str).length;
     return String(str).substring(iLen, iLen - n);
  }
}

function fixCurrency(theAmount) {
	theAmount = Math.round(parseFloat(theAmount) * 100)/100
	if (theAmount.toString().indexOf(".") > -1) {
		return "A$ " + Left(theAmount.toString() + "00",theAmount.toString().indexOf(".") + 3)
	} else {
		return "A$ " + theAmount.toString() + ".00"
	}
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
}
function isFloat(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if ((c < "0") || (c > "9") && (c!=".")) return false;
    }
    // All characters are numbers.
    return true;
}

// #### THE FOLLOWING FUNCTIONS ARE FOR DATE VALIDATION #############
// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
  // EXCEPT for centurial years which are not also divisible by 400.
  return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}
function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	day=parseInt(strDay)
	month=parseInt(strMonth)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd-mm-yy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}
// #################### END DATE VALIDATION #############

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	 if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;	if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

