/*******************************************
             COMMON FUNCTIONS
*******************************************/
function ge()
{
 var ea;
 for( var i = 0; i < arguments.length; i++ ) {
 var e = arguments[i];
 if( typeof e == 'string' )
 e = document.getElementById(e);
 if( arguments.length == 1 )
 return e;
 if( !ea )
 ea = new Array();
 ea[ea.length] = e;
 }
 return ea;
}


function addEvent(obj, type, fn, name_hash)
{
 if (obj.addEventListener)
 obj.addEventListener( type, fn, false );
 else if (obj.attachEvent)
 {
 obj["e"+type+fn+name_hash] = fn;
 obj[type+fn+name_hash] = function() { obj["e"+type+fn+name_hash]( window.event ); }
 obj.attachEvent( "on"+type, obj[type+fn+name_hash] );

 }
}


function removeEvent(obj, type, fn, name_hash)
{
 if (obj.removeEventListener)
 obj.removeEventListener( type, fn, false );
 else if (obj.detachEvent)
 {
 obj.detachEvent( "on"+type, obj[type+fn+name_hash] );
 obj[type+fn+name_hash] = null;
 obj["e"+type+fn+name_hash] = null;
 }
}

/*******************************************
             STRING FUNCTIONS
*******************************************/

function strtoupper(what){
  what.value = what.value.toUpperCase().replace(/([^0-9ABCDEFGHIJKLMNOPRSTUWYZ ])/g,"");
}

function strtopostcode(what){
  return what.toUpperCase().replace(/([^0-9ABCDEFGHIJKLMNOPRSTUWYZ ])/g,"");
}

function strtophone(what){
  return what.replace(/([^0-9 \(\)\+])/g,"");
}

function strtolower(what){
  what.value = what.value.toLowerCase().replace(/([^0-9a-z ])/g,"");
}

function strtofloat(what){
  what.value = what.value.toLowerCase().replace(/([^0-9\.])/g,"");
}

function strtoweblink(what){
  what = what.toLowerCase().replace(/([^0-9a-z\_\-])/g,"_");
  what = what.toLowerCase().replace(/^_/,"");
  return what.replace(/__/,"_");
  }

/*******************************************
           PLACEHOLDER FUNCTIONS
*******************************************/

function placeholderSetup(id) {
 var el = ge(id);
 if(!el) return;
 /*if(el.type != 'text') return;*/

 var ph = el.getAttribute("placeholder");
 if( ph && ph != "" ) {
 el.value = ph;
 el.style.color = '#777';
 el.is_focused = 0;
 addEvent(el, 'focus', placeholderFocus);
 addEvent(el, 'blur', placeholderBlur);
 }
}

function placeholderFocus() {
 if(!this.is_focused) {
 this.is_focused = 1;
 this.value = '';
 this.style.color = '#3F3704';

 var rs = this.getAttribute("radioselect");
 if( rs && rs != "" ) {
 var re = document.getElementById(rs);
 if(!re) { return; }
 if(re.type != 'radio') return;

 re.checked=true;
 }
 }
}

function placeholderBlur() {
 var ph = this.getAttribute("placeholder")
 if( this.is_focused && ph && this.value == "" ) {
 this.is_focused = 0;
 this.value = ph;
 this.style.color = '#777';
 }
}

/* Upload Callback */

var fN = function callBack(o) {
  bbar = ge('progressholder');
//  alert(o.responseText);
  bbar.style.display='';
  var resp = eval('(' + o.responseText + ')');
  var rate = parseInt(resp['rate']/1024);
  if(resp['cancel_upload']) {
    txt="Cancelled after "+parseInt(resp['current']/1024)+"KB"; 
    document.title = "Upload Aborted";
  } else {
    txt=parseInt(resp['current']/1024)+"KB Uploaded Successfully";
    document.title = "Upload Complete";
  }
  txt += "<br>Upload rate was "+rate+" kbps.";
  document.getElementById('progressbar').style.width = '100%';
  document.getElementById('percentage').innerHTML = "100%";
  document.getElementById('uploadmeter').innerHTML = txt;
  document.getElementById('upload_form').style.display = 'none';
  document.getElementById('introText').style.display = 'none';
  document.getElementById('progressholder').style.display = 'none';
  document.getElementById('completed').style.display = 'block';
  
  //setTimeout("window.location.reload(true);",2000);
  //hidebar(); window.locati...
}
var callback = { upload:fN }

var fP = function callBack(o) {
  var resp = eval('(' + o.responseText + ')');
  //var resp = JSON.parse(o.responseText);
  if(!resp['done']) { 
    if(resp['total']) {
      var pct = parseInt(100*(resp['current']/resp['total']));
      document.getElementById('progressbar').style.width = ''+pct+'%';
      if(pct > 49) document.getElementById('progressbar').style.color = '#ffffff'; 
      document.getElementById('percentage').innerHTML = " "+pct+"%";
			var crntByts = parseInt(resp['current']/1024);
			var estTime = parseInt((resp['total']-resp['current'])/resp['speed']);
      if(estTime > 59){

        minss = parseInt(estTime/60);
        secss = estTime - (60*minss);
        if(secss < 10){ estTime = minss+':0'+secss; }else{ estTime = minss+':'+secss; }
        
      }else{ 
        if(estTime < 10){
          estTime = '0:0'+estTime;
        }else{
          estTime = '0:'+estTime;
        }
        
      }

      document.title = 'Uploading: '+pct+'% ('+estTime+' left) - Studio North Exchange';

      document.getElementById('uploadmeter').innerHTML = (crntByts) +" of "+parseInt(resp['total']/1024)+" KB ("+parseInt(resp['speed']/1024)+'KB/sec - '+estTime+' left)';
    }
    setTimeout("update_progress()",1000);
  } else if(resp['cancel_upload']) {
    txt="Cancelled after "+resp['current']+" bytes!"; 
    document.getElementById('uploadmeter').innerHTML = txt;
    setTimeout("hidebar(); window.location.reload(true);",2000);
  }
}
var progress_callback = { success:fP }

function update_progress() {
  progress_key = document.getElementById('progress_key').value;
  var iRand=Math.floor(Math.random()*99999);
  YAHOO.util.Connect.asyncRequest('GET','./?progress_key='+progress_key+'&rnd='+iRand, progress_callback);
}

function hidebar(){
  bbar = ge('progressholder');
  bbar.style.display='none';
}

var progress_win;

function postForm(target,formName) {
  emVal = ge('txtRecipient').value;

  if(ge('test_file').value && emVal && (emVal.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)){
  btn = ge('btnUpload');
  btn.innerHTML = '<img src="images/icon_working.gif" alt="Working" width="16" height="16" /> Working...';
  btn.disabled = true;
  YAHOO.util.Connect.setForm(formName,true);
  YAHOO.util.Connect.asyncRequest('POST',target,callback);
  bbar = ge('progressholder');
  bbar.style.display='';  
  update_progress();
  }else{
    alert('Please complete the recipient\'s e-mail address with a valid e-mail\nand select a file from your computer.');
    return false;
  }
}

