﻿var http = null, popupId = '', popupCommand = '';

function shareMediaTM(id, title) {
    hideMedia(1);
    shareIt('share_media_tm', id, title)
}

function shareMediaMEDC(id, title) {
    hideMedia(1);
    shareIt('share_media_medc', id, title)
}
	
function shareIt(cmd, id, title) {
	var obj = null, post = 'Share';
	
	if (cmd == null) cmd = '';
	cmd = cmd.toLowerCase();
	
	switch (cmd) {
	    case 'share_media_medc' :
	        setMediaImage('share', 'biz');
	        break;
	    case 'share_media_tm' :
	        setMediaImage('share', 'tm');
	        break;
	    default :
	        setMediaImage('share', 'biz');
	        obj = getObject('itemTitle');
	        if (obj) title = obj.value;

	        obj = getObject('itemId');
	        if (obj) id = obj.value;
	        
	        obj = getObject('itemCommand');
			if (obj) cmd = obj.value;
            break;
    }
    
    // Set "popup" fields
    popupId = 'share';

    obj = getObject('command' + post);
	if (obj) obj.value = cmd;
	popupCommand = cmd;
    
    obj = getObject('title' + post);
	if (obj) obj.innerHTML = title;

    obj = getObject('id' + post);
	if (obj) obj.value = id;
	
	showPopup(popupId);
}

function reportIt(cmd) {
	var obj = null;
    popupId = 'feedback';
    popupCommand = cmd;
    if (popupCommand == null || popupCommand == '') popupCommand = 'feedback';
   	obj = getObject('commandFeedback');
	if (obj) obj.value = popupCommand;
	showPopup(popupId);
}

function sendEmail() {
	var obj = null, from = '', to = '', errorMsg = '', id = '', cmd = '', comments = '', requestPage = '', post = '', pageUrl = '';

    switch (popupCommand) {
        case 'feedback' :
        case 'feedback_org_upd' :
            post = 'Feedback';

			obj = getObject('from' + post);
			if (obj) from = obj.value;

			obj = getObject('comments' + post);
			if (obj) comments = obj.value;
            
			obj = getObject('pageUrl' + post);
			if (obj) pageUrl = obj.value;
            
            break;
        default :
            post = 'Share';
    
            obj = getObject('from' + post);
	        if (obj) from = obj.value;

	        obj = getObject('to' + post);
	        if (obj) to = obj.value;

            obj = getObject('id' + post);
	        if (obj) id = obj.value;
    
            break;
    }

    obj = getObject('command' + post);
	if (obj) cmd = obj.value;

     switch (popupCommand) {
	    case 'feedback' :
	    case 'feedback_org_upd' :
            errorMsg = validateShareStuff(from, '1@1.com', 'Your email address is invalid.', '');
	        break;
	    default :
	        errorMsg = validateShareStuff(from, to, '', '');
	        break;
    }

	if (errorMsg != '') {
		alert('Please correct the following error(s):' + errorMsg);
	}
	else {
        switch (cmd) {
	        case 'share_media_medc' :
	            requestPage = '/shareBizMedia.ashx'
	            break;
	        case 'share_media_tm' :
	            requestPage = '/shareTravelMedia.ashx'
	            break;
            case 'feedback' :
            case 'feedback_org_upd' :
                requestPage = '/feedback.ashx';
                break;
	        default :
                requestPage = '/share.ashx'
                break;
        }
		http = sendRequest("get", requestPage, 'cmd=' + cmd + '&f=' + escape(from) + '&t=' + escape(to) + '&id=' + id + '&c=' + escape(comments) + '&p=' + escape(pageUrl) + '&rnd=' + Math.random(), true, complete);
	}
}
	
function complete() {
	if (http) {
	    if (isRequestComplete(http.readyState)) {
		    var text = http.responseText, arr = null, msg = '', status = '', returnMsg = '';

		    http = null;
		    arr = text.split('~');
		    if (arr.length == 2) {
			    status = arr[0];
			    returnMsg = arr[1];
			    if (status == 'success') {
				    switch (popupCommand) {
				        case 'feedback' :
				        case 'feedback_org_upd' :
						    msg = 'Thank you for your feedback.  Your feedback was sent to a MEDC representative.';
						    break;
                        default :			    
				            switch (popupCommand) {
				                case 'share_media_medc' :
                                case 'share_media_tm' :
                                    msg = 'The media';
	                                break;
					            default :
					                msg = 'The MEDC item';
						            break;
				            }
				            msg += ' was sent to the following recipient(s):\n  ' + replace(returnMsg, ', ', '\n  ');
                    }
			    }
			    if (status == 'failure') msg = 'Please correct the following error(s):' + returnMsg;
		    }
		    else {
			    msg = 'There was an error processing your request.  Please try again later.';
		    }
		    alert(msg);
		    hidePopup(popupId);
	    } 
	}
}

function setMediaImage(cmd, type) {
    var id = '';
    type = type.toLowerCase();
    switch (cmd) {
        case 'media' :
            id = 'mediaImg'
            break;
        default :
            id = 'imgShare';
            break;
            
    }
    
    var obj = getObject(id);
    if (obj) {
    	if (type == 'tm') {
	        obj.src = '/global/images/puremichiganMedia.gif';
	    }
	    else {
	        obj.src = '/global/images/michiganupperhandMedia.gif';
	    }
    }
}

