// mailto.js

// disable the Enter key 
function stopRKey(evt) { 
 var evt = (evt) ? evt : ((event) ? event : null); 
 var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
/* if((evt.keyCode==13) && (node.type=="text"))  { return imitfrailsubmit(); return false; } */
 if(evt.keyCode==13) { return imitfrailsubmit(); } 
} 
document.onkeypress = stopRKey; 
var IFrFormObj; // our IFrame Form object
function pageWidth() {
 return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}
function pageHeight() {
 return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}
function posTop() {
 return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}
function findPosX(obj) {
 var curleft = 0;
 if(obj.offsetParent) {
  while(1) {
   curleft += obj.offsetLeft;
   if(!obj.offsetParent) break;
   obj = obj.offsetParent;
  }
 }
 else if(obj.x) curleft += obj.x;
 return curleft;
}
function findPosY(obj) {
 var curtop = 0;
 if(obj.offsetParent) {
  while(1) {
   curtop += obj.offsetTop;
   if(!obj.offsetParent) break;
   obj = obj.offsetParent;
  }
 }
 else if(obj.y) curtop += obj.y;
 return curtop;
}

var Drag = {
 obj : null,
 init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper) {
  o.onmousedown	= Drag.start;
  o.hmode	= bSwapHorzRef ? false : true ;
  o.vmode	= bSwapVertRef ? false : true ;

  o.root = oRoot && oRoot != null ? oRoot : o ;

  if(o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
  if(o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
  if(!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
  if(!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

  o.minX	= typeof minX != 'undefined' ? minX : null;
  o.minY	= typeof minY != 'undefined' ? minY : null;
  o.maxX	= typeof maxX != 'undefined' ? maxX : null;
  o.maxY	= typeof maxY != 'undefined' ? maxY : null;

  o.xMapper = fXMapper ? fXMapper : null;
  o.yMapper = fYMapper ? fYMapper : null;
  o.root.onDragStart	= new Function();
  o.root.onDragEnd	= new Function();
  o.root.onDrag		= new Function();
 },

 start : function(e) {
  var o = Drag.obj = this;
  e = Drag.fixE(e);
  var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
  var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
  o.root.onDragStart(x, y);
  o.lastMouseX	= e.clientX;
  o.lastMouseY	= e.clientY;
  if(o.hmode) {
   if(o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
   if(o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
  } else {
   if(o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
   if(o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
  }
  if(o.vmode) {
   if(o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
   if(o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
  } else {
   if(o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
   if(o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
  }
  document.onmousemove	= Drag.drag;
  document.onmouseup	= Drag.end;
  return false;
 },

 drag : function(e) {
  e = Drag.fixE(e);
  var o = Drag.obj;
  var ey = e.clientY;
  var ex = e.clientX;
  var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
  var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
  var nx, ny;
  if(o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
  if(o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
  if(o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
  if(o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
  nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
  ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
  if(o.xMapper)		nx = o.xMapper(y)
  else if(o.yMapper)	ny = o.yMapper(x)
  Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
  Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
  Drag.obj.lastMouseX	= ex;
  Drag.obj.lastMouseY	= ey;
  Drag.obj.root.onDrag(nx, ny);
  return false;
 },
 end : function() {
  document.onmousemove = null;
  document.onmouseup   = null;
  Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
			parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
  Drag.obj = null;
 },
 fixE : function(e) {
  if(typeof e == 'undefined') e = window.event;
  if(typeof e.layerX == 'undefined') e.layerX = e.offsetX;
  if(typeof e.layerY == 'undefined') e.layerY = e.offsetY;
  return e;
 }
};
function maskPForm() {
 setTimeout('hidePForm()',100);
}
function hidePForm() {
 if(document.getElementById('theroot')) {
  document.getElementById('theroot').style.left = '-500px';
  document.getElementById('theroot').innerHTML = '';
 }
}
function contactme(mid,inline,lid) {
 if(typeof(lid)=='undefined')
   lid = '';
 if(inline)
   document.write('<a class="here" href="javascript:displayPrevForm(\''+mid+'\',\''+lid+'\')">'+inline+'</a>');
 else
  document.write('<br /><a href="javascript:displayPrevForm(\''+mid+'\',\''+lid+'\')">Написати листа</a>');
}
function displayPrevForm(id,lid) {
 if(typeof(lid)=='undefined')
   lid = '';
 var cont = '';
 buildMForm(id,lid);
}
function displayForm(id,lid) {
 if(typeof(lid)=='undefined')
   lid = 'i';
 if(!lid)
   lid = 'i';
 if(document.getElementById('theroot') && document.getElementById('thehandle')) {
  var PosLeft;
  var PosTop;
  // action to compute the positon
  if(document.getElementById(lid+id)) {
   // don't play anymore; vertical position same as picture
   PosTop=findPosY(document.getElementById(lid+id));
   // just play a little bit with horizontal position
   var alertpageWidth = pageWidth();
   var alertfindPosX = findPosX(document.getElementById(lid+id));
   if(alertfindPosX>parseInt(pageWidth()/2))
     PosLeft = alertfindPosX-450;
   else
     PosLeft = alertfindPosX;
   Drag.init(document.getElementById("thehandle"), document.getElementById("theroot"));
   document.getElementById('theroot').style.left = PosLeft+'px';
   document.getElementById('theroot').style.top = PosTop+'px';
   setTimeout('displayVisual()',100); 
  }
  else {
   alert('There is no such object ID: '+id+';lid: '+lid);
  }
 }
 else {
  alert('Root is not here!');
 }
}
function displayVisual() {
 document.getElementById('theroot').style.display = "";
 document.getElementById('theroot').style.borderStyle="solid"; 
}
function textCounter() {
 var maxlimit = 500;
 var textarea = document.forms.ifrail.customrequest;
 var textcount = document.getElementById('remLen').innerHTML;
 if(textarea && textcount) {
  if(textarea.value.length > maxlimit) {
   textarea.value = textarea.value.substring(0, maxlimit);
   rszTf(textarea);
  }
  else {
   document.getElementById('remLen').innerHTML = maxlimit - textarea.value.length;
/*   rszTf(textarea); */
  }
 }
}
function rszTf(t) {
 var a = t.value.split('\n');
 var b=1;
 for(x=0;x < a.length; x++) {
  if(a[x].length >= t.cols) b+= Math.floor(a[x].length/t.cols);
 }
 b+= a.length;
 if(b > t.rows) t.rows = b;
}
function imitfrailsubmit() {
 if(document.forms.ifrail && document.activeElement) {
  if(document.activeElement.id!='customrequest')
   ifrailsubmit();
  else
   return true;
 }
 return false;
}
function ifrailsubmit() {
 var err = '';
 var focusField = null;
 if(document.forms.ifrail.mid) {
  var mid = document.forms.ifrail.mid.value;
  var lid = document.forms.ifrail.lid.value;
  // the email must contain 7 digits
  var mRegex = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]{1,}(\.[a-z0-9-]{2,})+$/;
  var nameField = '';
  var mailField = '';
  var requestField = '';
  var secField = '';
  var secval ='';
  nameField = document.forms.ifrail.customname;
  if(nameField.value.length < 4) {
   err += 'Ви забули вказати Ваше ім\'я!';
   if(!focusField)
    focusField = nameField;
  }
  mailField = document.forms.ifrail.customemail;
  if(!mRegex.test(mailField.value)) {
   if(err) err+='\n';
   err += 'Необхідно вказати Вашу адресу e-mail!';
   if(!focusField)
    focusField = mailField;
  }
  requestField = document.forms.ifrail.customrequest;
  if(requestField.value.length<5) {
   if(err) err+='\n';
   err += 'Ви забули написати текст Вашого листа!';
   if(!focusField)
    focusField = requestField;
  }
  else if(requestField.value.length>500) {
   if(err) err+='\n';
   err += 'Ваш текст задовгий!';
   if(!focusField)
    focusField = requestField;
  }
  var reqField = requestField.value;
  var reqFieldLength = reqField.length;
  var rExp = /\n/gi;  
  var nCount = 0;
  while (reqField.indexOf("\n") > -1) {
   reqField = reqField.replace(rExp, "Ё");
   nCount++;
  }
  rExp = /\r/gi;
  var rCount = 0;
  while (reqField.indexOf("\r") > -1) {
   reqField = reqField.replace(rExp, "Ё");
   rCount++;
  }
// alert('Length: '+reqFieldLength+'; nCount: '+nCount+'; rCount: '+rCount+'\n'+remmes);
  secField = document.forms.ifrail.secur1ty_code;
  if(secField.value.length < 4) {
   if(err) err+='\n';
   err += 'Будь-ласка, введіть захисний код!';
   if(!focusField)
    focusField = secField;
  }
  else
   secval = secField.value;
  // final logic
  if(err) {
/*   alert(err+"\n"+reqField); */
   alert(err);
   if(focusField) {
    focusField.select();
    focusField.focus();
   }
   return false;
  }
  else
   buildMForm(mid,lid,nameField.value,mailField.value,reqField,secval);
 }
}



function buildMForm(mid,lid,mname,mmail,mmes,msec) {
 if(typeof(lid)=='undefined')
   lid = '';
 if(!document.createElement)
   return true;
 if(mname && mmail && mmes) {
  var remname= encodeURIComponent(mname);
  var remmes = encodeURIComponent(mmes);
  var respMssg = document.getElementById('repdisplay');
/*  respMssg.innerHTML = '<img src="/mailto/im/uploading_01.gif" style="border:0;" alt="loading data..."><\/span>'; */
  respMssg.style.display = '';
  var URL = 'mailto/form_.php?i='+mid+'&l='+lid+'&n='+remname+'&e='+mmail+'&m='+remmes;
  if(msec) { URL += '&s='+msec; }
 }
 else {
  var respMssg = document.getElementById('theroot');
  // respMssg.innerHTML = '<span style="color:green">loading data...<\/span>';
  respMssg.style.display = '';
  var URL = 'mailto/form_.php?i='+mid+'&l='+lid;
 }
 var IFormDoc;
 if(!IFrFormObj && document.createElement) {
  try {
   var tempIFrame=document.createElement('iframe');
   tempIFrame.setAttribute('id','MailIform');
   tempIFrame.style.border='0px';
   tempIFrame.style.width='0px';
   tempIFrame.style.height='0px';
   IFrFormObj = document.body.appendChild(tempIFrame);
   if(document.frames) {
    // this is for IE5 Mac
    IFrFormObj = document.frames['MailIform'];
   }
  } catch(exception) {
   // This is for IE5 PC
   ifrhistHTML='<iframe id="MailIform" style="';
   ifrhistHTML+='border:0px;';
   ifrhistHTML+='width:0px;';
   ifrhistHTML+='height:0px;';
   ifrhistHTML+='"><\/iframe>';
   document.body.innerHTML+=ifrhistHTML;
   IFrFormObj = new Object();
   IFrFormObj.document = new Object();
   IFrFormObj.document.location = new Object();
   IFrFormObj.document.location.iframe = document.getElementById('MailIform');
   IFrFormObj.document.location.replace = function(location) {
    this.iframe.src = location;
   }
  }
 }
 if(navigator.userAgent.indexOf('Gecko') !=-1 && !IFrFormObj.contentDocument) {
  // we have to give NS6 a fraction of a second
  // to recognize the new IFrame
  setTimeout('buildMForm("'+mid+'","'+lid+'","'+mname+'","'+mmail+'","'+mmes+'","'+msec+'")',10);
  return false;
 }
 if(IFrFormObj.contentDocument) {
  // For NS6
  // and Firefox 3.0.11
  // and Google Chrome 2.0.172.173
  IFormDoc = IFrFormObj.contentDocument; 
 } else if (IFrFormObj.contentWindow) {
  // For IE5.5 and IE6
  IFormDoc = IFrFormObj.contentWindow.document;
 } else if (IFrFormObj.document) {
  // For IE5 and Opera 9.64
  // and IE 8.0.6001.18763
  // reading: http://www.dyn-web.com/tutorials/iframes/#refs2
  IFormDoc = IFrFormObj.document;
 } else {
  return true;
 }
 IFormDoc.location.replace(URL);
 return false;
}
function handleEMResponse(doc,merr,id,lid) {
 if(typeof(lid)=='undefined')
   lid = '';
 if(merr==1) {
  document.getElementById('theroot').innerHTML = doc.getElementById('EMResponse').innerHTML;
  setTimeout('displayForm("'+id+'","'+lid+'")',20);
 }
 else if(merr==2 || merr==-3) {
  if(document.getElementById('contactdata'))
   document.getElementById('contactdata').innerHTML = doc.getElementById('EMResponse').innerHTML;
  else
   alert('Id contactdata not found!');
 }
 else if(merr==-1) {
  document.getElementById('theroot').style.display = 'none';
  alert("There is no such ID "+id);
 }
 else if(merr==-2) {
  if(document.getElementById('repdisplay'))
   document.getElementById('repdisplay').innerHTML = doc.getElementById('EMResponse').innerHTML;
  else
   alert('Id repdisplay not found!');
  if(document.getElementById('seccode'))
   document.getElementById('seccode').innerHTML = doc.getElementById('secreplace').innerHTML;
  else
   alert('Id contactdata not found!');
  if(document.getElementById('secur1ty_code')) 
   document.getElementById('secur1ty_code').value = '';
  else
   alert('Id secur1ty_code not found!');
 }
 else {
  document.getElementById('theroot').innerHTML = doc.getElementById('EMResponse').innerHTML;
  alert(id);
 }
}


