function popup_promote_friend(data) {

   if(data['email']){
      data['email']='';
   }

   parameters_list =  gen_parameter_list(data);

        new Ajax.Request('/ajax/promote-a-friend-popup.html', {
        method: 'post',
        parameters: parameters_list,
        onSuccess: function(transport) {
          var resp_text = transport.responseText;
          var str = resp_text.split("|");
          $('mgm_home').innerHTML = resp_text;
        }
      });

    $('olayer').className = "grey_opacity";
  setDimensions('olayer');
  mgm_home_position();
  hidedisplay('mgm_home');
}

function gen_parameter_list(data){
      var parameters_list = '';

    if(data['auction_id'] != undefined){
      if(parameters_list != ''){
        parameters_list += '&';
      }
      parameters_list += 'aid='+data['auction_id'];
    }

    if(data['description'] != undefined){
      if(parameters_list != ''){
        parameters_list += '&';
      }
      parameters_list += 'description='+data['description'];
    }

    if(data['product_id'] != undefined){
      if(parameters_list != ''){
        parameters_list += '&';
      }
      parameters_list += 'pid='+data['product_id'];
    }

    if(data['savings'] != undefined){
      if(parameters_list != ''){
        parameters_list += '&';
      }
      parameters_list += 'savings='+data['savings'];
    }

    if(data['price'] != undefined){
      if(parameters_list != ''){
        parameters_list += '&';
      }
      parameters_list += 'price='+data['price'];
    }

    if(data['product_label'] != undefined){
      if(parameters_list != ''){
        parameters_list += '&';
      }
      parameters_list += 'label='+data['product_label'];
    }

    if(data['first_name'] != undefined){
      if(parameters_list != ''){
        parameters_list += '&';
      }
      parameters_list += 'first_name='+data['first_name'];
    }

    if(data['last_name'] != undefined){
      if(parameters_list != ''){
        parameters_list += '&';
      }
      parameters_list += 'last_name='+data['last_name'];
    }

    if(data['email'] != undefined){
      if(parameters_list != ''){
        parameters_list += '&';
      }
      parameters_list += 'email='+data['email'];
    }
    return parameters_list;
}


function setDimensions(div_name) {

  if (window.innerHeight && window.scrollMaxY) {
  sWidth = window.innerWidth + window.scrollMaxX;
  sHeight = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){
  sWidth = document.body.scrollWidth;
  sHeight = document.body.scrollHeight;
  } else {
  sWidth = document.body.offsetWidth;
  sHeight = document.body.offsetHeight;
  }

  var wWidth, wHeight;

  if (self.innerHeight) {
  if(document.documentElement.clientWidth){
    wWidth = document.documentElement.clientWidth;
    } else {
      wWidth = self.innerWidth;
    }
    wHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) {
    wWidth = document.documentElement.clientWidth;
    wHeight = document.documentElement.clientHeight;
  } else if (document.body) {
    wWidth = document.body.clientWidth;
    wHeight = document.body.clientHeight;
  }

  if(sHeight < wHeight){
      pageHeight = wHeight;
    } else {
      pageHeight = sHeight;
    }

  if(sWidth < wWidth){
      pageWidth = sWidth;
    } else {
      pageWidth = wWidth;
    }

  $(div_name).style.height = pageHeight +'px';
  $(div_name).style.width = pageWidth +'px';
}

function cancel_mgm_home(){
      $('mgm_home').innerHTML = "";
      hidedisplay('mgm_home');
      $('olayer').style.display = "";
      $('olayer').className = "";
      $('olayer').style.height = '0px';
    $('olayer').style.width = '0px';
}

 function chk_empty(str){
    if(str==null || str==""){
      $('empty_first_name').style.display = "";
      setTimeout("$('empty_first_name').style.display = 'none'", 4000);
      return false;
    }else{
      if(chk_unnormal(str)){
        $('empty_first_name').style.display = "none";
        return true;
      }else{
        $('empty_first_name').style.display = "";
        setTimeout("$('empty_first_name').style.display = 'none'", 4000);
        return false;
      }
    }
  }

 function chk_unnormal(str){
   var patten = new RegExp("[<>]","g");
   var result = str.match(patten);
   if(result == null){
      return true;
   }else{
      return false;
   }
 }

 function chk_email(str){

    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1){
       email_error_fade();
       return false;
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        email_error_fade();
       return false;
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        email_error_fade();
        return false;
    }

     if (str.indexOf(at,(lat+1))!=-1){
        email_error_fade();
        return false;
     }

     if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        email_error_fade();
        return false;
     }

     if (str.indexOf(dot,(lat+2))==-1){
        email_error_fade();
        return false;
     }

     if (str.indexOf(" ")!=-1){
        email_error_fade();
        return false;
     }

      $('email_invalid').style.display = "none";
      return true;
 }

function email_error_fade(){
  el = $('email_invalid');
  el.style.display = "";
  setTimeout("$('email_invalid').style.display = 'none'", 4000);
}


function email_form_error_fade(){
  el = $('form_warming');
  el.style.display = "";
  setTimeout("$('form_warming').style.display = 'none'", 4000);
}

function validate_form(){
  var emailID = $('email');
  var first_nameID = $('first_name');
  var last_nameID = $('last_name');

  if(chk_empty(first_nameID.value) && chk_email(emailID.value)){
    return true;
  }else{
    return false;
  }
}


function submit_send(data){

  data['first_name'] = $('first_name').value;
  data['last_name'] = $('last_name').value;
  data['email'] = $('email').value;

  parameters_list =  gen_parameter_list(data);

  if(validate_form()){
        new Ajax.Request('/ajax/promote-a-friend-popup.html', {
        method: 'post',
        parameters: parameters_list,
        onSuccess: function(transport) {
          var resp_text = transport.responseText;
          var str = resp_text.split("|");
          if (str.length > 1){
             $('mgm_home').innerHTML = str[1];
             $('form_warming').innerHTML = str[0];
             email_form_error_fade();
             $('email_invalid').style.display = "none";
             $('empty_first_name').style.display = "none";
          }else{
            $('mgm_home').innerHTML = "";
            $('mgm_home').innerHTML = resp_text;
          }

        }
      });
  }
}

function hide_preview(){
  $('mgm_home_email').style.display = "";
  $('mgm_home_preview').style.display = "none";
  $('mgm_home').style.position = "fixed";
}


function preview(){
    if(validate_form()){
       if(chk_unnormal($('last_name').value)){
         $('receiver').innerHTML = $('first_name').value + " "+$('last_name').value;
       }else{
         $('receiver').innerHTML = $('first_name').value;
       }
      show_preview();
    }
}

function show_preview(){
    $('mgm_home_email').style.display = "none";
    $('mgm_home_preview').style.display = "";
    $('mgm_home').style.position = "absolute";
}

function preview_mgm(){
   if(validate_form()){
       if(chk_unnormal($('last_name').value)){
         $('receiver').innerHTML = $('first_name').value + " "+$('last_name').value;
       }else{
         $('receiver').innerHTML = $('first_name').value;
       }
      show_preview_mgm();
    }
}

function show_preview_mgm(){
  //  mgm_home_position();
    $('mgm_home').style.display = "";
    $('mgm_home_preview').style.display = "";
    $('mgm_home_preview').className ="mgm_preview";
    $('olayer').className = "grey_opacity";
    setDimensions('olayer');


}


function cancel_preview(){
   $('olayer').className = "";
  $('mgm_home_preview').style.display = "none";
        $('olayer').style.height = '0px';
    $('olayer').style.width = '0px';
}


function mgm_home_position(){
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
        ( document.documentElement.clientWidth
          || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body &&
       ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;

 }



    div_vposition = (myWidth - 680 ) / 2 + 'px';
   $('mgm_home').style.left = div_vposition;

  // div_hposition = (myHeight - $('mgm_home').scrollHeight) / 2 + 'px';
  // $('mgm_home').style.top = div_hposition;

}


