var xmlHttp;

function CheckRegisterFormData(bUser) {
  if (!bUser) {
    if (document.Form.name.value.replace(/ /g, "").length == 0) {
      document.Form.name.focus();
      alert(gettext("Enter user name!"));
      return false;
    }
    if (CheckUserNameDuplicate(document.Form.name.value)) {
      document.Form.name.focus();
      alert(gettext("User name exists already!"));
      return false;
    }
    if (document.Form.password.value.replace(/ /g, "").length < 5) {
      document.Form.password.focus();
      alert(gettext("The password must be longer than 4 chars!"));
      return false;
    }
    if (document.Form.password.value != document.Form.password2.value) {
      document.Form.password.focus();
      alert(gettext("Password is incorrect!"));
      return false;
    }
  }
  if (document.Form.real_name.value.replace(/ /g, "").length == 0) {
    document.Form.real_name.focus();
    alert(gettext("Enter real name!"));
    return false;
  }
  if (document.Form.country.value.replace(/ /g, "").length == 0) {
    document.Form.country.focus();
    alert(gettext("Select country!"));
    return false;
  }
  if (document.Form.address.value.replace(/ /g, "").length == 0) {
    document.Form.address.focus();
    alert(gettext("Enter address!"));
    return false;
  }
  if (document.Form.country.value == 26 && !ChechAddress(document.Form.address.value)) {
    document.Form.address.focus();
    alert(gettext("Enter valid address!"));
    return false;
  }
  if ((document.Form.email.value.replace(/ /g, "").length == 0) ||
      (document.Form.email.value.indexOf('@') == -1)) {
    document.Form.email.focus();
    alert(gettext("Enter a valid e-mail!"));
    return false;
  }
  if (document.Form.phone.value.replace(/ /g, "").length == 0) {
    document.Form.phone.focus();
    alert(gettext("Enter phone!"));
    return false;
  }
  return true;
}

function RefreshCompany(sender, company_field, form) {
  company = document.getElementById(company_field);
  if (sender != company) {
    company.value = 0;
  }
  else {
    form.submit();
  }
}

function RefreshCompanies(sender) {
  RefreshCompany(sender, 'company1', document.Other);
  RefreshCompany(sender, 'company2', document.Finishing);
  RefreshCompany(sender, 'company3', document.Literature);
}

function GetXmlHttpObject() {
  var objXMLHttp = null;
  try {
    // Firefox, Opera 8.0+, Safari
    objXMLHttp = new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try {
      objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
      }
    }
  }
  return objXMLHttp;
}

function AreYouSure() {
  return confirm(gettext("Are you sure?"));
}

function CheckKIN() {
  if (document.Form.kin.value.replace(/ /g, "").length == 0) {
    document.Form.kin.focus();
    alert(gettext("Enter user identification number!"));
    return false;
  }
  return true;
}

function CheckOrder(order_id) {
  if (xmlHttp == null)
    xmlHttp = GetXmlHttpObject();
  xmlHttp.open("GET", "/shop/check_order/" + order_id + "/", false);
  xmlHttp.send(null);
  text = xmlHttp.responseText;
  if (text != "") {
    alert(text);
    return false;
  }
  return true;
}

function ChechAddress(address) {
  if (xmlHttp == null)
    xmlHttp = GetXmlHttpObject();
  xmlHttp.open("GET", "/shop/check_address/?address=" + encodeURI(address), false);
  xmlHttp.send(null);
  text = xmlHttp.responseText;
  if (text != "/files/images/icon_success.png") {
    return false;
  }
  return true;
}

function CheckUserNameDuplicate(username) {
  if (xmlHttp == null)
    xmlHttp = GetXmlHttpObject();
  xmlHttp.open("GET", "/shop/check_user/" + encodeURI(username) + "/", false);
  xmlHttp.send(null);
  text = xmlHttp.responseText;
  return text != "";
}

function ShowReducedProduct() {
  var count = document.getElementById("ReducedProductsCount");
  d = document.getElementById("ReducedProducts");
  if (d != null && count != null) {
    rp_index = Math.round(Math.random() * count.value)-1;
    d.scrollTop = 200 * rp_index;
  }
  setTimeout("ShowReducedProduct();", 3000);
}

function ResizeWindowForIE() {
  var fr = document.getElementById("Body");
  if (fr != null) {
    var h = document.documentElement.clientHeight;
    if (h > 500) {
      fr.style.height = (h - 278) + "px";
    }
  }
  setTimeout("ResizeWindowForIE();", 1000);
}

function OnLoad() {
  if (navigator.appName == "Microsoft Internet Explorer") {
    ResizeWindowForIE();
  }
  ShowReducedProduct();
}

document.onload = OnLoad();

document.getElementsByClassName = function(class_name) {
    var docList = this.all || this.getElementsByTagName('*');
    var matchArray = new Array();

    /*Create a regular expression object for class*/
    var re = new RegExp("(?:^|\\s)"+class_name+"(?:\\s|$)");
    for (var i = 0; i < docList.length; i++) {
        if (re.test(docList[i].className) ) {
            matchArray[matchArray.length] = docList[i];
        }
    }

	return matchArray;
}//eof annonymous function


function OpenProducts(type_id) {
  types = document.getElementsByClassName("ProductsTable");
  for (i=0; i < types.length; i++) {
    types[i].className = "ProductsTable Hidden";
  }
  selected_type = document.getElementById("Products" + type_id);
  if (selected_type != null) {
    selected_type.className = "ProductsTable";
  }
}

