window.onbeforeunload = UnloadPage; 
var LockDown=0; 
var NavOk=0;  
var PG = "";
var repval;
var picsrep;
var webrep;
var emailrep;
var datarep;
var tbpicsrep = "<table><tr><td>Create Report First</td></tr></table>";
var tbwebrep = "<table><tr><td>Create Report First</td></tr></table>";
var tbemailrep = "<table><tr><td>Create Report First</td></tr></table>";
var tbdatarep = "<table><tr><td>Create Report First</td></tr></table>";
var visrep = "<table><tr><td>Create Report First</td></tr></table>";
GetRep=0;

var TotBackGrround;
var BackGrounds;
var BKI;
var BKStart;
var eBack;
var eSelBack; 
var Ord=false;
var Sorting= new String(); 
      //alert(iPG);
      //alert(NavOk);
      //if (event.keyCode == 116) {
      //  alert("refresh");
      //}
      

function SortCol(col){
//alert(col); 

var sort = new String(col);
//alert(sort); 
//alert(Sorting); 
if(sort.indexOf(Sorting)==0){
Ord = !Ord;
//alert(Ord); 
}

Sorting=sort;
eSort = document.getElementById("Sort"); 
eSort.value = sort; 
eOrd = document.getElementById("Ord");
if(Ord==true) 
eOrd.value = "1"; 
else
eOrd.value = "0"; 
NavOk=1; 
//alert(Ord); 
eAC = document.getElementById("AC");
//alert(eAC);   
eAC.value = ""; 

fTradeForm = document.getElementById("TradeForm2");
fTradeForm.submit();    
        
}  
           
function ShowTeamEdit(inx){

    fEditTeam = document.getElementById("fEditTeam"+inx); 
    
    bEdit = document.getElementById("bEdit"+inx); 
    EditTeam = document.getElementById("EditTeam"+inx); 
    bEditTeam = document.getElementById("bEditTeam"+inx);  
    bEdit.style.visibility = "hidden";
    EditTeam.style.visibility = ""; 
    bEditTeam.style.visibility = ""; 
    //alert(bEdit);
}

function SubCopyForm(){
r = confirm("Do you want to copy your Bud Roster to this team?");

if(r==true){
//alert(r);    
fCopyForm = document.getElementById("CopyForm");
//alert("a"); 
fCopyForm.submit();
}
else{
    return false;
}
}          

function SubEditTeam(inx){
EditTeam = document.getElementById("EditTeam"+inx);
if(EditTeam.value.length==0){ 
    alert("Please enter new team name.");
    return false;
}
r = confirm("Do you want to change team name to "+EditTeam.value+" ?");
if(r==true){
fEditTeam = document.getElementById("fEditTeam"+inx); 
fEditTeam.submit();
}
else{
    return false;
}
} 

    
function ViewTrade(){
    //Registry
    //alert("ViewTrade");
    PG = "";
    NavOk=1;    
    fViewForm = document.getElementById("ViewForm2");
    fViewForm.submit();   
}
function ViewRoster(){
    //Registry
    //alert("roster");
    PG = "";
    NavOk=1;
    //document.location.href = "roster.php";      
    fViewForm = document.getElementById("ViewForm2");
    fViewForm.submit();   
}
function CancelTrades(){
    //Registry
    //alert("nva");
    PG = "";
    NavOk=1;      
    fTradeForm = document.getElementById("TradeForm2");
    fTradeForm.submit();   
}

function toggle(div_id) {
    var el = document.getElementById(div_id);
    if ( el.style.display == 'none' ) {el.style.display = 'block';}
    else {el.style.display = 'none';}
}

function blanket_size(popUpDivVar,content) {
    if (typeof window.innerWidth != 'undefined') {
        viewportheight = window.innerHeight;
    } else {
        viewportheight = document.documentElement.clientHeight;
    }
    if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
        blanket_height = viewportheight;
    } else {
        if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
            blanket_height = document.body.parentNode.clientHeight;
        } else {
            blanket_height = document.body.parentNode.scrollHeight;
        }
    }
    //var blanket = document.getElementById('blanket');
    //blanket.style.height = blanket_height + 'px';
    var popUpDiv = document.getElementById(popUpDivVar);
    popUpDiv_height=blanket_height/2-200;//150 is half popup's height
    //alert(popUpDiv_height);
    popUpDiv.style.top = '550px';
    
    //GET CONTENT AND POPULATE WINDOW
    popUpDiv.innerHTML = content; //"<a href=\"\" onclick=\"popup('popUpDiv')\">Click Me To Close</a>";
}

function getPlayer(GSISID,Week){
    httpObject2 = getHTTPObject();
    if (httpObject2 != null) {
        URL = "Player.php?GSISID="+GSISID + "&Week=" + Week;
        //alert(URL);
        httpObject2.open("POST", URL, true);
        //alert("SENT ajax_roster.php?UID="+UID + "&WEEK=" + weekID);
        httpObject2.send(null);
        httpObject2.onreadystatechange = setPlayer;
    }
}

function setPlayer(){
   if(httpObject2.readyState == 4){
       //alert('Setting db_roster content');
       //document.getElementById("popUpDiv").innerHTML = httpObject2.responseText;
        if(httpObject2.status == 200){
           //alert(httpObject2.responseText);
           showpopup(httpObject2.responseText);   
        } 
   }else{
        //alert("Not Setting");   
    }
}

function window_pos(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerHeight;
    } else {
        viewportwidth = document.documentElement.clientHeight;
    }
    if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
        window_width = viewportwidth;
    } else {
        if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
            window_width = document.body.parentNode.clientWidth;
        } else {
            window_width = document.body.parentNode.scrollWidth;
        }
    }
    var popUpDiv = document.getElementById(popUpDivVar);
    window_width=window_width/2-250;//150 is half popup's width
    popUpDiv.style.left = window_width + 'px';
}       

function popup(GSISID,Week){
    getPlayer(GSISID,Week);
}
    
function showpopup(content) { 
    blanket_size('popUpDiv',content);
    window_pos('popUpDiv');
    toggle('blanket');
    toggle('popUpDiv');        
}

function Trade(ERR){
    if(LockDown==1){
        CancelTrades();
        return false;
    }
    NavOk=1; 
      
    aTrading = document.getElementsByName("trading");
    sel = false;
    for(v=0;v<aTrading.length;v++){
        //alert(aTrading[v].checked); 
        if(aTrading[v].checked == true){
            sel = true;
            break;
        }
    }
    //alert(sel);
    if(sel == false){
        alert(ERR);
        return false;
    }      
    fTradeForm = document.getElementById("TradeForm2");
    //alert(fTradeForm);
    fTradeForm.submit();    
}

function ComitTrade(){
    //Registry
    //alert("ComitTrade");
    NavOk=1;      
    fTradeForm = document.getElementById("TradeForm2");
    fTradeForm.submit();    
}
      
function UnloadPage(){  
var sPG = new String(PG); 
var iPG = sPG.indexOf("confirm");
//alert(PG); 
if(iPG == 0 & NavOk == 0){
    return 'Your trades are not confirmed. Cancel to stay and confirm trade!';  
}
}
  
function swap(){
    if (document.images){
        for (var x=0;
        x<swap.arguments.length;
        x+=2) {
        document[swap.arguments[x]].src = eval(swap.arguments[x+1] + ".src");
        }
    }
}

function ClearQ4c(){
    e = document.getElementsByName("OptIn-4");
    //alert(e);  
    for(v=0;v<6;v++){
        e[v].checked = false;
    }
}

function ClearQ4o(){
    e = document.getElementById("OptIn-4-1");
    //alert(e);  
    e.value = "";
}

function MOV(id,img){ 
    //alert(document.getElementById(id).src); 
    document.getElementById(id).src = img.src; 
    
}

function swap(){
    if (document.images){
        for (var x=0;
        x<swap.arguments.length;
        x+=2) {
        document[swap.arguments[x]].src = eval(swap.arguments[x+1] + ".src");
        }
    }
}

function Login() { 
    //alert("aa");
    fLoginForm = document.getElementById("Login"); 
    fLoginForm.submit();    
}


function SearchPlayer(){
    eSearch = document.getElementById("searchfield");
    //alert(search.value);
    if(eSearch.value.length==0){ 
        alert("Please enter players last name.");
        return false;
    } 
    NavOk=1;     
    fSearchForm = document.getElementById("SearchForm2");
    fSearchForm.submit();    
}



function Register(){
    //Registry
    fRegForm = document.getElementById("Registry");
    fRegForm.submit();    
}
                                   
function getHTTPObject()
{
    var xmlhttp;
    /*@cc_on @if (@_jscript_version >= 5) try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } @else xmlhttp = false; @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
        try {
            xmlhttp = new XMLHttpRequest();
        }
        catch (e){
            return false;
        }
    }
    return xmlhttp;
}

function isIE()
{
return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function NumberOnly(f) {
    //alert("b"); 
    var re = /^[0-9]*$/;
    if (!re.test(f.value)) {
        f.value = f.value.replace(/[^0-9]/g,"");
        return false;
    }
    return true;
}

function MSDopwn(f) {  
    
    var st = new String(f.value);
    //alert(st.indexOf("ENTER YOUR PIN HERE"));
    if(st.indexOf("ENTER YOUR PIN HERE")==0 | st.indexOf("ENTREZ VOTRE NIP ICI")==0){
        f.value="";
        document.getElementById("UID").maxLength = 11;
    }
}
      


function CheckJustEmailField(e,L){
    var em = document.getElementById("Email"); 
    var che = CheckEmailField(em.value);
    if(che<1){
        e.returnValue = false;
        if(L==1) 
            alert("Email is not a properly formatted.");
        else
            alert("Adresse de courriel est pas un adresse électronique correctement composé.");
        return false;
    }
    return true;
}

function CheckEmailField(check){

//Valid entries
//A-Z a-z '-
    var bad=0;
    var sCheck = new String(check);
    if(sCheck.length<6)return -1;
    var sValid = new String("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@.,!#$%&'*+-/=?^_`{|}~");
    for(var x=0;x<sCheck.length;x++){
        if(sValid.indexOf(sCheck.charAt(x))==-1){
            bad=1;
            break;
        }
    }
    if(bad==1){
        return 0;
    }
    //if(sCheck.length-sCheck.lastIndexOf(".")>4)return 0;//last char more then 3 in last
    //if(sCheck.lastIndexOf(".")>sCheck.length-3)return 0;//last dot less then 3 from last
    //if(sCheck.indexOf(".")<3)return 0;
    if(sCheck.indexOf("@")==0)return 0;
    if(sCheck.indexOf(".")==-1)return 0;
    if(sCheck.indexOf("@")==-1)return 0;
    if(sCheck.lastIndexOf("@")==sCheck.length-1)return 0;
    if(sCheck.lastIndexOf("@")!=sCheck.indexOf("@"))return 0;//double
    return 1;
}

function VerifyForm(){
    var pe = document.getElementById("postal");
    var vl = new String(pe.value);  
    vl = vl.toUpperCase();
    pe.value = vl;

    // fill any void birthday fields
    var element = document.getElementById("birthday");
    sb = new String(element.value);
    //alert(sb);
    sb = sb.replace(/ /g,''); 
    sp = sb.split("/");
    var d=""+sp[0];
    var m=""+sp[1];
    if(sp[0].length==1)
        d=" "+sp[0];
    if(sp[0].length==0)
        d="  ";       
    if(sp[1].length==1)
        m=" "+sp[1];
    if(sp[1].length==0)
        m="  ";        
    sb=d+"/"+m+"/"+sp[2];
    if(sb.length==6)
        sb+="    ";
    if(sb.length==7)
        sb+="   ";
    if(sb.length==8)
        sb+="  ";
    if(sb.length==9)
        sb+=" ";
    //alert(sb); 
    element.value=sb;
}

function SetLan(lan){
	var LAN = document.getElementById("LAN");
	LAN.value = lan;
	//alert("s "+LAN.value );
	return 0;
}

function SubForm(e,L){
    if(VerifyUID(e,L)==true)
        document.forms[0].submit();
}

function VerifyUID(e,L){
    var UID = document.getElementById("UID");
    var sCheck = new String(UID.value);
    sCheck = sCheck.replace(' ','');
    UID.value = sCheck;
    if(sCheck.length < 9 | sCheck.length > 11){
        //if(L==1)
        alert("The PIC number is not the right length. Please enter the 11 digit number found on your card.\n\nLe format du numéro de photo est incorrect. Veuillez s.v.p. saisir le numéro de onze (11) chiffres indiqué sur votre carte.");
        e.returnValue = false; 
        return false;
    }
    e.returnValue = true; 
    return true;
}

function SubmitNumber(){
//Valid entries
//A-Z a-z '-
    var UID = document.getElementById("UID");
    //alert(UID);
    var bad=0;
    var sCheck = new String(UID.value);
    if(sCheck.length==0){
        //alert("Please enter an 11 number pin found on your card 1");
        return false;
    }
    return true;
    // not using now
    var sValid = new String("1234567890");
    for(var x=0;x<sCheck.length;x++){
        if(sValid.indexOf(sCheck.charAt(x))==-1){
            bad=1;
            break;
        }
    }
    if(bad==1){
        alert("Please enter an 11 number pin found on your card");
        return false;
    }
    return 1;
}

function CheckNameField(check){
    
//Valid entries
//A-Z a-z '-
    var bad=0;
    var sCheck = new String(check);
    if(sCheck.length<2)return -1;
    var sValid = new String(" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'-");
    for(var x=0;x<sCheck.length;x++){
        if(sValid.indexOf(sCheck.charAt(x))==-1){
            bad=1;
            break;
        }
    }
    if(bad==1){
        return 0;
    }
    return 1;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
/*
var sorry="Sorry. Right click is not available.";

function click(e)
{
   if (document.all)
   {
      if (event.button == 2)
      {
         alert(sorry);
         return false;
      }
   }
   if (document.layers)
   {
      if (e.which == 3)
      {
         alert(sorry);
         return false;
      }
   }
}
if (document.layers)
{
   document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown=click;
        */
function Load(){
    del=0;
    repval="";
    reppos=0;
}

function SetCursorPosition(obj, pos) { 
    if(obj.createTextRange) {
        /* IE Create a TextRange, set the internal pointer to
           a specified position and show the cursor at this
           position
        */ 
        var range = obj.createTextRange();
        range.move("character", pos); 
        range.select(); 
    }
    else if(obj.selectionStart) { 
        //Gecko
        obj.focus(); 
        obj.setSelectionRange(pos, pos); 
    } 
}

function GetEndCursorPosition(obj){
    var end;
    if(obj.createTextRange){
        // IE
        end = Math.abs(document.selection.createRange().moveEnd("character", -1000000)); 
    }        
    else{
        // Gecko
        end   = obj.selectionEnd; 
    }
    return end;
}

function GetStartCursorPosition(obj){
    var start;
    if(obj.createTextRange){
        // IE
        start = Math.abs(document.selection.createRange().moveStart("character", -1000000));
    }        
    else{
        // Gecko
         start = obj.selectionStart;
    }
    return start;
}
/////

function BDayMouseUp(sObj) {
    //return;
    // position at last number if space before it
    var element = document.getElementById(sObj);
    var sPos = GetStartCursorPosition(element);
    var sValue = new String(element.value);
    var sLen = sValue.length;
    // verify position
    if(sPos==0|sPos==1){
        var lchp1 = sValue.indexOf(" ",0);
        var lchp2 = sValue.indexOf(" ",1);
        if(lchp1 == 0 & lchp2 == 0){//if(lchp < sPos & lchp !=-1){
            var curpos = SetCursorPosition(element,0);
            return;
        }
    }
    if(sPos==3|sPos==4){
        var lchp1 = sValue.indexOf(" ",3);
        var lchp2 = sValue.indexOf(" ",4);
        //alert(lchp1);
        //alert(lchp2);
        if(lchp1 == 3 & lchp2 == 4){//if(lchp < sPos & lchp !=-1){
            var curpos = SetCursorPosition(element,3);
            return;
        }
    } 
    //alert(sPos);
    if(sPos==6|sPos==7|sPos==8|sPos==9|sPos==10){
        //alert("aa");
        var lchp1 = sValue.indexOf(" ",6);
        var lchp2 = sValue.indexOf(" ",7);
        var lchp3 = sValue.indexOf(" ",8);
        var lchp4 = sValue.indexOf(" ",9);        
        if(lchp1 == 6 & lchp2 == 7 & lchp3 == 8 & lchp4 == 9){
            var curpos = SetCursorPosition(element,6);
            return;
        }
    }   
    else if(sPos == 2 | sPos == 5){//if(lchp==3 | lchp==7)
        sPos=sPos+1;
        var curpos = SetCursorPosition(element,sPos);  
    }
}



function VerifyBDay(e,moz) {
//alert("ver");
//return 1;
    var element = document.getElementById("birthday");
    var skipover=0;
    var back=0;
    var dele=0;

    keynum = e.which;
    keyCode= e.keyCode;
    if(moz==0)
        keynum = keyCode = e.keyCode;
    //alert(keynum);
    //alert(keyCode);
    if(keyCode==46){
        skipover=1;
        dele=1;
    }
    if(keyCode==8){
        skipover=1;
        back=1;
    }
    if(keyCode==37){
        //left=1;
        //alert("l");
        var sPos = GetStartCursorPosition(element);
        if(sPos==3)
            var curpos = SetCursorPosition(element,2);
        if(sPos==6)
            var curpos = SetCursorPosition(element,5);            
        return 1;        
    }
    if(keyCode==39){
        //right=1;
        //alert("r");
        var sPos = GetStartCursorPosition(element);
        //alert(sPos);
        if(sPos==1)
            var curpos = SetCursorPosition(element,2);
        if(sPos==4)
            var curpos = SetCursorPosition(element,5);            
        return 1;
    }    
    if(keyCode==6|keyCode==9){
        //alert("aa");
        var sPos = GetStartCursorPosition(element);
        if(sPos==4)
            var curpos = SetCursorPosition(element,2);
        if(sPos==4)
            var curpos = SetCursorPosition(element,2);                
        return 1;
    }
    
    
    // pass paste comand
    if(e.ctrlKey==true & keynum == 86)
        return 1;
        
    keychar = String.fromCharCode(keynum);
    
    if(skipover==0){
        //alert(keynum);
        if( keynum < 106 & keynum > 95){ // num lock numbers
            switch (keynum) {
                case 96:
                keychar = "0";
                break;
                case 97:
                keychar = "1";
                break;
                case 98:
                keychar = "2";
                break;
                case 99:
                keychar = "3";
                break;
                case 100:
                keychar = "4";
                break;
                case 101:
                keychar = "5";
                break;
                case 102:
                keychar = "6";
                break;
                case 103:
                keychar = "7";
                break;
                case 104:
                keychar = "8";
                break;
                case 105:
                keychar = "9";
                break;
            }
        }
        else{
            for(var x=0;x<keychar.length;x++){
                var ch = keychar.charAt(x);
                //alert("b");
                //alert(ch);
                var cok = new String("1234567890");
                if(cok.indexOf(ch)==-1){
                    //alert("bc");
                    if(moz==1)
                        e.preventDefault();
                    else{
                        e.returnValue=0;
                    }
                    return false;  
                } 
            }
        }
    }
    
    var sValue = new String(element.value);
    var newValue = new String("");
    var len = sValue.length;

    var sPos = GetStartCursorPosition(element);
    var ePos = GetEndCursorPosition(element);

    // add char        
    //alert(back);
    if(back==1){
        //alert("back");
        // delete
        if(moz==1)
            e.preventDefault();
        else{
            e.returnValue=0;
        }
        var nsPos=sPos-1;
        if(nsPos==-1){
            return;
        }//nsPos=0;
        if(nsPos==2){
            //alert("aa");
            var curpos = SetCursorPosition(element,2);
            return 1;
        }
        if(nsPos==5){
            //alert("aa");
            var curpos = SetCursorPosition(element,5);
            return 1;
        }        
        var s1 = new String(sValue.substring(0,nsPos));
        var s2 = sValue.substring(sPos,len);
        sValue = s1 + " " + s2;
        element.value = sValue;
        var curpos = SetCursorPosition(element,nsPos);
        return 1;
        //sValue = sValue.substring(0,10);
    }
    //alert(dele);
    if(dele==1){
        // delete
        //alert("b1");
        var s1 = new String(sValue.substring(0,sPos));
        var s2 = sValue.substring(sPos+1,len);

        sValue = s1 + " " + s2;
        element.value = sValue;
        //alert(sPos);
        
        if(moz==1)
            e.preventDefault();
        else{
            e.returnValue=0;
        }
        element.value = sValue;
        var curpos = SetCursorPosition(element,sPos);
        return 1;
        //sValue = sValue.substring(0,10);
    }
    
    var s1 = new String(sValue.substring(0,sPos));
    var s2 = sValue.substring(sPos+1,len);
    //newValue.replace(
    sValue = s1 + keychar + s2;
    sValue = sValue.substring(0,10);
    if(moz==1)
        e.preventDefault();
    else{
        e.returnValue=0;
    }
    element.value = sValue;

    if(sPos==1){
        var curpos = SetCursorPosition(element,3);
        return;
    }
    if(sPos==4){
        var curpos = SetCursorPosition(element,6);
        return;
    }
    var curpos = SetCursorPosition(element,sPos+1);
}

function BDayEVerify(e,sObj) {
//alert("ee");
    if(window.event) // IE
    {
        //alert("ee");
        VerifyBDay(e,0);
    }
}

function BDayNSVerify(e) {
//alert("ns");
    if(window.event) // IE
    {
        return 1;
    }
    else{
        ///alert("ns");
        VerifyBDay(e,1);
    }
    return 1;
}

function GetVal(){
var element = document.getElementById("birthday");
repval = element.value;
}

function BDayPasteNS(e){
var element = document.getElementById("birthday");
//alert(element.value);
    element.value = repval;
    return 0;
}

function BDayPasteIE(sObj){
    event.returnValue=0;
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

