var ID = ""; //This is used for JQuery TinyMCE plugin
var Slides = "";
var Children = "";
var ActiveID = 1;
var NextID = 2;
var IE = false;

function CheckStoryForm() {
	var FName = $('#FirstName').val();
	var LName = $('#LastName').val();
	var Story = $('#StoryText').val();
	var Error = "";
	var Good = true;
	if (FName == "") {
		Error += 'Please enter your first name<br/>';
		$('#FirstName').addClass('Alert');
		Good = false;
	} else {
		$('#FirstName').removeClass('Alert');
	}
	if (LName == "") {
		Error += 'Please enter your last name<br/>';
		$('#LastName').addClass('Alert');
		Good = false;
	} else {
		$('#LastName').removeClass('Alert');
	}
	if (Story == "") {
		Error += 'Please enter your story<br/>';
		$('#StoryText').addClass('Alert');
		Good = false;
	} else {
		$('#StoryText').removeClass('Alert');
	}
	$('#BadNews').html(Error);
	return Good;
}

function FadeSlides(ID) {
	Children = $('#'+ID).children(".Slide");
	ActiveElement = $('#'+ID).children(".Active");
	setInterval( "SwitchSlides()", 3200 );
	$.each($.browser,function(i,v) {
		if (i == "msie") {
			IE = true;
		}
	});
}

function SwitchSlides() {
	var Count = $('#Movie').children().size();
	if (ActiveID > Count) {
		ActiveID = 1;
		NextID = 2;
	}
	if (NextID > Count) {
		NextID = 1;
	}
	if (!IE) {
		$('#'+ActiveID).fadeOut(650,function() {
			$('#'+NextID).delay(400).fadeIn(650);
			ActiveID = NextID;
			NextID += 1;
		});
	} else {
		$('#'+ActiveID).fadeOut(550,function() {
			$('#'+NextID).delay(400).fadeIn(550);
			ActiveID = NextID;
			NextID += 1;
		});
	}
}


function NoSpaces(area,evt) {
	//console.log(evt);
  	var charCode = (evt.which) ? evt.which : evt.keyCode;
	if (charCode == 32) {
		area.value += ',';
		return false;
	} else {
		return true;
	}
}
function TrimKeywords(field) {
	var Value = $(field).val().toLowerCase();
	var Counter=0; 
	var NewValue = "";
	var LastCharacterBad = false;
	$.each(Value,function(Number) {
		Letter = Value.substring(Number,parseInt(Number) + 1);
		if (Letter != " ") {
			NewValue = NewValue + Letter;
			Counter = Counter + 1
		}
		if (Letter == "," || Letter == " ") {
			LastCharacterBad = true;
		} else {
			LastCharacterBad = false;
		}
	});
	//alert(NewValue);
	//console.log("Counter: "+ Counter);
	while (LastCharacterBad) {
		Counter = Counter - 1;
		NewValue = NewValue.substring(0,Counter);
		LastCharacterBad = false;
		//console.log(NewValue);
		$.each(NewValue,function(Number,Letter) {
			//console.log(Number+" : "+Letter+" : "+Counter);
			if ((Number + 1) == Counter) { //You need the plus one because Number starts at zero, Counter starts at 1
				//console.log(Number+" ? "+Letter);
				if (Letter == ' ' || Letter == ',') {
					LastCharacterBad = true;
				}
			}
		});
	}
	$(field).val(NewValue);
}

function SuggestKeywords(FieldID,Class) {
	var Suggestions = "";
	var WordArray = "";
	var Value = "";
	//console.log(Class+'TextField');
	//var TextFields = $('input:text');
	var TextFields = $('input[rel='+Class+'TextField]');
	//console.log(TextFields);
	var TextAreaFields = $('textarea[rel='+Class+'TextAreaField]');
	CurrentKeywords = $('#'+Class+FieldID).val();
	TextFields.each(function() {
		if (WordArray.length > 0 && $(this).val() != "") {
			WordArray = WordArray + ",";
		}
		Value = $(this).val().replace(",","");
		Value = Value.replace(".","");
		Value = Value.replace("?","");
		Value = Value.replace("!","");
		WordArray = WordArray + Value.split(" ");
		//console.log($(this).attr('id')+" : "+Value);
	});
	TextAreaFields.each(function() {
		if ($(this).attr('id') != FieldID) {
			if ($(this).hasClass('mceEditor')) {
				//console.log(tinyMCE);
				var oEditor = tinyMCE.getInstanceById($(this).attr('id')) ;
				var oDOM = oEditor.getDoc() ;
				var iLength ;
				// The are two diffent ways to get the text (without HTML markups).
				// It is browser specific.
				if (document.all) {
					// If Internet Explorer.
					iLength = oDOM.body.innerText;
				} else {
					// If Gecko.
					var r = oDOM.createRange() ;
					r.selectNodeContents(oDOM.body);
					iLength = r.toString();
				}

				//console.log($(this).attr('id')+" : "+iLength);
				if (WordArray.length > 0 && iLength != "") {
					WordArray = WordArray + ",";
				}
				Value = iLength.replace(",","");
				Value = Value.replace(".","");
				Value = Value.replace("?","");
				Value = Value.replace("!","");
				WordArray = WordArray + Value.split(" ");
			} else {
				if (WordArray.length > 0 && $(this).val() != "") {
					WordArray = WordArray + ",";
				}
				Value = $(this).val().replace(",","");
				Value = Value.replace(".","");
				Value = Value.replace("?","");
				Value = Value.replace("!","");
				WordArray = WordArray + Value.split(" ");
			}
		}
	});
	//Ok, now we should have a list of the words that they currently have entered
	var NewArray = WordArray.split(",");
	var NextArray = new Array();
	var Counter = 0;
	$.each(NewArray,function(Number,Word) {
		if (Word.length > 3 && Word != parseInt(Word)) {
			NextArray[Counter] = Word;
			Counter = Counter + 1;
		}
	});
	//Now, we have a list of all the words that are long enough and aren't numbers;
	var Suggestions = new Array();
	var WordExists = false;
	$.each(NextArray,function(Number,Word1) {
		WordExists = false;
		$.each(Suggestions,function(Number,WordArray) {
			//console.log(Word1.toLowerCase());
			if (Word1.toLowerCase() == WordArray[0]) {
				//We found it
				WordExists = true;
				WordArray[1] = parseInt(WordArray[1]) + 1;
			}
			//console.log(WordArray);
		});
		if (!WordExists) {
			var Count = Suggestions.length;
			//console.log(Count);
			Suggestions[Count] = new Array(2);
			Suggestions[Count][0] = Word1.toLowerCase();
			Suggestions[Count][1] = '1';
		}
	});
	var PossibleSuggestions = new Array();
	$.each(Suggestions,function(Number,WordArray) {
		if (WordArray[1] >= 3) { //Change this number if the word needs to exists more or less times to be suggested
			var Count = PossibleSuggestions.length;
			PossibleSuggestions[Count] = WordArray;
		}
	});
	//So close. Now we have all of the words that we want to suggest to them, so we need to make sure that they aren't already in their box of keywords
	var KeywordArray = CurrentKeywords.split(",");
	var Counter = 0;
	//alert(KeywordArray);
	$.each(PossibleSuggestions,function(Number,WordArray) {
		WordExists = false;
		$.each(KeywordArray,function(Number,Word) {
			if (WordArray[0] == Word) {
				WordExists = true;
				return false;
			}
		});
		if (!WordExists) {
			Counter = Counter + 1;
			if (CurrentKeywords != "" && CurrentKeywords != 'undefined') {
				CurrentKeywords = CurrentKeywords + ","+WordArray[0];
			} else {
				CurrentKeywords = WordArray[0];
			}
		}
	});
	if (Counter > 0) {
		if (Counter == 1) {
			$('#'+Class+'KeywordButtonResults').html('<span style="color: green">1 Keyword Suggestion Found</span>');
		} else {
			$('#'+Class+'KeywordButtonResults').html('<span style="color: green">'+Counter+' Keyword Suggestions Found</span>');
		}
	} else {
		$('#'+Class+'KeywordButtonResults').html('<span style="color: red">No Keyword Suggestions Found</span>');
	}
	$('#'+Class+FieldID).val(CurrentKeywords); 
	//console.log(CurrentKeywords);
}

function isNumberKey(evt) {
  	var charCode = (evt.which) ? evt.which : event.keyCode;
      //if (charCode > 31 && (charCode < 48 || charCode > 57)) {
	  //alert(charCode);
	if (charCode == 8 
		|| charCode == 9
		|| charCode == 46
		|| charCode == 48
		|| charCode == 49
		|| charCode == 50
		|| charCode == 51
		|| charCode == 52
		|| charCode == 53
		|| charCode == 54
		|| charCode == 55
		|| charCode == 56
		|| charCode == 57) {
        return true;
	} else {
        return false;
	}
}

function ShowHideLinks(div,image,url) {
	ActualDiv = document.getElementById(div);
	ActualImage = document.getElementById(image);
	if (ActualDiv.style.display == 'block') {
		ActualDiv.style.display = 'none';
		ActualImage.src = url+'Images/ar_right.gif';
	} else {
		ActualDiv.style.display = 'block';
		ActualImage.src = url+'Images/ar_down.gif';
	}
}
			
function CheckRequiredFields(which, errorname) {
	var pass=true;
	var i;
	if (document.images) {
		for (i=0;i<which.length;i++) {
			var tempobj=which.elements[i];
			if (tempobj.name.substring(0,8)=='required') {
				if (((tempobj.type=='text'||tempobj.type=='textarea'||tempobj.type=="password")&& tempobj.value=='')||(tempobj.type.toString().charAt(0)=='s'&& tempobj.selectedIndex==0)) {
					pass=false; 
					break;
				}
			}
		}
	}
	//alert(pass);
	if (!pass) {
		shortFieldName=tempobj.name.substring(8,30).toUpperCase();
		document.getElementById(errorname).innerHTML = 'Please make sure the '+shortFieldName+' field was properly completed.';
	} else {
		document.getElementById(errorname).innerHTML = '';
	}
	return pass;

}

function CheckEmailField(field) {
	value = document.getElementById(field).value;
	apos=value.indexOf("@");
	dotpos=value.lastIndexOf(".");
	if (apos<1||dotpos-apos<2){
		document.getElementById('SubmitError').innerHTML = 'The email address you entered is not valid!';
		return false;
	} else {
		document.getElementById('SubmitError').innerHTML = '';
		return true;
	}
}

function CheckPhoneField(field) {
	value = document.getElementById(field).value;
	var stripped = value.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	var error='';
	if (isNaN(parseInt(stripped))) {
   		error = "The phone number contains illegal characters.";
	}
	if (stripped.length == 10) {
	} else {
		error = "The phone number you entered is not valid";
	}
	if (error.length > 0 ) {
		document.getElementById('SubmitError').innerHTML = error;
		return false;
	} else {
		document.getElementById('SubmitError').innerHTML = '';
		return true;
	}
	return false;
}

function CheckFieldsMatch(field1, field2) {
	val1 = document.getElementById(field1).value;
	val2 = document.getElementById(field2).value;
	if (val1 == val2) {
		document.getElementById('SubmitError').innerHTML = '';
		return true;
	} else {
		if (field1.substring(0,8)=='required') {
			shortFieldName1=field1.substring(8,30).toUpperCase();
		} else {
			shortFieldName1=field1.toUpperCase();
		}
		if (field2.substring(0,8)=='required') {
			shortFieldName2=field2.substring(8,30).toUpperCase();
		} else {
			shortFieldName2=field2.toUpperCase();
		}
		document.getElementById('SubmitError').innerHTML = shortFieldName1+' and '+shortFieldName2+' do not match.';
		return false;
	}
}



function AreYouSure(Type, Name, Forward) {
	if (confirm("Clicking OK will delete " + Type + " '" + Name + "' permanently.  Are you sure you want to proceed?")) {
		window.location= Forward;
	}
}

function AreYouSurePost(Type, Name) {
	return confirm("Clicking OK will delete " + Type + " '" + Name + "' permanently.  Are you sure you want to proceed?");
}


