function submitenter(myfield,e){
  var keycode;
  if (window.event) keycode = window.event.keyCode;
  else if (e) keycode = e.which;
  else return true;
  if (keycode == 13){
    myfield.form.submit();
    return false;
  }else
    return true;
}

function checkEmail(aEmail){ return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(aEmail)); }
function doClear(aElementId){ document.getElementById(aElementId).value = ''; };

function checkAFM(afm){      
   if(!afm.match(/^\d{9}$/)) return false;
   afm = afm.split('').reverse().join('');

   var Num1 = 0;
   for(var iDigit= 1; iDigit <=  8; iDigit++){
      Num1 += afm.charAt(iDigit) << iDigit;     
   }

   return (Num1 % 11) % 10 == afm.charAt(0);   
}

function get_radio_value(aRadio){
  if(!aRadio.length) return aRadio.value;
  for (var i=0; i < aRadio.length; i++)
    if (aRadio[i].checked)
      return aRadio[i].value;
}

function set_radio_value(radioObj, newValue) {
  if(!radioObj) return;
  var radioLength = radioObj.length;
  if(radioLength == undefined) {
    radioObj.checked = (radioObj.value == newValue.toString());
    return;
  }
  for(var i = 0; i < radioLength; i++) {
    radioObj[i].checked = false;
    if(radioObj[i].value == newValue.toString()) {
      radioObj[i].checked = true;
    }
  }
}

function vUnregistration(){
  if (document.frmUnreg.edEmail.value == ''){ alert("Δεν έχετε συμπληρώσει το E-mail σας!"); return false; }
  if (!checkEmail(document.frmUnreg.edEmail.value)) { alert("Μή έγκυρο E-mail!"); return false; }
  return true;
}

function vRegistration(){
  if (document.frmNewsletter.edName.value == ''){ alert("Δεν έχετε συμπληρώσει το όνομά σας!"); return false; }
  if (document.frmNewsletter.edEmail.value == ''){ alert("Δεν έχετε συμπληρώσει το E-mail σας!"); return false; }
  if (!checkEmail(document.frmNewsletter.edEmail.value)) { alert("Μή έγκυρο E-mail!"); return false; }
  return true;
}

function vRegUser(){
  if (document.frmRegister.edUsername.value == ''){ alert("Δεν έχετε συμπληρώσει το ψευδώνυμο σας!"); return false; }
  if (document.frmRegister.edPassword.value == ''){ alert("Δεν έχετε συμπληρώσει τον κωδικό πρόσβασή σας!"); return false; }
  if (document.frmRegister.edPassword.value!=document.frmRegister.edPassword2.value){ alert("Η επιβεβαίωση του κωδικού ήταν λάθος!"); return false; }
  if (document.frmRegister.edEmail.value == ''){ alert("Δεν έχετε συμπληρώσει το E-mail σας!"); return false; }
  if (!checkEmail(document.frmRegister.edEmail.value)) { alert("Μή έγκυρο E-mail!"); return false; }
  return true;
}

function vUpdUser(){
  if (document.frmUpdate.password.value != ''){ 
    if (document.frmUpdate.password.value!=document.frmUpdate.edPassword2.value){ alert("Η επιβεβαίωση του κωδικού ήταν λάθος!"); return false; }
  }
  if (document.frmUpdate.email.value == ''){ alert("Δεν έχετε συμπληρώσει το E-mail σας!"); return false; }
  if (!checkEmail(document.frmUpdate.email.value)) { alert("Μή έγκυρο E-mail!"); return false; }
  return true;
}

function vUpgrdUser(){
  if (document.frmUpdate.fullname.value == ''){ alert("Δεν έχετε συμπληρώσει το όνομά σας!"); return false; }
  if (document.frmUpgrade.address.value == ''){ alert("Δεν έχετε συμπληρώσει τη διεύθυνσή σας!"); return false; }
  if (document.frmUpgrade.postcode.value == ''){ alert("Δεν έχετε συμπληρώσει τον ταχυδρομικό σας κώδικα!"); return false; }
  if (document.frmUpgrade.city.value == ''){ alert("Δεν έχετε συμπληρώσει την πόλη σας!"); return false; }
  if (document.frmUpgrade.phone.value == ''){ alert("Δεν έχετε συμπληρώσει το τηλέφωνό σας!"); return false; }
  if (document.frmUpgrade.vat.value == ''){ alert("Δεν έχετε συμπληρώσει το ΑΦΜ σας!"); return false; }
  if (!checkAFM(document.frmUpgrade.vat.value)){ alert("Μη έγκυρο AFM!"); return false; }
  if (!document.frmUpgrade.terms.checked){ alert("Πρέπει να αποδεχτείτε τους όρους χρήσης!"); return false; }
  return true;
}

function vReport(){
  if (document.frmReport.fullname.value == ''){ alert("Δεν έχετε συμπληρώσει το όνομά σας!"); return false; }
  if (document.frmReport.email.value == ''){ alert("Δεν έχετε συμπληρώσει το email σας!"); return false; }
  if (!checkEmail(document.frmReport.email.value)) { alert("Μή έγκυρο E-mail!"); return false; }
  if (document.frmReport.description.value == ''){ alert("Δεν έχετε συμπληρώσει την αναφορά σας!"); return false; }
  return true;
}

function edClear(aElem, aCaption){
  if(aElem.value==aCaption) aElem.value='';
  return true;
}

function edSet(aElem, aCaption){
  if(aElem.value=="") aElem.value=aCaption;
  return true;
}

function validateResReg(){
  if (document.frmNewRestaurant.title.value == ''){ alert("Δεν έχετε συμπληρώσει την επωνυμία!"); return false; }
  if (document.frmNewRestaurant.address.value == ''){ alert("Δεν έχετε συμπληρώσει την διεύθυνση!"); return false; }
  if (document.frmNewRestaurant.districtid.value == ''){ alert("Δεν έχετε συμπληρώσει την Πόλη/Περιοχή!"); return false; }
  if (document.frmNewRestaurant.po.value == ''){ alert("Δεν έχετε συμπληρώσει τον ταχυδρομικό κώδικα!"); return false; }
  if (document.frmNewRestaurant.mapLat.value<=0){ alert("Η διεύθυνση που έχετε συμπληρώσει δεν αντιστοιχεί στον χάρτη!"); return false; }
  if (document.frmNewRestaurant.mapLng.value<=0){ alert("Η διεύθυνση που έχετε συμπληρώσει δεν αντιστοιχεί στον χάρτη!"); return false; }
  if (document.frmNewRestaurant.phone.value == ''){ alert("Δεν έχετε συμπληρώσει το τηλέφωνο!"); return false; }
  if (document.frmNewRestaurant.email.value == ''){ alert("Μή έγκυρο E-mail!"); return false; }
  if (!checkEmail(document.frmNewRestaurant.email.value)) { alert("Μή έγκυρο E-mail!"); return false; }
  return true;
}

function doDeleteMenu(aRestaurantId, aMenuId, aFormId){
  if(confirm("Είστε σίγουροι ότι θέλετε να διαγράψετε το συγκεκριμένο πιάτο;"))
    window.location = "index.php?p=editmenu&id="+aRestaurantId+"&action=deleteMenu&menuid="+aMenuId+"&formId="+aFormId;
}

function doDeleteImage(aRestaurantId, aPhotoId, aFormId){
  if(confirm("Είστε σίγουροι ότι θέλετε να τη συγκεκριμένη εικόνα;"))
    window.location = "index.php?p=editimages&id="+aRestaurantId+"&action=deleteImage&photoid="+aPhotoId+"&formId="+aFormId;
}

function toggleMenuSpecial(aRestaurantId, aMenuId, aFormId){
  window.location = "index.php?p=editmenu&id="+aRestaurantId+"&action=toggleMenu&menuid="+aMenuId+"&formId="+aFormId;
}

function refreshSmallOffer(aTargetElement, aOfferId, aOfferType, aPromoCaption, aSmallCaption){
  aTargetElement.html("");
  var req = jQuery.ajax({
    url:'ajax/offerSmall.php',
    data: { 'id':aOfferId, 'type':aOfferType, 'promo':aPromoCaption, 'small':aSmallCaption },
    success: function(response) { aTargetElement.html(response); }
  });
}

function refreshExtOffer(aTargetElement, aOfferId, aOfferType, aPromoCaption, aSmallCaption, aCost, aTitle, aDescription, aInitValue, aDiscount, aProfit, aEndDate, aResTitle, aResPhoto){
  aTargetElement.html("");
  var req = jQuery.ajax({
    url:'ajax/offerExtended.php',
    data: { 'id':aOfferId, 'type':aOfferType, 'promo':aPromoCaption, 'small':aSmallCaption, 'cost':aCost, 'title':aTitle, 'description':aDescription,
            'initvalue':aInitValue, 'enddate':aEndDate, 'restitle':aResTitle, 'resphoto':aResPhoto },
    success: function(response) { aTargetElement.html(response); }
  });
}

function setOfferType(aType){
  switch(aType){
    case '1' : jQuery('#trInitValue').show();
               jQuery('#trOfferCost').show();
               jQuery('#trOfferQty').show();
             break;
    case '2' : jQuery('#trInitValue').hide();
               jQuery('#trOfferCost').hide();
               jQuery('#trOfferQty').hide();
               break;
  }
  doRefreshPreview();
}

function doInternalRefreshPreview(aResTitle, aResPhoto){
  aTargetElement = jQuery('#dvPreviewOfferSmall');
  aOfferType = jQuery('#offertype').val();
  aOfferPromo = jQuery('#promocaption').val();
  aOfferSmallCaption = jQuery('#smallcaption').val();
  refreshSmallOffer(aTargetElement, -1, aOfferType, aOfferPromo, aOfferSmallCaption);
  aTargetElement = jQuery('#dvPreviewOfferExt');
  aCost = jQuery('#offercost').val();
  aTitle = jQuery('#offertitle').val();
  aDescription = jQuery('#offerdescription').val();
  aInitValue = jQuery('#offerinitvalue').val();
  aDiscount = jQuery('#offerdiscount').val();
  aProfit = jQuery('#offerprofit').val();
  aEndDate = jQuery('#offerend').val();
  refreshExtOffer(aTargetElement, -1, aOfferType, aOfferPromo, aOfferSmallCaption, aCost, aTitle, aDescription, aInitValue, aDiscount, aProfit, aEndDate, aResTitle, aResPhoto);
}

function syncBool(aChecked, aFieldId){
  if(aChecked){
    document.getElementById(aFieldId).value = 1;
  }else
    document.getElementById(aFieldId).value = 0;
}

/*
 * to be used by the registration process
 */
function resetMarkerPos(){
  aAddress = jQuery('#address').val();
  if(jQuery('#districtid').val()>0) {
    aDistrict = jQuery("#districtid option:selected").text();
    if(aDistrict=='Αττική / Πειραιάς') aDistrict = 'Πειραιάς';
    aDistrict = aDistrict.replace("/", ",");
    aAddress += "," + aDistrict;
  }
  if(jQuery('#po').val()!='') aAddress += ', ' + jQuery('#po').val();
  positionMarker(jQuery('#title').val(), aAddress);
}

function doColorBox(){
  jQuery('.cbox').colorbox({ 
                             title: function(){ return jQuery(this).attr('title'); },
                             width: function(){ return jQuery(this).attr('cbw'); },
                             height: function(){ return jQuery(this).attr('cbh'); },
                             iframe: true
                           });
  jQuery('.cboximg').colorbox({ 
                             title: function(){ return jQuery(this).attr('title'); },
                             width: function(){ return jQuery(this).attr('cbw'); },
                             height: function(){ return jQuery(this).attr('cbh'); }
                           });
}

function validateSubscription(){
  aRadioVal = jQuery('input:radio[name=entityid]:checked').val();
  if(!aRadioVal) {
    alert("Δεν έχετε επιλέξει είδος συνδρομής!");
    return false;
  }
  return true;
}

function decQuantity(aUnitCost){
  aQuantity = parseInt(jQuery('#quantity').val());
  if(aQuantity>1) aQuantity -= 1;
  jQuery('#quantity').val(aQuantity);
  jQuery('#qtyDisplay').html(aQuantity);
  jQuery('#costDisplay').html((aQuantity * aUnitCost) + '&euro;');
}

function incQuantity(aUnitCost){
  aQuantity = parseInt(jQuery('#quantity').val());
  aQuantity += 1;
  jQuery('#quantity').val(aQuantity);
  jQuery('#qtyDisplay').html(aQuantity);
  jQuery('#costDisplay').html((aQuantity * aUnitCost) + '&euro;');
}

jQuery(window).load(function(){
  jQuery('.login').colorbox({ 
    href: function(){ return jQuery(this).attr('url'); },
    title: function(){ return jQuery(this).attr('title'); },
    width: function(){ return jQuery(this).attr('cbw'); },
    height: function(){ return jQuery(this).attr('cbh'); },
    iframe: true
  });
});
