// JavaScript Document

function MM_writeCart() {
var userid = getCookie2("_name");
var cart = getCookie("_cart");

if(cart == null)
{
cart = "<center>Items in Cart: 0 ($0.00)</center>";
}

if(userid != null && userid != "Guest")
{
document.write("<p><a href=http://www.theoutdoorsource.com/cgi-bin/commerce.cgi?display=action class=pdgmain><b>" + cart + "</b></a></p>");
}
else
{
document.write("<p><a href=http://www.theoutdoorsource.com/cgi-bin/commerce.cgi?display=action class=pdgmain><b>" + cart + "</b></a></p>");
}
}

function MM_writeLogin() {
var userid = getCookie2("_name");
var cart = getCookie("_cart");

if(cart == null)
{
cart = "Items in Cart: 0 ($0.00)";
}

if(userid != null && userid != "Guest")
{
document.write("<p align=center><font size=2 color=#BF4026><b>Welcome " + userid + "</b></font></p><center><form action=/cgi-bin/commerce.cgi method=post><INPUT type=image src=/PDGCommTemplates/TheOutdoorSourceV5/images/logout.gif alt=Logout name=logout value=Logout></center></form>");
document.write("<p class=left align=center><a href=/cgi-bin/commerce.cgi?displayuser=action>Update Your Contact Information</a>");
}
else
{
document.write("<form action=/cgi-bin/commerce.cgi method=post><p align=center ><font color=#BF4026><nobr><b>User:</b> </font><input type=text name=username size=12></nobr><br><font color=#BF4026><nobr><b>Pass:</b> </font><input type=password name=password size=12></nobr></p><center><INPUT type=image src=/PDGCommTemplates/TheOutdoorSourceV5/images/login.gif alt=Login name=login value=Login></center></form>");
document.write("<p class=left align=center><a href=https://www.theoutdoorsource.com/cgi-bin/commerce.cgi?register=action class=pdgmain>Sign up for an Account</a>");
}
}

function getCookie (cart) {
var dcookie = document.cookie;
var ccart = cart + "=";
var clen = dcookie.length;
var cbegin = 0;
while (cbegin < clen) {
var vbegin = cbegin + ccart.length;
if (dcookie.substring(cbegin, vbegin) == ccart) {
var vend = dcookie.indexOf (";", vbegin);
if (vend == -1) vend = clen;
return unescape(dcookie.substring(vbegin, vend));
}
cbegin = dcookie.indexOf(" ", cbegin) + 1;
if (cbegin == 0)
break;
}
return null;
}

function getCookie2 (name) {
var dcookie = document.cookie;
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
while (cbegin < clen) {
var vbegin = cbegin + cname.length;
if (dcookie.substring(cbegin, vbegin) == cname) {
var vend = dcookie.indexOf (";", vbegin);
if (vend == -1) vend = clen;
return unescape(dcookie.substring(vbegin, vend));
}
cbegin = dcookie.indexOf(" ", cbegin) + 1;
if (cbegin == 0)
break;
}
return null;
}

function MM_load() {
var expdate = new Date ();
expdate.setTime (expdate.getTime() + (1000 * 60 * 60 * 24 * 31));
setCookie ("_cart", "Items in Cart: " + "<!---CURRENT_ITEMS_NUM--->" + " (" +  "<!---CURRENT_SUBTOTAL--->" + ")", expdate);
setCookie2 ("_name", "<!---USERNAME--->", expdate);
}

function setCookie (cart, value, expires) {
if (!expires) expires = new Date();
document.cookie = cart + "=" + escape (value) +
"; expires=" + expires.toGMTString() +  "; path=/";
}

function setCookie2 (name, value, expires) {
if (!expires) expires = new Date();
document.cookie = name + "=" + escape (value) +
"; expires=" + expires.toGMTString() +  "; path=/";
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}

function convertSpaces(frm) {
var str=frm.keywords.value
var out = "\"", flag = 0;
for (i = 0; i < str.length; i++) {
out += str.charAt(i);
}
out += "\""
frm.keywords.value=out;
return true;
}


function OpenCertDetails() {
thewindow = window.open('https://www.thawte.com/core/process?process=public-site-seal-cert-details&public-site-seal-cert-details.referer=http://www.theoutdoorsource.com',  'anew', config='height=400,width=575,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,directories=no,status=yes');
}
function checkrequired(which) {
if (document.my_form.code.value=='') {
return false;
}
else
{
return true;
}
}
function getCookie (cart) {
var dcookie = document.cookie;
var ccart = cart + "=";
var clen = dcookie.length;
var cbegin = 0;
while (cbegin < clen) {
var vbegin = cbegin + ccart.length;
if (dcookie.substring(cbegin, vbegin) == ccart) {
var vend = dcookie.indexOf (";", vbegin);
if (vend == -1) vend = clen;
return unescape(dcookie.substring(vbegin, vend));
}
cbegin = dcookie.indexOf(" ", cbegin) + 1;
if (cbegin == 0)
break;
}
return null;
}
function getCookie2 (name) {
var dcookie = document.cookie;
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
while (cbegin < clen) {
var vbegin = cbegin + cname.length;
if (dcookie.substring(cbegin, vbegin) == cname) {
var vend = dcookie.indexOf (";", vbegin);
if (vend == -1) vend = clen;
return unescape(dcookie.substring(vbegin, vend));
}
cbegin = dcookie.indexOf(" ", cbegin) + 1;
if (cbegin == 0)
break;
}
return null;
}
function MM_load() {
}
function setCookie (cart, value, expires) {
if (!expires) expires = new Date();
document.cookie = cart + "=" + escape (value) +
"; expires=" + expires.toGMTString() +  "; path=/";
}
function setCookie2 (name, value, expires) {
if (!expires) expires = new Date();
document.cookie = name + "=" + escape (value) +
"; expires=" + expires.toGMTString() +  "; path=/";
}

function getReferringCategory() {
   // Still needs more work

   var url=location.search;
   var index=url.lastIndexOf("=");
   var cat=url.substring(index+1);

   document.writeln('<a href=/cgi-bin/commerce.cgi?listcategories=action&parent=' + cat + '>Category Drill Down</a>');

}


var catNum=0;
function writeCategory(catParent, catDesc, catImage, catLink) {
   // Still needs more work

   var url=location.search;
   var index=url.lastIndexOf("=");
   var urlCat=url.substring(index+1);

   if (catImage != '/images/cats/.jpg') {
      if (((catNum%2) == 0) && (catNum !=0)){
         document.write('</tr>');
         document.write('<tr align="center">');
      }
      document.write('   <td align="center" class="pdgmain">');
      document.write('      <table width="100%" cellpadding="2" cellspacing="2" border="0" align="left">');
      document.write('         <tr align="center">');
      document.write('            <td width="50%" align="right" class="pdgmain">');
      document.write('               <a href="' + catLink + '" class="pdgmainlnk" title="'+ catDesc +'"><img src="' + catImage + '" border="0"></a>');
      document.write('            </td>');
      document.write('            <td width="50%" align="left" class="pdgmain">');
      document.write('               <a href="' + catLink + '" class="pdgmainlnk" title="'+ catDesc +'">'+ catDesc +'</a>');
      document.write('            </td>');
      document.write('         </tr>');
      document.write('      </table>');
      document.write('   </td>');
   }
}


function checkrequired(which) {
  if (document.my_form.contactemail.value=='') {
    alert("Please enter an e-mail address.");
    document.my_form.contactemail.focus();
    document.my_form.contactemail.style.backgroundColor = "red";
    return false;
  }
  else
  {
    document.my_form.contactemail.style.backgroundColor = "white";
  }

  if (document.my_form.billfirstname.value=='') {
    alert("Please enter a first name for billing info.");
    document.my_form.billfirstname.focus();
    document.my_form.billfirstname.style.backgroundColor = "red";
    return false;
  }
  else
  {
    document.my_form.billfirstname.style.backgroundColor = "white";
  }

  if (document.my_form.billlastname.value=='') {
    alert("Please enter a last name for billing info.");
    document.my_form.billlastname.focus();
    document.my_form.billlastname.style.backgroundColor = "red";
    return false;
  }
  else
  {
    document.my_form.billlastname.style.backgroundColor = "white";
  }

  if (document.my_form.billstreet1.value=='') {
    alert("Please enter a street address for billing info.");
    document.my_form.billstreet1.focus();
    document.my_form.billstreet1.style.backgroundColor = "red";
    return false;
  }
  else
  {
    document.my_form.billstreet1.style.backgroundColor = "white";
  }

  if (document.my_form.billcity.value=='') {
    alert("Please enter a city for billing info.");
    document.my_form.billcity.focus();
    document.my_form.billcity.style.backgroundColor = "red";
    return false;
  }
  else
  {
    document.my_form.billcity.style.backgroundColor = "white";
  }

  if (document.my_form.billstate.value=='ZZ' && document.my_form.billcountry.value=='461') {
    alert("Please supply a state for billing info.");
    document.my_form.billstate.focus();
    document.my_form.billstate.style.backgroundColor = "red";
    return false;
  }
  else
  {
    document.my_form.billstate.style.backgroundColor = "white";
  }

  if (document.my_form.billzip.value=='' && document.my_form.billcountry.value=='461') {
    alert("Please supply a zip code for billing info.");
    document.my_form.billzip.focus();
    document.my_form.billzip.style.backgroundColor = "red";
    return false;
  }
  else
  {
    document.my_form.billzip.style.backgroundColor = "white";
  }

  if(document.my_form.shiptobilladdr.checked)
  {
    return true;
  }

  if (document.my_form.shipstreet1.value=='') {
    alert("Please enter a street address for shipping info.");
    document.my_form.shipstreet1.focus();
    document.my_form.shipstreet1.style.backgroundColor = "red";
    return false;
  }
  else
  {
    document.my_form.shipstreet1.style.backgroundColor = "white";
  }

  if (document.my_form.shipcity.value=='') {
    alert("Please enter a city for shipping info.");
    document.my_form.shipcity.focus();
    document.my_form.shipcity.style.backgroundColor = "red";
    return false;
  }
  else
  {
    document.my_form.shipcity.style.backgroundColor = "white";
  }

  if (document.my_form.shipstate.value=='ZZ' && document.my_form.shipcountry.value=='461') {
    alert("Please supply a state for shipping info.");
    document.my_form.shipstate.focus();
    document.my_form.shipstate.style.backgroundColor = "red";
    return false;
  }
  else
  {
    document.my_form.shipstate.style.backgroundColor = "white";
  }

  if (document.my_form.shipzip.value=='' && document.my_form.shipcountry.value=='461') {
    alert("Please supply a zip code for shipping info.");
    document.my_form.shipzip.focus();
    document.my_form.shipzip.style.backgroundColor = "red";
    return false;
  }
  else
  {
    document.my_form.shipzip.style.backgroundColor = "white";
  }
}

function domticker(content, divId, divClass, delay, fadeornot){
   this.content=content
   this.tickerid=divId //ID of master ticker div. Message is contained inside first child of ticker div
   this.delay=delay //Delay between msg change, in miliseconds.
   this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over ticker (and pause it if it is)
   this.pointer=1
   this.opacitystring=(typeof fadeornot!="undefined")? "width: 100%; filter:progid:DXImageTransform.Microsoft.alpha(opacity=100); -moz-opacity: 1" : ""
   if (this.opacitystring!="") this.delay+=500 //add 1/2 sec to account for fade effect, if enabled
   this.opacitysetting=0.2 //Opacity value when reset. Internal use.
   document.write('<div id="'+divId+'" class="'+divClass+'"><div style="'+this.opacitystring+'">'+content[0]+'</div></div>')
   var instanceOfTicker=this
   setTimeout(function(){instanceOfTicker.initialize()}, delay)
   }
   domticker.prototype.initialize=function(){
   var instanceOfTicker=this
   this.contentdiv=document.getElementById(this.tickerid).firstChild //div of inner content that holds the messages
   document.getElementById(this.tickerid).onmouseover=function(){instanceOfTicker.mouseoverBol=1}
   document.getElementById(this.tickerid).onmouseout=function(){instanceOfTicker.mouseoverBol=0}
   this.rotatemsg()
   }
   domticker.prototype.rotatemsg=function(){
   var instanceOfTicker=this
   if (this.mouseoverBol==1) //if mouse is currently over ticker, do nothing (pause it)
   setTimeout(function(){instanceOfTicker.rotatemsg()}, 100)
   else{
   this.fadetransition("reset") //FADE EFFECT- RESET OPACITY
   this.contentdiv.innerHTML=this.content[this.pointer]
   this.fadetimer1=setInterval(function(){instanceOfTicker.fadetransition('up', 'fadetimer1')}, 100) //FADE EFFECT- PLAY IT
   this.pointer=(this.pointer<this.content.length-1)? this.pointer+1 : 0
   setTimeout(function(){instanceOfTicker.rotatemsg()}, this.delay) //update container
   }
   }
   // -------------------------------------------------------------------
   // fadetransition()- cross browser fade method for IE5.5+ and Mozilla/Firefox
   // -------------------------------------------------------------------
   domticker.prototype.fadetransition=function(fadetype, timerid){
   var contentdiv=this.contentdiv
   if (fadetype=="reset")
   this.opacitysetting=0.2
   if (contentdiv.filters && contentdiv.filters[0]){
   if (typeof contentdiv.filters[0].opacity=="number") //IE6+
   contentdiv.filters[0].opacity=this.opacitysetting*100
   else //IE 5.5
   contentdiv.style.filter="alpha(opacity="+this.opacitysetting*100+")"
   }
   else if (typeof contentdiv.style.MozOpacity!="undefined" && this.opacitystring!=""){
   contentdiv.style.MozOpacity=this.opacitysetting
   }
   else
   this.opacitysetting=1
   if (fadetype=="up")
   this.opacitysetting+=0.2
   if (fadetype=="up" && this.opacitysetting>=1)
   clearInterval(this[timerid])
}



function writeProductOptions() {
   var x=document.getElementsByName("stylekey");
   try {
      document.write('<option value=nope>Choose an option</option>');
      for(var i=0;i<=x[0].length-1;i=i+1) {
         if("<!---SKU--->" == x[0].item(i).value) {
            var temp1 = x[0].item(i).text;
            var temp2 = new Array();
            temp2 = temp1.split(',');
            document.writeln('<option selected=selected value=' + '"' + x[0].item(i).value + '"' + '>' + temp2[1] + '</option>');
         }
         else {
            var temp1 = x[0].item(i).text;
            var temp2 = new Array();
            temp2 = temp1.split(',');
            document.writeln('<option value=' + '"' + x[0].item(i).value + '"' + '>' + temp2[1] + '</option>');
         }
      }
   }
   catch(err) {
   }

   var y=document.getElementsByName("newDDL");
   var value = y[0].value;
   if(y[0].length == 1) {
      var z = document.getElementById("styleHid");
      z.className = 'hidden';
   }
}

function writeProductOptions2() {
   var x=document.getElementsByName("stylekey");
   document.write('<option>Choose an option</option>');
   for(var i=0;i<=x[0].length-1;i=i+1) {
     document.write('<option>' + x[0].item(i).value + '</option>');
   }
}

function MM_checkForStyle() {
var y=document.getElementsByName("qtyDDL");
var z=document.getElementsByName("qty");
z[0].value = y[0].value;
var x=document.getElementsByName("newDDL");
var value = x[0].value;
if("<!---INV--->" == "")
{
alert("Please select a style option");
return false;
}
else if("<!---INV--->" == "Out of Stock")
{
alert("The style option you selected is currently unavailable");
return false;
}
}

function writeAvail() {
   // old code that I have commented out
   //if("<!---INV--->" == "Available" || "<!---INV--->" == "Out of Stock")
   //{
   //document.write("<!---INV--->");
   //}
   //else
   //{
   //document.write("Please select an option below");
   //}

   if ("<!---INV--->" == "") {
      document.write("Please select an option below");
   }
   else {

      if(Number("<!---INV--->") > 0) {
         document.write("<!---INV---> Available");
      }
      else {
         document.write("Out of Stock");
      }
   }
}


function writeQuantityOptions() {

   if(Number("<!---INV--->") > 0) {
      document.writeln('<p>');
      document.writeln('   <b>Quantity:</b>');
      document.writeln('   <br>');
      document.writeln('   <select name="qtyDDL">');
      for(var quantity=1;quantity<=Number("<!---INV--->");quantity=quantity+1) {
         document.writeln('<option value=' + '"' + quantity + '"' + '>' + quantity + '</option>');
      }
      document.writeln('   </select>');
      document.writeln('</p>');
   }

}



function MM_jumpToProduct() {
var x=document.getElementsByName("newDDL");
var value = x[0].value;
if(value == "nope")
{
}
else
{
window.location="http://www.theoutdoorsource.com/cgi-bin/commerce.cgi?preadd=action&key=" + value;
}
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}


function resetaddr(){
document.upsvaladdr.newshipcity.value="";
document.upsvaladdr.newshipzip.value="";
}
function setnewaddr(){
if(document.upsvaladdr.newshipcity.value!="")
document.upsvaladdr.shipcity.value=document.upsvaladdr.newshipcity.value;
if(document.upsvaladdr.newshipstate.value!="ZZ")
document.upsvaladdr.shipstate.value=document.upsvaladdr.newshipstate.value;
if(document.upsvaladdr.newshipzip.value!="")
document.upsvaladdr.shipzip.value=document.upsvaladdr.newshipzip.value;
}
function selectnewradio(){
document.upsvaladdr.addrvalselect[1].checked=true;
}
function setnewvalues(){
if(document.upsvaladdr.addrvalselect[1].checked==true){
setnewaddr();
}
}
function bypassaddrval(){
document.upsvaladdr.bypassvalidation.value="yes";
document.upsvaladdr.addrvalselect[0].checked=true;
}
function resetaddrval(){
document.upsvaladdr.bypassvalidation.value="no";
}


function writeTitleSaleHighlight() {

   if ("<!---DISCOUNT_AMOUNT--->" != "") {
      document.write('SAVE <!---DISCOUNT_AMOUNT--->!');
   }
}


function writeOriginalPrice() {

   if ("<!---ORIGINAL_PRICE--->" != "") {
      document.write('      <!---TEXT_ORIGINAL_PRICE---><br><!---ORIGINAL_PRICE--->');
   }

}


function writeDiscountedAmount() {

   if ("<!---DISCOUNT_AMOUNT--->" != "") {
      document.write('      <!---TEXT_DISCOUNT_AMOUNT---><br><!---DISCOUNT_AMOUNT--->');
   }
}


function writePercentOff() {

   var discountAmountText="<!---DISCOUNT_AMOUNT--->";
   var originalPriceText="<!---ORIGINAL_PRICE--->";

   if ((discountAmountText != "") && (originalPriceText != "")) {

      var discountAmount=Number(discountAmountText.substr(1));
      var originalPrice=Number(originalPriceText.substr(1));

      if (originalPrice > 0) {
         var percentOff=(discountAmount/originalPrice)*100;
         if (percentOff.toFixed(0) == percentOff) {
            document.write(percentOff + '% OFF! <br>');
         }
      }
   }
}



function writeNotAvailableImage() {

   /* if the image doesn't exist, write the appropriate Not Available image */

}


