var state_ID = null;

function loadSubListingGroup(category_ID){
	url = '/ajax/json/Listing_Group_View/Search?parent_listing_group_ID=' + category_ID;
	ajaxFeed(url,processLoadSubListingGroup);
}

function processLoadSubListingGroup(data){
	var sub_listing_group_ID = document.getElementById('sub_listing_group_ID');
	clearDrowDown(sub_listing_group_ID);
	if (data.results.Success == true){
		

		var option = new Option('Select a Sub-Category','',false,false);
		sub_listing_group_ID.options[0]=option;
		
		for (var i=0; i<data.results.DataCount;i++){
			option = new Option(data.data[i].listing_group,data.data[i].listing_group_ID,false,false);
			var select = i+1;
			sub_listing_group_ID.options[select]=option;
		}
	}
}

function clearDrowDown(dropDown){

	var length = dropDown.length;
	
	while(dropDown.length != 0)
		dropDown.options[0] = null;
	return true;
}

function updateState(country){
	url = '/ajax/json/State/Search?country_ID=' + country + '&orderBy[]=state,asc';
	ajaxFeed(url,processUpdateState);
}

function processUpdateState(data){
	var state = document.getElementById('state_ID');
	clearDrowDown(state);
	if (data.results.Success == true){
		if (data.results.DataCount == 0){
			state.disabled = true;
			return true;
		}

		var option = new Option('Select','',false,false);
		state.options[0]=option;
		
		for (var i=0; i<data.results.DataCount;i++){
			option = new Option(data.data[i].state,data.data[i].state_ID,false,false);
			var select = i+1;
			state.options[select]=option;
		}
	}

	if (state_ID != null){
		document.getElementById('state_ID').value = state_ID;
	}
}

function loadContactInformation(){
	var toggle = document.getElementById('sameAsRegistration').checked;
	
	if (toggle == true){
		url = '/Listing/loadContactInfo';
		ajaxFeed(url,procssLoadContactInformation);
	} else{
		document.getElementById('address').value = '';
		document.getElementById('city').value = '';
		document.getElementById('postal_code').value = '';
		document.getElementById('contact_name').value = '';
		document.getElementById('contact_number').value = '';
		document.getElementById('country_ID').value = '';
		document.getElementById('state_ID').value = '';
	}
}

function procssLoadContactInformation(data){
	if (data.results.Success == true){
		document.getElementById('address').value = data.data.address;
		document.getElementById('city').value = data.data.city;
		document.getElementById('postal_code').value = data.data.postal_code;
		document.getElementById('contact_name').value = data.data.name;
		document.getElementById('contact_number').value = data.data.contact_number;
		document.getElementById('country_ID').value = data.data.country_ID;
		updateState(data.data.country_ID);
		state_ID = data.data.state_ID;
	}
}

function toggleSameAsRegistration(){
	toggle = document.getElementById('sameAsRegistration');
	
	if (toggle.checked == true)
		toggle.checked = false;
	else
		toggle.checked = true;
}


function checkNewAdForm(){
	var errors = '';

	
	// Advertisement Information
	if (document.getElementById('listing_group_ID').value == ''){
		errors = errors + '    -Category is Required\n';
	}
	
	if (document.getElementById('sub_listing_group_ID').value == ''){
		errors = errors + '    -Sub-Category is Required\n';
	}
	if (document.getElementById('subject').value == ''){
		errors = errors + '    -Advertisement Tittle/Subject is Required\n';
	}
	if (document.getElementById('content').value == ''){
		errors = errors + '    -Description is Required\n';
	}
	if (document.getElementById('price').value == ''){
		errors = errors + '    -Asking Price/Min Price is Required\n';
	}
	
	if (document.getElementById('max_price').value != ''){
		if (document.getElementById('price').value > document.getElementById('max_price').value)
			errors = errors + '    -Max Price must be greater than Asking Price/Min Price\n';
	}
	
	
	// pictures are not required.
	// advertisement content section
	
	if (document.getElementById('address').value == ''){
		errors = errors + '    -Address is Required\n';
	}
	if (document.getElementById('country_ID').value == ''){
		errors = errors + '    -Country is Required\n';
	}
	if (document.getElementById('state_ID').value == ''){
		errors = errors + '    -State is Required\n';
	}
	if (document.getElementById('city').value == ''){
		errors = errors + '    -City is Required\n';
	}
	if (document.getElementById('postal_code').value == ''){
		errors = errors + '    -Postal Code is Required\n';
	}
	if (document.getElementById('contact_name').value == ''){
		errors = errors + '    -Contact Name is Required\n';
	}
	if (document.getElementById('contact_number').value == ''){
		errors = errors + '    -Contact_number is Required\n';
	}

	if (!checkFileUpload(document.getElementById('picture1').value)){
		errors = errors + '    -Main picture must be .jpg, .gif, or .png\n';
	}
	
	if (!checkFileUpload(document.getElementById('picture2').value)){
		errors = errors + '    -Extra picture 1 must be .jpg, .gif, or .png\n';
	}
	
	if (!checkFileUpload(document.getElementById('picture3').value)){
		errors = errors + '    -Extra picture 2 must be .jpg, .gif, or .png\n';
	}
	
	if (!checkFileUpload(document.getElementById('picture4').value)){
		errors = errors + '    -Extra picture 3 must be .jpg, .gif, or .png\n';
	}
	
	if (!checkFileUpload(document.getElementById('picture5').value)){
		errors = errors + '    -Extra picture 4 must be .jpg, .gif, or .png\n';
	}
	
	if (!checkFileUpload(document.getElementById('picture6').value)){
		errors = errors + '    -Extra picture 5 must be .jpg, .gif, or .png\n';
	}
	
	if (errors != ''){
		errors = 'Please correct the folloween error(s)\n' + errors;
		alert(errors);
		return false;
	}
	
	document.getElementById('submit').style.disabled = true;
	document.getElementById('reset').style.disabled = true;
	//alert('Please note that this may take several minutes depending on now big the pictures are that you are uploading. Do not press the refresh or reload button while uploading. Thanks.');
	return true;
}


function checkFileUpload(value){
	value = value.toLowerCase();
	if (value == '')
		return true;

	if (value.match('.jpg'))
		return true;
	if (value.match('.gif'))
		return true;
	if (value.match('.png'))
		return true;
	return false;
}


function switchToEdit(id){
	document.getElementById('pic' + id + 'Image').style.display='none';
	document.getElementById('pic' + id).style.display='inline';
}