var CurrentProvinceState = '';
var CurrentCountry = '';
var CurrentProvinceState = '';
var SuggestInputItemSelected = 0;
var SuggestionListArray = Array();
var SuggestionKeyRealses = true;
var SuggestionTimeDelay;
var AlertMessage;

function SelectionProvince(StateValue) {
	CurrentProvinceState = StateValue;
}

function InsertSelection (ObjectID,ArrayName,EmptyLine) {
	CurrentCountry = EmptyLine;
	if (CurrentCountry == 'Canada') {
		document.getElementById(ObjectID).options[0]=new Option('Province', '', false, false);
	} else {
		document.getElementById(ObjectID).options[0]=new Option('Select State', '', false, false);
	}

	CurrentProvinceState = '';
	for (i=1; i< ArrayName.length; i++) {
		document.getElementById(ObjectID).options[i]=new Option(ArrayName[i-1], ArrayName[i-1], false, false);
	}

	if (0 == ArrayName.length) {
		document.getElementById(ObjectID).options[0]=new Option(EmptyLine, EmptyLine, false, false);
	}
}

function SuggestInput(ObjectID,ListName,ListArray,evt) {
	if (SuggestionKeyRealses && CurrentProvinceState != '') {
		// SAFARI 2.0 FIX
		SuggestionKeyRealses = false;
		ObjectID.onkeydown = function () { SuggestionKeyRealses = true };

		// GET VERIABLES
		var InputText = ObjectID.value.toLowerCase();
		var iKeyCode = evt.keyCode? evt.keyCode : evt.charCode;

		// CHECK IF CHARACTER BUTTON

		var ShowSuggestions = true;
		if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
			ShowSuggestions = false;
		}

		// CHECK IF DOWN AND UP
		var SuggestionsScroll = false;
		if (iKeyCode == 38 || iKeyCode == 40) {
			SuggestionsScroll = true;
		} else {
			SuggestInputItemSelected = 0;
		}

		// DISPLAY LIST
		if (InputText.length > 0 && !SuggestionsScroll) {
			ObjectID.onblur = function () { InsertSuggestionHide(ListName,ObjectID); };
			document.getElementById(ListName).style.display = "block";
			document.getElementById(ListName).style.width = ObjectID.offsetWidth-2+"px";
			document.getElementById(ListName).innerHTML = '';

			var OneSuggestion = false;
			var FirstSuggestion = '';
			var LastSuggestion = '';
			var SuggestionCount = 0;

			SuggestInputItemSelected = 0;
			SuggestionListArray = Array();

			var ListLengthCount = 0;
			var ListLengthCountMax = 20;

			for (var i = 0; i < ListArray.length; i++) {
				var TempName = ListArray[i].toLowerCase();
				var Patter = '/^'+InputText+'/';
				var SearchPatter = eval(Patter);			
				if (TempName.match(SearchPatter)) {
					if (ListLengthCount < ListLengthCountMax) {
						document.getElementById(ListName).innerHTML +=  "<div class=\"Item\" id=\""+ListName+"_"+SuggestionCount+"\" onmouseover=\"document.getElementById('"+ListName+"_'+SuggestInputItemSelected).className = 'Item';\" onclick=\"InsertSuggestion('"+ListArray[i]+"','"+ObjectID.id+"','"+ListName+"')\">"+ListArray[i]+"</div>";
						SuggestionListArray[SuggestionCount] = ListArray[i];
						SuggestionCount++;
						if (!OneSuggestion) {
							FirstSuggestion = ListArray[i];			
						}
						OneSuggestion = true;
						LastSuggestion = TempName;			
					}
					ListLengthCount++;
				}
			}	
			if (!OneSuggestion || LastSuggestion == InputText) {
				document.getElementById(ListName).style.display = "none";
				ObjectID.onblur = null;
			} else if (ShowSuggestions) {			
				if(ObjectID.setSelectionRange) { 
					ObjectID.value = FirstSuggestion;			
					ObjectID.setSelectionRange(InputText.length,FirstSuggestion.length);
				} else if(ObjectID.createTextRange) {			
					ObjectID.value = FirstSuggestion;	
					var oRange = ObjectID.createTextRange(); 
					oRange.moveStart("character", InputText.length); 
					oRange.moveEnd("character", FirstSuggestion.length); 
					oRange.select();		
				} 
			}		
		} else if (SuggestionsScroll && document.getElementById(ListName).style.display == 'block') {
			document.getElementById(ListName+'_'+SuggestInputItemSelected).className = "Item";	
			if (iKeyCode == 40) { // DOWN
				if (SuggestInputItemSelected < (SuggestionListArray.length-1)) {
					SuggestInputItemSelected++;
				} else {
					SuggestInputItemSelected = 0;
				}
			}
			if (iKeyCode == 38) { // UP
				if (SuggestInputItemSelected > 0) {
					SuggestInputItemSelected--;
				} else {
					SuggestInputItemSelected = (SuggestionListArray.length-1);
				}
			}
			ObjectID.value = SuggestionListArray[SuggestInputItemSelected];
			document.getElementById(ListName+'_'+SuggestInputItemSelected).className = "ItemSelected";	
		} else {
			document.getElementById(ListName).style.display = "none";
			ObjectID.onblur = null;
		}
	}
}

function InsertSuggestion(InsertValue,ObjectIDName,ListName) {
	document.getElementById(ObjectIDName).value = InsertValue;
	document.getElementById(ListName).style.display = "none";
	document.getElementById(ObjectIDName).onblur = null;
	
	if (document.getElementById(ObjectIDName).setSelectionRange) { 			
		document.getElementById(ObjectIDName).setSelectionRange(0,0);
	} else if(document.getElementById(ObjectIDName).createTextRange) {	
		var oRange = document.getElementById(ObjectIDName).createTextRange(); 
		oRange.moveStart("character", document.getElementById(ObjectIDName).value.length); 
		oRange.moveEnd("character", document.getElementById(ObjectIDName).value.length); 
		oRange.select();		
	} 
}

function InsertSuggestionHide(ListName,ObjectID) {
	SuggestionTimeDelay = setTimeout('InsertSuggestionHideCompleted("'+ListName+'","'+ObjectID.id+'")', 150);
}

function InsertSuggestionHideCompleted(ListName,ObjectIDName) {
	clearTimeout(SuggestionTimeDelay);
	document.getElementById(ListName).style.display = "none";
	if (document.getElementById(ObjectIDName).setSelectionRange) { 			
		document.getElementById(ObjectIDName).setSelectionRange(0,0);
	} else if (document.getElementById(ObjectIDName).createTextRange) {	
		var oRange = document.getElementById(ObjectIDName).createTextRange(); 
		oRange.moveStart("character", document.getElementById(ObjectIDName).value.length); 
		oRange.moveEnd("character", document.getElementById(ObjectIDName).value.length); 
		oRange.select();		
	} 
}

function checkform() {
	valid = true;
	AlertMessage = '';
	valid = RequiredSeacherFiled();

	if (!valid) {
		alert(AlertMessage);
	}
	return valid;
}

function RequiredSeacherFiled () {
	for (i=0; i< RequiredFiled.length; i++) {
		if (RequiredFiled[i].match(/email/gi)) {
			var checkEmail = document.getElementById(RequiredFiled[i]).value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);

			if (checkEmail) {
			} else {
				AlertMessage += RequiredFiledMessage[i]+'\n\n';
				valid = false;
			}
		} else if (RequiredFiled[i].match(/checkbox/gi)) {
			var check = RequiredFiled[i].split("|");
			var empty = true;
			valid = false;
			for (j=0; j<parseInt(check[1]); j++) {
				var che = document.getElementById(check[0]+"_"+j);
				if (che.checked == true)
					empty = false;
			}
			if (empty) {
				AlertMessage += RequiredFiledMessage[i]+'\n\n';
				valid = false;
			}
		} else if (RequiredFiled[i] == "promote") {
			var promote = document.getElementById('promote');
			var duration = document.getElementById('promotionDuration').value;
			if (promote.checked == true && (duration == "n/a" || duration == "")) { 
				AlertMessage += RequiredFiledMessage[i]+'\n\n';
				valid = false;
			}
		} else {
			if (document.getElementById(RequiredFiled[i]).value == '' || document.getElementById(RequiredFiled[i]).value == 'REQUIRED' || document.getElementById(RequiredFiled[i]).value == 'n/a') {
				AlertMessage += RequiredFiledMessage[i]+'\n\n';
				valid = false;
			}
		}
	}
	return valid;
}	

function updateCost(value) {
	var newVal;
	if (value == "7")
		newVal = "3.99";
	else if (value == "3")
		newVal = "1.99";
	else
		newVal = "0.00";
	document.getElementById('cost').innerHTML = newVal;
}

function updateCostRental(value) {
	var newVal;
	if (value == "7")
		newVal = "4.99";
	else if (value == "30")
		newVal = "14.99";
	else
		newVal = "0.00";
	document.getElementById('cost').innerHTML = newVal;
}

function viewpopup(loadpage,PageWidth,PageHeight) {
	var intWidth = PageWidth;
	var intHeight = PageHeight;
	var intLeft = (screen.availWidth / 2) - (intWidth / 2 );
	var intTop = (screen.availHeight / 2) - (intHeight / 2 );	
	OpenWin = this.open(loadpage, "Load", 'toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,width='+PageWidth+',height='+PageHeight+', screenX=' + intLeft + ',screenY=' + intTop + ',left=' + intLeft + ',top= ' + intTop + ''); 
	OpenWin.focus();
}

function loadPhotos() {
	$(".content").jCarouselLite({
		btnNext: ".itemNext",
		btnPrev: ".itemPrev",
		btnGo: ["#section1", "#section2", "#section3"],
		visible: 1
	});
}