var newwin = null;
function doPopup() {
  newwin = window.open('', 'paywin', 'scrollbars,status,width=550,height=600');
  newwin.focus();
  alert("test");
  return true;
}

function OpenModalPopUP()
{            
     window.showModalDialog('page.aspx');
    window.location.reload();
}

function testfunction() {
  webtradeLightbox(0, true);
}

function submitSearch(searchForm, siteUrl) {
  document.location.href = ('http://'+siteUrl+'/sok/'+$F(searchForm.searchString)+'/'+$F(searchForm.type)+'/');
}

function stickyPopup(element, JSON) {

  if(JSON == 'close') {
//      $('stickyPopup').setStyle({width: '240px', height: '100px'});
      $('stickyPopup').hide();
//#      Event.stopObserving(element, 'mousemove');
    return;
  }

  if($('stickyPopup').visible() == false) {
    $('stickyPopup').show();

      new Ajax.Request('/ajaxTarget/stickyPopup.php', {
        method: 'post',
        parameters: 'JSON='+JSON,
        onComplete: function(transport) {
          $('stickyPopupLoading').hide();
          $('stickyPopupContent').update(transport.responseText);

          $('stickyPopupContent').show();

        }
      });

    $('stickyPopup').setStyle({left: ($(element).cumulativeOffset().left + $(element).getWidth() + 10)+'px', top: $(element).cumulativeOffset().top+'px'});

  } else {
    $('stickyPopup').hide();
//    Event.stopObserving(element, 'mousemove');
  }
}

function webtradeLightbox(JSON, showLoad, stayOpen) {
  var toWidth;
  var toNegLeft;
  var addToHeight;

  if($('webtradeLightbox').visible() == false || stayOpen) {

    $('webtradeLightbox').setStyle({top: $('webtradeLightbox').cumulativeScrollOffset().top + 80});
    $('webtradeLightboxClose').setStyle({top: $('webtradeLightbox').cumulativeScrollOffset().top + 80});

    $('webtradeLightbox').show();
    $('webtradeLightboxLoading').show();
    $('webtradeLightboxContent').hide();
    $('webtradeShadebox').setStyle({top: $('webtradeShadebox').cumulativeScrollOffset().top});

    Event.observe(window, 'scroll', function() {
      if(showLoad == true) {
        $('webtradeLightbox').setStyle({top: $('webtradeLightbox').cumulativeScrollOffset().top + 80});
        $('webtradeLightboxClose').setStyle({top: $('webtradeLightbox').cumulativeScrollOffset().top + 80});
      } else {
        $('webtradeShadebox').setStyle({top: $('webtradeShadebox').cumulativeScrollOffset().top});
      }
    })

    if(showLoad == true) {
      $('webtradeLightboxClose').show();
    } else {
      $('webtradeShadebox').show();
//      scroll(0,0);

      new Ajax.Request('/ajaxTarget/webtradeLightbox.php', {
        method: 'post',
        parameters: 'JSON='+JSON,
        onSuccess: function(transport) {
          $('webtradeLightboxLoading').hide();
          $('webtradeLightboxContent').update(transport.responseText);

          toWidth = $('webtradeLightboxContent').getWidth();
          toNegLeft = (toWidth / 2);

          addToHeight = 0;
          if(toWidth > 1000) {
            toWidth = 1000;
            toNegLeft = 500;
            $('webtradeLightbox').setStyle({overflow: 'auto'});
          }

          if(navigator.appName == "Microsoft Internet Explorer") {
            addToHeight = 16;
            toWidth += 16;
          }

          if(toWidth == 0) {
            webtradeLightbox();
          } else {
            new Effect.Morph($('webtradeLightbox'), {
              style: 'width: ' + toWidth + 'px; left: -' + toNegLeft + 'px; height: ' + ($('webtradeLightboxContent').getHeight() + addToHeight) + 'px;',
              duration: 0.4,
              afterFinish: function() {
                $('webtradeLightboxClose').show();
                $('webtradeLightboxClose').setStyle({left: '-' + (toNegLeft - 8) + 'px', marginLeft: '50%'});
                $('webtradeLightboxContent').show();
              }            
            });
          }
        }
      });
    }
  } else {
    $('webtradeLightbox').hide();
    $('webtradeLightboxClose').hide();
    Event.stopObserving(window, 'scroll');
    $('webtradeShadebox').hide();
    $('webtradeLightbox').setStyle({width: '240px', height: '100px', left: '-120px'});
  }    
}

function confirmTab() {
  new Ajax.Request('/ajaxTarget/checkout.php', {
    method: 'post',
    parameters: 'action=confirmTabAuth&'+$('pmForm').serialize(),
    onComplete: function(transport) {
      if(transport.responseText.strip() == '') {
        new Ajax.Updater('checkoutErrorContent', '/empty.php');
        new Ajax.Updater('checkoutContent', '/ajaxTarget/checkout.php', {
          method: 'post',
          parameters: 'action=confirmTab&'+$('pmForm').serialize()
        });
      } else {
        scroll(0,0);
        new Ajax.Updater('checkoutErrorContent', '/ajaxTarget/checkout.php', {
          method: 'post',
          parameters: 'action=errorMessage&messages='+transport.responseText.strip()
        });
      }
    }
  });
}

function receiptTab(hasCartContent) {
  var errorMessages = '';

  if(hasCartContent != 1) {
    errorMessages += '25,';
  }

  if($('terms').getValue() != 'on') {
    errorMessages += '30';
  }

  if(errorMessages == '') {

    new Ajax.Request('/ajaxTarget/checkoutEmail.php', {
      method: 'post',
      parameters: 'action=tryLogin',
      onSuccess: function(transport) { 
        webtradeLightbox(0, true);
        new Ajax.Updater('checkoutErrorContent', '/empty.php');
        new Ajax.Updater('checkoutContent', '/ajaxTarget/checkout.php', {
          method: 'post',
          parameters: {
            action: 'receiptTab'
          }
        });
      },
      onComplete: function(transport) {
        webtradeLightbox();
        if(transport.responseText.strip().empty()) {
        } else {
          alert(transport.responseText);
        }
      }
    });


  } else {
    scroll(0,0);
    new Ajax.Updater('checkoutErrorContent', '/ajaxTarget/checkout.php', {
      method: 'post',
      parameters: 'action=errorMessage&messages='+errorMessages
    });


  }
}

function registrationRequest() {
  param = 'action=';
  if($('registrationForm').serialize().endsWith('0')) {
    param += 'registerFormPrivate&formID=58';
  } else {
    param += 'registerFormCompany&formID=59';
  }
  param += '&type=insert';

  new Ajax.Updater('registrationPane', '/ajaxTarget/checkout.php', {
    method: 'post',
    parameters: param,
    onComplete: function() {
      $('registrationPane').insert({before: '<div id=\"registrationHeader\"><input type=\"button\" id=\"cancelButton\" value=\"Tillbaka\"  onClick=\"cancelCheckoutRegistration();\"></div>'});
    }
  });
}

function lightboxPasswordRecovery() {
  new Ajax.Request('/ajaxTarget/passwordRecovery.php', {
    method: 'post',
    parameters: 'email='+$('passwordRecoveryEmail').getValue(),
    onComplete: function(transport) {
      if(transport.responseText.strip() == '') {
        webtradeLightbox(Object.toJSON({'template': 'passwordRecoverySuccess.php'}), false, 'stayOpen');
      } else {
        if(transport.responseText.strip() == '1') {
          webtradeLightbox(Object.toJSON({'template': 'passwordRecoveryFailureNoEmail.php'}), false, 'stayOpen');
        } else if(transport.responseText.strip() == '2') {
          webtradeLightbox(Object.toJSON({'template': 'passwordRecoveryFailureWait.php'}), false, 'stayOpen');
        }
      }
    }
  });
}

function lightboxLoginRequest(updateWithURL) {
  new Ajax.Request('/ajaxTarget/checkout.php', {
    method: 'post',
    parameters: 'action=tryLogin&'+$('loginForm').serialize(),
    onComplete: function(transport) {
      if(transport.responseText.strip() == '') {
        webtradeLightbox();
        window.location = 'http://'+updateWithURL;
      } else {
        $('loginErrorContainer').update('Felaktigt användarnamn eller lösenord.')
        $('password').clear();
        $('username').select();
      }
    }
  });
}

function loginRequest() {
  new Ajax.Request('/ajaxTarget/checkout.php', {
    method: 'post',
    parameters: 'action=tryLogin&'+$('loginForm').serialize(),
    onComplete: function(transport) {
      if(transport.responseText.strip() == '') {
        new Ajax.Updater('checkoutErrorContent', '/empty.php');
        if($('checkoutContent')) {
          new Ajax.Updater('checkoutContent', '/ajaxTarget/checkout.php');
        } else if($('offertContainer')) {
          new Ajax.Updater('offertContainer', '/ajaxTarget/offert.php', {
            method: 'GET',
            parameters: {
              action: 'viewOffer'
            }
          });
        }
      } else {
        scroll(0,0);
        new Ajax.Updater('checkoutErrorContent', '/ajaxTarget/checkout.php', {
          method: 'post',
          parameters: 'action=errorMessage&messages='+transport.responseText.strip()
        });

        $('password').clear();
        $('username').select();
      }
    }
  });
}

function cancelCheckoutRegistration() {
  $('cancelButton').remove();
  new Ajax.Updater('registrationPane', '/ajaxTarget/checkout.php', {
    method: 'post',
    parameters: {
      action: 'costumerTypeForm'
    }
  });
}

function builderFormPost( name, formID, formPostID, type ) {
  var form = name + formPostID + 'Form';
  var div = name + formPostID + 'Div';

  $('buttonSend'+formPostID+formID).disable();

  if( type == 'update' ) {
    var params = 'formID='+formID+'&type='+type+'&formPostID='+formPostID;

  } else if( type == 'updateAbort' ) {
    var params = 'formID='+formID+'&type='+type+'&formPostID='+formPostID;

  } else if( type == 'updateInsert' ) {
    var params = $(form).serialize()+'&formID='+formID+'&type='+type+'&formPostID='+formPostID;

  } else {
    var params = $(form).serialize()+'&formID='+formID+'&type='+type+'&formPostID='+formPostID;
  }

  new Ajax.Updater(div, '/ajaxTarget/builder.php', {
    method: 'post',
    parameters: params,
    onComplete: function(transport) {
      if(transport.responseText.strip() == '') {
        new Ajax.Updater('checkoutContent', '/ajaxTarget/checkout.php');        
      }
    }
  });


}

function checkRadioSame() {
  if( $( 'extra1' ).getValue() == "Ja" ) {
    $('7').value = "-";
    $('8').value = "-";
    $('9').value = "-";
    $('9A').value = "-";
    $('9B').value = "-";
    $('11').value = "-";
  }
}

function checkRadioSameCompany() {
  if( $( 'extra1' ).getValue() == "Ja" ) {
    $('7').value = "-";
    $('8').value = "-";
    $('9').value = "-";
    $('10').value = "-";
    $('9A').value = "-";
    $('9B').value = "-";
    $('12').value = "-";
  }
}

function extra2Click() {
  $('7').value = "";
  $('8').value = "";
  $('9').value = "";
  $('9A').value = "";
  $('9B').value = "";
  $('11').value = "";
}

function extra2ClickCompany() {
  $('7').value = "";
  $('8').value = "";
  $('9').value = "";
  $('10').value = "";
  $('9A').value = "";
  $('9B').value = "";
  $('12').value = "";
}

function hideC() {
  new Ajax.Request('/ajaxTarget/setHideAddCartLightbox.php', {
  });
}

