/*
Copyright (c) 2008-2009, NjNyIndia.com. All rights reserved.
*/

	var subCatKeys = new Array(9);
	subCatKeys[0] = new Array(1,2,3,4,5,6,7,8,9,10);
	subCatKeys[1] = new Array(11,12);
	subCatKeys[2] = new Array(21,22);
	subCatKeys[3] = new Array(31,32,33,34,35);
	subCatKeys[4] = new Array(41,42,43);
	subCatKeys[5] = new Array(51,52,53,54);
	subCatKeys[6] = new Array(61,62,63,64,65);
	subCatKeys[7] = new Array(71,72,73);
	subCatKeys[8] = new Array('81');
	
	var subCatDescs = new Array(9);
	subCatDescs[0] = new Array("Tax Related", "Beauty Services", "Food Services", "Want Help", "Help Available", "Music", "Phone Cards", "Want a Tutor", "Tutor Available", "Other");
	subCatDescs[1] = new Array("Want to buy a Car", "Want to sell a Car");
	subCatDescs[2] = new Array("Want a Nanny", "Available Nanny");
	subCatDescs[3] = new Array("Desktops", "Laptops", "Internet", "Software", "Hardware");
	subCatDescs[4] = new Array("Home Appliances", "Audio", "Visual");
	subCatDescs[5] = new Array("Furniture Items", "Garage Sale", "Moving Sale", "Used Toys");
	subCatDescs[6] = new Array("Condominiums", "TownHouses", "Commercial Property", "Single Family Houses", "Apartment For Rent");
	subCatDescs[7] = new Array("Female/Male", "Female Only", "Male Only");
	subCatDescs[8] = new Array("Lease Transfer");


	function reloadSubCategory() {
	
	    // First clear the list
	    document.advForm.subcategory.length = 0;
	
		var sortedCategories = new Array();
	
	    var divLen = document.advForm.category.length;
	    if(divLen > 0) 
		{
			// else loop through the array for each selected category
			var indx = 0;
	        for(var i = 0; i < divLen; i++) {
				if(document.advForm.category.options[i].selected == true) {
					var keysArray = subCatKeys[i];
					var valueArray = subCatDescs[i];
					if(keysArray != null) {
						for(var x = 0; x < keysArray.length; x++) {
							sortedCategories[indx++] = new Array(keysArray[x], valueArray[x]);
						}
					}
				}
			}
		}
	
		var cntr = 0;
		for(var j = 0; j < sortedCategories.length; j++) {
			document.advForm.subcategory.options[cntr++] = new Option(sortedCategories[j][1], sortedCategories[j][0]);
		}
	}
	
	/**
	* This method is used to validate all the elemets in the Advertisements Form.
	* @method eventFormValidator
	*/
	function advFormValidator() {
	
		if(document.advForm.advtitle.value.length <= 0) {
			alert("Please enter the Title of your Advertisement");
			document.advForm.advtitle.focus();
			return false;
		}
	
		if(document.advForm.contact.value.length <= 0) {
			alert("Please enter your Name");
			document.advForm.contact.focus();
			return false;
		}
	
		// Validating Amount Field ...
		if(document.advForm.amount.value.length > 0) {
			if(!validateDecimal(document.advForm.amount.value)) {
				alert("Please enter a valid Amount.");
				document.advForm.amount.focus();
				return false;
			}
		}
	
		// Validating Email Field ...
		if(document.advForm.email.value.length > 0) {
			if(-1 == document.advForm.email.value.indexOf("@")) {
				document.advForm.email.focus();
				alert("Email must have a '@'.");
				return false;
			}
			if(-1 != document.advForm.email.value.indexOf(",")) {
				document.advForm.email.focus();
				alert("Email must not have a ',' in it.");
				return false;
			}
			if(-1 != document.advForm.email.value.indexOf("#")) {
				document.advForm.email.focus();
				alert("Email must not have an '#' in it.");
				return false;
			}
			if(-1 != document.advForm.email.value.indexOf("!")) {
				document.advForm.email.focus();
				alert("Email must not have a '!' in it.");
				return false;
			}
			if(-1 != document.advForm.email.value.indexOf(" ")) {
				document.advForm.email.focus();
				alert('Email must not have a space in it.');
				return false;
			}
			if(document.advForm.email.value.length == (document.advForm.email.value.indexOf("@")+1) ) {
			   document.advForm.email.focus();
			   alert("Email must have a domain name after the '@'.");
			   return false;
			}
		}
		// Validating Passcode and Confirm Passcode.
		if(document.advForm.passcode.value.length <= 0) {
			   document.advForm.passcode.focus();
			   alert("Passcode is required.");
			   return false;
		}
		if(document.advForm.c_passcode.value.length <= 0) {
			   document.advForm.c_passcode.focus();
			   alert("Confirm Passcode is required.");
			   return false;
		}
		if(document.advForm.c_passcode.value != document.advForm.passcode.value) {
			   document.advForm.c_passcode.focus();
			   alert("Confirm Passcode is incorrect.");
			   return false;
		}
		var oEditor = FCKeditorAPI.GetInstance('description');
		document.getElementsByName('description')[0].value = oEditor.GetXHTML(true);
	
	}

	/**
	* This method is used to load the FCKEditor for Descriptions.
	* @method loadEditor
	*/
	function loadEditor() {
		var oFCKeditor = new FCKeditor( 'description' ) ;
			oFCKeditor.BasePath = 'fckeditor/'
			oFCKeditor.ToolbarSet = 'Basic' ;
			oFCKeditor.ReplaceTextarea() ;
	}

	/**
	* This method is used to validate all the elemets in the Events Form.
	* @method eventFormValidator
	*/
	function eventFormValidator() {
	
		if(document.eventForm.eventTitle.value.length <= 0) {
			alert("Please enter the Title of your Event");
			document.eventForm.eventTitle.focus();
			return false;
		}
	
		if(document.eventForm.eventDate.value.length <= 0) {
			alert("Please enter the Date of your Event");
			document.eventForm.eventDate.focus();
			return false;
		}
	
		// Validating Email Field ...
		if(document.eventForm.email.value.length > 0) {
			if(-1 == document.eventForm.email.value.indexOf("@")) {
				document.eventForm.email.focus();
				alert("Email must have a '@'.");
				return false;
			}
			if(-1 != document.eventForm.email.value.indexOf(",")) {
				document.eventForm.email.focus();
				alert("Email must not have a ',' in it.");
				return false;
			}
			if(-1 != document.eventForm.email.value.indexOf("#")) {
				document.eventForm.email.focus();
				alert("Email must not have an '#' in it.");
				return false;
			}
			if(-1 != document.eventForm.email.value.indexOf("!")) {
				document.eventForm.email.focus();
				alert("Email must not have a '!' in it.");
				return false;
			}
			if(-1 != document.eventForm.email.value.indexOf(" ")) {
				document.eventForm.email.focus();
				alert('Email must not have a space in it.');
				return false;
			}
			if(document.eventForm.email.value.length == (document.eventForm.email.value.indexOf("@")+1) ) {
			   document.eventForm.email.focus();
			   alert("Email must have a domain name after the '@'.");
			   return false;
			}
		}
		
		var oEditor = FCKeditorAPI.GetInstance('description');
		document.getElementsByName('description')[0].value = oEditor.GetXHTML(true);
	}

	/**
	* This method is used to validate, if the passed fieldValue is Decimal or not.
	* @method validateDecimal
	*/
	function validateDecimal(fieldValue)
	{		
		if(trim(fieldValue) == "") {
			return false; 
		}	
	
		var valid = "0123456789";
		var foundPoint = false;
		var mantissa = 0;
		
		for(var i = 0; i < fieldValue.length; i++) {
			var temp = "" + fieldValue.substring(i, i + 1);
			
			if(temp == "." && foundPoint == false) {	
				foundPoint = true; // first decimal place, set flag and continue
			} else if(valid.indexOf(temp) == -1) {	
				return false; 
			} else if(foundPoint == true) {
				// legal character in decimal place.
				mantissa++;
				if(mantissa > 2) {
					return false;
				}
			}
		}
		return true; 	
	}

	/**
	* This method is used to left trim the String.
	* @method ltrim
	*/
	function ltrim(n) {
		var n1 = n.search(/\S/);
		if(n1 >-1 )
			return n.substr(n1);
		return "";
	}
	
	/**
	* This method is used to trim the String.
	* @method trim
	*/
	function trim(n) {
		var tmp = ltrim(n); 
		var n2 = tmp.search(/\S(\s)*$/); 
		tmp = tmp.substr(0,n2+1);
		return tmp
	}


	function clickclear(thisfield, defaulttext) {
		if (thisfield.value == defaulttext) {
			thisfield.value = "";
		}
	}
	
	function clickrecall(thisfield, defaulttext) {
		if (thisfield.value == "") {
			thisfield.value = defaulttext;
		}
	}

	/**
	* Sets the selected date to element eventDate.
	* @method handleStartDateSelect
	*/
	function handleStartDateSelect(type,args,obj) {
		var dates = args[0]; 
		var date = dates[0];
		var year = date[0], month = date[1], day = date[2];
		var txtDate1 = document.getElementById("eventDate"); 
		txtDate1.value = month + "/" + day + "/" + year;
		sdCalendar.hide();
	}
	
	/**
	* Sets the selected date to element eventDate.
	* @method handleEndDateSelect
	*/
	function handleEndDateSelect(type,args,obj) {
		var dates = args[0]; 
		var date = dates[0];
		var year = date[0], month = date[1], day = date[2];
		var txtDate1 = document.getElementById("eventEndDate"); 
		txtDate1.value = month + "/" + day + "/" + year;
		edCalendar.hide();
	}
	
	/**
	* Displays the Calendar for element eventDate.
	* @method showStartDate
	*/
	function showStartDate() {
		sdCalendar = new YAHOO.widget.Calendar("sdCal","startDateContainer", { title:"Choose Start Date:", close:true });
		sdCalendar.selectEvent.subscribe(handleStartDateSelect, sdCalendar, true);
		sdCalendar.render();
		YAHOO.util.Event.addListener("startDateLink", "click", sdCalendar.show, sdCalendar, true);
	}
	
	/**
	* Displays the Calendar for element eventDate.
	* @method showEndDate
	*/
	function showEndDate() {
		edCalendar = new YAHOO.widget.Calendar("edCal","endDateContainer", { title:"Choose End Date:", close:true });
		edCalendar.selectEvent.subscribe(handleEndDateSelect, edCalendar, true);
		edCalendar.render();
		YAHOO.util.Event.addListener("endDateLink", "click", edCalendar.show, edCalendar, true);
	}

