
var html_doc = document.getElementsByTagName('head').item(0);
var js = document.createElement('script');
html_doc.appendChild(js);

function do_it_all(div_id){
	s_str = window.location.search.toString();
	if(div_id && typeof($('#'+div_id)) != 'undefined'){
		$('#'+div_id).html(s_str);
		$('#'+div_id).show();
	}else{
		document.write(s_str);
	}
}

function subscribe(){
    if(typeof(MAILER_CLIENT_ID) == 'undefined' || isNaN(Number(MAILER_CLIENT_ID))){
        alert('Newsletter signup is not configured correctly. Incorrect client id.');
        return('');
    }
    
    if($('#subemail').val()){
        url = 'http://admin.tcsmailer.com/signup/'+MAILER_CLIENT_ID;
        pars = "e_mail="+$('#subemail').val();
        //pars += "&first_name="+(($('#subfirst_name') == null) ? '' : $('#subfirst_name').val());
        //pars += "&last_name="+(($('#sublast_name') == null) ? '' : $('#sublast_name').val());
        pars += "&first_name=test";
        pars += "&last_name=test";
		if(typeof(MAILER_LIST_ID) != 'undefined')
			pars += "&list="+MAILER_LIST_ID;
        var response = document.createElement('img');
        response.src=(url+"?"+pars);
        response.id = 'resp_img';
        document.getElementsByTagName('body').item(0).appendChild(response);
         $('#resp_img').bind("load", function(){ 
            sub_response(response);
        });   
    }else{
        sub_response(0);
    }
    return(true);
}

function conf_response_timeout(){}

function confirm(){
    if(typeof(MAILER_CLIENT_ID) == 'undefined'){
        alert('Newsletter configuration is not correct. No client id defined.');
        return('');
    }
    s_str = window.location.search.toString();
    s_str = s_str.replace("?","");
    s_arr = s_str.split("&");
    data = new Array();
    s_arr.each( function(inputval){
            tmp = inputval.split("=");
            data[tmp[0]] = tmp[1];
        });
    if(data['confirm']){
        url = 'http://admin.tcsmailer.com/signup/confirm/'+MAILER_CLIENT_ID;
        pars = "confirm="+data['confirm'];
        var response = document.createElement('img');
        response.src=(url+"?"+pars);
        response.id = 'resp_img';
        document.getElementsByTagName('body').item(0).appendChild(response);
        $('#resp_img').bind("load", function(){ 
            conf_response();
        });
    }else{
        conf_response(0);
    }
}

function remove(manual){
    if(typeof(MAILER_CLIENT_ID) == 'undefined'){
        alert('Newsletter configuration is not correct. No client id defined.');
        return('');
    }
    s_str = window.location.search.toString();
    if(s_str){
        s_str = s_str.replace("?","");
        s_arr = s_str.split("&");
        data = new Array();
        s_arr.each( function(inputval){
                tmp = inputval.split("=");
                data[tmp[0]] = tmp[1];
            });
    }else{
        data=new Array();
    }
    if(manual){
        data['remove'] = '1';
        data['e_mail'] = $('#rememail').val();
    }
    if(data['remove'] == '1' && data['e_mail']){
        url = 'http://admin.tcsmailer.com/signup/remove/'+MAILER_CLIENT_ID;
        pars = "e_mail="+data['e_mail'];
        var response = document.createElement('img');
        response.src=(url+"?"+pars);
        response.id = 'resp_img';
        document.getElementsByTagName('body').item(0).appendChild(response);
        //Event.observe('resp_img', 'load', remove_response, false);
         $('#resp_img').bind("load", function(){ 
            remove_response();
        });
    }else{
        remove_response(0);
    }
}

function confirm_remove(){
	s_str = window.location.search.toString();
	if(s_str.search(/confirm/) != -1){
		confirm();
	}else if(s_str.search(/remove/) != -1){
		remove();
	}else{
		$('#remove_manual').show();
	}
	return('');
}

if( typeof(window[MAILER_MESSAGE]) == 'undefined'){
    var MAILER_MESSAGE = new Array();
}

	/*----------------------------------------------------
	--													--
	--		Subscribe Response codes.					--
	--													--
	--	1.	success!									--
	--	2.	Could not find client in db.				--
	--	3.	Subscriber all ready exists.				--
	--	4.	Subscribed but could not send email			--
	--	5.	First Name not entered						--
	--	6.	Bad email format. 							--
	--	0.	incomplete information.(java check)			--
	--													--
	----------------------------------------------------*/
		if( typeof(window.MAILER_MESSAGE.subscribe) == 'undefined'){
			MAILER_MESSAGE['subscribe'] = new Array();
		}
		MAILER_MESSAGE['subscribe']['1'] = ( (typeof( window.MAILER_MESSAGE.subscribe['1'] ) != 'undefined') ? MAILER_MESSAGE['subscribe']['1'] : 'Thank you for joining our newsletter.\n You should receive a confirmation email shortly please follow instructions in the email to complete subscription.');
		MAILER_MESSAGE['subscribe']['2'] = ( (typeof( window.MAILER_MESSAGE.subscribe['2'] ) != 'undefined') ? MAILER_MESSAGE['subscribe']['2'] : 'There was an error processing this request please try again later.');
		MAILER_MESSAGE['subscribe']['3'] = ( (typeof( window.MAILER_MESSAGE.subscribe['3'] ) != 'undefined') ? MAILER_MESSAGE['subscribe']['3'] : 'It appears you have been subscribed in the past. If you are not receiving our newsletter or have not received our confirmation email please contact us.');
		MAILER_MESSAGE['subscribe']['4'] = ( (typeof( window.MAILER_MESSAGE.subscribe['4'] ) != 'undefined') ? MAILER_MESSAGE['subscribe']['4'] : 'We have saved your information but there was an error when sending your confirmation email please contact us to complete subscription.');
		MAILER_MESSAGE['subscribe']['5'] = ( (typeof( window.MAILER_MESSAGE.subscribe['5'] ) != 'undefined') ? MAILER_MESSAGE['subscribe']['5'] : 'Please enter your first name.');
		MAILER_MESSAGE['subscribe']['6'] = ( (typeof( window.MAILER_MESSAGE.subscribe['6'] ) != 'undefined') ? MAILER_MESSAGE['subscribe']['6'] : 'Unrecognized email format please re-enter your email address. (example@domain.net)');
		MAILER_MESSAGE['subscribe']['0'] = ( (typeof( window.MAILER_MESSAGE.subscribe['0'] ) != 'undefined') ? MAILER_MESSAGE['subscribe']['0'] : 'You must fill out your name and email address to subscribe.');
	
	/*------------------------------------------------
	--												--
	--		Conformation Response codes.			--
	--												--
	--	1.	success!								--
	--	2.	Confirmer was not found.				--
	--	3.	Bad Email Format.						--
	--	4.	Client not found.						--
	--	5.	Email not found.						--
	--	0.	incomplete information.(java check)	--
	--												--
	------------------------------------------------*/
		if( typeof(window.MAILER_MESSAGE.confirm) == 'undefined'){
			MAILER_MESSAGE['confirm'] = new Array();
		}
		MAILER_MESSAGE['confirm']['1'] = ( (typeof( window.MAILER_MESSAGE.confirm['1'] ) != 'undefined') ? MAILER_MESSAGE['confirm']['1'] : 'You have successfully been subscribed to our newsletter. Thank you.');
		MAILER_MESSAGE['confirm']['2'] = ( (typeof( window.MAILER_MESSAGE.confirm['2'] ) != 'undefined') ? MAILER_MESSAGE['confirm']['2'] : 'We could not find you in our records please contact us to complete your subscription.');
		MAILER_MESSAGE['confirm']['3'] = ( (typeof( window.MAILER_MESSAGE.confirm['3'] ) != 'undefined') ? MAILER_MESSAGE['confirm']['3'] : 'Your email address was not in a recignised format please contact us to complete your subscription.');
		MAILER_MESSAGE['confirm']['4'] = ( (typeof( window.MAILER_MESSAGE.confirm['4'] ) != 'undefined') ? MAILER_MESSAGE['confirm']['4'] : 'There was an error processing conformation please contact us or try again later.');
		MAILER_MESSAGE['confirm']['5'] = ( (typeof( window.MAILER_MESSAGE.confirm['5'] ) != 'undefined') ? MAILER_MESSAGE['confirm']['5'] : 'Your email address was not found in our records please contact us to complete your subscription.');
		MAILER_MESSAGE['confirm']['0'] = ( (typeof( window.MAILER_MESSAGE.confirm['0'] ) != 'undefined') ? MAILER_MESSAGE['confirm']['0'] : 'There was an error processing conformation please contact us or try again later.');

	/*------------------------------------------------
	--												--
	--		Removal Response codes.					--
	--												--
	--	1.	success!								--
	--	2.	Remover was not found.					--
	--	3.	Bad Email Format.						--
	--	4.	Client not found.						--
	--	5.	Email not found.						--
	--	0.	incomplete information.(java check)		--
	--												--
	------------------------------------------------*/
		if( typeof(window.MAILER_MESSAGE.remove) == 'undefined'){
			MAILER_MESSAGE['remove'] = new Array();
		}
		MAILER_MESSAGE['remove']['1'] = ( (typeof( window.MAILER_MESSAGE.remove['1'] ) != 'undefined') ? MAILER_MESSAGE['remove']['1'] : 'You have been removed from our newsletter.');
		MAILER_MESSAGE['remove']['2'] = ( (typeof( window.MAILER_MESSAGE.remove['2'] ) != 'undefined') ? MAILER_MESSAGE['remove']['2'] : 'We could not find your information on file. Double check your information and try again. If you continue having problems please contact us.');
		MAILER_MESSAGE['remove']['3'] = ( (typeof( window.MAILER_MESSAGE.remove['3'] ) != 'undefined') ? MAILER_MESSAGE['remove']['3'] : 'Email was not in recignised format please re-enter and try again. (example@domain.net)');
		MAILER_MESSAGE['remove']['4'] = ( (typeof( window.MAILER_MESSAGE.remove['4'] ) != 'undefined') ? MAILER_MESSAGE['remove']['4'] : 'There was an error processing your request pleas contact us to be removed.');
		MAILER_MESSAGE['remove']['5'] = ( (typeof( window.MAILER_MESSAGE.remove['5'] ) != 'undefined') ? MAILER_MESSAGE['remove']['5'] : 'We could not find your information on file. Double check your information and try again. If you continue having problems please contact us.');
		MAILER_MESSAGE['remove']['0'] = ( (typeof( window.MAILER_MESSAGE.remove['0'] ) != 'undefined') ? MAILER_MESSAGE['remove']['0'] : 'Please fill out form completely to unsubscribe.');


function sub_response(response){

    if($('#resp_img') ==  null){
        response = 0;
    }else{
        response = response.width;
    }
    
    if(!response || response == 0){
        //ajax call was not made do to in complete information
        message = MAILER_MESSAGE['subscribe']['0'];
    }else{
        message = ((typeof( window.MAILER_MESSAGE.subscribe[response] ) != 'undefined') ? MAILER_MESSAGE['subscribe'][response] : '');
    }
	if(typeof(MESSAGE_BOX) != 'undefined' && MESSAGE_BOX && typeof($('#'+MESSAGE_BOX)) == 'object'){
		//$('#'+MESSAGE_BOX).html(message);
		//$('#'+MESSAGE_BOX).show();
        alert(message);
	}
	else{
		alert(message);
	}
}

function conf_response(response){  
    if(response ==  null){
        response = 0;
    }else{
        response = response.width;
    }
	if(!response || response == 0){
		//ajax call was not made do to in complete information
		message = MAILER_MESSAGE['confirm']['0'];
	}else{
		message = ((typeof( window.MAILER_MESSAGE.confirm[response] ) != 'undefined') ? MAILER_MESSAGE['confirm'][response] : '');
	}
	if(typeof(MESSAGE_BOX) != 'undefined' && MESSAGE_BOX && typeof($('#'+MESSAGE_BOX)) == 'object'){
		//$('#'+MESSAGE_BOX).html(message);
		//$('#'+MESSAGE_BOX).show();
	}
	else{
		alert(message);
	}
}

function remove_response(response){
    if(response ==  null){
        response = 0;
    }else{
        response = response.width;
    }
    if(!response || response == 0){
        //ajax call was not made do to in complete information
        message = MAILER_MESSAGE['subscribe']['0'];
    }else{
		message = ((typeof( window.MAILER_MESSAGE.remove[response] ) != 'undefined') ? MAILER_MESSAGE['remove'][response] : '');
	}
	if(typeof(MESSAGE_BOX) != 'undefined' && MESSAGE_BOX && typeof($('#'+MESSAGE_BOX)) == 'object'){
		//$('#'+MESSAGE_BOX).html(message);
		//$('#'+MESSAGE_BOX).show();
	}else{
		alert(message);
	}
}
