function confirm_base(text) {
  var reply = confirm(text);
  if (reply) {
     return true;
  } else {
     return false;
  }
}

function confirm_del() {
  return confirm_base("This will permanently delete this record!");
}

function confirm_del_user() {
  return confirm_base("Are you sure you wish to delete this user?");
}

function confirm_set_profanity() {
  return confirm_base("Are you sure you wish to set profanity?");
}

function confirm_remove_profanity() {
  return confirm_base("Are you sure you wish to remove profanity?");
}

function confirm_ban_user() {
  return confirm_base("Are you sure you wish to ban user?");
}

function confirm_unban_user() {
  return confirm_base("Are you sure you wish to unban user?");
}

function confirm_queue_approve() {
  return confirm_base("Are you sure you wish to approve this user?");
}

function confirm_queue_deny() {
  return confirm_base("Are you sure you wish to deny this user?");
}

function confirm_change_user() {
  return confirm_base("Are you sure you wish to change the publisher of this article?");
}

function SelectAllCheck(obj, mark, name) {
   var form = obj.form;
   for (i = 0; i < form.elements.length; i++) {
      var item = form.elements[i];
      if (item.name == name) {
         item.checked = mark;
      };
      if (item.name == obj.name) {
         item.checked = obj.checked;
      }
   }
}

function select_check(obj,name, name2) {
   var form = obj.form;
   var k = 0; var j = 0; var x = 0;
   var x = new Array();
   for (i = 0; i < form.elements.length; i++) {
      var item = form.elements[i];
      if (item.name == name) {
         j++;
         if (item.checked) {
           k++;
         }
      }
      if (item.name == name2) {
         x[x.length] = item;
//         x = item;
      }
   }
   if (x.length && j == k) {
      for (i = 0; i < x.length; i++) {
         x[i].checked = true;
      }
//      x.checked = true;
   } else {
      for (i = 0; i < x.length; i++) {
         x[i].checked = false;
      }
//      x.checked = false;
   }
}

opera = (navigator.userAgent.indexOf('Opera') >= 0)? true : false;
ie = (document.all && !opera)? true : false;
dom = (document.getElementById && !ie && !opera)? true : false;

function SetCookie(name, value, expires, path, domain, secure) {
   var mycookie = name + "=" + escape(value);
   var myexpires = new Date();
   myexpires.setTime(myexpires.getTime() + 5000);
   mycookie = mycookie + "; myexpires=" + myexpires.toGMTString();
   if (path) {
      mycookie = mycookie + ";path=" + path;
   }
   if (domain) {
      mycookie = mycookie + ";domain=" + domain;
   }
   document.cookie = mycookie;
}

function GetCookie(name) {
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen) {
     var j = i + alen;
     if (document.cookie.substring(i, j) == arg)  return getCookieVal (j);
     i = document.cookie.indexOf(" ", i) + 1;
     if (i == 0) break;
   }

   return null;
}


function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1) endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}

function validEmail(email) {
  invalidChars = " /:,;";
  if (email == "") {
    return false;
  }
  for (i=0; i<invalidChars.length;i++) {
    badChar = invalidChars.charAt(i);
    if (email.indexOf(badChar,0) > -1) {
      return false;
    }
  }
  atPos = email.indexOf("@",1);
  if (atPos == -1) {
    return false;
  }
  if (email.indexOf("@",atPos+1) > -1) {
    return false;
  }
  periodPos = email.indexOf(".",atPos);
  if (periodPos == -1) {
    return false;
  }
  if (periodPos+3 > email.length) {
    return false;
  }
  return true;
}


function date_to_str(date, month, year) {
  if (date < 10) { 
    date = '0' + date; 
  } 
  if (month < 10) { 
    month = '0' + month; 
  } 
  return month + '/' + date + '/' + year; 
}

function convert_date(str){
   var arr = str.split('-');
   return arr[1] + '/' + arr[2] + '/' + arr[0];
}

function inputUploadInfo(frm) {
   var prefix0 = 'get_';
   var input_array = new Array('name','email','comments');
   var error_array = new Array('Name','Email','Comments');

   for (var i=0; i<input_array.length; i++) {
      var source = prefix0+input_array[i];          
      if (frm.elements[source].value.length == 0) {
         alert('Please enter '+error_array[i]);
         frm.elements[source].focus();
         return false;
      }
   }
 
   return true;
}


var img_i = 1;

function changeImages(id, imagesCount, images_dir) {
   var el = document.getElementById(id);
   var i = img_i;
   el.src = images_dir + sprintf("%08.0d", i) + ".jpg";
   img_i = (i + 1) % imagesCount;
}


function checkContentMod(oldEl, newEl) {
  var el_old = document.getElementById(oldEl);

  if (el_old == null) return true;

  var el_new = document.getElementById(newEl);

  if (el_old.innerHTML != "" && el_new.value == "") {
     return confirm_change_content();
  }
  return true;
}

function confirm_change_content() {
  var reply = confirm("CONFIRM - Are you sure you wish to remove all content for this page?");

  if (reply) {
     return true;
  } else {
     return false;
  }
}

function viewform(m,name, w, h) {
   if (name == '') name = 'pre';
   var mine = window.open('m','pre','width=1,height=1,left=0,top=0,scrollbars=no');
     if(mine && mine != 'undefined') {
         var popUpsBlocked = false;
     } else {
         var popUpsBlocked = true;
     }
     if (popUpsBlocked) {
        alert("Your browser doesn\'t support tabs or there is a Popup blocker in place. You must turn off popup blockers or upgrade to a browser that supports tabs.");
        return false;
     }
   mine.close();  

   var scroll = 'yes';
   var settings;
   var LeftPosition;
   var TopPosition;
   var win;
          
   LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
   TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  
   settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll;
   settings = settings + ',resizable=yes,maximize=no,status=no,toolbar=no,directories=no,menubar=no,location=no';

   if (win != null && win != 'undefined') {
      win.focus();

   } else {       
      win = window.open(m, name, settings);
      if (win && win != 'undefined') {
         win.name = name;
         win.focus();
      } else {
        alert("Your browser doesn\'t support tabs or there is a Popup blocker in place. You must turn off popup blockers or upgrade to a browser that supports tabs.");
      }
   }
   return false;
}

function check_login_form(frm) { 
 var i = 0;
 if (!frm.username.value) { 
     i++;
     document.getElementById('div_username').style.display = 'block'; 
  } else { 
     document.getElementById('div_username').style.display = 'none';
  }
  if (!frm.password.value) {
     i++;
     document.getElementById('div_password').style.display = 'block'; 
  } else { 
     document.getElementById('div_password').style.display = 'none';
  }
  if (i) {
     document.getElementById('div_login_err').style.display = 'none';
     return false;
  }
  return true;
}



function getBrowserWindowSize() {
   var bodyWidth = document.documentElement.clientWidth;
   var bodyHeight = document.documentElement.clientHeight;
   var bodyWidth, bodyHeight; 
   if (self.innerHeight){ // all except Explorer 
      bodyWidth = self.innerWidth; 
      bodyHeight = self.innerHeight; 
   }  else if (document.documentElement && document.documentElement.clientHeight) {
      // Explorer 6 Strict Mode     
      bodyWidth = document.documentElement.clientWidth; 
      bodyHeight = document.documentElement.clientHeight; 
   } else if (document.body) {// other Explorers      
      bodyWidth = document.body.clientWidth; 
      bodyHeight = document.body.clientHeight; 
   } 
   return [bodyWidth,bodyHeight];    
}

function resizeStaticMessage(w,h) {
   mm = document.getElementById('DHTMLSuite_modalBox_contentDiv'); 
   div = document.getElementById("gallerydetail");
   span = document.getElementById("gallerytitle");
   if (mm) {
       div.style.width = mm.offsetWidth;
       div.style.heigth = mm.offsetHeight;
       span.style.width = mm.offsetWidth-82;
   }
   return false;
}

function showLargeImage() {
  document.getElementById('smallimage').style.display='none';
  document.getElementById('largeimage').style.display='block';
  img = document.getElementById("imglarge");
  if (img) {
    if (img.width > img.height && img.width>GALLERY_IMAGE_MAX_WIDTH) img.width= GALLERY_IMAGE_MAX_WIDTH;
    if (img.height >= img.width && img.height>GALLERY_IMAGE_MAX_HEIGHT) img.height= GALLERY_IMAGE_MAX_HEIGHT;
    if (img.width > 400 || img.height> 200) {
      div = document.getElementById("gallerydetail");
      span = document.getElementById("gallerytitle");
      var brSize = getBrowserWindowSize();
      var bodyWidth = brSize[0];
      var bodyHeight = brSize[1];
      mm = document.getElementById('DHTMLSuite_modalBox_contentDiv'); 
      w = img.width+44;
      div.style.width = w+"px";
      w1 = img.width-38;
      span.style.width = w1+"px";
      mm.style.width = w+"px";
      var topOffset = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
      if (img.height > bodyHeight-60) {
         topOffset -= 60;
      }
      var tmpWidth = img.width+44;  
      var tmpHeight = img.height+44;
      mm.style.left = Math.ceil((bodyWidth - tmpWidth) / 2) + 'px';
      mm.style.top = (Math.ceil((bodyHeight - tmpHeight) / 2) +  topOffset) + 'px';
    }
  }
  return false;
}

  function validateItemAdd() {
    xajax_validateItemAdd(xajax.getFormValues("GalleryItemForm"));
    return false;
  }
  function enable_rate_article(id) {
    document.getElementById('stars_cant_rate'+id).style.display = 'none';
    document.getElementById('stars_can_rate'+id).style.display = 'block';
    return false;
  }

function onReadMoreClick(id, container) {
   var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
   var sl = Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
//   showProgressBar(); 
   //   collapseBox('item_desc_' + id, container, 0.65, 0.30, 0, id); 
//   setTimeout('closeMessage();window.scrollTo(' + sl + ',' + st + ');', CLOSE_MESSAGE_DELAY * 1000);
   return false;
}

function verifySendForm(ref_user) {
   xajax_sendMessage(xajax.getFormValues("SendForm"),ref_user);
   return false;
}
function verifySendToFriendForm(ref_article) {
   xajax_sendToFriend(xajax.getFormValues("SendToFriendForm"), ref_article);
   return false;
}
function verifySendReportAbuse(id) {
   el = document.getElementById('rate_allowed_'+id);
   if (el != null && el.value == 1) {
      xajax_addArticleAbuseReport(id);
   }
   return false;
}
function show_abuse_link(show,id) {
  if (show == 1) {
     document.getElementById('report_abuse'+id).style.display ='inline';
  }
}


function verifyInviteForm() {
   var InviteFormSubmitting = document.getElementById("InviteFormSubmitting");
   if (!InviteFormSubmitting.value) {
      InviteFormSubmitting.value = 1;
      xajax_inviteFriend(xajax.getFormValues("InviteForm"));
   }
   return false;
}
//*****************prevent living page************//
function set_modified(){
  cf_modified = true;
}

function ignore_modified() {

  if ((typeof(root.onbeforeunload) != "undefined" && root.attachEvent) || root.addEventListener) {
       root.onbeforeunload = null;
  }
}

function init(){
  if ((typeof(root.onbeforeunload) != "undefined" && root.attachEvent) || root.addEventListener) {

      window.onbeforeunload = function(evt){

        if (cf_modified) return ''; 
      }
  }
  else return;

  for (var i = 0; oCurrForm = document.forms[i]; i++){
    for (var j = 0; oCurrFormElem = oCurrForm.elements[j]; j++){
       element = oCurrForm.elements[j];
       if (element != null && element != "undefined") {
            if (oCurrFormElem.addEventListener) oCurrFormElem.addEventListener("change", set_modified, false);
            else if (oCurrFormElem.attachEvent) oCurrFormElem.attachEvent("onchange", set_modified);
       }
    }
    if (oCurrForm.addEventListener) oCurrForm.addEventListener("submit", ignore_modified, false);
    else if (oCurrForm.attachEvent) oCurrForm.attachEvent("onsubmit", ignore_modified);
  }
}
//*****************end prevent living page************//

function manageGalleryWhileEditArticle() {
// var body = frames['XinhaIFrame_html_area_body'].document.body.innerHTML;
 var body = frames['html_area_body_ifr'].document.body.innerHTML;
 xajax_manageGalleryWhileEditArticle(xajax.getFormValues("ModArticleForm"), body);
}

function manageGalleryWhileEditResponse(id) {
// var body = frames['XinhaIFrame_html_area_body'].document.body.innerHTML;
 var body = frames['html_area_body_ifr'].document.body.innerHTML;
 xajax_manageGalleryWhileEditResponse(xajax.getFormValues("ModResponseForm"), id, body);
}
function ChangeDiv(div1, div2) {
   document.getElementById(div1).style.display = "inline";
   document.getElementById(div2).style.display = "none";
}
function searchContacts() {
   xajax_searchContacts(xajax.getFormValues("SearchContactForm"));
}
function verifySearchContactsForm() {
 xajax_inviteFoundContacts(xajax.getFormValues("SearchContactForm"));
 return false;
}

function allow_submit(field_name, code) {
   var allow_field = document.getElementById(field_name);
   if (allow_field.value) {
      allow_field.value = "";
      eval(code);
      return true;
   } else {
      return false;
   }
}

function search_allow_submit(el) {
   if (el.value) {
      return true;
   } else {
      el.focus();
      return false;
   }
}

function check_search_in(el) {
   var el_all = document.getElementById("get_search_in_all");
   if ((el == el_all) && el.checked) {
      document.getElementById("get_search_in_title").checked = true;
      document.getElementById("get_search_in_tags").checked = true;
      document.getElementById("get_search_in_author").checked = true;
      document.getElementById("get_search_in_body").checked = true;
   } else {
      if (!el.checked) {
         el_all.checked = false;
      }
   }
}

function adjust_search_dates(el) {
   if (el.options[el.selectedIndex].value) {
      var date_from = new Date();
      var date_to = new Date();
      var el_month_from = document.getElementById("get_month_from");
      var el_day_from   = document.getElementById("get_day_from");
      var el_year_from  = document.getElementById("get_year_from");
      var el_month_to   = document.getElementById("get_month_to");
      var el_day_to     = document.getElementById("get_day_to");
      var el_year_to    = document.getElementById("get_year_to");
      switch (el.options[el.selectedIndex].value) {
         case "24hours":
                  date_from.setTime(date_from.getTime() - 1000*60*60*24 - 1);
                  break;
         case "3days":
                  date_from.setTime(date_from.getTime() - 1000*60*60*24*3);
                  break;
         case "week":
                  date_from.setTime(date_from.getTime() - 1000*60*60*24*7);
                  break;
         case "month":
                  date_from.setMonth(date_from.getMonth() - 1);
                  break;
         case "0":
                  el_month_from.selectedIndex = 0;
                  el_day_from.selectedIndex = 0;
                  el_year_from.selectedIndex = 0;
                  el_month_to.selectedIndex = 0;
                  el_day_to.selectedIndex = 0;
                  el_year_to.selectedIndex = 0;
                  return;
      }

      var year_from = date_from.getFullYear();
      var year_to = date_to.getFullYear();
      el_month_from.selectedIndex = date_from.getMonth() + 1;
      el_day_from.selectedIndex = date_from.getDate() + 1;
      el_month_to.selectedIndex = date_to.getMonth() + 1;
      el_day_to.selectedIndex = date_to.getDate() + 1;
      for (i = 0; i < el_year_from.options.length; i++) {
         if (el_year_from.options[i].value == year_from) {
            el_year_from.selectedIndex = i;
            break;
         }
      }
      for (i = 0; i < el_year_to.options.length; i++) {
         if (el_year_to.options[i].value == year_to) {
            el_year_to.selectedIndex = i;
            break;
         }
      }
   }
}

function set_custom_date() {
   var el = document.getElementById("get_date");
   el.selectedIndex = el.options.length - 1;
}
