//subscribe form
function clearIfNotSet(elem){
  var newValue 
  if(elem.value == "enter your email"){
    elem.value = "";
  }
  else if(elem.value == ""){
    elem.value = "enter your email";
  }
}
//email form
function hideShow(hide_id,show_id){
  //track email_link
  if(hide_id=='email_link'){ track(document.getElementById(hide_id)); }
  document.getElementById(hide_id).style.display='none';
  document.getElementById(show_id).style.display='block';
  return false;
}
function isEmail(field, event_type){
  var s       = field.value;
  var e_field = field.name + '_error';
  var e_text  = document.getElementById(e_field);
  if(s.match('^[0-9a-z_]([-_.]?[0-9a-z])*@[0-9a-z][-.0-9a-z]*\\.[a-z]{2,4}[.]?$')){
    e_text.innerHTML = '';
  }
  else if(event_type=='blur') {
    e_text.innerHTML = '<b style="color:red; font-weight: bold;">x<\/b>';
    return false;
  }
}
function validateForm(whichform){
  for(var i=0; i<whichform.elements.length; i++){
    var element = whichform.elements[i];
    if(element.className.indexOf('required') != -1){
      if(!element.value.match('^[0-9a-z_]([-_.]?[0-9a-z])*@[0-9a-z][-.0-9a-z]*\\.[a-z]{2,4}[.]?$')){
        alert('Please fill in the required fields');
        return false;
      } 
    } 
  }
  document.form.submit();
}
//ajax
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) {
  try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (E) { xmlhttp = false; }
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  try {  xmlhttp = new XMLHttpRequest();}
  catch (e) {  xmlhttp=false;}
}
if (!xmlhttp && window.createRequest) {
  try {  xmlhttp = window.createRequest();}
  catch (e) {  xmlhttp=false;}
}
//tracking
function track(clicked_url){
  //track subscribe
  if(clicked_url=='subscribe'){ var where_to = clicked_url; var link_type = clicked_url; }
  //all except subcribe
  else{ var where_to = clicked_url.href; var link_type=clicked_url.name; }
  where_to = where_to.replace(/#/,"");
  var loc = "#\/gallery_cms\/functions\/track\/index.php?clicked_url="+where_to+"&clicked_from="+document.location.href+"&link_type="+link_type+"&i="+document.getElementById('issue').value;
  xmlhttp.open("GET.html", loc, true);
  xmlhttp.send(null);
  //delicious
  if(clicked_url.href=='http://del.icio.us/post'){
    window.open('http://del.icio.us/post?v=4&noui&jump=close&url=' + encodeURIComponent(location.href) + '&title=' + encodeURIComponent(document.title), 'delicious','toolbar=no,width=700,height=400');
    return false;
  }
  //facebook
  else if(clicked_url.href=='http://www.facebook.com/share.php?u='+document.location.href){
    u=location.href;
    t=document.title;
    window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
  }
  //ffffound
  else if(clicked_url.href=='http://ffffound.com/'){
    var e = document.createElement('script');
    e.setAttribute('type','text/javascript');
    e.setAttribute('charset','UTF-8');
    e.setAttribute('src','http://ffffound.com/bookmarklet.js');
    document.body.appendChild(e);
    return false;
  }
  //subcribe
  else if(clicked_url=='subscribe'){
    document.subscribe_form.submit();
  }
  //all except email to friend link
  else if(clicked_url.id!='email_link'){
    document.location.href = clicked_url.href;
  }
}
//add tracking event handler to all links
function prepareLinks(){
  var links = document.getElementsByTagName('a');
  for (var i=0; i<links.length; i++) {
    if(links[i].id!='email_link'){ //all except email to friend link
      links[i].onclick = function(){
        track(this);
        return false;
      };
    }
  }
}

