var MainWindow=false;
var ReportReadyToPost = false;
var CommentReadyToPost = false;

function PhoneCheck(e)
{
  if (!e) e = window.event;
  //ev = window.event;
  var KeyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
  //document.getElementById('debugwin').innerHTML= " "+KeyCode;
  return ((KeyCode == 8) // backspace
        || (KeyCode == 9) // tab
        || (KeyCode == 13) // enter
        || (KeyCode == 35) // Home
        || (KeyCode == 36) // End
        || (KeyCode == 37) // left arrow
        || (KeyCode == 39) // right arrow
        || (KeyCode == 46) // delete .
        || (KeyCode == 45) // -
        || (KeyCode == 44) // ,
        || ((KeyCode > 47) && (KeyCode < 58)) // 0 - 9
        );
}

function EmailCheck(email)
{
 var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
 return emailfilter.test(email);
}

function AjaxResponse(text,exec)
{
  //alert(text);
  if (!exec) exec = false;
  msg = text.substr(0,3);
  textcut = text.substr(4);
  if (msg == 'OKE')
   eval(textcut);
  else
  if (msg == 'OKI');
  else
  if (msg == 'ERE')
    eval(textcut);
  else
  if (msg == 'ERR' && exec)
    eval (exec);
  else
    error(text);
}

function error(text)
 {
  if (!text)
  // text ='ERROR!<br>Please contact the <a href="mailto:g-tester@inbox.lv">administration</a>';
   text ='ERROR! Please contact the administration';
  //make_confirmation('<img src="/img/err.gif" style="float:left;">'+text+'</a>!',260,110,'','OK!');
  alert(text);
 }


/*function show_report_form(phone)
{
 name = readCookie('user_nickname');
 if (!name) name = "";
 if (!phone) phone = "";
 var seed = Math.random();
 seed = seed * 1000000;
 seed = Math.ceil(seed);
 var text = '<div class="mainheader1" style="margin:15px 20px 10px 20px;">Report a Caller</div><center><table style="font-size:13px;margin-bottom:15px;"><tr><td style="" colspan="2" style="height:21px;"><div id="wposterror" style="text-align:center; border:1px solid #DD0000; background-color:#FFEFEF;color:#DD0000; visibility:hidden;">&nbsp;</div></td></tr><tr><td style="width:200px;">Your Name or Nickname</td><td style="text-align:left;"><input type="text" id="wreport_name" value="'+name+'" class="forminpt" onchange="check_form(\'w\')"></td></tr><tr><td>Caller\'s Phone Number</td><td style="text-align:left;"><input type="text" id="wreport_phone" class="forminpt" value="'+phone+'" style="vertical-align:middle;" onchange="check_form(\'w\');check_phone(this.value,\'w\');"> <span id="wphonestatus" style="font-size:11px;color:#AAAAAA;"></span></td></tr><tr><td>Caller Name (optional)</td><td style="text-align:left;"><input type="text" id="wreport_phoneowner" class="forminpt" onchange="check_form(\'w\')"></td></tr><tr><td>Caller Type</td><td style="text-align:left;"><select style="height:23px;" id="wreport_callertype" onchange="check_form(\'w\')"><option value="1">Telemarketer</option><option value="2">Dept Collector</option><option value="3">Political Call</option><option value="4">Prank Caller</option><option value="5" selected="true">Unknown</option></select></td></tr><tr><td style="vertical-align:top;">Call Details</td><td><textarea id="wreport_details" style="width:370px; height:130px; border:1px solid #AAAAAA" onchange="check_form(\'w\')"></textarea></td></tr><tr><td>Confirmation code</td><td><table><tr><td><div style="border:1px solid #AAAAAA"><img id="wauthimg" src="/authcode.php?seed='+seed+'" style="width:80px; height:19px;border:0px;"></td><td><input type="text" class="forminpt" id="wreport_authcode" style="width:100px;" onchange="check_form(\'w\')"></td></tr></table></td></tr><tr><td></td><td style="text-align:left;padding-top:15px;"><input type="button" onclick="go_report(\'w\');" id="wreport_btn" class="btn_blank btn_blank_dim" onmouseover="this.className=\'btn_blank btn_blank_lit\'" onmouseout="this.className=\'btn_blank btn_blank_dim\'" value="Submit report"></td></tr></table></center>';
 MainWindow = showwinbox(false,false,620,400,'Report a caller',false,true);
 SetWinResponseText2(text,MainWindow,true);
}*/

function refresh_authcode(add)
{
 var seed = Math.random();
 seed = seed * 1000000;
 seed = Math.ceil(seed);
 if (!add) add = '';
 document.getElementById(add+'authimg').src='/authcode.php?seed='+seed;
}

function setWrong(obj,cls)
{
  obj.className = cls+' '+cls+'_wrong';
}

function setRight(obj,cls)
{
  obj.className = cls;
}

function check_phone(phone,add)
{
  if (!add) add='';
  document.getElementById(add+'phonestatus').innerHTML = ' <img src="/img/smallloader.gif" style="vertical-align:middle;width:20px;height:20px;"> Quering Database...';
  post_str="&action=checkphone&phone="+encodeURI(phone);
  AjaxRequest(post_str,'AjaxResponse(text,"'+add+'");',false);
}

function parse_phone_result(entries,add)
{
 if (!add) add='';
 var msg = '';
 if (entries == 0)
   msg = '(No Entries found)';
 else
  msg='(Found '+entries+' entries)';
 document.getElementById(add+'phonestatus').innerHTML = msg;
}

function check_form(add)
{
  if (!add) add='';
  ReportReadyToPost = true;
  var Inputs  = new Array(document.getElementById(add+'report_name'),document.getElementById(add+'report_phone'),document.getElementById(add+'report_details'),document.getElementById(add+'authcode'));
  var InputsC = new Array(document.getElementById(add+'report_name_c'),document.getElementById(add+'report_phone_c'),document.getElementById(add+'report_details_c'),document.getElementById(add+'report_authcode_c'));
  var Classes = new Array('forminpt','forminpt','formtxtfield','formauthcode');
  var Lengths = new Array (1,10,1,5)
  for (i = 0; i < Inputs.length; i++)
  {
    if ((Inputs[i].value).length < Lengths[i])
    {
      ReportReadyToPost = false;
      setWrong(InputsC[i],Classes[i]);
    }
    else
      setRight(InputsC[i],Classes[i]);
  }
  
  if (!EmailCheck(document.getElementById(add+'report_email').value))
  {
    ReportReadyToPost = false;
    setWrong(document.getElementById(add+'report_email_c'),'forminpt');
  }
  else
    setRight(document.getElementById(add+'report_email_c'),'forminpt');
  
  
  if (!ReportReadyToPost)
  {
    document.getElementById(add+'posterror').style.visibility="visible";
    document.getElementById(add+'posterror').innerHTML = 'Fields in red are not filled or filled incorrectly.';
  }
  else
  {
    document.getElementById(add+'posterror').style.visibility="hidden";
    document.getElementById(add+'posterror').innerHTML = '&nbsp;'
  }
}

function check_comment_form(add)
{
  if (!add) add='';
  CommentReadyToPost = true;
  var Inputs  = new Array(document.getElementById(add+'comment_name'),document.getElementById(add+'comment_details'),document.getElementById(add+'authcode'));
  var InputsC = new Array(document.getElementById(add+'comment_name_c'),document.getElementById(add+'comment_details_c'),document.getElementById(add+'comment_authcode_c'));
  var Classes = new Array('forminpt','formtxtfield','formauthcode');
  var Lengths = new Array (1,1,5)
  for (i = 0; i < Inputs.length; i++)
  {
    if ((Inputs[i].value).length < Lengths[i])
    {
      CommentReadyToPost = false;
      setWrong(InputsC[i],Classes[i]);
    }
    else
      setRight(InputsC[i],Classes[i]);
  }
  
  if (!EmailCheck(document.getElementById(add+'comment_email').value))
  {
    CommentReadyToPost = false;
    setWrong(document.getElementById(add+'comment_email_c'),'forminpt');
  }
  else
    setRight(document.getElementById(add+'comment_email_c'),'forminpt');
  
  
  if (!CommentReadyToPost)
  {
    document.getElementById(add+'posterror').style.visibility="visible";
    document.getElementById(add+'posterror').innerHTML = 'Fields in red are not filled or filled incorrectly.';
  }
  else
  {
    document.getElementById(add+'posterror').style.visibility="hidden";
    document.getElementById(add+'posterror').innerHTML = '&nbsp;'
  }
}

function go_subscribe()
{
  if (!EmailCheck(document.getElementById('subscribe_email').value))
  {
    document.getElementById('posterror').innerHTML = 'Incorrect e-mail!';
    document.getElementById('posterror').style.visibility="visible";
    return false;
  }
  else
    return true;
}

function incor_email(add)
{
  if (!add) add='';
  document.getElementById(add+'posterror').style.visibility="visible";
  document.getElementById(add+'posterror').innerHTML = 'Invalid e-mail address!.';
  setWrong(document.getElementById(add+'report_email_c'),'forminpt');
  alert('Invalid e-mail address!\n\r');
  document.getElementById(add+'report_email').focus();
}

function incor_authcode(add)
{
  if (!add) add='';
  document.getElementById(add+'posterror').style.visibility="visible";
  document.getElementById(add+'posterror').innerHTML = 'Incorrect confirmation code!.';
  setWrong(document.getElementById(add+'report_authcode_c'));
  document.getElementById(add+'authcode').value = '';
  alert('Wrong Confrimation Code!\n\r Please Try Again!');
  refresh_authcode(add);
  document.getElementById(add+'authcode').focus();
}

function go_report(add)
{
  if (!add) add='';
  check_form(add);
  var name = encodeURI(document.getElementById(add+'report_name').value).replace(/&/g, "%26");
  var email = encodeURI(document.getElementById(add+'report_email').value).replace(/&/g, "%26");
  var phone = encodeURI(document.getElementById(add+'report_phone').value).replace(/&/g, "%26");
  var callerid = encodeURI(document.getElementById(add+'report_callerid').value).replace(/&/g, "%26");
  var phoneowner = encodeURI(document.getElementById(add+'report_phoneowner').value).replace(/&/g, "%26");
  var callertype = document.getElementById(add+'report_callertype').value;
  var details = encodeURI(document.getElementById(add+'report_details').value).replace(/&/g, "%26");
  var authcode = document.getElementById(add+'authcode').value;
  createCookie('user_nickname',name,60);

  if (ReportReadyToPost)
  {
    post_str="&action=postreport&name="+name+"&email="+email+"&phone="+phone+"&phoneowner="+phoneowner+"&callertype="+callertype+"&text="+details+"&authcode="+authcode;
    AjaxRequest(post_str,'AjaxResponse(text,"'+add+'");',showloadbox(document.getElementById(add+'report_btn')));
  }
}

function go_add_comment(add,postto)
{
  if (!add) add='';
  if (!postto) return false;
  check_comment_form(add);
  var name = encodeURI(document.getElementById(add+'comment_name').value).replace(/&/g, "%26");
  var email = encodeURI(document.getElementById(add+'comment_email').value).replace(/&/g, "%26");
  var details = encodeURI(document.getElementById(add+'comment_details').value).replace(/&/g, "%26");
  var id = document.getElementById(add+'comment_id').value;
  var authcode = document.getElementById(add+'authcode').value;
  createCookie('user_nickname',name,60);
  createCookie('user_email',email,60);

  if (CommentReadyToPost)
  {
    post_str="&action=postcomment&postto="+postto+"&id="+id+"&name="+name+"&email="+email+"&text="+details+"&authcode="+authcode;
    AjaxRequest(post_str,'AjaxResponse(text,"'+add+'");',showloadbox(document.getElementById(add+'report_btn')));
  } 
}


function show_buttons(id)
{
  if (buts = document.getElementById('comment_buttons_'+id))
    buts.style.visibility = 'visible';
}

function hide_buttons(id)
{
  if (buts = document.getElementById('comment_buttons_'+id))
    buts.style.visibility = 'hidden';
}

function vote(id,dir)
{
  if (dir == "up")
   dir = "up";
  else
   dir = "dn";
  FadeOut(document.getElementById('vote_btn_'+id),true,true,'<img src="/img/smallloader.gif" style="vertical-align:middle">');
  post_str="&action=vote&id="+id+"&dir="+dir;
  AjaxRequest(post_str,'createCookie("voted_comment_'+id+'","'+dir+'",365);AjaxResponse(text);',false);
}

function vote_comment(id,postto,dir)
{
  if (dir == "up")
   dir = "up";
  else
   dir = "dn";
   
  FadeOut(document.getElementById('vote_btn_'+id),true,true,'<img src="/img/smallloader.gif" style="vertical-align:middle">');
  post_str="&action=votecomment&id="+id+"&postto="+postto+"&dir="+dir;
  AjaxRequest(post_str,'createCookie("voted_'+postto+'_comment_'+id+'","'+dir+'",365);AjaxResponse(text);',false);
}

