function createXHR() {
 var request = false;
 try {
  request = new ActiveXObject('Msxml2.XMLHTTP');
 }
 catch (err2) {
  try {
   request = new ActiveXObject('Microsoft.XMLHTTP');
  }
  catch (err3) {
   try {
    request = new XMLHttpRequest();
   }
   catch (err1) {
    request = false;
   }
  }
 }
 return request;
}

function mailCheck(str) {
 var at = "@";
 var dot = ".";
 var lat = str.indexOf(at);
 var lstr = str.length;
 var ldot = str.indexOf(dot);
 if (str.indexOf(at) == -1) { return false; }
 if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) { return false; }
 if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) { return false; }
 if (str.indexOf(at,(lat+1)) != -1) { return false; }
 if (str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot) { return false; }
 if (str.indexOf(dot,(lat+2)) == -1) { return false; }
 if (str.indexOf(" ") != -1) { return false; }
 return true;
}

function sendMail() {
 var xhr = createXHR();
 var url = "contents/contact.php";
 var variables = "Name: " + document.contact.name.value + "\r\n";
 variables += "Company: " + document.contact.company.value + "\r\n";
 variables += "E-mail: " + document.contact.email.value + "\r\n";
 variables += "Phone: " + document.contact.phone.value + "\r\n";
 variables += "Comment: " + document.contact.comment.value + "\r\n";
 var content = "action=send&content=" + variables;
 var storing = document.getElementById("contact");
 var errormsg = document.getElementById("errmsg");
 xhr.onreadystatechange = function() { 
  if(xhr.readyState == 4) {
   // do nothing
  }
 }; 
 xhr.open("POST", url, true);  
 xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 if (mailCheck(document.contact.email.value) && document.contact.name.value != null && document.contact.name.value != "" && document.contact.name.value != " ") {
  xhr.send(content); 
  storing.innerHTML = "<div style=\"text-align:center;padding:16px;height:400px;\">Thank you!</div>";
 } else {
  errormsg.innerHTML = "<div style=\"text-align:center;padding:16px;color:#f00;\">Please enter a valid name and&#47;or e-mail address.</div>";
 }
}

function createCookie(name,value,days) {
 if (days) {
  var date = new Date();
  date.setTime(date.getTime()+(days*24*60*60*1000));
  var expires = "; expires="+date.toGMTString();
 }
 else var expires = "";
 document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
 var nameEQ = name + "=";
 var ca = document.cookie.split(';');
 for(var i=0;i < ca.length;i++) {
  var c = ca[i];
  while (c.charAt(0)==' ') c = c.substring(1,c.length);
  if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
 }
 return null;
}
function eraseCookie(name) {
 createCookie(name,"",-1);
}

function cart(action, id, attr) {
 if (action == "add") {
  current_attr = window.document.getElementById("option_"+id).value;
  var oldcookie = readCookie("store");
  if (oldcookie == null) {
   createCookie("store", id + "-" + current_attr);
  } else {
   createCookie("store", oldcookie + "," + id + "-" + current_attr);
  }
  window.document.location = 'index.php?p=store&cart=show';
 }
 if (action == "show") {
  window.document.location = 'index.php?p=store&cart=show';
 }
 if (action == "remove") {
  var cid = id + "-" + attr;
  var oldcookie = readCookie("store");
  if (oldcookie == null) {
   oldcookie = "";
  } else {
   var items = new Array();
   items = oldcookie.split(",");
   var l = items.length-1;
   for (k=l; k>=0; k--) {
    if (items[k] == cid) {
     items.splice(k, 1);
    }
   }
  }
  oldcookie = "";
  for (j=0; j<items.length; j++) {
   if (j == 0) {
    oldcookie += items[j];
   } else {
    oldcookie += "," + items[j];
   }
  }
  createCookie("store", oldcookie);
  window.document.location = 'index.php?p=store&cart=show';
 }
 if (action == "option") {
  if (attr != null) {
   nextattr = 0;
   nextoption = window.document.getElementById("option_"+id+"_"+nextattr);
   while (nextoption != null) {
    window.document.getElementById("option_"+id+"_"+nextattr).setAttribute("class", "");
    nextattr++;
    nextoption = window.document.getElementById("option_"+id+"_"+nextattr);
   }
   window.document.getElementById("option_"+id+"_"+attr).setAttribute("class", "shopoptions_selected");
   window.document.getElementById("option_"+id).value = attr;
  }
 }
 if (action == "update") {
  var cid = id + "-" + attr;
  var oldcookie = readCookie("store");
  if (oldcookie == null) {
   oldcookie = "";
  }
  var new_quantity = window.document.getElementById("change_quantity_"+id+"_"+attr).value;
  var anum = /(^\d+$)|(^\d+\.\d+$)/;
  if (anum.test(new_quantity)) {
   var remove_id_a = new RegExp(cid + ",", "g");
   var remove_id_b = new RegExp("," + cid, "g");
   var remove_id_c = new RegExp(cid, "g");
   oldcookie = oldcookie.replace(remove_id_a, '');
   oldcookie = oldcookie.replace(remove_id_b, '');
   oldcookie = oldcookie.replace(remove_id_c, '');
   if (oldcookie == "") {
    oldcookie = "";
    for (x=1; x<=new_quantity; x++) {
     if (x == 1) { oldcookie += cid; }
     if (x != 1) { oldcookie += "," + cid; }
    }
   } else {
    for (x=1; x<=new_quantity; x++) {
     oldcookie += "," + cid;
    }
   }
   createCookie("store", oldcookie);
  }
  window.document.location = 'index.php?p=store&cart=show';
 }
}

