﻿// JScript File

PINT.upload ={

    uploadPhoto :function(){
        
         var filPhoto = document.getElementById('photos');
         
        if(PINT.upload.validatePhoto(filPhoto))
        {
            PINT.upload.ShowLoadStyles('photo');
            var sUrl = 'upload.ashx';
            var postData = 'action=add&type=photo';
            YAHOO.util.Connect.setForm('aspnetForm', true);
            YAHOO.util.Connect.asyncRequest('POST', sUrl, uploadPhotoHandler, postData);
        }else{
            PINT.upload.HideLoadStyles('photo');
        }
    },
    
    
    uploadAvatar: function(){
        var avatar = document.getElementById('avatar');
        if(PINT.upload.validatePhoto(avatar))
        {
            PINT.upload.ShowLoadStyles('avatar');
            var sUrl = '/upload.ashx';
            var postData = 'action=add&type=avatar';
            YAHOO.util.Connect.setForm('aspnetForm', true);
            YAHOO.util.Connect.asyncRequest('POST', sUrl, uploadAvatarHandler, postData);
        }else{
            PINT.upload.HideLoadStyles('avatar');
        }
    },
    
    uploadInst: function(){
        
         var filInst = document.getElementById('insts');
         var txtInst = document.getElementById('txtInstruction');
         if(PINT.upload.validateInst(filInst,txtInst)){
            PINT.upload.ShowLoadStyles('inst');
            var sUrl = 'upload.ashx';
            var postData = 'action=add&type=inst';
            YAHOO.util.Connect.setForm('aspnetForm', true);
            YAHOO.util.Connect.asyncRequest('POST', sUrl, uploadInstHandler, postData);
         }else
             PINT.upload.HideLoadStyles('inst');
    },
    
    
    uploadSupply :function  (){
        var filSupply = document.getElementById('supply');
        
        if(filSupply.value.length > 0){
            PINT.upload.ShowLoadStyles('supply');
            var sUrl = 'upload.ashx';
            var postData = 'action=add&type=supply';
            YAHOO.util.Connect.setForm('aspnetForm', true);
            YAHOO.util.Connect.asyncRequest('POST', sUrl, uploadSupplyHandler, postData);
        }else{
            PINT.upload.HideLoadStyles('supply');
            alert("Please enter supply name");
        }
    },
        
      
        
    uploadFile : function(){
        
        var filFile = document.getElementById('file');
        var title = document.getElementById('filename');
        if(PINT.upload.validateFile(filFile,title))
        {
            PINT.upload.ShowLoadStyles('file');
            var sUrl = 'upload.ashx';
            var postData = 'action=add&type=file';
            YAHOO.util.Connect.setForm('aspnetForm', true);
            YAHOO.util.Connect.asyncRequest('POST', sUrl, uploadFileHandler, postData);
        }else
        {
            PINT.upload.HideLoadStyles('file');
        }
    },

    deleteItem : function(id,type){
        if( confirm("Are you sure you wish to delete this entry?")){
            PINT.upload.ShowLoadStyles(type);
            var sUrl = 'upload.ashx';
            var postData = 'action=delete&type='+ type + '&id='+ id;
            YAHOO.util.Connect.setForm('aspnetForm', true);
            switch (type){
                case 'photo':
                    YAHOO.util.Connect.asyncRequest('POST', sUrl, uploadPhotoHandler, postData);
                    break;
                case 'inst':
                    YAHOO.util.Connect.asyncRequest('POST', sUrl, uploadInstHandler, postData);
                    break;
                case 'supply':
                    YAHOO.util.Connect.asyncRequest('POST', sUrl, uploadSupplyHandler, postData);
                    break;
                case 'file':
                    YAHOO.util.Connect.asyncRequest('POST', sUrl, uploadFileHandler, postData);
                    break;
            }
        }
    },
        
    reorderItem :function  (id,type,dir){
        var sUrl = 'upload.ashx';
        var postData = 'action=reorder&type='+ type + '&id='+ id + '&dir=' + dir;
        YAHOO.util.Connect.setForm('aspnetForm', true);
        PINT.upload.ShowLoadStyles(type);
        switch (type){
            case 'photo':
                YAHOO.util.Connect.asyncRequest('POST', sUrl, uploadPhotoHandler, postData);
                break;
            case 'inst':
                YAHOO.util.Connect.asyncRequest('POST', sUrl, uploadInstHandler, postData);
                break;
            case 'supply':
                YAHOO.util.Connect.asyncRequest('POST', sUrl, uploadSupplyHandler, postData);
                break;
            case 'file':
                YAHOO.util.Connect.asyncRequest('POST', sUrl, uploadFileHandler, postData);
                break;
        }
    },
        
    ShowLoadStyles :function(type){
        switch (type){
            case 'photo':
                document.getElementById('loadPhoto').style.display = 'block';
                break;
            case 'inst':
                document.getElementById('loadInst').style.display = 'block';
                break;
            case 'supply':
                document.getElementById('loadSupply').style.display = 'block';
                break;
            case 'file':
                document.getElementById('loadFile').style.display = 'block';
                break;
            case 'avatar':
                document.getElementById('loadAvatar').style.display = 'block';
                break;
        }
    },
        
    HideLoadStyles :function(type){
        switch (type){
            case 'photo':
                document.getElementById('loadPhoto').style.display = 'none';
                break;
            case 'inst':
                document.getElementById('loadInst').style.display = 'none';
                break;
            case 'supply':
                document.getElementById('loadSupply').style.display = 'none';
                break;
            case 'file':
                document.getElementById('loadFile').style.display = 'none';
                break;
            case 'avatar':
                document.getElementById('loadAvatar').style.display = 'none';
                break;
        }
    },
    
    validatePhoto : function(fileName){
        if (!fileName) return;
        if(fileName.value.length > 0){
            return PINT.upload.checkFileType(fileName,['jpg','jpeg','gif', 'png', 'bmp','tif']);
        }else{
            alert("Please select a Photo to Upload");
            return false;
        }
        
    },
    
    validateInst : function(fileName, txtInst){
        if((fileName.value.length == 0) && (txtInst.value.length == 0)){
             alert("Please add Instruction text or Photo");
            return false;
        }else
        {
            if(fileName.value.length > 1){
                return PINT.upload.checkFileType(fileName,['jpg','jpeg','gif', 'png', 'bmp','tif']);
            }else{
                return true;
            }
        }
    },
    
    validateFile : function(fileName, title){
        if((fileName.value.length > 0) && (title.value.length > 0)){
            return PINT.upload.checkFileType(fileName,['jpg','jpeg','gif', 'png', 'bmp','tif','pdf','doc']);
        }else{
             alert("Please enter a file name and  select a file to Upload");
        }
    
    },
    
    checkFileType : function(fileName,fileTypes){
        dots = fileName.value.split(".")
        //get the part AFTER the LAST period.
        fileType = "." + dots[dots.length-1];
        
        if(("." + fileTypes.join(".").indexOf(fileType) == -1)){
            alert("Please only upload files that end in types: \n\n" + ("." + fileTypes.join(".")));
            fileName.value = '';
            return false;
//        }else if(fileName.files[0].fileSize > 2097152 ){  // 2097152  = 2mb
//             alert("Max allowed file size is 2MB");
//                fileName.value = '';
//                return false;
        }else
            return true;
    },
        
    textCounter: function(field,cntfield,limit){
        var maxlimit = limit;
        
        if (field.value.length > maxlimit){
            field.value = field.value.substring(0, maxlimit);
        }else{
            cntfield.value = maxlimit - field.value.length;
        }
    }
}


