﻿var uniqueID;
var frmObj;
var isFormSubmited = false;

function ShowAJAXProgress()	{
	CallServer(uniqueID, "");
}
		
function ReceiveServerData(rValue) {   
	//For Mozilla/FireFox browser first we need to get at least 1 AJAX response and after that to send POST form.
	if (!isFormSubmited) {
	    frmObj.submit();
		isFormSubmited = true;
	}
			
	if (rValue!=-1) {
		var data = rValue.split("SEPARATOR");
		document.getElementById('ProgressOutput').innerHTML = data[0];
		
		if (data[1]!='') eval(data[1]);
		setTimeout('ShowAJAXProgress()', 1000);  
	}
}

function FormSubmit() {
    if (!isFormSubmited && frmObj) frmObj.submit();
}

function StartUpload(formObj) {
    var retVal = true;
    
    if (!(document.getElementById('chkAgree').checked)) {
        alert('You must consent to the Privacy Policy and the Terms and Conditions before you can continue.');
        retVal = false;
    }

    if (retVal) {
        if (title.value.length == 0) {
            alert('You must enter a title before you can continue.');
            retVal = false;
        }
    }

    if (retVal) {
        if (caption.value.length == 0) {
            alert('You must enter a caption before you can continue.');
            retVal = false;
        }
    }

    if (retVal) {
        if (email.value.length == 0) {
            alert('You must enter your email address before you can continue.');
            retVal = false;
        }
    }

    if (retVal) {
        if (submittedBy.value.length == 0) {
            alert('You must enter your name before you can continue.');
            retVal = false;
        }
    }
    
//        if (retVal) {
//            var arr = document.getElementById('file1').value.split('.');
//            ext = arr[arr.length - 1].toLowerCase();
//            if ((ext != 'jpg') && (ext != 'jpeg') && (ext != 'gif')) {
//                alert('Incorrect file format. Only jpgs and gifs allowed.');
//                retVal = false;
//            }
//        }

    if (retVal) {
	    var IE=navigator.userAgent.toLowerCase().indexOf("msie")!=-1?true:false
	    if (typeof(formObj)=="string") formObj = document.getElementById(formObj);

	    uniqueID = Math.floor(Math.random() * 10000000) + (new Date()).getTime() % 1000000000;							

        thePos = formObj.action.indexOf("UploadID");
	    //Cut earlier added UploadID.
	    if (thePos >= 1) formObj.action = formObj.action.substring(0, thePos-1);
	    if (formObj.action.indexOf("?") == -1) formObj.action += '?UploadID=' + uniqueID;
	    else formObj.action += '&UploadID=' + uniqueID;

	    formObj.action += '&cat=' + category.value + '&t=' + escape(title.value) + '&cap=' + escape(caption.value) + '&e=' + escape(email.value) + '&s=' + escape(submittedBy.value) + '&gid=' + gallery + '&ad=' + ad;

	    ShowAJAXProgress();
	    
	    //document.getElementById('btnSubmit').style.display = 'none'
	    //document.getElementById('btnCancel').style.display = '';
    	
	    if (IE) {
	        isFormSubmited = true;						
		    formObj.submit();						
		    retVal = true;
	    } else { //For Mozilla/FireFox browser first we need to get at least 1 AJAX response and after that to send POST form.
		    frmObj = formObj;					
		    setTimeout('FormSubmit();', 3000); //Submit FORM after 3 seconds automaticaly (If AJAX response willn't got earlier). 
		    retVal = false; //Don't post form before at least 1 AJAX response. 
	    }
	}
	
	return retVal;
}
