/***************************************************************************************
$Workfile: ABCProgress.js $
$Author: Muhammad Naeem $
$Revision: 1 $
$Created Date: 08/29/06 06:06 PM $
$Description: Contains the JavaScript utility function for Progress bar.
*
* Modification History
* SGA-08/03/2007 - Add MV4 as the supported file type
* MNH-09/06/2007 - Check document being uploaded from which panel (media panel, doc or image), so that file type must be checked according to that area.
* SGA-09/24/2007 - Disable the form submit code and let the caller object submit the form
***************************************************************************************/

    var itemFileCount = 0;  // Stores the total count of images
    var maximumFileCount = 10;
    var progresswndtimeoutid;
    //Show progress bar during file uploading.
    function ShowProgress(uniqueID)
	{
		var w = 600; // New window Width, Heigth, Top, Left parameters. 
		var h = 120;
		var l = (screen.width - w) / 2;
		var t = (screen.height - h) / 2;
//		var winParams = 'toolbar=no,scrollbars=yes,location=no,status=yes,menubar=no,resizable=no,width=' + w + ',height=' + h + ',left=' + l + ',top=' + t;				
//		window.open('/Items/ABCProgress.aspx?ProgressID=' + uniqueID, uniqueID, winParams) //Show in a new window
		Telligent_Modal.Configure('/utility/loading.html',['CommonModal'],['CommonModalTitle'],['CommonModalClose'],['CommonModalContent'],['CommonModalFooter'],['CommonModalResize'],['CommonModalMask'],100);
		var src='/Items/ABCProgress.aspx?ProgressID=' + uniqueID;
		Telligent_Modal.Open(src,600,135,null);
		progresswndtimeoutid=window.setTimeout("updateModSrc(\""+src+"\")", 500);
	}
	function updateModSrc(src)
	{
		Telligent_Modal._modalIframe.src='';
		Telligent_Modal._modalIframe.src=src;
		window.clearTimeout(progresswndtimeoutid);
	}
     function StartUpload(formObj)
	{
		if(typeof(formObj)=="string")
			formObj = document.getElementById(formObj);
			var uniqueID =   (Math.floor(Math.random() * 10000050) + (new Date()).getTime() % 1000000000);
			ShowProgress(uniqueID);
			if (formObj.action.indexOf("UploadID=") >0 )
		    {
		       formObj.action = formObj.action.substring(0, formObj.action.indexOf("UploadID=") - 1 );
		    }
			
			if(formObj.action.indexOf("?")==-1)
				formObj.action += '?UploadID=' + uniqueID;
			else
				formObj.action += '&UploadID=' + uniqueID;	
			
			// SGA-09/24/2007 - Disable the form submit code and let the caller object submit the form	
			// Submitting this way, create problems for the Safari where it doesn't correctly generate the button click events		
			//formObj.submit();
    }
    
    // MNH-09/06/2007 - Check document being uploaded from which upload area (media, doc or image)
    function StartUploadWithVerify(uploadControlID, formObj,uploadAreaType){
        var UploadControl = document.getElementById(uploadControlID);
        if (UploadControl.value.length > 0){
            if(IsValidFile(UploadControl,uploadAreaType))
                StartUpload(formObj)                      
            else{
                alert('Please select a valid file to upload.');
                return false;
            }
        }
        else{
            alert('Please select the file to upload.');
            return false;
        }
    }
    // MNH-09/06/2007 - added uploadAreaType argument, which tells from which panel(media,doc,image) upload is being made.
    function UploadItemAsset(uploadControlID, formObj,uploadAreaType)
    {        
        // Set the area type to image if not passed
        if (!uploadAreaType)
            uploadAreaType = "image"
            
        // Now check for the maximum file upload limit
        if (itemFileCount >= maximumFileCount)
        {
            alert("You can't upload more than " + maximumFileCount + " files per item." )
            return false;
        }
        else
            return StartUploadWithVerify(uploadControlID, formObj,uploadAreaType)
    }
    // Returns true if file in the given FILE control is of valid type
    function IsValidFile(UploadControl,uploadAreaType)
    {
        var filePath = UploadControl.value.toLowerCase();
        var fileExtension = filePath.substring(filePath.lastIndexOf("."), filePath.length)

        // Checking type of document being uploaded according to that panel(media panel, doc or image) allowed extensions.
        if(uploadAreaType=="media")
            var arrExtions= [".mov",".rv",".mpg",".ra",".mp3",".avi",".wmv",".wma",".mp4",".wav",".mv4"];
        else if(uploadAreaType=="doc")
            var arrExtions= [".txt",".xml",".ppt", ".xls",".pdf",".doc",".docx",".xlsx",".pptx"];
        else if(uploadAreaType=="image")
            var arrExtions= [".jpg",".jpeg",".png",".ico",".tif",".gif"];
            
        var validFileFormat = false;
        for (var iCounter = 0; iCounter <= arrExtions.length; iCounter++)
            if (arrExtions[iCounter] == fileExtension ){
                validFileFormat = true;
                break;
            }
            
        return validFileFormat;
    }
    function StartImageUploadWithVerify(uploadControlID, formObj){
        
        if (IsValidFile(uploadControlID, "image"))
            StartUpload(formObj) 
        else
        {
            alert('Please select a valid image file to upload.\nOnly jpg, png, gif and tif files allowed');
            return false;
        }
    }
