// jshop.js JavaScript Development by JavaScriptDesign.com
// email solutions@javascriptdesign.com  Copyright 2000
// All Rights Reserved. Copyright Notice Must Remain in
// Source files and Catalog Pages.

// ****Begin code section for catalog display****

// **Establish array variables. These must match the**
// **catalog array constructionin each page.**

// LAST EDITED 11052002

var catNo = new Array(); // The catalog number
var itemName = new Array(); // The item name
var itemDesc = new Array(); // The item description
var price = new Array(); // The item price
var ordQty = new Array(); // Array to store order qty
var ordQty2 = new Array(); // Array to store order qty KOE
var Pic01 = new String();
var Pic02 = new String();
var Pic03 = new String();
var Pic04 = new String();
var Pic05 = new String();
//var Logo1 = new Array(); // ladataan aktiiviselta sivulta
var tarjous = new Array(); // ladataan aktiiviselta sivulta
var tarjouslogo = new Array(); // ladataan aktiiviselta sivulta
var logo = new Array(); // selitteet
var Opt01 = new Array(); //muuttujat option arvoille

// määritellään muuttujat option valinnoille by SO
function loadOpts(num, aOpt) {
	Opt01[num] = aOpt;
		}

function loadTeksti(num, teksti) {
	tarjous[num] = teksti;
		}
		
function loadsale(num, sale) {
	tarjouslogo[num] = sale;
		}
// määritellään muuttujat kuville by SO

function loadPics(num, aPic, bPic, cPic, dPic, ePic) {
	Pic01[num] = aPic;
	Pic02[num] = bPic;
	Pic03[num] = cPic;
	Pic04[num] = dPic;
	Pic05[num] = ePic;
		}

//varmuuskopio: function loadLogo(LogoPic) {
//	return Logo1 = LogoPic;
//		}

// **Object constructor to build the catalog array.**
// **Catalog entries are in an array on each page.**

function loadCatalog(num,iNum,iName,iDesc,cost, logot) {
 catNo[num] = iNum;
 itemName[num] = iName;
 itemDesc[num] = iDesc;
 price[num] = cost;
 logo[num] = logot;
}

// **Function to add the respective image of product by SO.**

function getImage() {
if (i < "20") 
  document.write('<td align="center" valign="middle"></td></tr>');
  	else if (i == "1")
   document.write('<td align="center" valing="middle"></td></tr>');
     else if (i == "2")
   document.write('<td align="center" valing="middle"></td></tr>');
      else if (i == "3")
   document.write('<td align="center" valing="middle"></td></tr>');
     else if (i == "4")
   document.write('<td align="center" valing="middle"></td></tr>');
   //return;
   }
   
// **Function to write the content for each table row.**
// **This reads the array data and populates a table **
// **for catalog display. 
 
function writeTableRow(i) {
 document.write('<tr>' // poistettu nimikkeen koodi: ' + itemName[i] + '<br>' + loadLogo(Logo1) + '
 + '<td bgcolor="#333333"></td><td bgcolor="#333333" align="left" valign="middle" class="leipis">' + itemDesc[i] + '</td><td bgcolor="#333333" align="left"  valign="middle" class="leipis">' + price[i] + ' <br> ' + tarjous[i] + '</td>' // 010908 poistettu item desc perästä logon lataus <br>' + logo[i] + '
 + '<td bgcolor="#333333" align="center" valign="middle">' + tarjouslogo[i] + '</td>' // lisätty td 29072003
  + '<td bgcolor="#333333" align="left"  valign="middle" class="leipis"><input style="width: 8mm" type="text" size="2" value="" name="q2'+i+'"></td>' // lisätty td 29072003
 + '<td bgcolor="#333333" align="center"  valign="middle" class="fwhite10bold">' + Opt01[i] + '</td>' // lisätty td 010808
  + '<td bgcolor="#333333" align="left" valign="middle" class="leipis">'
 // + '<a href="javascript:addItem2(' + i + ')"><img src="src/addcart.gif" border="0">Lisää</td></a><td bgcolor="#252525"></td></tr><tr><td background="src/whitedot.gif" width="356" height="1" colspan="8"></td></tr>'); //KOE VAIHDETTU <img src="src/tocart.gif" border="0" alt="Lisää tuote ostoskoriin">
+ '<a href="javascript:addItem2(' + i + ')"><img src="src/addcart.gif" border="0" alt="Lisää ostoskoriin">Lisää</td></a><td bgcolor="#333333"></td></tr><tr><td background="src/whitedot.gif" width="356" height="1" colspan="8"></td></tr>');

 
 // getImage();
 }

// **Function to write the catalog display table to **
// **the current document.**

function writeTable() {
 document.write('<form name="form1"><table border="0" cellpadding="0" cellspacing="0" width="390"> '
  + '<tr><td><img src="src/vaskulma.gif" width="6"></td><td background="src/orange.gif" bgcolor="#FF9933" class="fwhite10boldwhite" width="183">Tuotenimi</td>'
  + '<td background="src/orange.gif" bgcolor="#FF9933" width="45" class="fwhite10boldwhite">Hinta</td>'
  + '<td background="src/orange.gif" class="fwhite10boldwhite"></td>' //tarjous hinta
  + '<td background="src/orange.gif" align="center" bgcolor="#FF9933" width="35" class="fwhite10boldwhite">Määrä</td>'
 + '<td background="src/orange.gif" align="center" bgcolor="#FF9933" width="65" class="fwhite10boldwhite">Saatavuus</td>' //KOE VAIHDETTU height="1" bgcolor="FF9933" 
  + '<td background="src/orange.gif" bgcolor="#FF9933" width="50" align="center" class="fwhite10boldwhite">Osta</td><td><img src="src/oikyla.gif" width="6"></td>'
  + '</tr><tr><td colspan="8" width="390" height="1" bgColor="#000000"><img src="src/emp.gif" height="1"></td></tr>');
 for (i=0; i < catNo.length; i++)
  writeTableRow(i);
 document.write('</table></form>');
}



// ****End code section for writing catalog content.****


// ****Begin shared cookie functions****

// **Global expdate variable for cookies**
// **Cookie is set to expire in 24 hours**

var expdate = new Date()
expdate.setTime (expdate.getTime() + (1 * 24 * 60 * 60 * 1000))

// **Read cookie data.**

function getCookieData(name) {
	var label = name + "="
	var labelLen = label.length
	var cLen = document.cookie.length
	var i = 0
	while (i < cLen) {
		var j = i + labelLen
		if (document.cookie.substring(i,j) == label) {
			var cEnd = document.cookie.indexOf(";",j)
			if (cEnd == -1) {
				cEnd = document.cookie.length
			}
			//return unescape(document.cookie.substring(j,cEnd))
			return decodeURIComponent(document.cookie.substring(j,cEnd))
		}
		i++
	}
	return ""
}

// **Write cookie data**

function setCookieData(name,value,expires) {
	counter++;
	//document.cookie = name + "=" + counter + "@" + value + "; expires=" + expires;
	value = encodeURIComponent(value);
	document.cookie = name + "=" + counter + "@" + value + "; expires=" + expires;
}

// **Kill cookie function. When the order is submitted**
// **the cookie is killed via an event handler call.**

function killCookie(name) {
 if (getCookieData(name)) {
 document.cookie = name + "=" + "; expires = Thu, 01-Jan-70 00:00:01 GMT";
 cookData = ""
 counter = 0
// history.go(0)
 }
}

// set Name-Value cookie attribute
function setNVCookieAttribute(name, value) {
  // add "ScartNV." prefix to a cookie name
  document.cookie = "ScartNV." + name + "=" + encodeURIComponent(value) + "; expires=" + expdate.toGMTString();
}

// get Name-Value cookie hashmap
function getNVCookies() {
  // parse all "ScartNV.*" cookies to hashmap, drop prefix from the name part
  var value;
  var map = new Object();

  var cLen = document.cookie.length;
  if (cLen > 0) {
    var items = document.cookie.split(";");
    for(var idx=0; idx < items.length; idx++) {
      var iDelim = items[idx].indexOf("=");
      if (iDelim > 0) {
        var name = trim(items[idx].substring(0, iDelim));
        if (name.length > 8 && name.indexOf("ScartNV.", 0) == 0) {
          name = trim(name.substring(8)); // drop prefix
          var value= trim(items[idx].substring(iDelim+1));
          map[name] = decodeURIComponent(value);
        }
      }
    }
  }

  return map;
}

// kill all Name-Value cookies
function killNVCookies() {
  var nvCookies = getNVCookies();
  for(var name in nvCookies) {
    name = "ScartNV." + name;
    document.cookie = name + "=" + "; expires = Thu, 01-Jan-70 00:00:01 GMT";    
  }
}


// ****End shared cookie functions****


// ****Begin code section to update the cookie to ****
// ****add items to the shopping cart cookie 'Scart'.****

// **Global variables.**

var counter = 0
var cookData = ""

// **Extract current value of cookie when page loads **
// **and store the values in the global variables.**

if (getCookieData("Scart")) {
 orderString = getCookieData("Scart")
 cLen = orderString.length
 countEnd = orderString.indexOf("@")
 pointer = countEnd + 1
 counter = orderString.substring(0,countEnd)
 cookData = orderString.substring(pointer,cLen)
}

// **Function *to add an item to the shopping cart cookie. KOE

function addItem(num) {
 a = eval("itemDesc[num]") // vaihdettu catNo[num] -> itemDesc[i]
 ordQty[num]=eval("document.form1.q"+num+".value");
 b = eval("ordQty2[num]")
 if (ordQty[num] == 0) {
 alert("Et antanut linssin väriä, onko oikein?");
 
 return;
  }
 }
 

// **Function to add an item to the shopping cart cookie.**

function addItem2(num) {
 a = eval("itemDesc[num]") // vaidettu catNo[num] => 
 ordQty2[num]=eval("document.form1.q2"+num+".value");
 b = eval("ordQty2[num]")
 
// OTETTU POIS KÄYTÖSTÄ LINSSIN VÄRIN LISÄÄMISEN TAKIA addItem(num); // LISÄÄ KEKSIIN LINSSIN VÄRIN
 
 if (ordQty2[num] == 0) {
  alert("Määrä kenttä on tyhjä");
  return;
 }
 
  for (var i = 0; i < b.length; i++) {
  var oneChar = b.substring(i, i + 1)
  if (oneChar < "0" || oneChar > "9") {
   alert("Tilattavat määrät pitää olla numeroita ja määrä kokonaisnumeroita")
  eval("document.form1.q2"+num+".value = ''")
   return;
	}
  }
// return true;
 //if (confirm("Lisätäänkö tuote " + a + " ostoskoriin?")) 
 {addCart = '' + catNo[num] + '`' + ordQty2[num] + '*' + itemName[num] + ' ' + itemDesc[num] + ' ' + '~' + price[num] + '^' //KOE LISÄTTY '?' + ordQty2[num] + // a korvattu catNo[num]
cookData += eval("addCart")
setCookieData("Scart", cookData, expdate.toGMTString())
confirm(" Olet lisännyt seuraavan tuotteen ostoskoriin:\n\n " + a + "  \n\n Jos haluat siirtyä ostoskoriin, paina sivun oikeassa\r yläreunassa olevaa 'OSTOSKORI' linkkiä.")
 }
}

// ****End code section for updating the cookie.****

// ****Function to reset the form elements everytime the page loads
// ****Remove th onLoad() event handler from the catalog body tags
// ****to eliminate this feature.

function clearQtys() {
 document.form1.reset()
}

// ****Debugging function to display cookie contents.****

function showMe() {
 display = getCookieData("Scart");
 alert(display)
}



//***********************************************************************
//***********************************************************************

// trim
function trim(strVal) {
  var iStart = 0;
  var c;
  while(iStart < strVal.length) {
     c = strVal.charAt(iStart);
     if (c == ' ' || c == '\r' || c == '\n' || c == '\t')
        iStart++;
     else
        break;
  }

  if (iStart >= strVal.length) { return ""; }

  var iEnd = strVal.length-1;
  while(iEnd >= 0) {
     c = strVal.charAt(iEnd);
     if (c == ' ' || c == '\r' || c == '\n' || c == '\t')
        iEnd--;
     else
        break;
  }
  iEnd++;

  // iStart inclusive, iEnd exclusive index
  return strVal.substring(iStart, iEnd);  
}
