///////////////////////////////  VIEMAS-ESOFT PROJECT  ////////////////////////////
//                                                                            //
//             PROGRAMMING AND SOLUTIONS BY LUONG TUAN ANH                    //
//             CONTACT EMAIL : proschooltechsite@yahoo.com                    //
//                                                                            //
//  you can redistribute and modify this module under the terms of GNU General//
//  Public License as published by the Free Software Foundation either version//
//  2 of the License. I saw someone use open sources and solutions but they   //
//  delete all the author's copyright from modules and replace by their       //
//  copyright. That's not good and not like real programmers. You're reading  //
//  this copyright of my module. Hope you like it and don't remove copyright  //
//                      www.programmingschool.net                             //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
  function AjaxDefaultAction(where, url){
         xmlHttp = new GetXmlHttpObject()
         if(xmlHttp==null){
                 return;
                 }
         xmlHttp.onreadystatechange= function(){
                 if(xmlHttp.readyState==4 || xmlHttp.readyState == 200){
                          document.getElementById(where).innerHTML = xmlHttp.responseText
                         }
                 }
         xmlHttp.open("GET", url, true);
         xmlHttp.send(null);
  }

  function AjaxActionNon(where, url){
         xmlHttp = new GetXmlHttpObject()
         if(xmlHttp==null){
                 return;
                 }
         var bar = '<img border="0" src="images/loading.gif" width="16" height="16" style="margin-left: 5; margin-right: 5">';
         document.getElementById(where).innerHTML = bar
         xmlHttp.onreadystatechange= function(){
                 if(xmlHttp.readyState==4 || xmlHttp.readyState == 200){
                          document.getElementById(where).innerHTML = xmlHttp.responseText
                         }
                 }
         xmlHttp.open("GET", url, true);
         xmlHttp.send(null);
        }

  function AjaxAction(where, url){
         xmlHttp = new GetXmlHttpObject()
         if(xmlHttp==null){
                 return;
                 }
         var bar = '<img border="0" src="images/loading.gif" style="margin-left: 5; margin-right: 5; float: left;"> Loading data...';
         document.getElementById(where).innerHTML = bar
         xmlHttp.onreadystatechange= function(){
                 if(xmlHttp.readyState==4 || xmlHttp.readyState == 200){
                          document.getElementById(where).innerHTML = xmlHttp.responseText
                         }
                 }
         xmlHttp.open("GET", url, true);
         xmlHttp.send(null);
        }

  function AjaxPostAction(where, url, params){
         xmlHttp = new GetXmlHttpObject()
         if(xmlHttp==null){
                 return;
                 }
         var bar = '<table border="0" width="150" height="21" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF"><tr><td height="19" width="13" class="smalltext" align="right"><img border="0" src="images/loading.gif" width="16" height="16" style="margin-left: 5; margin-right: 5"></td><td height="19" width="150" class="smalltext" align="center">Loading data...</td></tr></table>';
         document.getElementById(where).innerHTML = bar
         xmlHttp.onreadystatechange= function(){
                 if(xmlHttp.readyState==4 || xmlHttp.readyState == 200){
                          document.getElementById(where).innerHTML = xmlHttp.responseText
                         }
                 }
         xmlHttp.open("POST", url, true);
         xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
         xmlHttp.setRequestHeader("Content-length", params.length);
         xmlHttp.setRequestHeader("Connection", "close");
         xmlHttp.send(params);
        }
/*checking phone number (mobile+phoneline)*/
  function phoneCheck(type, number){
      if(type=='mobile'){
        var inValid = /^(\(\+\d{1,3}\))? ?(\d{4,10})?$/i
           if(number.match(inValid)){return true;}else{return false;}
      }else if(type=='phoneline'){
        var inValid = /^(\+\d{1,3})? ?(\(\d{2,5}\))? ?(\d{3,10})?$/i;
           if(number.match(inValid)){return true;}else{return false;}
      }
  }
/*Register data from user*/
/*Phone Alert*/
 function phoneAlert(type, number, where){
     if(phoneCheck(type, number)==true){
       document.getElementById(where).innerHTML = 'This data is ok';
     }else if(phoneCheck(type, number)==false){
       document.getElementById(where).innerHTML = 'Maybe is not in right way';
     }
 }
/*Phone Alert*/

  function register(step){
   if(step==1){
    var username          = document.Register1.username.value
    var email             = document.Register1.email.value
    var verify_email      = document.Register1.verify_email.value
    var password          = document.Register1.password.value
    var verify_password   = document.Register1.verify_password.value
    var userKey           = /^[a-z]{4,11}([0-9]{2,3})?$/i
    var emailKey          = /^\w(\.?[\w-])*@\w(\.?[-\w])*\.[a-z]{2,4}$/i;
    if(username.length ==0 || username.length > 14 || username.match(userKey)==null){
       confirm("Username's Maximum must be 14 characters, minimum must be 4 characters and we don't support special chars!");
    }else{
       if(email.match(emailKey)==null){
         confirm("Maybe your email is not in right way");
       }else{
         if(email!=verify_email){
           confirm("Check your verify email please!");
         }else{
           if(password.length==0 || password!=verify_password){
             confirm("Check your password and verify password please");
           }else{
               var username = document.Register1.username.value
               var email    = document.Register1.email.value
               var password = document.Register1.password.value
               var data     = "&username="+username+"&email="+email+"&pass="+password;
               RegMe('2','reg1',data);
           }
         }
       }
    }
   }

   if(step==2){
    var icq    = document.Register2.icq.value;
    var skype  = document.Register2.skype.value;
    var yahoo  = document.Register2.yahoo.value;
    var msn    = document.Register2.msn.value;
    var mobile = document.Register2.mobile.value;
    var cemail = document.Register2.contact_email.value;
    var emailKey          = /^\w(\.?[\w-])*@\w(\.?[-\w])*\.[a-z]{2,4}$/i;
         data = "/icq|"+icq+"/skype|"+skype+"/yahoo|"+yahoo+"/mobile|"+mobile+"/cemail|"+cemail
         RegMe('4','reg2',data);
   }

   if(step==3){
    var sex='';
    if(document.Register3.gender1.checked==true && document.Register3.gender2.checked==true){
            sex += '0';
            }else if(document.Register3.gender1.checked==true){
                    sex += '1';
                    }else if(document.Register3.gender2.checked==true){
                            sex +='2';
                    }
    var birthday = '';
        var bday   =  document.Register3.day.options[document.Register3.day.selectedIndex].value
        var bmonth =  document.Register3.month.options[document.Register3.month.selectedIndex].value
        var byear  =  document.Register3.year.options[document.Register3.year.selectedIndex].value
        birthday += bday + '/' + bmonth + '/' + byear;
        var country = document.Register3.country.options[document.Register3.country.selectedIndex].value;
    var myData = new Array(document.Register3.first_name.value, document.Register3.last_name.value, document.Register3.identifier.value, document.Register3.hphone.value, sex, birthday, document.Register3.address.value, country, document.Register3.occupation.value, document.Register3.city.value, document.Register3.zipcode.value)
    var myIndex = new Array('first_name','last_name','identifier','homephone','sex','birthday','address','country','occupation','city','zipcode');
    var check = 0;
    var data  = '';
       for(var i=0; i<11; i++){
/*          data +='&'+myIndex[i]+'='+myData[i];*/
            data +='&'+myIndex[i]+'='+myData[i];
       RegMe('6','reg3',data);
       }
   }

  if(step==4){
   var GD = document.Register4.GDSecurity.value;
   var encode = document.Register4.security_GD.value;
   var question = document.Register4.secuques.value;
   var answer   = document.Register4.security_answer.value;
   var data ='';
       data += '/question|'+question+'/answer|'+answer;
   if(question.length == 0 || answer.length == 0){
     confirm('you must choose a question and answer it');
   }else if(GD!=encode){
     confirm('The security code is not matched');
   }else{
       RegMe('8', 'reg4', data);
   }
  }
 }
/*Checking password Level*/
  function PassLevel(password){
   var pass = new Array();
   var basic      = "abcdefghijklmnopqrstuvwxyz";
   var medium     = "0123456789";
   var strong     = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";
   var lev1       = 0;
   var lev2       = 0;
   var lev3       = 0;
   for(var i=0; i<password.length; i++){
     pass[i] = password.charAt(i);
   }
   for (var i=0; i<password.length; i++){
     if(basic.match(pass[i])){
       lev1++;
     }else if(medium.match(pass[i])){
       lev2++;
     }else if(strong.match(pass[i])){
       lev3++;
     }
   }
   if(lev1>3 && lev2>1 && lev3>1){
      document.getElementById("strong").innerHTML = '<table border=0  cellspacing="0" cellpadding="0"><tr><td width="15%" align="center" bgcolor="#FFCC00"><font color="#C0BFCE" size="1" face="Tahoma"><b>Strong</b></font></td></tr></table>'
      document.getElementById("medium").innerHTML = '<table border=0  cellspacing="0" cellpadding="0"><tr><td width="15%" align="center" bgcolor="#0000FF"><font color="#C0BFCE" size="1" face="Tahoma"><b>Medium</b></font></td></tr></table>'
      document.getElementById("weak").innerHTML = '<table border=0  cellspacing="0" cellpadding="0"><tr><td width="15%" align="center" bgcolor="#CC0000"><font color="#C0BFCE" size="1" face="Tahoma"><b>Weak</b></font></td></tr></table>'
   }else if(lev1>=0 && lev2==0 && lev3==0){
      document.getElementById("weak").innerHTML = '<table border=0  cellspacing="0" cellpadding="0"><tr><td width="15%" align="center" bgcolor="#CC0000"><font color="#C0BFCE" size="1" face="Tahoma"><b>Weak</b></font></td></tr></table>'
   }else{
      document.getElementById("medium").innerHTML = '<table border=0  cellspacing="0" cellpadding="0"><tr><td width="15%" align="center" bgcolor="#0000FF"><font color="#C0BFCE" size="1" face="Tahoma"><b>Medium</b></font></td></tr></table>'
      document.getElementById("weak").innerHTML = '<table border=0  cellspacing="0" cellpadding="0"><tr><td width="15%" align="center" bgcolor="#CC0000"><font color="#C0BFCE" size="1" face="Tahoma"><b>Weak</b></font></td></tr></table>'
   }
  }
/*Auto Generating a password*/
  function GenIt(){
    /*Generating Security Password*/
    var basic      = "abcdefghijklmnopqrstuvwxyz";
    var medium     = "0123456789";
    var strong     = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";
    /*Generating basic password in Array*/
    var GenBasic   = "";
    var MyPass     = "";
    for(var i=0; i<9; i++){
      for(var j=0; j<4; j++){
       GenBasic = GenBasic + basic.charAt(Math.ceil(Math.random()*24))
      }
      GenBasic = GenBasic + medium.charAt(Math.ceil(Math.random()*8)) + strong.charAt(Math.ceil(Math.random()*32))
    }
    for (var i=0; i<10; i++){
       MyPass  = MyPass + GenBasic.charAt(Math.ceil(Math.random()*44))
    }
    document.Register1.password.value = MyPass;
    document.Register1.verify_password.value = MyPass;
    PassLevel(MyPass);
  }
 /*Generating security code for unti automated registrations*/
  function GD(){
    var Upcase = new String('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
    var LowCase = new String('abcdefghijklmnopqrstuvwxyz');
    var Number  = new String('0123456789');
    var myString = Upcase + LowCase + Number;
    var VarMe    = '';
    var GD   = '';
    for(var i=0; i<myString.length; i++){
       VarMe += myString.charAt(Math.ceil(Math.random()*(myString.length-1)));
    }
    for(var i=0; i<7; i++){
       GD += VarMe.charAt(Math.ceil(Math.random()*(VarMe.length-1)));
    }
    document.Register4.GDSecurity.value = GD;
  }

 /*checking GD code from user*/
  function ReSet(){
    document.Register1.generate.value = 'Generate';
    document.getElementById("weak").innerHTML = '<font color="#C0BFCE" size="1" face="Tahoma"><div id="weak">Weak</div></font></b>'
    document.getElementById("medium").innerHTML = '<font color="#C0BFCE" size="1" face="Tahoma"><div id="medium">Medium</div></font></b>'
    document.getElementById("strong").innerHTML = '<font color="#C0BFCE" size="1" face="Tahoma"><div id="strong">Strong</div></font></b>'

  }

  //function on index
//view topic from index

function view_topic(id, locate, title){
        var url = "?name=News&cs=viewstory&id="+id+"&where="+locate;
        var  e = window.event || e;
        var brinfo = Browser.properties();
        if(locate!='view_in_stage'){
            var control = document.getElementById(locate);
            if(control){
                AjaxAction(locate, url,'dotloading');
            }else{
                myWindow.create(e, title, locate+id, 874, brinfo[1]-25, "article", 'yes', 'yes', 'yes', 'topleft');
                AjaxAction(locate+id+'_win_maincontent', url,'dotloading');                
            }
        }else{
            AjaxAction(locate, url,'dotloading');
        }
}


//editing function onindex for user
function uEditIt(where, value, name, type, id, sql, row, rowch){
     var url = "?module/name|Account/op|UserEdit"
     var params = "&value="+value+"&nam="+name+"&type="+type+"&id="+id+"&sql="+sql+"&row="+row+"&rowch="+rowch+"&where="+where
     AjaxPostAction(where, url, params);
}

function UsaveMod(value, where, sql, id, row, rowch){
         var url = "?module/name|Account/op|UserSaveEdit/value|"+value+"/sql|"+sql+"/id|"+id+"/row|"+row+"/rowch|"+rowch
         if(value.length==0){
          alert('value is empty')
         }else{
           AjaxAction(where, url);
         }
        }

function userChangePass(){
        var oldpass = document.changePass.old_pass.value;
        var newpass = document.changePass.new_pass.value;
        if(oldpass.length==0 || newpass.length==0){
                confirm('retry again please');
        }else{
          var url = "?module/name|Account/op|userchangpass/key|change/oldpass|"+oldpass+"/newpass|"+newpass;
          AjaxAction('userchangepass', url)
        }
}

function sendAvatarUpload(key, value){
            var url = "?module/name|Account/op|changeAvatar/key|"+key+"/link|"+value;
            AjaxAction('userchangeavatar', url);
}

function OpenUploadAvatar(){
            var url="?module/name|Account/op|changeAvatar/key|uploadform";
            AjaxAction('action_stage', url);
}

function refreshAvatar(where){
            var url = "?module/name|Account/op|refreshAvatar";
            AjaxAction(where, url);
}

function OpenItemCat(where , id, tran)
{
	var url = "?module&name=projects&cs=fincat&id="+id;
        var control = document.getElementById('LTA_psonline_download_file_content');
        if(control){
            AjaxAction('LTA_psonline_download_file_content', url);
            control.style.overflow = 'auto';
        }else{
            Dialogs.newInfoWindow(url ,'LTA_psonline_download_file','Programming School Online', true, false, true, 670, 450, 670, 450, 'center');
            var control = document.getElementById('LTA_psonline_download_file_content');
            control.style.overflow = 'auto';
        }
}

function OpenItemDocCat(where, id, tran)
{
    var url = "?name=Projects&cs=viewdocat&docid="+id;
    AjaxAction(where, url);
}

function ProgressBar(bar){
	var progress ='<table border="0" width="195" height="36" cellspacing="0" cellpadding="0"><tr><td height="13" width="195" align="center"><img border="0" src="images/loadingbar.gif" width="150" height="13"></td></tr><tr><td height="23" width="195" align="center">file is uploading</td></tr></table>';
	document.getElementById(bar).innerHTML = progress;
}

function PagingFile(where, url)
{
	AjaxAction(where, url);
}

function viewDownloadDetail(where, td, id)
{
	var url = "?module&name=projects&cs=detail&td="+td+"&dlid="+id;
	AjaxAction(where, url);
} 
    function scrollMe(table, content, imgcloseid, imgopenid)
    {
        var xheight = document.getElementById(table).clientHeight;
        var yheight = document.getElementById(table).clientHeight;
        document.getElementById(imgcloseid).style.display = 'none';
        document.getElementById(imgopenid).style.display = 'block'; 
        document.getElementById(content).style.height = xheight - 25;
        document.getElementById(content).style.overflow  = "auto";
        document.getElementById(table).style.height = xheight - 25;
        if(xheight-28 > 0)
        {
            setTimeout("scrollMe('"+table+"','"+ content+"','"+imgcloseid+"','"+imgopenid+"')", 15);
        }else{
            document.getElementById(content).style.color = '#ffffff';  
            document.getElementById(content).style.visibility = 'hidden';
            document.getElementById(content).style.height = "1px";
            document.getElementById(table).style.height = 23;            
        }  
    }

    function scrollMeOut(table, content, imgcloseid, imgopenid)
    {
       document.getElementById(content).style.color = '#000000';
       document.getElementById(imgcloseid).style.display = 'block';
       document.getElementById(imgopenid).style.display = 'none';       
			 document.getElementById(content).style.height = "";
       document.getElementById(table).style.height = "";
       document.getElementById(content).style.overflow  = 'none'; 
       document.getElementById(content).style.visibility = 'visible';        
    }
    
function displayStory(num)
{
    for(var i = 1; i < stories+1; i++)
    {
        if(num==i)
        {
            document.getElementById('story'+i).style.display = 'block';
        }else{
            document.getElementById('story'+i).style.display = 'none';
        }
    }
}   
 /************************************/
/* PSO BookMark                      */
/* Author : Vu Kien                  */
/* Email  : vutrungkiennd@gmail.com  */
 /************************************/  
function getbookmark(where,fid)
{
    if (document.getElementById(where).innerHTML == "")
    {
    var url = "?name=bookmark&op=listbookmarkin&fid="+fid;
    AjaxAction(where, url);
    }else{
    document.getElementById(where).innerHTML = "";
    }
}


function rename(where,fid)
{
    var idtext = "t"+fid;
    var name = document.getElementById(idtext).value;
    params="&fid="+fid+"&name="+name; 
    url="?name=bookmark&op=renamebookmarkfolder";
    AjaxPostAction (where, url, params);
}

function renameb(where, bid)
{
    var name = document.getElementById('textbookmark').value;
    
}

function addnewbfolder(where)
{
    var title= document.getElementById('titlebookmarkfolder').value;
    var url = "?name=bookmark&op=addnewfolder";
    var params= "&titlefolder="+title;
    AjaxPostAction(where, url, params);
}

function deletefolder(where,fid)
{
   var url = "?name=bookmark&op=deletebookmarkfolder";
   var params = "&fid="+fid;
   AjaxPostAction(where, url, params);
}
 
function deletebookmark(where,bid)
{
    var url = "?name=bookmark&op=deletebookmark";
    var params = "&bid="+bid;
    AjaxPostAction(where, url, params);
}   

function renamebookmark(bid)
{
    var value=document.getElementById(bid).innerHTML; 
    document.getElementById(bid).innerHTML="<input type='text' name='T1' size='20' id='textbookmark'>";
    document.getElementById('test').value = value;
}
function renamefolder(id)
{
     var idtext = "t"+id;
     var title = document.getElementById(id).innerHTML;
     document.getElementById(idtext).type= 'text';
     document.getElementById(idtext).value= title;
     document.getElementById(id).innerHTML="";
     var rename = "confrename"+id;
     document.getElementById(rename).innerHTML="Rename";
     
}

function showoption(fid)
{
    var rename = "rename"+fid;
    document.getElementById(rename).innerHTML="rename";
}

 function bookmarknow(where,idstory)
{
    var title= document.getElementById('titleb').value;
    var fid = document.getElementById('selectf').value;
    var params = '&title='+title+'&select='+fid+'&id='+idstory;
    var url = '?name=bookmark&op=bookmarkitnow';
    AjaxPostAction(where, url, params);
}
function hiddenoption(fid)
{
    var rename = "rename"+fid;
    document.getElementById(rename).innerHTML="";
}

function showdelete(fid,op)
{
    var deletef= op+fid;
    document.getElementById(deletef).innerHTML="delete";
}

function hiddendelete(fid,op)
{
    var deletef= op+fid;
    document.getElementById(deletef).innerHTML="";
}

 function open_download(browser){
   if(browser=='NetScape'){
     document.getElementById('downloadbutton').innerHTML = '<a href="download/Netscape.zip"><img border="0" src="images/netscape.gif"></a>'
   }

   if(browser=='FireFox'){
     document.getElementById('downloadbutton').innerHTML = '<a href="download/Firefox301.zip"><img border="0" src="images/firefoxdownload.gif"></a>'
   }

   if(browser=='Opera'){
     document.getElementById('downloadbutton').innerHTML = '<a href="download/Opera952.zip"><img border="0" src="images/operadownload.gif"></a>'
   }
}

function browserDownloadLoading(){
    document.getElementById('downloadbutton').innerHTML = 'Loading data, please wait a moment...';
}


