function ajaxsubmit(divToUpdate, formName, urlToCall){

	var div = document.getElementById(divToUpdate);
   
	var handleSuccess = function(o){
	 	
	 	if( o.getAllResponseHeaders.toLowerCase().match("content-disposition: attachment;")) {
	 		document.location.href= urlToCall;
	 	} else {
		try{
		
		div.innerHTML = o.responseText;

        var carousel = new YAHOO.widget.Carousel("carousel-container", { isCircular: true, numVisible: 8 });

        carousel.render();
        carousel.show();

		}catch(e) {
			//alert( "Error [ajaxsubmit]: " + e.message);
		} 

		}
	};
	
	var handleFailure = function(o){
		div.innerHTML = "<li><strong>Error in executing your request: " + o.statusText + "</strong></li>" + div.innerHTML;
	};
	
	var callback = {
	  success:handleSuccess,
	  failure:handleFailure
	};		
	
	if(formName != null) {
		var formObject = document.getElementById(formName); 
	 	YAHOO.util.Connect.setForm(formObject);
		var request = YAHOO.util.Connect.asyncRequest('POST', urlToCall, callback, formObject);
		
	}
	else {
		var request = YAHOO.util.Connect.asyncRequest('GET', urlToCall, callback);
	}
};	


function checkCatCodeDuplicate(field){

}

function checkCatNameDuplicate(field){

}

function composeReview()
{
	ajaxsubmit('submit-review', 'revform', '/action/public/writeReview.htm?redirectReview=1&reviewMode=compose');
}

function previewReview()
{
	ajaxsubmit('submit-review', 'revform', '/action/public/writeReview.htm?redirectReview=1&reviewMode=preview');		
}

function submitReview()
{
	if($('origComment').value==null || $('origComment').value=="") 
	{
		ajaxsubmit('submit-review', 'revform', '/action/public/writeReview.htm?redirectReview=1&reviewMode=error');
		return;
	}
	document.revform.submit();
}

function addLinkCategory()
{
	ajaxsubmit('addCategory', null, '/action/member/manageListing.htm?addCategory=1&cat=' + $('category').value + "&listing=" + $('listingId').value);		
}


function replaceListingPhoto(listId)
{
	ajaxsubmit('uploadImage', null, '/action/member/manageImage.htm?rep=1&editListingImage=1&listId=' + listId);
}
	
function replaceProfilePhoto(userId)
{
	ajaxsubmit('uploadImage', null, '/action/member/manageImage.htm?rep=1&editProfileImage=1&userId=' + userId);
}
	
var updater = null;
var ctr = 0;

// main function for upload monitoring
function monitorFileUpload() {                
    try {
        // get upload status
                               
        updater = new Ajax.PeriodicalUpdater('uploadStatusDiv','/upload', {
            asynchronous:true, 
            frequency:1, 
            method:'get',
            onSuccess:function(request) {
            
                if (request.responseText.length > 1) {     
                    $('progressIcon').style.visibility = 'visible';
                    $('progressIcon').innerHTML = "<img src='/images/loading.gif' style='height:15px; width:15px' />" + '<span> Uploading... ' + request.responseText + '% </span>';
                    $('submitImage').style.display = 'none';
                } else {
                	ctr++;
                	if(ctr>5) {
                		handleUploadFinished();
                	}	
                }
            }                                                                     
        });
    } catch(e) {
        alert('monitorFileUpload() failed, reason: ' + e);
    } finally {
    }                                       
    return false;
}                                                                       
    
// executes function after page loaded
function addLoadEvent(func) {
    
    var oldonload = window.onload;  
         
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}                                       
    
// this function will be executed after "target" IFRAME content changed
function handleUploadFinished() {
    // stop updater manually
    if ( updater != null && typeof updater != 'undefined') {
        updater.stop();
        updater = null;
    }     
    if ( $('progressIcon') != null && typeof $('progressIcon') != 'undefined') {             
    $('progressIcon').style.visibility = 'hidden';
    }
    ctr = 0;                                             
}
    
// observe IFrame object for "load" event to stop AJAX updater
function observeFormSubmit() {
    // add event to observe upload target
    if ( $('uploadTrg') != null && typeof $('uploadTrg') != 'undefined') {
    Event.observe('uploadTrg', 'load', handleUploadFinished);
    }
}                               

function showUploadMsg(msg) {
	if( $('listingId') != null && typeof $('listingId') != 'undefined') 
	{
		var listId = $('listingId').value;
		ajaxsubmit('uploadImage', null, '/action/member/manageImage.htm?editListingImage=1&listId=' + listId + '&msg=' + msg);
	}
	else if( $('userId') != null && typeof $('userId') != 'undefined') 
	{
		var userId = $('userId').value;
		ajaxsubmit('uploadImage', null, '/action/member/manageImage.htm?editProfileImage=1&userId=' + userId + '&msg=' + msg);
	}
}   

// add event handler only after page loaded
addLoadEvent(observeFormSubmit); 



