
function fit_window(){
  pictureWidth = objGet('pic').width;
  pictureHeight = objGet('pic').height;
  window.resizeTo(pictureWidth+60, pictureHeight+96);
}

function checkPayment(){
  f=objGet('order_form');
  if(f.delivery.value=="osobní odběr"){
    f.payment.selectedIndex = 3;
  }
  else return true;
}

function isset(varname)  {
  if(typeof( window[ varname ] ) != "undefined") return true;
  else return false;
}

function validate_order(){
  f=objGet('order_form');
  if(regUser==true){
    if(objGet('agreement').checked==true)
      objGet('submit').disabled=false;
    else
      objGet('submit').disabled=true;
  }
  else{
    if(objGet('address_match').checked==true){
      f.s_name.value=f.name.value + ' ' + f.surname.value; objGet('s_name').readOnly=true;
      f.s_company.value=f.company.value; objGet('s_company').readOnly=true;
      f.s_address.value=f.address.value; objGet('s_address').readOnly=true;
      f.s_city.value=f.city.value; objGet('s_city').readOnly=true;
      f.s_post_code.value=f.post_code.value; objGet('s_post_code').readOnly=true;
    }
    else{
      objGet('s_name').readOnly=false;
      objGet('s_address').readOnly=false;
      objGet('s_city').readOnly=false;
      objGet('s_post_code').readOnly=false;
      objGet('s_company').readOnly=false;
    }

    if(f.name.value=="" || f.surname.value=="" || f.address.value==""
    || f.city.value=="" || f.post_code.value==""
    || wrong_mail_address(f.email.value) || f.phone.value==""  || objGet('agreement').checked==false
    || ( !objGet('address_match').checked && ( f.s_address.value==""
    || f.s_city.value=="" || f.s_post_code.value=="" ))
    ) objGet('submit').disabled=true;
    else objGet('submit').disabled=false;
  }

}

function wrong_mail_address(adresa){
  var error=false;
  var pozice_zavinace = adresa.indexOf("@");
    if (pozice_zavinace < 0)
        error=true;
    var cast_pred_zavinacem = adresa.substring(0,pozice_zavinace);
    var cast_po_zavinaci = adresa.substring(pozice_zavinace+1,adresa.length);
    if (cast_po_zavinaci.indexOf("@") >= 0)
        error=true;
    if (cast_pred_zavinacem.length <= 0)
        error=true;
    if (cast_po_zavinaci.length <= 0)
        error=true;

    return error;
}

function objGet(id){
   if(Boolean(document.getElementById))
      return document.getElementById(id);
   else if(Boolean(document.all))
      return eval('document.all.'+id);
   else
      return eval('document.'+id);
}

function switchImg(url){
  objGet('main-pic').src=url;
}

function openImage(file){
  url = '/photo.php?file='+escape(file);
  win = window.open(url, 'image', 'resizable=1,top=20,left=20,menubar=0,scrollbars=1,width=' + 300 + ',height=' + 220 );
  win.focus();
}

function openStringPopup(val){
  if(val != 0){
    url = '/popup_detail.php?product='+val;
    win = window.open(url, 'String', 'resizable=1,top=20,left=20,menubar=0,scrollbars=0,width=540,height=480' );
    win.focus();
  }
  return false;
}

function clearPassword(){
  x=objGet('login_form');
  if(x.password.value=='Vložte heslo...') x.password.value='';
}

function clearEmail(){
  x=objGet('login_form');
  if(x.login.value=='Vložte e-mail...') x.login.value='';
}

function clearNewsletter(){
  x=objGet('newsletter-box');
  if(x.value=='Vložte Váš e-mail...') x.value='';
}

function clearkeywords(){
  x=objGet('search');
  if(x.keywords.value=='Zadejte klíčová slova...') x.keywords.value='';
}

function validateReg(edit){
  form=objGet('reg_form');
  if(objGet('customer_btn').checked){   // ------- koncovy zakaznik -----
    if(form.name.value=="" || form.surname.value==""
       || form.address.value=="" || form.city.value==""
       || form.post_code.value==""  || form.company_phone.value==""
       || wrong_mail_address(form.company_email.value) || (form.password.value=="" && !edit))
      objGet('submit').disabled=true;

    else objGet('submit').disabled=false;
  }
  else{                                 // ------- firma -----
    if(form.company_name.value=="" || form.country.value==""
      || form.address.value=="" || form.city.value==""
      || form.post_code.value=="" || form.company_phone.value==""
      || wrong_mail_address(form.company_email.value) || (form.country.value=='48' && form.ico.value=="")
      || (form.country.value!='48' && form.vat.value=="") || form.contact_name.value==""
      || form.contact_surname.value=="" || form.contact_phone.value==""
      || wrong_mail_address(form.contact_email.value)
      || (form.password.value=="" && !edit)){
      objGet('submit').disabled=true;
    }
    else objGet('submit').disabled=false;
  }
}

function updateRegForm(){
  if(objGet('customer_btn').checked){   // ------- koncovy zakaznik -----
    objGet('end-user').style.display = 'block';
    objGet('company-box').style.display = 'none';
  }
  else{                                 // ------- firma -----
    objGet('end-user').style.display = 'none';
    objGet('company-box').style.display = 'block';
  }
  validateReg();
}


function copyright(e){
  alert('Copyright');
  return false;
}

function denyCopy() {

//   inp = document.getElementsByTagName('p');
//   for(i=0; i<inp.length; i++){
//     inp[i].oncopy=msg;
//     inp[i].oncut=msg;
//   }

    try{
     objGet('detail').oncopy=copyright;
     objGet('detail').oncut=copyright;
    }catch(e){}
}

window.onload = denyCopy;

function checkAccount(inp){
    if(!wrong_mail_address(inp.value)){
        send_xmlhttprequest(validateAccount, 'GET', '/inc/get_account.php?email='+inp.value);
    }
}

pswdHash = '';

function validateAccount(xmlhttp){
    if(xmlhttp.readyState == 4) {
        exists = parseInt(xmlhttp.responseXML.getElementsByTagName('pswd')[0].firstChild.data);
        if(exists){
            pswdHash = xmlhttp.responseXML.getElementsByTagName('pswd')[0].firstChild.data;
            msg('<p>Zadali jste e-mail, který je již v systému evidován. Nejspíše jste již v obchodě Tenisprofi.cz nakupovali a máte již vytvořený účet.</p>'+
         '<form name="modal" action=""><p>Zadejte prosím Vaše heslo: <input name="pswd" type="password" id="pswd" /> <input type="button" value="přihlásit se" onclick="checkPassword()" /></p></form>' +
         '<p>Pokud jste heslo zapomněli, můžete si jej <a href="#" onclick="sendPswd()">nechat zaslat</a> nebo nás kontaktujte - 605 40 37 37.</p>','','');
        }
    }
}


// odeslání XMLHttp požadavku

function send_xmlhttprequest(obsluha, method, url, content, headers) {
    var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    if (!xmlhttp) {
        return false;
    }
    xmlhttp.open(method, url);
    xmlhttp.onreadystatechange = function() {
        obsluha(xmlhttp);
    };
    if (headers) {
        for (var key in headers) {
            xmlhttp.setRequestHeader(key, headers[key]);
        }
    }
    xmlhttp.send(content);
    return true;
}

function closeModal(){
    $('overlay').fade({duration: 0.3});
    $('modal-layer').fade({duration: 0.3});
}

function sendPswd(){
    location.href = '/uzivatel/?send_password=1&email='+$('email_input').value;
}

function checkPassword(){
    pswd = $('pswd').value;
    mail = $('email_input').value;
    if(pswdHash == MD5(pswd)){
        send_xmlhttprequest(loginRefresh, 'GET', '/inc/login.php?login='+mail+'&password='+pswd);
    }
    else{
        $('modal-msg').className = 'error';
        $('modal-msg').innerHTML = 'Zadané heslo není správné. Zkuste to prosím znovu.';
    }
}

function loginRefresh(xmlhttp){
   if(xmlhttp.readyState == 4) {
        logged = parseInt(xmlhttp.responseXML.getElementsByTagName('logged')[0].firstChild.data);
        if(logged){
            location.href='/objednavka/?msg=Vítejte! Přihlášení proběho úspěšně.';
        }
        else{
            msg('','Nastala chyba při ověřování účtu. Účet není aktivní.', 'error');
        }
    }
}

function msg(content, message, type){
    $('modal-msg').className = type;
    $('modal-msg').innerHTML = message;
    $('modal-content').innerHTML = content;
    $('overlay').appear({ duration: 0.3, to: 0.6 });
    $('modal-layer').appear({ duration: 0.3 });
}
