// Moves the box object to be directly beneath an object.
function move_box(an, box)
{
    var cleft = 0;
    var ctop = 0;
    var obj = an;

    while (obj.offsetParent)
    {
        cleft += obj.offsetLeft;
        ctop += obj.offsetTop;
        obj = obj.offsetParent;
    }

    box.style.left = cleft + 'px';

    ctop += an.offsetHeight + 8;

    // Handle Internet Explorer body margins,
    // which affect normal document, but not
    // abosolute-positioned stuff.
    if (document.body.currentStyle &&
       document.body.currentStyle['marginTop'])
    {
        ctop += parseInt(
            document.body.currentStyle['marginTop']);
    }

    box.style.top = ctop + 'px';
}

// Shows a box if it wasn't shown yet or is hidden
// or hides it if it is curre
function show_hide_box(an, id, content)
{
    var boxdiv = document.getElementById(id);

    if (boxdiv != null)
    {
        if (boxdiv.style.display=='none')
        {
            // Show existing box, move it
            // if document changed layout
            move_box(an, boxdiv);
            boxdiv.style.display='block';
        }
        else
            // Hide currently shown box.
            boxdiv.style.display='none';
        return false;
    }

    // Create box object through DOM
    boxdiv = document.createElement('div');

    // Assign id equalling to the document it will show
    boxdiv.setAttribute('id', id);

    boxdiv.style.display = 'block';
    boxdiv.style.position = 'absolute';
    boxdiv.style["z-index"] = '10';
    boxdiv.className = 'help';

    boxdiv.innerHTML = content;

    document.body.appendChild(boxdiv);
    move_box(an, boxdiv);

    // The script has successfully shown the box,
    // prevent hyperlink navigation.
    return false;
}

function mailserverMethod(value){
	if(value == "mail"){
		document.getElementById('sendmail').style['display'] = 'none';
		document.getElementById('smtp_1').style['display'] = 'none';
		document.getElementById('smtp_2').style['display'] = 'none';
		document.getElementById('smtp_3').style['display'] = 'none';
		document.getElementById('smtp_4').style['display'] = 'none';
	}else if(value == "sendmail"){
		document.getElementById('sendmail').style['display'] = '';
		document.getElementById('smtp_1').style['display'] = 'none';
		document.getElementById('smtp_2').style['display'] = 'none';
		document.getElementById('smtp_3').style['display'] = 'none';
		document.getElementById('smtp_4').style['display'] = 'none';
	}else if(value == "smtp"){
		document.getElementById('sendmail').style['display'] = 'none';
		document.getElementById('smtp_1').style['display'] = '';
		document.getElementById('smtp_2').style['display'] = '';
		document.getElementById('smtp_3').style['display'] = '';
		document.getElementById('smtp_4').style['display'] = '';
	}
}

function install_check(form){
    var xmlhttp = false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    try {
        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
        try { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); } catch (E) { xmlhttp = false; }
    }
    @end @*/
    if(!xmlhttp) { xmlhttp=new XMLHttpRequest(); }
    xmlhttp.open('POST','./modules/install_check.php', true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlhttp.send(	'license_key=' + encodeURIComponent(form.license_key.value) + '&' +
    				'url=' + encodeURIComponent(form.url.value) + '&' +
    				'mysql_host=' + encodeURIComponent(form.mysql_host.value) + '&' +
    				'mysql_username=' + encodeURIComponent(form.mysql_username.value) + '&' +
    				'mysql_password=' + encodeURIComponent(form.mysql_password.value) + '&' +
    				'mysql_database=' + encodeURIComponent(form.mysql_database.value)
    			);

    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState==4) {
        	if(xmlhttp.responseText == "1"){
				document.getElementById('valid').value = 1;
				document.getElementById('submit').click();
        	}else{
        		alert(xmlhttp.responseText);
        	}
        	return false;
        }
    }
}

function fillBouncedList(){
    var xmlhttp = false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    try {
        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
        try { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); } catch (E) { xmlhttp = false; }
    }
    @end @*/
    if(!xmlhttp) { xmlhttp=new XMLHttpRequest(); }
    xmlhttp.open('GET','./modules/bounced.php', true);
    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4) {
        	if(xmlhttp.responseText != ''){
	            document.getElementById('bounced_list').innerHTML = xmlhttp.responseText;
	            document.getElementById('bounced').style["display"] = "block";
	            document.getElementById('bounced_list').style["display"] = "block";
	        }
        }
    }
    xmlhttp.send(null);
}

function sendAjax(interval){
    var xmlhttp = false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    try {
        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
        try { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); } catch (E) { xmlhttp = false; }
    }
    @end @*/
    if(!xmlhttp) { xmlhttp=new XMLHttpRequest(); }
    xmlhttp.open('GET','./modules/send_ajax.php', true);
    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState==4) {
            if(xmlhttp.responseText != "101"){
                setTimeout('sendAjax(' + interval + ')',(interval*1000));
            	document.getElementById('progress').style["width"] = (xmlhttp.responseText*5) + "px";
            	document.getElementById('percent').innerHTML = xmlhttp.responseText + "%";
            }else{
            	document.getElementById('percent').innerHTML = 100 + "%";
            	document.getElementById('progress').style["width"] = "500px";
            	document.getElementById('loading').style["display"] = "none";
            	document.getElementById('busy_sending').style["display"] = "none";
            	document.getElementById('done_sending').style["display"] = "block";

            	if(document.getElementById('bounced').innerHTML != ''){
            		fillBouncedList();
            	}
            }
        }
    }
    xmlhttp.send(null);
}

function to_choice_change(value){
	document.getElementById('to_choice_all_groups').style['display'] = 'none';
	document.getElementById('to_choice_one_group').style['display'] = 'none';
	document.getElementById('to_choice_one_email').style['display'] = 'none';

	if(value == 'one_group'){
		document.getElementById('to_choice_one_group').style['display'] = 'inline';
	}else if(value == 'one_email'){
		document.getElementById('to_choice_one_email').style['display'] = 'inline';
	}else{
		document.getElementById('to_choice_all_groups').style['display'] = 'inline';
	}
}

function popup(url, width, height, title){
	/* Make popup height bigger when IEX */
	if(navigator.appName.indexOf('Internet Explorer') != -1){
		var height = (height + 5);

		/* Make popup height even bigger when IEX 6.x */
		if(navigator.appVersion.indexOf('MSIE 6.') != -1){
			var height = (height + 15);
		}
	}

    var win = new Window('popup_window', {className: "dialog", title: title, minimizable: false, maximizable: false, url: url, width:width, height:height, zIndex:150, opacity:1, resizable: true})
    win.setDestroyOnClose();
    win.showCenter();
    win.show(true);
}

function closePopup(){
	window.parent.Windows.close(null, 'popup_window');
}

sfHover = function() {
	if(document.getElementById("nav") != null){
	    var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	    for (var i=0; i<sfEls.length; i++) {
	        sfEls[i].onmouseover=function() {
	            this.className = "sfhover";
	        }

	        sfEls[i].onmouseout=function() {
	            this.className = "";
	        }
	    }
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function updateBoxPreview(){
    var td_style = 'font-family:' + document.getElementById('general_font_family').value + '; font-size:' + document.getElementById('general_font_size').value + 'px; background-color:' + document.getElementById('general_background_color').value + '; color:' + document.getElementById('general_text_color').value;
    var input_style = 'font-family:' + document.getElementById('input_font_family').value + '; font-size:' + document.getElementById('input_font_size').value + 'px; color:' + document.getElementById('input_text_color').value + '; background-color: ' + document.getElementById('input_background_color').value + '; border: 1px ' + document.getElementById('input_border_color').value + ' solid;"';

	var preview_html = 	'<form name="mtgp" action="' + document.getElementById('website_url').value + 'subscribe.php" method="post">\n' +
							'\t<input type="hidden" name="group_id" value="' + document.getElementById('group_id_1').value + '" />\n' +
							'\t<input type="hidden" name="return_url" value="' + document.getElementById('general_return_url').value + '" />\n' +
	                        '\t<table cellspacing="0" cellpadding="' +  document.getElementById('general_padding').value + '" style="border: 1px ' +  document.getElementById('general_border_color').value + ' solid;" align="center">\n' +
	                            '\t\t<tr>\n' +
	                                '\t\t\t<td colspan="2" style="font-family: ' +  document.getElementById('header_font_family').value + '; font-size:' +  document.getElementById('header_font_size').value + 'px; color:' +  document.getElementById('header_text_color').value + '; background-color:' +  document.getElementById('header_background_color').value + '"><strong>' +  document.getElementById('header_text').value + ' (1)</strong></td>\n' +
	                            '\t\t</tr>\n' +
	                            '\t\t<tr>\n' +
	                                '\t\t\t<td style="' +  td_style + '">' +  document.getElementById('name').value + ': (3)</td>\n' +
	                                '\t\t\t<td style="' +  td_style + '"><input type="text" name="name" style="' +  input_style + '" value="(2)" /></td>\n' +
	                            '\t\t</tr>\n' +
	                            '\t\t<tr>\n' +
	                                '\t\t\t<td style="padding-right: 15px;' +  td_style + '">' +  document.getElementById('email').value + ': (3)</td>\n' +
	                                '\t\t\t<td style="' +  td_style + '"><input type="text" name="email" style="' +  input_style + '" value="(2)" /></td>\n' +
	                            '\t\t</tr>\n' +
	                            '\t\t<tr>\n' +
	                                '\t\t\t<td style="' +  td_style + '"></td>\n' +
	                                '\t\t\t<td style="' +  td_style + '"><input type="submit" name="submit" value="' +  document.getElementById('subscribe').value + ' (2)" style="' +  input_style + '" /></td>\n' +
	                            '\t\t</tr>\n' +
	                        '\t</table>\n' +
	                    '</form>';

	document.getElementById('box_wizard_preview').innerHTML = preview_html;
	preview_html = preview_html.replace(' (1)','');
	preview_html = preview_html.replace(' (2)','');
	preview_html = preview_html.replace(' (3)','');
	preview_html = preview_html.replace(' (3)','');
	preview_html = preview_html.replace('(2)','');
	preview_html = preview_html.replace('(2)','');
	document.getElementById('box_wizard_html').value = preview_html;
}

