var profileDialogs = new Array();

function toggleProfileDetail(obj){
    var contentNode = obj.parentNode.nextSibling;    //IE
    if(contentNode.nodeType == 3) contentNode = contentNode.nextSibling;    // Mozilla
    var img = document.images['img_' + obj.id];

    if(contentNode && img){
        if(contentNode.style.display == ''){
	        contentNode.style.display = 'none';
            img.src = '../images/triangle_right.gif';
        }
        else{
	        contentNode.style.display = '';
            img.src = '../images/triangle_down.gif';
        }
    }
}

function openProfileDetail(obj){
    var contentNode = obj.parentNode.nextSibling;    //IE
    if(contentNode.nodeType == 3) contentNode = contentNode.nextSibling;    // Mozilla
    var img = document.images['img_' + obj.id];

    if(contentNode && img && contentNode.style.display == 'none'){
        contentNode.style.display = '';
        img.src = '../images/triangle_down.gif';
    }
}

function editProfile(divID, validateHnd, responseHnd, viewName, statusObj, w, h){
    argv = editProfile.arguments;  
    argc = argv.length;
    var xPos = (argc > 7 ? argv[7] : 200);  // optional parameter x-position
    var yPos = (argc > 8 ? argv[8] : 200);  // optional parameter y-position
   
    // Remove the abs position. 
    var divObj = getElement(divID);
    if(divObj) divObj.style.display = '';
    
    //dialog = new YAHOO.widget.Dialog(divID, { postmethod:"async", width:w+"px", height:h+"px", underlay:"none", draggable:false, fixedcenter:true } );
    
    var dialog;
    if(profileDialogs[divID]){
        dialog = profileDialogs[divID];
        dialog.show();
    }
    else{
        //dialog = new YAHOO.widget.Dialog(divID, { postmethod:"async", width:w+"px", height:h+"px", x:xPos, y:yPos, underlay:"none", draggable:false, fixedcenter:true } );
        if(divID == 'pnlContactInfo') dialog = new YAHOO.widget.Dialog(divID, { postmethod:"async", width:w+"px", x:xPos, y:yPos, underlay:"none", draggable:false } );
        else dialog = new YAHOO.widget.Dialog(divID, { postmethod:"async", width:w+"px", x:xPos, y:yPos, underlay:"none", draggable:false, fixedcenter:true } );
        profileDialogs[divID] = dialog;
    }
    
    var handleSubmit = function() { this.submit(); this.show(); };
    var btnSubmit = [ { text:"Save", handler:handleSubmit, isDefault:true } ];
  
    var onSuccess = function(obj) {
        if(responseHnd(obj, statusObj, viewName)) dialog.hide();
    }
    var onFailure = function(obj) {
        displayDialogStatusMessage(statusObj.id, STATUS_ERROR, 'Your submission failed! Status: ' + obj.status);
    }

    // Hook up a validator if one is defined.
    if(validateHnd){
        dialog.validate = function() {
            var data = this.getData();
            return validateHnd(data, statusObj);
        }
    }
    dialog.callback.success = onSuccess;
    dialog.callback.failure = onFailure;
    dialog.cfg.queueProperty("buttons", btnSubmit);
    dialog.render();

    return false;
}

function editPersonalDetails(divID, validateHnd, statusObj, w, h){
    argv = editPersonalDetails.arguments;  
    argc = argv.length;
    var xPos = (argc > 5 ? argv[5] : 200);  // optional parameter x-position
    var yPos = (argc > 6 ? argv[6] : 200);  // optional parameter y-position
    
    // Remove the abs position. 
    var divObj = getElement(divID);
    if(divObj) divObj.style.display = '';
    
    var dialog;
    if(profileDialogs[divID]){
        dialog = profileDialogs[divID];
        dialog.show();
    }
    else{
        //dialog = new YAHOO.widget.Dialog(divID, { postmethod:"form", width:w+"px", height:h+"px", x:xPos, y:yPos, underlay:"none", draggable:false, fixedcenter:true } );
        dialog = new YAHOO.widget.Dialog(divID, { postmethod:"form", width:w+"px", x:xPos, y:yPos, underlay:"none", draggable:false, fixedcenter:true } );
        profileDialogs[divID] = dialog;
    }
    
    var handleSubmit = function() { this.submit(); };
    var buttons = [ { text:"Save", handler:handleSubmit, isDefault:true } ];
  
    if(validateHnd){
        dialog.validate = function(){
            return validateHnd(this.getData(), statusObj);
        };
    }
    dialog.cfg.queueProperty("buttons", buttons);
    dialog.render();

    return false;
}

function genericResponse(responseObj, statusObj, view){
    var xmldoc = responseObj.responseXML;
    var result = false;
        
    try{
        var root = xmldoc.getElementsByTagName('root').item(0);
        var status = root.getElementsByTagName("status")[0].firstChild.nodeValue;

        if(status == 'true'){
            result = true;
            // Add the view in the query string so the page would know which view to open on refresh.
            // Personal details, contact info & statement automatically opens, no need to set them.
            var baseUrl = location.href;
            var index = baseUrl.indexOf('?');
            if(index >= 0) baseUrl = baseUrl.substring(0, index);
            if(view == 'personalDetails' || view == 'contactInfo') location.replace(baseUrl + '?msg=1');
            else location.replace(baseUrl + '?view=' + view + '&msg=1');
        }
        else{
            var desc = root.getElementsByTagName('description')[0].firstChild.nodeValue;
            displayDialogStatusMessage(statusObj.id, STATUS_ERROR, desc);
        }
    
    }
    catch(ex){
        alert('There was an error updating your profile! ' + ex.message);
    }

    return result;   
}

// Validation for artist and curator profiles.
function validatePersonalDetails1(data, lblStatus) {
    if(data.txtName1.trim() == ''){
        displayDialogStatusMessage(lblStatus.id, STATUS_ERROR, MSG_ERROR_REQUIRED_FIRST_NAME);
	    return false;
    }
    if(data.txtName2.trim() == ''){
        displayDialogStatusMessage(lblStatus.id, STATUS_ERROR, MSG_ERROR_REQUIRED_LAST_NAME);
	    return false;
    }

    if(data.ddlMonth){
        var dt = new Date(data.ddlYear, data.ddlMonth - 1, data.ddlDay);

        // If you enter an invalid date the month will roll over.
        if(data.ddlMonth != '0' || data.ddlDay != '' || data.ddlYear != ''){
            if(dt.getMonth() + 1 != data.ddlMonth || data.ddlYear == ''){
                displayDialogStatusMessage(lblStatus.id, STATUS_ERROR, MSG_ERROR_INVALID_DATE);
	            return false;
            }
        }
    }
    return true;
}

// Validation for all profile excluding artist and curator.
function validatePersonalDetails2(data, lblStatus) {
    if(data.txtName1.trim() == ''){
        displayDialogStatusMessage(lblStatus.id, STATUS_ERROR, MSG_ERROR_REQUIRED_ORGANIZATION_NAME);
	    return false;
    }

    return true;
}

function validateContactInfo(data, lblStatus) {

//    if(data.txtPhone1.length > 0 && isNaN(data.txtPhone1)){
//	    lblStatus.innerHTML = 'Please enter a validate phone number.';
//	    return false;
//    }

//    if(data.txtPhone2.length > 0 && isNaN(data.txtPhone2)){
//	    lblStatus.innerHTML = 'Please enter a validate phone number.';
//	    return false;
//    }
    
    if(data.txtEmail == ''){
        displayDialogStatusMessage(lblStatus.id, STATUS_ERROR, MSG_ERROR_REQUIRED_EMAIL);
	    return false;
    }
    if(!isEmail(data.txtEmail)){
        displayDialogStatusMessage(lblStatus.id, STATUS_ERROR, MSG_ERROR_EMAIL_FORMAT);
	    return false;
    }

    return true;
}
    
function profileViewRequestMessageBox(){
    var statusObj = getElement('lblProfileViewStatus');
    statusObj.innerHTML = '';

    var handleRequestSubmit = function() {
        this.submit();
        this.show();
    };
    var handleRequestCancel = function() {
        this.cancel();
    };

    var onFailure = function(obj) {
        displayDialogStatusMessage(statusObj.id, STATUS_ERROR, 'Your submission failed! Status: ' + obj.status);
    }

    var buttons = [ { text:"Send", handler:handleRequestSubmit, isDefault:true },
                    { text:"Cancel", handler:handleRequestCancel }     ];

    var divObj = getElement('pnlProfileViewRequest');
    if(divObj) divObj.style.display = '';

    var dialog;
    if(profileDialogs['pnlProfileViewRequest']){
        dialog = profileDialogs['pnlProfileViewRequest'];
        dialog.show();
    }
    else{
        //dialog = new YAHOO.widget.Dialog('pnlProfileViewRequest', { postmethod:"async", width:"360px", height:"400px", x:200, y:260, underlay:"none", draggable:false, fixedcenter:true } );
        dialog = new YAHOO.widget.Dialog('pnlProfileViewRequest', { postmethod:"async", width:"360px", x:200, y:260, underlay:"none", draggable:false, fixedcenter:true } );
        profileDialogs['pnlProfileViewRequest'] = dialog;
    }

    var onSuccess = function(obj) {
        try {
            var xmldoc = obj.responseXML;
            var root = xmldoc.getElementsByTagName('root').item(0);
            var status = root.getElementsByTagName("status")[0].firstChild.nodeValue;

            if(status == 'true'){
                dialog.form.reset();
                //dialog.hide();
                displayDialogStatusMessage(statusObj.id, STATUS_SUCCESS, MSG_DISPLAY_MESSAGE_SENT );
            }
            else{
                var desc = root.getElementsByTagName('description')[0].firstChild.nodeValue;
                displayDialogStatusMessage(statusObj.id, STATUS_ERROR, desc);
            }        
        }
        catch(ex){
            alert('There was an error sending your message. ' + ex.message);
        }
    }

    dialog.callback.success = onSuccess;
    dialog.callback.failure = onFailure;
    dialog.cfg.queueProperty("buttons", buttons);
    dialog.render();

    return false;
}
