﻿var g_Request = null;

var CB = function() 
{
	return {

	Tally : function(object, method, task, numthings)
	{
	
	    var data = 'tallyObject=' + object + '&' +
				'tallyMethod=' + method + '&' +
				'tallyTask=' + task;
				
        if(numthings != null)
        {
            data += '&tallyNumThings=' + numthings;
        }
        
        var page = 'tally.aspx';	
        
        if(typeof ajaxRelativePrefix != 'undefined')
		{
			// prefix page with relative path prefix var that was inserted by MatrixPage
			page = ajaxRelativePrefix + page;
		}
		else
		{
		    //try careerbuilder.com in the ajax folder
		    
		    page = 'http://www.careerbuilder.com/ajax/' + page;
		}
					   
	    submitCBTally(data, page);

    },
    
 
    IsValidEmail : function(emails, allowMultipleEmails, giveMsg, emailInputField)
    {
	    var emailRegEx = new RegExp("^([-A-Za-z0-9._\'\&+])+@[-A-Za-z0-9._\'+]+[.][A-Za-z]{2,}$", "g" );
        var emailCSV = new RegExp(",|,,|;|\r|\n|,\r", "g" );
	    var emailToServer = "";

	    //this method relies on 2 crucial variables that should be set in the page. This is the regex code.
	    if (typeof emailRegEx == "undefined" || typeof emailCSV == "undefined" || emailRegEx == null || emailCSV == null)
	    {
		    alert('Error: Email validator is not available at the moment or is corrupted. Please contact technology');
		    return false;
	    }
	    
	    if(allowMultipleEmails == null)
	        allowMultipleEmails = false;
	        
	    if(giveMsg == null)
	        giveMsg = true;    

	    if(emails.length > 0)
	    {
		    //see if the passed in string is a csv
		    //we'll also allow semi-colon seperated strings
		    if( ( emails.indexOf(",") > 0 || emails.indexOf(";") > 0) && allowMultipleEmails == true )
		    {
			    //email seems to be comma/semi-comma seperated.
    			
			    //make sure we remove trails
			    emails = emails.emailtrim();
    			
			    //Do a string replacement to make sure CSV is in proper form
			    var standardizedEmailCSV = emails.replace(emailCSV, ","); 
			    var arEmails = standardizedEmailCSV.split(",");
    			
			    var email = ""
    			
			    for(var i=0; i< arEmails.length ; i++)
			    {
				    email = arEmails[i];
    				
				    //make sure we remove trails
				    email = email.emailtrim();

				    if(email == "" || email == ' ')
				    {
					    //skip this round
					    continue;
				    }
				    if(email.match(emailRegEx) == null)
				    {
					    //invalid email, bail out
    					
					    //if we have this to true, this will give us the wrong email for the user
    					
					    if(giveMsg == true)
							    alert('Please correct the following invalid email address, and try again.\n' + email);
    					
					    return false;
				    }
    				
				    //reconstruct emails
				    if (emailToServer != "")
					    emailToServer += ";";
    					
				    emailToServer += email;
			    }
    			
		    }
		    else
		    {
                    //Not CSV
                    
                    //make sure we remove trails
				    emails = emails.emailtrim();
    					
                    bool = emails.match(emailRegEx); 	
				    if (bool == null)
				    {
					    if(giveMsg)
						    alert('Your email address is invalid. Please correct it and try again.');
    						
					    return false;
				    }
				    else
				    {
					    emailToServer = emails;
				    }
		    }	
    				
	    }
	    else
	    {
		    //should be empty string here
		    emailToServer = emails;
	    }
    	
	    //if they get to this point, it should be TRUE

	    if (typeof emailInputField != "undefined")
	    {
		    emailInputField.value = emailToServer;
	    }
    	
	    return true;
    	
       	
    },
    
    OtherFunction : function()
    {
        return '0';
    },
    
	object: function (o) 
	{
	    function F(){}
	    F.prototype = o;
	    return new F();
	},
	
	// convenience function for grabbing an element
	e: function (id){
		return document.getElementById(id);
	}
    
	};

}();


//utilities

function InitializeRequestManager()
{
    try
    {
        g_Request=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            g_Request=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(oc)
        {
            g_Request=null;
        }
    }

    if(!g_Request && typeof XMLHttpRequest!="undefined")
    {
        g_Request = new XMLHttpRequest();
    }
} 

function submitCBTally(data, page)
{
    InitializeRequestManager();
	g_Request.onreadystatechange = processCBTallyCallBack;
    g_Request.open('POST', page, true); 
    g_Request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    g_Request.send(data);
}

function processCBTallyCallBack()
{
    if (g_Request.readyState == 4)
    {
        // only if "OK"
            if (g_Request.status == 200)
            {	
                if(g_Request.responseText == "TALLY OK") 
		        {
			        //if anyone finds any need to do anything, that can be done here on success
		        }
                else
		        {
			        //same here, on failure  
			    }    
            }
            else
            {
		        //some other failure occured
            }
     }
}

String.prototype.emailtrim = function()
{
 // skip leading and trailing whitespace
 // and return everything in between
  var x=this;
  x=x.replace(/^\s*(.*)/, "$1");
  x=x.replace(/(.*?)\s*$/, "$1");
  x=x.replace(/(.*?),*$/, "$1");
  return x;
}

// taken from Mootools to help sniff out the browser
if (window.ActiveXObject) window.ie = window[window.XMLHttpRequest ? 'ie7' : 'ie6'] = true;
else if (document.childNodes && !document.all && !navigator.taintEnabled) window.khtml = true;
else if (document.getBoxObjectFor != null) window.gecko = true;


/*
Property: getWidth
	Returns an integer representing the width of the browser window (without the scrollbar).
*/

window.getWidth = function(){
		if (this.khtml) return this.innerWidth;
		if (this.opera) return document.body.clientWidth;
		return document.documentElement.clientWidth;
	};

/*
Property: getHeight
	Returns an integer representing the height of the browser window (without the scrollbar).
*/

window.getHeight = function(){
		if (this.khtml) return this.innerHeight;
		if (this.opera) return document.body.clientHeight;
		return document.documentElement.clientHeight;
	};

/*
Property: getScrollWidth
	Returns an integer representing the scrollWidth of the window.
	This value is equal to or bigger than <getWidth>.

See Also:
	<http://developer.mozilla.org/en/docs/DOM:element.scrollWidth>
*/

window.getScrollWidth = function(){
		if (this.ie) return Math.max(document.documentElement.offsetWidth, document.documentElement.scrollWidth);
		if (this.khtml) return document.body.scrollWidth;
		return document.documentElement.scrollWidth;
	};

/*
Property: getScrollHeight
	Returns an integer representing the scrollHeight of the window.
	This value is equal to or bigger than <getHeight>.

See Also:
	<http://developer.mozilla.org/en/docs/DOM:element.scrollHeight>
*/

window.getScrollHeight = function(){
		if (this.ie) return Math.max(document.documentElement.offsetHeight, document.documentElement.scrollHeight);
		if (this.khtml) return document.body.scrollHeight;
		return document.documentElement.scrollHeight;
	};

/*
Property: getScrollLeft
	Returns an integer representing the scrollLeft of the window (the number of pixels the window has scrolled from the left).

See Also:
	<http://developer.mozilla.org/en/docs/DOM:element.scrollLeft>
*/

window.getScrollLeft = function(){
		return this.pageXOffset || document.documentElement.scrollLeft;
	};

/*
Property: getScrollTop
	Returns an integer representing the scrollTop of the window (the number of pixels the window has scrolled from the top).

See Also:
	<http://developer.mozilla.org/en/docs/DOM:element.scrollTop>
*/

window.getScrollTop = function(){
		return this.pageYOffset || document.documentElement.scrollTop;
	};

/*
Property: getSize
	Same as <Element.getSize>
*/

window.getSize = function(){
		return {
			'size': {'x': this.getWidth(), 'y': this.getHeight()},
			'scrollSize': {'x': this.getScrollWidth(), 'y': this.getScrollHeight()},
			'scroll': {'x': this.getScrollLeft(), 'y': this.getScrollTop()}
		};
	};