// Utilities
//gets query string and presented in an object
var getQuery = function ()
{
    result = {};
    str = window.location.search;
    if (str)
    {
        str = str.substring(1).split('&');
        str.each ( function (i)
            {
                item = i.split('=');
                result[item[0]] = item[1];
            }
        );
    }
    return result;
};

// Contact Us Page
var USE_IN_POPUP = '';

var qry = getQuery ();

for ( i in qry)
{
    if (qry['interest']) $E('#contact select[name=interest]').value = qry['interest'].toLowerCase();
    break;
}

TB_callBack.onShow = function()
{
    /*
    if ($E('#TB_ajaxContent .name span')) $E('#TB_ajaxContent .name span').setText ($E('#contact input[name=fname]').value);
    if ($E('#TB_ajaxContent .title span')) $E('#TB_ajaxContent .title span').setText ($E('#contact input[name=title]').value);
    if ($E('#TB_ajaxContent .company span')) $E('#TB_ajaxContent .company span').setText ($E('#contact input[name=company]').value);
    if ($E('#TB_ajaxContent .email span')) $E('#TB_ajaxContent .email span').setText ($E('#contact input[name=email]').value);
    if ($E('#TB_ajaxContent .phoneNumber span')) $E('#TB_ajaxContent .phoneNumber span').setText ($E('#contact input[name=phone_number]').value);
    */
    $('form-name').value = $E('#contact input[name=fname]').value;
	$('form-sname').value = $E('#contact input[name=sname]').value;
    $('form-title').value = $E('#contact input[name=title]').value;
    $('form-company').value = $E('#contact input[name=company]').value;
    $('form-email').value = $E('#contact input[name=email]').value;
    $('form-phone').value = $E('#contact input[name=phone_number]').value;
    $('form-interestType').value = $E('#contact select[name=interest]').value;
    $('form-comments').value = $E('#contact textarea[name=comments]').value;
}

$('submitCommon').addEvent(
    'click',
    function(e)
    {
        e = new Event(e).preventDefault();
        var _errors = new Array();
        USE_IN_POPUP = Template.data[$E('#contact select[name=interest]').value];
        
        if ( '' == $E('#contact input[name=fname]').value.split(' ').join('') ) _errors.include('You need to fill in your First Name.');
		if ( '' == $E('#contact input[name=sname]').value.split(' ').join('') ) _errors.include('You need to fill in your Surname.');
        //if ( '' == $E('#contact input[name=company]').value.split(' ').join('') ) _errors.include('You need to fill in your Company.');
        if ( '' == $E('#contact input[name=email]').value.split(' ').join('') ) _errors.include('You need to fill in your Email Address.');
        
        if ( _errors.length ) {
            alert("Incomplete Form : \n - " + _errors.join("\n - "));
            return false;
        }
        
        var _req = new XHR ({
            method : 'post',
            onSuccess : function ( _response ){
                //alert ( _response );
            },
            onFailure : function ( _response ){
                //alert ( 'Error in sending email.' );
            }
        });
        
        var _getPost = function ( _el ){
        	var _el = $E( _el );
            return _el.name + '=' + escape( _el.value );
        };
        
        var _post = 'name=' + $E ( '#contact input[name=fname]' ).value
            + '&' + _getPost ( '#contact input[name=sname]' )
			+ '&' + _getPost ( '#contact input[name=title]' )
            + '&' + _getPost ( '#contact input[name=company]' )
       	    + '&' + _getPost ( '#contact input[name=email]' )
            + '&phone=' + $E ( '#contact input[name=phone_number]' ).value
            + '&interestType=' + $E ( '#contact select[name=interest]' ).value
            + '&' + _getPost ( '#contact textarea[name=comments]' )
            + '&respond=RESPOND&page=1';

        _req.send ( 'php/script/sendmail.php', _post );
        $('openMoreInfo').fireEvent('click', e);
	    pageTracker._trackPageview ( '/contact/' + $('form-interestType').value + '/submit-one');
    }
);

function closePopup ()
{
    TB_remove();
	//commented out following line because this is now included in TB_remove()	
    //sendEmail( true );
}

function sendEmail ( _ignore )
{
    _ignore = _ignore?_ignore:false;
    var _errors = new Array();
    USE_IN_POPUP = Template.data[$('form-name').value];
    
    if ( _ignore ) $('form-ignore').value = 'IGNORE';
    else
    {
	    if ( '' == $('form-name').value.split(' ').join('') ) _errors.include('You need to fill in your First Name.');
		if ( '' == $('form-sname').value.split(' ').join('') ) _errors.include('You need to fill in your Surname.');
	    //if ( '' == $('form-company').value.split(' ').join('') ) _errors.include('You need to fill in your Company.');
	    if ( '' == $('form-email').value.split(' ').join('') ) _errors.include('You need to fill in your Email Address.');
	    if ($('uploadresume') && '' == $('uploadresume').value ) _errors.include('You need to attach a resume / cover letter.');
	    //if ($('interestedPosition') && '' == $('interestedPosition').value ) _errors.include('You need to fill in your desired job position.');
    }
    
    if ( _errors.length ) alert("Incomplete Form : \n - " + _errors.join("\n - "));
    else 
    {
        //commented out following line because this is now handled by ga.js instead of urchin
		//urchinTracker ( '/contact/' + $('form-interestType').value + '/'); 
		pageTracker._trackPageview ( '/contact/' + $('form-interestType').value + '/'); 
        $E('form.finalForm').submit();
    }
}

