
var theUniqueID = (new Date()).getTime() % 1000000000;
var gUserID = '';
var IsClicked = false;
var uploadImgMin = 0;
var uploadImgMax = 4000;
function SubmitInformationForm() {
  if(IsClicked) return;
  IsClicked = true;
  var req = new XMLHttpRequest();  
  var URL = window.baseUrl  + 'competition/register.asp';
  var FormQuery = getFormQuery();   
  var ELEM1 = document.getElementById('ErrorInfo');
  var ELEM2 = document.getElementById('UploadInfo');
  var ELEM3 = document.getElementById('EnterPhtoInfo');
  ELEM2.style.display = 'none';
  ELEM3.style.display = 'block';
  //regenerate the ID;
  theUniqueID = (new Date()).getTime() % 1000000000;
  req.open('POST', URL, false);
  //open(URL + '?' + FormQuery);
  //Send the proper header information along with the request
  req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  req.setRequestHeader("Content-length", FormQuery.length);
  req.setRequestHeader("Connection", "close");
  req.send(FormQuery + "&rand=" + Math.random());
  //Reading response from the server
  if (req.readyState == 4) {
    var Text = req.responseText;
    if(Text.substr(0,4) == 'OK\r\n') {
    	ELEM3.style.display='none';
    	ELEM2.innerHTML = '<P style="margin-top:20px;">Submiting photo. Please wait...</P>';
      SubmitPhotos(Text.substr(4));
    } else {
      ELEM1.innerHTML = Text;
      ELEM1.style.display = 'block';
      IsClicked = false;
    }
  }      
}


function SubmitPhotos(UserID) {
  gUserID = UserID;
	var Params = 
	'ImageSetId=' + UserID + 
	'&RefImageSetId=COMPETITION1' + 
	'&Limit=0-4' + 
	'&ThumbnailPath=contact/' + UserID  + '/images' + 
	'&MediumPath=contact/' + UserID  + '/images/hi-res' + 
	'&LargePath=contact/' + UserID  + '/images/hi-res' + 
	'&UploadPath=contact/' + UserID  + '/images/original' +
	'&ID=' + theUniqueID
  var ELEM1 = document.getElementById('ErrorInfo');
  var ELEM2 = document.getElementById('UploadInfo');
  var ELEM3 = document.getElementById('EnterPhtoInfo');
  ELEM1.style.display = 'none';
  ELEM2.style.display = 'block';
  ELEM3.style.display = 'none';

  var FORM = document.forms['RegisterForm'];
  var LastFormURL = window.baseUrl  + '_Imageset/upload/upload.asp';
  FORM.action = LastFormURL + "?" + Params;
  FORM.target = "IFrameUpload"
  FORM.submit();
  OpenProgressBar(UserID);
}

function OpenProgressBar(UserID) {  
  var PROGRESS = document.getElementById('UploadInfo');
  req = new XMLHttpRequest(); 
  req.onreadystatechange = function() {
    if (req.readyState==4) {
      var srvMesg = req.responseText;
      eval(srvMesg);
      PROGRESS.innerHTML = getProgressBar(ProgressInfo);
      PROGRESS.style.display = "block"      
      if(ProgressInfo['Started'].toLowerCase() == "true" &&
        ((parseInt(ProgressInfo['BytesTotal'])/1024) < uploadImgMin || 
        (parseInt(ProgressInfo['BytesTotal'])/1024) > uploadImgMax)) 
      {
        ErrorMessage('Image must be between 1MB and 3MB in JPG,\nPNG or GIF formats.')
        return;
      } else if(ProgressInfo['PercentDone'] == "100") {
        UploadCompleted(ProgressInfo);
        return;
      } else {
        window.setTimeout('OpenProgressBar()', 1000);
      }
    } //if (req.readyState==4)
  } //req.onreadystatechange 
  req.open("GET", window.baseUrl + '_Imageset/upload/progressbar_info.asp?ID=' + theUniqueID + '&amp;rand=' + Math.random(), true);
  req.send("");
}

function ErrorMessage(Mesg) {
  var ELEM1 = document.getElementById('ErrorInfo');
  var ELEM2 = document.getElementById('UploadInfo');
  var ELEM3 = document.getElementById('EnterPhtoInfo');  
  ELEM1.innerHTML = Mesg;
  ELEM1.style.display = 'block';
  ELEM3.style.display = 'block';
  ELEM2.style.display = 'none'
  IsClicked = false;
  if(document.getElementById('IFrameUpload')) {
    document.getElementById('IFrameUpload').src='about:blank';
  }
}

function getProgressBar(theProgress) {
  return(
  "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" id=\"AutoNumber1\">" + "\n" + 
  "<tr>" + "\n" + 
  "  <td>" + "\n" + 
  "  <p>Now Uploading: Please Wait:</p>" + "\n" + 
  "  </td>" + "\n" + 
  "</tr>" + "\n" + 
  "</table>" + "\n" + 
  "<table border=\"0\" width=\"90%\" cellpadding=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" cellspacing=\"0\">" + "\n" + 
  "<tr>" + "\n" + 
  "  <td width=\"100%\">" + "\n" + 
  "  <p>&nbsp;</p>" + "\n" + 
  "  </td>" + "\n" + 
  "  <td>" + "\n" + 
  "  <p>&nbsp;</p>" + "\n" + 
  "  </td>" + "\n" + 
  "  <td>" + "\n" + 
  "  <p>&nbsp;</p>" + "\n" + 
  "  </td>" + "\n" + 
  "</tr>" + "\n" + 
  "<tr>" + "\n" + 
  "  <td bgcolor=\"#999999\" width=\"100%\">" + "\n" + 
  "  <table border=\"0\" width=\"" + theProgress['PercentDone'] + "%\" cellspacing=\"1\" xbgcolor=\"#0033FF\" style=\"border-style: solid; border-width: 0px; background-color: #D9D2CE\">" + "\n" + 
  "  <tr>" + "\n" + 
  "    <td>" + "\n" + 
  "    <p>&nbsp;</p>" + "\n" + 
  "    </td>" + "\n" + 
  "  </tr>" + "\n" + 
  "  </table>" + "\n" + 
  "  </td>" + "\n" + 
  "  <td>" + "\n" + 
  "  <p>&nbsp; </p>" + "\n" + 
  "  </td>" + "\n" + 
  "  <td>" + "\n" + 
  "  <p>" + theProgress['PercentDone'] + "%</p>" + "\n" + 
  "  </td>" + "\n" + 
  "</tr>" + "\n" + 
  "<tr>" + "\n" + 
  "  <td width=\"100%\">" + "\n" + 
  "  <p>&nbsp;</p>" + "\n" + 
  "  </td>" + "\n" + 
  "  <td>" + "\n" + 
  "  <p>&nbsp;</p>" + "\n" + 
  "  </td>" + "\n" + 
  "  <td>" + "\n" + 
  "  <p>&nbsp;</p>" + "\n" + 
  "  </td>" + "\n" + 
  "</tr>" + "\n" + 
  "</table>" + "\n" + 
  "<table border=\"0\" cellpadding=\"3\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" cellspacing=\"0\">" + "\n" + 
  "<tr>" + "\n" + 
  "  <td>" + "\n" + 
  "  <p>Estimated time left:</p>" + "\n" + 
  "  </td>" + "\n" + 
  "  <td nowrap>" + "\n" + 
  "  <p>"+ parseInt(parseInt(theProgress['SecondsLeft']) / 60) + " min " + (parseInt(theProgress['SecondsLeft']) % 60) + "\n" + 
  "  sec&#39;s (" + Math.round(parseInt(theProgress['BytesDone']) / 1024) + " KB of " + Math.round(parseInt(theProgress['BytesTotal']) / 1024) + "\n" + 
  "  KB uploaded)</p>" + "\n" + 
  "  </td>" + "\n" + 
  "</tr>" + "\n" + 
  "<tr>" + "\n" + 
  "  <td>" + "\n" + 
  "  <p>Elapsed time</p>" + "\n" + 
  "  </td>" + "\n" + 
  "  <td nowrap>" + "\n" + 
  "  <p>" + parseInt(parseInt(theProgress['ElapsedSeconds']) / 60, 2) + " min " + (parseInt(theProgress['ElapsedSeconds']) % 60) + "\n" + 
  "  sec&#39;s (" + parseInt(theProgress['ElapsedSeconds']) + " Seconds)</p>" + "\n" + 
  "  </td>" + "\n" + 
  "</tr>" + "\n" + 
  "<tr>" + "\n" + 
  "  <td>" + "\n" + 
  "  <p>Transfer Rate:</p>" + "\n" + 
  "  </td>" + "\n" + 
  "  <td nowrap>" + "\n" + 
  "  <p>" + Math.round(parseInt(theProgress['BytesPerSecond'])/1024, 1) + " KB/sec</p>" + "\n" + 
  "  </td>" + "\n" + 
  "</tr>" + "\n" + 
  "<tr>" + "\n" + 
  "  <td>" + "\n" + 
  "  <p>Status:</p>" + "\n" + 
  "  </td>" + "\n" + 
  "  <td nowrap>" + "\n" + 
  "  <p align=\"left\">" + theProgress['Note'] + "</p>" + "\n" + 
  "  </td>" + "\n" + 
  "</tr>" + "\n" + 
  "</table>" + "\n")
}

function UploadCompleted(theProgress) {
  var myIFrame = document.getElementById('IFrameUpload');
  var imessage = myIFrame.contentWindow.document.body.innerHTML;
  
  var ELEM1 = document.getElementById('ErrorInfo');
  var ELEM2 = document.getElementById('UploadInfo');
  var ELEM3 = document.getElementById('EnterPhtoInfo');
  
  if(imessage.indexOf("Error") >= 0) {
  	ELEM1.style.display = 'block';
  	ELEM2.style.display = 'none';
  	ELEM3.style.display = 'block';
  	var Index = imessage.indexOf('Total images');
  	if(Index > 0) {
  		imessage = imessage.substring(imessage, Index);
  		imessage = imessage.replace(/\<BR\>/g,'');
  	}
    ELEM1.innerHTML = imessage;  
    IsClicked = false;
  } else if(theProgress['Note'] == "Finishing..." && theProgress['PercentDone'] == "100") {
  	UpdateUserToCompetition();
    IsClicked = false;
  }
}

function UpdateUserToCompetition() {  
  var req = new XMLHttpRequest(); 
  var URL = window.baseUrl + 'competition/set.asp?ref=' + gUserID;
  req.onreadystatechange = function() {
    if (req.readyState == 4) document.location.href = 'thankyou.asp';
  } //req.onreadystatechange 
  req.open("GET", URL, true);
  req.send("");  
}

function getFormQuery() {
  var FORM = document.forms['RegisterForm'];
  var Query = '';      
  for (var i = 0; i < FORM.length; i++){    
    var isBuildQuery = true;
    var Value = '';  
    switch(FORM[i].type + '') {
    case 'select-multiple':
      //Value = WizardFormSelectAll(FORM[i]);    
      break;
    case 'radio':
    case 'checkbox':    
      if(FORM[i].checked) Value = FORM[i].value;    
      break;
    case 'password':
    case 'text':
    case 'select-one':
    case 'hidden':
      Value = FORM[i].value;    
      break;    
    case 'file':    
      Value = FORM[i].value;    
      break;    
    case 'button':
    case 'submit':
    case 'undefined':
    case 'image':
      isBuildQuery = false;
      break;    
    default:
      Value = FORM[i].value;
      alert('Wizard Submit error for field :' + FORM[i].name + 
      '\nField Type:' + FORM[i].type + '\n'+
      '\nPlease fix it in wizard.js');
      break;
    }
    if(isBuildQuery && Value != '') {
      if(Query != '') Query += '&';
      Query += FORM[i].name + '=' + escape(Value);
    }//if(FORM[i].type != 'button')
  } 
  //alert(Query);
  return Query;  
}
