var xmlhttp = false;
var xmlhttp_v = false;

function escapeParam( val ) {
    return escape(encodeURIComponent(val)).replace(/\+/g,'%2B');
}

function initRequest(){

    var xmlhttptemp = false;

    if(window.XMLHttpRequest) {
        try {
            xmlhttptemp = new XMLHttpRequest();
        } catch(e) {
            xmlhttptemp = false;
        }
    } else if(window.ActiveXObject) {
        try {
            xmlhttptemp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                xmlhttptemp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                xmlhttptemp = false;
            }
        }
    }
    return xmlhttptemp;
}

function escapeParam( val ) {
    return escape(encodeURI(val)).replace(/\+/g,'%2B');
}


function get_phone_code(country) {
    xmlhttp = initRequest();
    xmlhttp.onreadystatechange=set_phone_code;
    xmlhttp.open("GET", baseuri + '/phone_code?country='+escape(country).replace(/\+/g,'%2B'), true);
    xmlhttp.send(null);
}

function set_phone_code()
{
    if (xmlhttp.readyState==4){
        if (xmlhttp.status == 200) {
            if(xmlhttp.responseText.length == 0){
                _el('phone_code').value='';
            } else {
                _el('phone_code').value = xmlhttp.responseText;
                _el('phone_code').size = _el('phone_code').value.length;
            }
        } else {
            alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
        }
    }
}


function viewpurchases(id) {
    _el('purchases').innerHTML = '';
    xmlhttp_v = initRequest();
    xmlhttp_v.onreadystatechange=viewpurchases_results;
    xmlhttp_v.open("GET", baseuri+'/account/purchases/vieworder/'+id
    , true);
    xmlhttp_v.send(null);
    return false;
}

function viewpurchases_results()
{
    if (xmlhttp_v.readyState==4){
        if (xmlhttp_v.status == 200) {
            _el('purchases').innerHTML = xmlhttp_v.responseText;
        } else {
            alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
        }
    }
}


function orders_do_qsearch(from) {
    xmlhttp = initRequest();
    xmlhttp.onreadystatechange=show_orderssearch_results;
    xmlhttp.open("GET", baseuri + '/account/purchases/qsearch/?'
        +'from='+escape(from).replace(/\+/g,'%2B')
        +((arguments.length==2)?'&new_page_count='+escape(arguments[1]).replace(/\+/g,'%2B'):'')
        , true);


    xmlhttp.send(null);
}
function show_orderssearch_results()
{
    if (xmlhttp.readyState==4){
        if (xmlhttp.status == 200) {
            _el('p1').innerHTML = xmlhttp.responseText;
        } else {
            alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
        }
    }
}


function updateHomePage(url) {
    xmlhttp = initRequest();

    _el('prDiv').style.display = 'block';

    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4){
            if (xmlhttp.status == 200) {
    			_el('prDiv').style.display = 'none';
                _el('contentpad').innerHTML = xmlhttp.responseText;
            } else {
                alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
            }
        }
    };
    xmlhttp.open("GET",baseuri+url,true);
    xmlhttp.send(null);
}



function searchforuser(email) {
    xmlhttp = initRequest();

    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4){
        	if (xmlhttp.responseText=='') {
        		$('fname').value = '';
        		$('lname').value = '';
        	} else {
				eval(xmlhttp.responseText);
    		}
    	}
    };
    xmlhttp.open("GET",baseuri + '/account/credits/assign/qsearch/?email='+escape(email).replace(/\+/g,'%2B'),true);
    xmlhttp.send(null);
}



function showCreditsDetails(id) {

	var el = $('creditsDetails');

	var url = baseuri + '/account/credits/details/';

    new Ajax.Updater(
    	{success: el},
    	url, {
            parameters 	: 'id='+id,
            method		: "get",
            evalScripts	: true,
            onComplete	: function() {}
        }
    );
}

function sendReminder(id) {

	var el = $('rem'+id);

	el.innerHTML = 'Sending reminder..';

	var url = baseuri + '/account/credits/sendreminder/';

    new Ajax.Updater(
    	{success: el},
    	url, {
            parameters 	: 'id='+id,
            method		: "get",
            evalScripts	: true,
            onComplete	: function() {
            	//el.innerHTML = 'Reminder has been sent.';
            }
        }
    );
}


function showBooklet(product_id,type) {
  myWin= open(baseuri+'/viewbooklet/'+type.toLowerCase()+'/'+product_id+'/', "_blank", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width="+(bookletsizes[type.toLowerCase()]['w']+20)+",height="+(bookletsizes[type.toLowerCase()]['h']+50));
}

function showCreditTable(id)
{
    xmlDoc = $('creditTable');
    x      = xmlDoc.childNodes;
    for (i=0; i<x.length; i++) {
        if ('DIV' == x[i].nodeName && 1 == x[i].nodeType) {
            x[i].style.display = 'none';
        }
    }
    $(id).style.display = 'block';
}

function removeFromGroup(user, group)
{
	var url      = baseuri + '/account/credits/groups/user_remove/';
    new Ajax.Request(url,
      {
        method: 'post',
        parameters: {user: user, group: group },
        onSuccess: function(transport) {
            window.location.href=window.location.href
        },
        onFailure: function(){ alert('Something went wrong...') }
      });
}

function switchCreditBasis(value)
{
    $('basis_email').style.display = 'none';
    $('basis_group').style.display = 'none';
    $(value).style.display         = 'block';
}
function updateCountry() {
    var redirect = Array();
	var el = document.getElementById('selectcountry');
    switch (el.value) {
        case 'uk': window.location = "http://www.scriptographic.co.uk/"; break;
        case 'ie': window.location = "http://www.scriptographic.co.uk/"; break;
        case 'au': window.location = "http://www.channing-bete.com/"; break;
        case 'usa': window.location = "http://www.channing-bete.com/"; break;
        case 'nz': window.location = "http://www.channing-bete.com/"; break;
        case 'ca': window.location = "http://www.channing-bete.com/"; break;
        case 'xx': window.open("mailto:sales@scriptographic.co.uk", '_self'); break;
    }
}