/**
 *   Object to handle the special effects on the join forms
 *
 *   @link http://www.webcamclub.com/
 *   @package WCC
 *   @author $Author: joe $
 *   @copyright Copyright (c) 2008 by Deniro Marketing
 *
 *   @version $Revision: 6713 $
 *   @filesource
 */

/* $Id: wccJoin.js 6713 2010-07-20 22:39:36Z joe $ */

var wccJoin = {

    /**
     *   Array of input elements on the form that are to be evaluated
     *
     *   @var array
     *
     */
    elements : null,
    /**
     *   Preloaded image for the processing gif
     *
     *   @var object
     *
     */
    img_processing : null,
    /**
     *   Preloaded image to display if the input value is valid
     *
     *   @var object
     *
     */
    img_valid : null,
    /**
     *   Preloaded image to display if the input value is not valid
     *
     *   @var object
     *
     */
    img_invalid : null,
    /**
     *   url to send off the ajax calls ...
     *
     *   @var string
     *
     */
    ajax_url : location.protocol + "//"+location.hostname+"/xml/validate.php",
    /**
     *   url to send off the ajax calls ...
     *
     *   @var string
     *
     */
    post_url : location.protocol + "//"+location.hostname+"/members/index.php",
    /**
     *   number of seconds to pause ...
     *
     *   @var int
     *
     */
    ajax_timeout : null,
    /**
     *   waiting gif - spinner ...
     *
     *   @var string
     *
     */
	waiting_img : null,
    /**
     *   this is the cell that will display the ajax stuff (step 3)
     *
     *   @var string
     *
     */
    cc_handler_cell : "cc_handler_cell",
    /**
     *   the html string that will display processing ....
     *
     *   @var string
     *
     */
	processing_html : null,
    /**
     *   contains the id of the popped model on the geo join page ....
     *
     *   @var string
     *
     */
	geo_popped_id : null,

    /**
     *   Initializes the class; preloads the images; creates the ajax object
     *
     *   @access public
     *   @param string imgProcessing: url to the image to load to indicating there is a process running
     *   @param string imgValid: url to the image to load to indicating the input value is valid
     *   @param string imgInvalid: url to the image to load to indicating the input value is not valid
     *   @return void
     *
     */
    initialize : function( imgProcessing, imgValid, imgInvalid, waiting_img_url )
    {
        // Preloading images ...
        wccJoin.img_processing = new Image();
        wccJoin.img_processing.src = imgProcessing;

        // Preloading images ...
        wccJoin.img_valid = new Image();
        wccJoin.img_valid.src = imgValid;

        // Preloading images ...
        wccJoin.img_invalid = new Image();
        wccJoin.img_invalid.src = imgInvalid;

		// Preloading the waiting image
		wccJoin.waiting_img = new Image();
		wccJoin.waiting_img.src = waiting_img_url;

        // Array of elements that will be examined ...
        wccJoin.elements = new Array();

        // How long to hold the timeout for
        wccJoin.ajax_timeout = null;

		// html that will display processing ....
	    var html  = "";
		html += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" style=\"padding-top:25px;\">";
		html += "    <tr>";
		html += "        <td align=\"right\" valign=\"middle\" width=\"30\">";
		html += "			<img src=\""+wccJoin.waiting_img.src+"\" />";
		html += "		</td>";
		html += "        <td align=\"left\" valign=\"middle\">";
		html += "			&nbsp;&nbsp;<font style=\"font-family: tahoma; font-size: 13px;\">Processing...</font>";
		html += "		</td>";
		html += "    </tr>";
		html += "</table>";
		wccJoin.processing_html = html;
    },

    /**
     *   Adds an element to the list of elements
     *
     *   @access public
     *   @param object e: Element object to add
     *   @return void
     *
     */
    add_element : function ( elem )
    {
        // Set up the ajax calls
        var ajax_url = location.protocol + "//"+location.hostname+"/xml/validate.php";
        wccJoin.elements[elem.input_id] = elem;
        wccJoin.elements[elem.input_id].oInput = document.getElementById( elem.input_id );
        wccJoin.elements[elem.input_id].oImage = document.getElementById( elem.image_id );
        wccJoin.elements[elem.input_id].oMessage = document.getElementById( elem.message_id );
        wccJoin.elements[elem.input_id].oLabel = document.getElementById( elem.label_id );
		wccJoin.elements[elem.input_id].oMsgRow = document.getElementById( elem.msg_row );
        wccJoin.elements[elem.input_id].processing = false;
        wccJoin.elements[elem.input_id].ajax = new oAjax( ajax_url, wccJoin.response_handler );
        wccJoin.elements[elem.input_id].ajax_timeout = null;
        wccJoin.elements[elem.input_id].response = {};
        wccJoin.elements[elem.input_id].response.is_valid = 0;
        wccJoin.elements[elem.input_id].response.message = "";
        wccJoin.elements[elem.input_id].response.reason = "";
        wccJoin.elements[elem.input_id].response.id = "";
        wccJoin.elements[elem.input_id].response.value = "";
    },

    /**
     *   Wrapper method around setTimeout
     *
     *   @access public
     *   @param string func: The js code or function to execute
     *   @return void
     *
     */
    set_timeout : function( input_id, func )
    {
        if ( wccJoin.elements[input_id].ajax_timeout != null )
        {
            wccJoin.clear_timeout( input_id );
        }

        wccJoin.elements[input_id].ajax_timeout = setTimeout( func, 2000 );
    },

    clear_timeout : function( input_id )
    {
        if ( wccJoin.elements[input_id].ajax_timeout != null )
        {
            clearTimeout( wccJoin.elements[input_id].ajax_timeout );
            wccJoin.elements[input_id].ajax_timeout = null;
        }
    },

    /**
     *   Marks the current element as valid
     *
     *   @access public
     *   @param void :
     *   @return void
     *
     */
    mark_valid : function( input_id )
    {
        if ( input_id != null && wccJoin.elements[input_id].response.is_valid == 1 )
        {
            wccJoin.elements[input_id].oImage.src = wccJoin.img_valid.src;
            wccJoin.elements[input_id].oLabel.className = "label_standard";
            wccJoin.elements[input_id].oMessage.className = "element_message";

			if ( wccJoin.elements[input_id].response.message != "" )
			{
				wccJoin.elements[input_id].oMsgRow.className = "show";
				wccJoin.elements[input_id].oMessage.innerHTML = wccJoin.elements[input_id].response.message;
			}
			else
			{
				wccJoin.elements[input_id].oMsgRow.className = "hide";
			}

			if ( wccJoin.elements[input_id].valid_message_append != null && wccJoin.elements[input_id].oMessage.innerHTML != "")
				wccJoin.elements[input_id].oMessage.innerHTML += wccJoin.elements[input_id].valid_message_append;
        }
    },

    /**
     *   Marks the current element as in-valid
     *
     *   @access public
     *   @param void :
     *   @return void
     *
     */
    mark_invalid : function( input_id )
    {
        if ( input_id != null && wccJoin.elements[input_id].response.is_valid == 0 )
        {
            wccJoin.elements[input_id].oImage.src = wccJoin.img_invalid.src;
            wccJoin.elements[input_id].oLabel.className = "label_error";
            wccJoin.elements[input_id].oMessage.className = "error_message";

			wccJoin.elements[input_id].oMessage.innerHTML = wccJoin.elements[input_id].response.message;
			wccJoin.elements[input_id].oMsgRow.className = "show";

			if ( wccJoin.elements[input_id].invalid_message_append != null )
				wccJoin.elements[input_id].oMessage.innerHTML += wccJoin.elements[input_id].invalid_message_append;
        }
    },

    /**
     *   Marks the current element as processing
     *
     *   @access public
     *   @param void :
     *   @return void
     *
     */
    show_processing : function( input_id )
    {
        if ( !wccJoin.is_empty(wccJoin.elements[input_id].update_message) )
        {
            var html = "<img id=\"img_processing_flag_on\" src=\""+wccJoin.img_processing.src+"\" border=\"0\" />&nbsp;"+wccJoin.elements[input_id].update_message;

            if ( wccJoin.elements[input_id].oMessage.innerHTML != html )
            {
                wccJoin.elements[input_id].oMessage.className = "element_message";
                wccJoin.elements[input_id].oMessage.innerHTML = html;
            }
        }
    },

    /**
     *   Sends the validation request to the server on each keystroke
     *
     *   @access public
     *   @param object e: the event being handled
     *   @return bool
     *
     */
    doValidate : function ( event, elem )
    {
        var input_value;
        var keynum;
        var keychar;
        var numcheck;

        if ( window.event )
        {
            keynum = event.keyCode;
        }
        else if ( event.wich )
        {
            keynum = event.which;
        }

        input_value = wccJoin.elements[elem.id].oInput.value;

        if ( ( keynum >= 48 && keynum <= 57 ) || ( keynum >= 65 && keynum <= 90 ) || ( keynum >= 97 && keynum <= 122 ) )
        {
            keychar = input_value + String.fromCharCode( keynum );
        }

        if ( !wccJoin.is_empty( input_value ) )
        {
            wccJoin.show_processing( elem.id );
            wccJoin.elements[elem.id].processing = true;
            var params = "b64=b64";
            params += "&id="+wccJoin.elements[elem.id].oInput.id;
            params += "&value="+input_value;
            
            wccJoin.set_timeout( elem.id, "wccJoin.send_request('"+elem.id+"', '"+params+"');", 2000);
        }

        return ( true );
    },

    /**
     *   Sends the actual ajax call to the server
     *
     *   @access public
     *   @param string params: list of params delimited with an "&"
     *   @return bool
     *
     */
    send_request : function( input_id, params )
    {
        try
        {
            var validation_sent;

            if ( wccJoin.elements[input_id].ajax.updating )
            {
                wccJoin.elements[input_id].ajax.abort();
            }
            
            validation_sent = wccJoin.elements[input_id].ajax.update( params, "GET" );
            wccJoin.clear_timeout( input_id );
        }
        catch(e)
        {
        }

        return ( validation_sent );
    },

    /**
     *   The response handler for the ajax call to the server
     *
     *   @access public
     *   @param string r: The response string
     *   @param int s: The response status from the server
     *   @return void
     *
     */
    response_handler : function ( r, s )
    {
        try
        {
            if ( s == 200 )
            {
                var response = eval( r );
                wccJoin.elements[response.id].response = response;

                if ( response.is_valid == 0 )
                {
                    wccJoin.mark_invalid( response.id );
                }
                else
                {
                    wccJoin.mark_valid( response.id );
                }
            }
        }
        catch(e)
        {
        }
    },

	mark_selects : function ( input_id )
	{
    	wccJoin.elements[input_id].response.is_valid = 1;
		wccJoin.mark_valid( input_id );
	},

    /**
     *   Method to compare the values of two input elements
     *
     *   @access public
     *   @param object oInput: This the element being examined; typically self or this
     *   @return bool
     *
     */
    doCompare : function( oInput, oInputSource )
    {
    	if ( oInput != null && oInputSource != null )
		{
	        if ( !wccJoin.is_empty( oInput.value ) )
	        {
	            // turn the processing indicators on
	            wccJoin.show_processing( oInput.id );

	            // Check the values for a match
	            if ( oInput.value != oInputSource.value )
	            {
	                response = "({ is_valid : 0 , message : \"Email addresses do not match!\" , reason : \"Email addresses do not match!\" , id : \""+oInput.id+"\" , value : \""+oInput.value+"\" })";
	            }
	            else
	            {
	                response = "({ is_valid : 1 , message : \"Email addresses match!\" , reason : \"Email addresses match!\" , id : \""+oInput.id+"\" , value : \""+oInput.value+"\" })";
	            }

	            wccJoin.response_handler( response, 200 );
	        }
		}

        return ( true );
    },

    is_blank : function( elem )
    {
        if ( !wccJoin.elements[elem.id].processing )
        {
            if ( wccJoin.is_empty(elem.value) )
            {
                response = "({ is_valid : -1 , message : \"\" , reason : \"\" , id : \""+elem.id+"\" , value : \""+elem.value+"\" })";
	            wccJoin.response_handler( response, 200 );
            }
            else
            {
            	wccJoin.doCompare(elem, document.getElementById('email') );
            }
        }

        return ( true );
    },

    notify : function( elem )
    {
        if ( !wccJoin.elements[elem.id].processing )
        {
            if ( wccJoin.is_empty(elem.value) )
            {
				column_name = wccJoin.elements[elem.id].column_name;
                response = "({ is_valid : 0 , message : \"Please enter a value for " + column_name + " \" , reason : \"\" , id : \""+elem.id+"\" , value : \""+elem.value+"\" })";
	            wccJoin.response_handler( response, 200 );
            }
			else
			{
                response = "({ is_valid : 1 , message : \"\" , reason : \"\" , id : \""+elem.id+"\" , value : \""+elem.value+"\" })";
	            wccJoin.response_handler( response, 200 );
			}
        }

        return ( true );
    },

    /**
     *   Checks to see if a string is empty
     *
     *   @access public
     *   @param string v: The string to evaluate if it is empty or not
     *   @return bool
     *
     */
    is_empty : function ( v )
    {
    	return ( v.replace( /\s+/g, "" ) .length == 0 );
    },

    is_zipcode : function( zipcode )
    {
        return ( zipcode.match(/\b[0-9]{5}(?:-[0-9]{4})?\b/) );
    },

    dummy : function()
    {/* do nothing ... */},

	submitCC : function ()
	{
		$("#tdProcessingGif").html(wccJoin.processing_html);
		document.getElementById("ccjAuthForm").submit();
		return true;
	},

	submitCCType : function (oID, ccID)
	{
        var ajax_url = location.protocol + "//"+location.hostname+"/index.php";
		$("#cc_handler_cell").html( wccJoin.processing_html ).show();
		var ajax = new oAjax( ajax_url, wccJoin.handle_cctype );
		var params = "show=ccjoin&xhprof=1&dc=yes&ccjStep2=doit&ajax=1&ccType="+ccID;
		ajax.update(params, "POST");
	},

	submitCCTypeLabel : function ( oID, ccID )
	{
		document.getElementById(oID).checked = true;
		wccJoin.submitCCType( oID, ccID );
	},

	handle_cctype : function ( r, s )
	{
		if ( s == 200 )
		{
			$("#cc_handler_cell").html(r).show();
			$("#ccjErrors").html('');
			findPopUps();
		}
	},

	geoClearPopup : function()
	{
		if (wccJoin.geo_popped_id != null)
		{
			$(wccJoin.geo_popped_id).hide();
			wccJoin.geo_popped_id = null;
		}
	},

	checkForm : function ()
	{
		if(document.application.termsAgree.checked)
		{
			return true;
		}
		else
		{
			alert("You must agree to the terms and conditions!");
			return false;
		}
	},

	clearOnBeforeUnload : function ()
	{
		if ( window.document.body.onbeforeunload != null )
		{
			window.document.body.onbeforeunload = ";";
		}
		else
		{
			window.onbeforeunload = null;
		}
	},

	geoShowPopup : function (pop_id)
	{
		wccJoin.geoClearPopup()
		$(pop_id).show();
		wccJoin.geo_popped_id = pop_id;
	},

	ajaxSubmit : function ()
	{
       	var results = false;
		if (wccJoin.checkForm())
		{
			wccJoin.clearOnBeforeUnload();
		    var ajax = new oAjax(this.post_url, wccJoin.ajaxSubmitHandler);
			var params = "";
			params += "show="+$("#id_show").val();
			params += "&joinpage="+$("#id_joinpage").val();
			params += "&ajax="+$("#id_ajax").val();
			params += "&uname="+$("#uname").val();
			params += "&email="+$("#email").val();
			params += "&cemail="+$("#cemail").val();
			params += "&country="+$("#country").val();
			params += "&captchastring="+$("#captchastring").val();
			params += "&termsAgree="+$("#termsAgree").val();

			ajax.update(params, "POST");
			results = true;
		}

		return results;
	},

	ajaxSubmitHandler : function (r,s,p)
	{
        try
        {
            if ( s == 200 )
            {
                var response = eval(r);

                if ( response.is_valid == 0 )
                {
					alert(response.message);
                }
                else
                {
                    location.href = location.protocol+"//"+location.hostname+response.value;
                }
            }
        }
        catch(e){}
	}
}