

// Set a global array structure to store all the perncet field ids, this array is used later to do a check.
var arr_percent_ids = new Array();

// Set a global array to store the field ids for each image link, this array is used later to do a check.
var arr_images_ids = new Array();

/**
 * Add a new row to the content ads table.
 *
 * @return void
 */
function content_ads_add_new_row()
{
 	// Look if we have the table there.
 	var obj_content_ads_form_table = document.getElementById( "content_ads_table" );

 	// If the table is there, try to do something.
 	//
 	// NOTE: I was trying to return false here, but when I try to return something the page breaks and do an echo of the return :|.
 	if ( obj_content_ads_form_table )
 	{
		// Clone last row.
		var obj_cloned_row = $( "#content_ads_table tr:last" ).clone();

		var str_last_row_id = $( obj_cloned_row ).attr( "id" );

		var int_current_row_index = parseInt( str_last_row_id.replace( /content_ads_row_/, '' ), 10 );

		var int_new_row_index = int_current_row_index + 2;


		// Set image td html string.
		var str_image_td = '<input size="22" name="content_ads[' + int_new_row_index + '][image_link]" id="content_ad_image_' + int_new_row_index + '" accesskey="' + int_new_row_index + '" type="text" required="true"  err_label="Please set an image link."> <img src="/shared-js/tiny_mce/themes/advanced/images/browse.gif" onclick="window.open( \'image.select.php?field=content_ad_image_' + int_new_row_index + '&win=window.opener\', \'content_ad_image_popup_' + int_new_row_index + '\', 700, 600 );" style="cursor: pointer;" title="Add System Image"></td>';

		// Set link td html string.
		var str_link_td = '<input size="13" name="content_ads[' + int_new_row_index + '][page_link]" id="content_ad_link_' + int_new_row_index + '" type="text" required="true" err_label="Please set a page link."> <img src="/shared-js/tiny_mce/themes/advanced/images/browse.gif" onclick="window.open( \'content.select.php?field=content_ad_link_' + int_new_row_index + '&win=window.opener&return=url\', \'content_ad_image_popup_' + int_new_row_index + '\', 700, 600 );" style="cursor: pointer;" title="Add System Page">';

		// Set percent td html string.
		var str_percent_td = '<input size="4" name="content_ads[' + int_new_row_index + '][percent]" id="content_ad_percent_' + int_new_row_index + '" type="text" required="true" err_label="Please set rotation percent value." check_characters="[^0-9]" item_label="Rotation Percent">';

		// Set views html.
		var str_views_td = '<span id="content_ad_views_' + int_new_row_index + '">&nbsp;</span>';

		// Set clicks html.
		var str_clicks_td = '<span id="content_ad_clicks_' + int_new_row_index + '">&nbsp;</span>';

		// Delete link html string.
		var str_delete_td_link = '<span class="buttons" id="content_ad_delete_' + int_new_row_index + '"><a href="javascript:;" onclick="content_ads_delete_row( \'' + int_new_row_index + '\' );" style="width: 60px;">Delete</a></span>';

	   // Add cloned row to the main table.
		$( "#content_ads_table" ).append( obj_cloned_row );

		// add an id to the new row created, this id is used to remove the row from the table :).
		$( "#content_ads_table tr:last" ).attr( { "id" : "content_ads_row_" + int_new_row_index } );

		// Now add the percent field id to the global structure.
		arr_percent_ids[int_new_row_index] = "content_ad_percent_" + int_new_row_index;

		// Set image id into the global structure to do a check later.
		arr_images_ids[int_new_row_index] = "content_ad_image_" + int_new_row_index;

		// combine all td strings from above
        var td_strings = "<td style=\"padding:2px;\">" + str_image_td + "</td><td style=\"padding:2px;\">" + str_link_td + "</td><td style=\"padding:2px;\">" + str_percent_td + "</td><td style=\"padding:2px;\">" + str_views_td + "</td><td style=\"padding:2px;\">" + str_clicks_td + "</td><td>" + str_delete_td_link +"</td>";

        // set tr html with the td's
        $( "#content_ads_table tr:last" ).html(td_strings);




	}
 }

/**
 * Delete an page ad row.
 *
 * @param inetger int_row_index Row index number.
 * @return void
 */
function content_ads_delete_row( int_row_index,int_element_id )
{

		// If we have a row id here, try to remove it.
 	if ( int_row_index == "" )
 	{
		return false;
	}

	// Confirm to remove a row.
	if ( confirm( "Are you sure to remove this page ad?" ) )
	{

        content_ads_add_new_row(); // add new row and then delete the old one.

        var deleteaction = document.getElementById('deleteaction');
        deleteaction.value = "t";


		// If we value of the rows of the table is different than 2, try to remove that row from the main table.
		if ( $( "#content_ads_table tr" ).length != "2" )
		{
			var obj_content_ads_row = document.getElementById( "content_ads_row_" + int_row_index );

			if ( !obj_content_ads_row )
			{
				return false;
			}

			// Try to remove this row.
			$( "#content_ads_table #content_ads_row_" + int_row_index ).remove();
		}
		// Reset values from the unique row that we have at this point, why ? cause we need this row to clone it, if the user want another.
		else
		{
			// kinda hackish, but what you gonna do?
			// check to see if they are using the element id (the database ID) and use that first
			// if not, try to see if the row index works.
			// BUT not for the percent field - that always uses the row index. Brilliant!

			if (int_element_id != '')
			{
				$( "#content_ad_image_" + int_element_id ).val( "" );
				$( "#content_ad_link_" + int_element_id ).val( "" );
			}
			else
			{
				$( "#content_ad_image_" + int_row_index ).val( "" );
				$( "#content_ad_link_" + int_row_index ).val( "" );
			}

			$( "#content_ad_percent_" + int_row_index ).val( "" );
		}
	}
	else
	{
		return false;
	}
}

/**
 * Save ads for this page.
 *
 * @return boolean FALSE, error, TRUE, done.
 */
function save_ads()
{

	// Get form object.
	var obj_content_ads_form = document.getElementById( 'content_ads_form' );
	var deleteaction = document.getElementById('deleteaction');

	// No form ? stop this.
	if ( !obj_content_ads_form )
	{
		return false;
	}



	// Try to validate the fields of the form.
	// skip if deleting an item
	if (deleteaction.value == 'f')
	{
    	if ( false == validate_form( obj_content_ads_form ) )
    	{
    		return false;
    	}


    	// Set summatory variable.
    	var int_percents_summatory = null;

    	// Loop through all percent fields and make a summatory of all his values.
    	for ( int_percent_fields_counter = 0; int_percent_fields_counter < arr_percent_ids.length; int_percent_fields_counter++ )
    	{
    		var obj_percent_field = document.getElementById( arr_percent_ids[int_percent_fields_counter] );

    		if ( obj_percent_field )
    		{
    			int_percents_summatory += parseInt( obj_percent_field.value );
    		}
    	}

    	// Set a string to store some errors.
    	var str_form_errors = "";

    	// If the total of percents is not 100% set an alert with a warning message.
    	if ( int_percents_summatory != 100 )
    	{
    		str_form_errors += "The summatory of all percents must be equal to 100%.\n";
    	}

    	// Set images error flag.
    	var bln_images_error = false;

    	for ( int_image_fields_counter = 0; int_image_fields_counter < arr_images_ids.length; int_image_fields_counter++ )
    	{
    		var obj_field_image = document.getElementById( arr_images_ids[int_image_fields_counter] );

    		var str_image_field_value = $( "#" + arr_images_ids[int_image_fields_counter] ).val();

    		if ( obj_field_image )
    		{
    			for ( int_image_fields_sec_counter = 0; int_image_fields_sec_counter < arr_images_ids.length; int_image_fields_sec_counter++ )
    			{
    				if ( int_image_fields_counter != int_image_fields_sec_counter )
    				{
    					if ( str_image_field_value == $( "#" + arr_images_ids[int_image_fields_sec_counter] ).val() )
    					{
    						bln_images_error = true;
    					}
    				}
    			}
    		}
    	}

    	// We have an error into the images, set a special message.
    	if ( bln_images_error )
    	{
    		str_form_errors += "Seems like the some image link is the same on another page ad, image link needs to be different for each ad.\n";
    	}

    	if ( str_form_errors != "" )
    	{
    		alert( "There are some errors on the form: \n\n" + str_form_errors );

    		return false;
    	}
	}

	// Ok, now try to submit the form.
	obj_content_ads_form.submit();

	return true;
}

/**
 * Save ad clicks :O.
 *
 * @param integer int_ad_id Ad id.
 * @return boolean FALSE, error, TRUE, done.
 */
function save_ad_clicks( int_ad_id )
{
	if ( !int_ad_id )
	{
		return false;
	}

	$( document ).load( '/content_ads.clicks.php?id=' + int_ad_id );

	return true;
}
