// Unclaim the "$" namespace
jQuery.noConflict();
var $j = jQuery;

/**
 * page() is used in the pagination of the background and icon pages
 */
function page(pg) {

	// Maintain icon selections
	//checkIconSelection();

	document.getElementById('p').value = pg;
	document.ticker_sort.submit();
}

function getFlickr(page) {

	if (!page) {
		page = 1;
	}

	var flickr_box = document.getElementById("flickrbox");

	// Loading message
	flickr_box.innerHTML = "Retrieving Flickr Photos...";

	// Get the comments
	url = '/create/get_flickr_photos.php?fp=' + page;
	xmlhttp.open("GET", url ,true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			flickr_box.innerHTML = xmlhttp.responseText;
		}
	}

	xmlhttp.send(null)
}

function use(image_url) {
	document.getElementById('flckr').src = image_url;
	document.getElementById('flickr_image').value = image_url;
	document.getElementById('selectedphotodiv').style.display = 'block';
}

function clearSelection() {
	document.getElementById('flckr').src = '';
	document.getElementById('flickr_image').value = '';
	document.getElementById('selectedphotodiv').style.display = 'none';
}

// saveIconSelection: Used to send the final icon selections from step 2 to step 3
function saveIconSelection() {

	// Write the current selection of icons to the 'icon' value of the main ticker form
	$j("#icon").attr('value', $j("#selectedicons").attr("value"));
}

// Verify an email address
function valEmail(theEmail) {
	with (theEmail) {
		apos	= theEmail.indexOf("@");
		dotpos	= theEmail.lastIndexOf(".");
		
		if (apos < 1 || dotpos - apos < 2) {
			return false;
		} else {
			return true;
		}
	}
}

// Verify the form
function verifyForm(form) {
	// Loop through form elements
	var errNum	= 0;
	var errMsg	= "";
	
	for (var i= 0; i < form.elements.length; i++) {
		var formType	= form.elements[i].type;
		var formName	= form.elements[i].name;
		var formVal		= document.getElementById(formName).value;
		//alert(formName);
			
		switch(formName) {
			//__________________________________________________ TRACKER : TTCT
			case "ttct_fd_month":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please choose the month for your First Cycle.\n";
				}
				break;
			case "ttct_fd_day":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please choose the day for your First Cycle.\n";
				}
				break;
			case "ttct_fd_year":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please choose the year for your First Cycle.\n";
				}
				break;
				
			case "ttct_lid":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please choose the length in days.\n";
				}
				break;
				
			case "ttct_dc_month":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please choose the month for the date of your cycle event.\n";
				}
				break;
			case "ttct_dc_day":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please choose the day for the date of your cycle event.\n";
				}
				break;
			case "ttct_dc_year":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please choose the year for the date of your cycle event.\n";
				}
				break;
				
			case "ttct_pos_month":
				if (document.getElementById("ttct_pos").checked == true) {
					if (formVal == "") {
						errNum++;
						errMsg	+= "Please choose the month for the date of your cycle event.\n";
					}
				}
				break;
			case "ttct_pos_day":
				if (document.getElementById("ttct_pos").checked == true) {
					if (formVal == "") {
						errNum++;
						errMsg	+= "Please choose the day for the date of your cycle event.\n";
					}
				}
				break;
			case "ttct_pos_year":
				if (document.getElementById("ttct_pos").checked == true) {
					if (formVal == "") {
						errNum++;
						errMsg	+= "Please choose the year for the date of your cycle event.\n";
					}
				}
				break;
				
			case "timezone":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please choose your timezone.\n";
				}
				break;
			
			
			//__________________________________________________ MISC
			case "_name":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please fill out your name.\n";
				}
				break;
				
			case "_email":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please fill out your email address.\n";
				} else {
					// Not blank, check against pattern
					if (!valEmail(formVal)) {
						errNum++;
						errMsg	+= "Please format your email address properly.\n";
					}
				}
				break;
				
			case "_address":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please fill out your address.\n";
				}
				break;
				
			case "_city":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please fill out your city.\n";
				}
				break;
				
			case "_state":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please choose your state.\n";
				}
				break;
				
			case "_country":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please choose your country.\n";
				}
				break;
				
			case "_howfound":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please choose how you found out about us.\n";
				}
				break;
			
			case "_captcha":
				if (formVal == "") {
					errNum++;
					errMsg	+= "Please complete the captcha.\n";
				}
				break;
		}
	}
	
	if (errNum >= 1) {
		alert(errMsg);
		return false;
	} else {
		return true;
	}
}

// TTCT POS Check
function posCheckCheck(checkStatus) {
	if (checkStatus) {
		document.getElementById("custom_text").value	= '';
		document.getElementById("custom_text").disabled	= true;
	
	} else {
		document.getElementById("custom_text").value	= '';
		document.getElementById("custom_text").disabled	= false;
	}
}

// Jump menu functionality
function selectJumpMenu(theUrl) {
 	if (theUrl != "") {
		  window.top.location.href = theUrl;
	 }
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function onlyNumbers(evt) {
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        //status = "This field accepts numbers only."
        return false
    }
    //status = ""
    return true
}


// Remove an icon from the icon list in the 'Your Current Icons' div
function removeIcon(iconname) {

	var removedyet = false;
	var newiconstring = '';

	// Loop through and find the one to remove
	$j("#selectedicons").attr('value').split(',').each(function(i, value) {

		if (i != '') {
			if (removedyet != true) {

				if (i != iconname) {
					newiconstring += i + ',';
				}
				else {
					removedyet = true;
				}
			}
			else {
				newiconstring += i + ',';
			}
		}
	});

	// Update the string in the selectedicons field and update the current icon display
	$j("#selectedicons").attr('value', newiconstring);

	// Update the current icon selection in the 'Your Current Icons' div at the top
	$j(".yourcurrenticons").html('');
	$j("#selectedicons").attr('value').split(',').each(function(i, value) {
			if (i != '') {
				$j(".yourcurrenticons").html( $j(".yourcurrenticons").html() + '<img src="/create/tickergraphics/' + i + '.png" /> <a href="#" onclick="javascript: removeIcon(\'' + i + '\'); return false;"><img src="/gif/del.gif" width=16 height=16 style="margin-left:2px;border:0px;" /></a><br />');
			}
		}
	);
}


/* For use in Step 2: Handle the addition to and modification of the icon choices array */
$j(document).ready(function() {

	// See if we should load the 'Your current Icons' div with something
	if ($j("#selectedicons").attr('value')) {
		$j("#selectedicons").attr('value').split(',').each(function(i, value) {
				if (i != '') {
					$j(".yourcurrenticons").html( $j(".yourcurrenticons").html() + '<img src="/create/tickergraphics/' + i + '.png" /><a href="#" onclick="javascript: removeIcon(\'' + i + '\'); return false;"><img src="/gif/del.gif" width=16 height=16 style="margin-left:2px;border:0;" /></a><br />');
				}
			}
		);
	}

	// Do something when we click on an icon
	$j(".iconcheckbox").click(function() { 

		// Determine whether or not this is one icon too many (limit is 5)
		if ($j("#selectedicons").attr('value').split(',').length > 5) {

			// Note: There'll always be a blank last entry in the  array that results from 
			// the split because of how we store icons in the list as plain text (i.e., "icon###,"). It's cool.
			alert("Er, I think 5 icons is enough.");
		}
		else {
		
			// Add the clicked-on icon to the selected icon list
			$j("#selectedicons").attr('value', $j("#selectedicons").attr('value') + this.name + ',');

			// Update the current icon selection in the 'Your Current Icons' div at the top
			$j(".yourcurrenticons").html('');
			$j("#selectedicons").attr('value').split(',').each(function(i, value) {
					if (i != '') {
						$j(".yourcurrenticons").html( $j(".yourcurrenticons").html() + '<img src="/create/tickergraphics/' + i + '.png" /><a href="#" onclick="javascript: removeIcon(\'' + i + '\'); return false;"><img src="/gif/del.gif" width=16 height=16 style="margin-left:2px;border:0;" /></a><br />');
					}
				}
			);
		}
	});
});
