function Initialize() {
return true;
}

///
/// Function "preview" is called to generate a preview of the
///   telugu before submission.
///


function preview(form) {
  var input = form.song.value;
  input = input.replace(/_/g,"\\_");//escape all underscores
 
  prWind=window.open("","Preview","HEIGHT=300,WIDTH=500,TOOLBAR=0,LOCATION=0,"+
         "DIRECTORIES=0,STATUS=0,MENUBAR=1,SCROLLBARS=1,RESIZABLE=1,COPYHISTORY=0")

  outdoc=prWind.document

  writeHeader(outdoc);
  var outline=convertTIK(input);
  window.document.forms[0].tikkana.value=outline;
  endWrite(outline,outdoc);
  prWind.focus();
  return true;
}

function help() {
  helpwin=window.open("","HELP","HEIGHT=400,WIDTH=600,TOOLBAR=0,LOCATION=0,"+
         "DIRECTORIES=0,STATUS=0,MENUBAR=1,SCROLLBARS=1,RESIZABLE=1,COPYHISTORY=0")
  helpwin.location.href="help.html"
  helpwin.focus();
  return true;
}
/// Function "feedback" will open the feedback form into a new window.

function feedback() {
  fbckwin=window.open("","feedback","HEIGHT=400,WIDTH=600,TOOLBAR=0,LOCATION=0,"+
         "DIRECTORIES=0,STATUS=0,MENUBAR=1,SCROLLBARS=1,RESIZABLE=1,COPYHISTORY=0")
  fbckwin.location.href="http://www.ghantasala.info/feedback/index.html"
  fbckwin.focus();
  return true;
}
function submitCheck(){
var message;

// email must be filled

  emailval=window.document.forms[0].email.value
  if(!emailval){
    message="            EMAIL field must be filled \n"+
            "(DONT WORRY: Your email address will not be displayed)"
    alert (message)
    return false}

  if(!emailval.match(".+\@.+\..+")){
    message="Invalid email address"
    alert (message)
    return false}


// song must be filled, or challenge song number
  var input=window.document.forms[0].song.value+window.document.forms[0].challenge.value
  if(!(input)){
    alert ("No Song Entered!")
    return false}


// convert RTS to telugu and save in hidden field
  input=window.document.forms[0].song.value
  var tik=convertTIK(input);
  window.document.forms[0].tikkana.value=tik
  return true
}

function convertTIK(input){

  var iplen = input.length;
  var bnd = nextBnd(input,0);
  var outl="";

// Nothing quoted: Assume it is telugu
  if (bnd == -1) {
    outl=outl+writeAsTelugu(input);
    return outl;
  }

  //echo the first non-RTS block
  if (bnd != 0) writeAsIs(input.substring(0,bnd));

  var lastBnd = bnd;
  var inRTS = true;
  while (lastBnd < iplen-1 && (bnd = nextBnd(input,lastBnd+1)) != -1){

    if (inRTS)  
      outl=outl+writeAsTelugu(input.substring(lastBnd+1,bnd));
    else outl=outl+writeAsIs(input.substring(lastBnd+1,bnd));
    inRTS = !inRTS;
    lastBnd = bnd;

  }
  if (inRTS) 
      outl=outl+writeAsTelugu(input.substring(lastBnd+1));
  else outl=outl+writeAsIs(input.substring(lastBnd+1));
  return outl;
}

//return index of next '#' in str after start
//returns -1 if next hash not found
//start should belong to [0,str.length)
function nextBnd(str, start) {
  var bnd = str.indexOf('#',start);
  if (bnd <= 0) return bnd;
  var strl = str.length;

  //check if the # was escaped
  while (str.charAt(bnd-1) == '\\') {
    if (bnd+1 == strl) return -1;//no hash found
    bnd = str.indexOf('#',bnd+1); //look for next
    if (bnd == -1) return -1; //no hash found
  } 
  return bnd;
}

function writeAsTelugu(str) {
  var tmpstr=new String(window.document.rangavalli.parse(str));
  tmpstr=tmpstr.replace(/\n/g,"<br>")
  return '<FONT FACE=Tikkana SIZE="5"><B>'+tmpstr+'</b></FONT>';
}

function writeAsIs(str) {
  return str.replace(/\\#/g,"#").replace(/\\_/g,"_").replace(/\n\n/g,"<P>");
}

function writeHeader(doc) {
  doc.writeln("<head>");
  doc.writeln("<title>Verify Telugu Spelling</title>");
  doc.writeln("</head>");
  doc.writeln('<body background="back.jpg">');
  doc.writeln('<center><table border="3"><tr><td>');
}

function endWrite(outline,doc) {
  doc.writeln(outline);
  doc.writeln('</td></tr></table>');
  doc.writeln("</body></html>");
  doc.close();
}

