// JavaScript Document
$(document).ready(function() {
	blurLinks();
	
	$('a.lightbox').lightBox();
	
	$('.searchBtn, .loginBtn, .sendBtn, .registerBtn, .postPropertyBtn, .continueBtn').hover(
	function () {
		$(this).addClass('overState');
	}, 
	function () {
		$(this).removeClass('overState');
	}
	);
	
	$('#customNav.profile span').hover(
      function () {
        $(this).addClass('overState');
      }, 
      function () {
        $(this).removeClass('overState');
      }
    );
	
	$('#registerForm input#company').click(function (){
		$('#registerForm fieldset.companyDetails').css('display', 'block');
		$('#registerForm fieldset.companyDetails input').addClass('required');
	});
	
	$('#registerForm input#private').click(function (){
		$('#registerForm fieldset.companyDetails').css('display', 'none');
		$('#registerForm fieldset.companyDetails input').removeClass('required');
	});
	
	$('#registerForm input#company:checked').each(function (){
		$('#registerForm fieldset.companyDetails').css('display', 'block');
		$('#registerForm fieldset.companyDetails input').addClass('required');
	});
});



//empty input boxes
function emptyFormFields(el,val) {
if (el.value == val ) {
el.value = '';
} 
}

function fillFormFields(el,val) {
if (el.value == '' ) {
el.value = val;
}
}

function changeToTypePass(el, val){
emptyFormFields(el,val);
el.type = 'password';
}
function changeToTypeText(el, val){
if (el.value == ''){
fillFormFields(el,val);
el.type = 'text';
}
}
//\empty input boxes


function changeInputType(oldObject, oType, val) {
  var newObject = document.createElement('input');
  newObject.type = oType;
  if(oldObject.size) newObject.size = oldObject.size;
  if(oldObject.value) newObject.value = val;
  if(oldObject.name) newObject.name = oldObject.name;
  if(oldObject.id) newObject.id = oldObject.id;
  if(oldObject.className) newObject.className = oldObject.className;
  oldObject.className = 'hidden';
  oldObject.parentNode.replaceChild(newObject,oldObject);
  setTimeout(function(){newObject.focus();},10);
  return newObject;
}


function blurLinks() {
	links = document.getElementsByTagName("a");
	for(i=0; i<links.length; i++) {
		links[i].onfocus = unblur;
	}
	inputs = document.getElementsByTagName("input");
	for(i=0; i<inputs.length; i++) {
		if (inputs[i].type == 'button' || inputs[i].type == 'submit') {
			inputs[i].onfocus = unblur;
		}
	}
}

function unblur() {
	this.blur();
}




function resetForm() {
	$('#cartInformation textarea').val('');
	$('#cartInformation input:not(#clear, #submit)').val('');
	$('#company').attr("checked", "checked"); 
	return false;
}

function popUpPrint(URL) {
	window.open( URL, "myWindow", "status = 1, height = 600, width = 960, toolbar=no, scrollbars=yes, menubar=no, resizable = yes" )	
}

function checkInt(evt){
	var charCode = evt.which
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
}


//select box manipulation
function selectOption(val, el) {
	$('#'+el).selectOptions(val,true);
}



//Countries, Regions, Cities, Areas
function showRegions(countryId, selected, region_id, city_id){
	if (countryId >0) {
		$('#'+region_id).removeOption(/./);
	
		var url = rootUrl + 'Ajax_0_' + countryId + '_' + langId + '.html';
		args = new Array('' + selected + '', region_id);
		$('#'+region_id).addOption("", vars['pleaseChoose']);
		$('#'+region_id).ajaxAddOption(url, '', false, selectOption, args);	
	}else{
		$('#'+region_id).removeOption(/./);
		$('#'+region_id).addOption("", vars['pleaseChoose']);
	}
	
	$('#'+city_id).removeOption(/./);
	$('#'+city_id).addOption("", vars['pleaseChoose']);

}

function showCities(regionId, selected, city_id){
	if (regionId >0) {
		$('#'+city_id).removeOption(/./);
				
		var url = 'Ajax_1_' + regionId + '_' + langId + '.html';
		args = new Array('' + selected + '', city_id);
		$('#'+city_id).addOption("", vars['pleaseChoose']);
		$('#'+city_id).ajaxAddOption(url, '', false, selectOption, args);
		
	}else{
		$('#'+city_id).removeOption(/./);
		$('#'+city_id).addOption("", vars['pleaseChoose']);
	}
}

function showCitiesNew(regionId, selected, city_id){
	if (regionId >0) {	
		var url = 'Ajax_4_' + regionId + '_' + langId + '_' + selected + '.html';

		$.get(url,
		  function(data){ 
			$('#' + city_id).html(data);
		  });
	}else{
		$('#'+city_id).removeOption(/./);
		$('#'+city_id).addOption("", vars['pleaseChoose']);
	}
}

function addMore(picid){
	var id = document.getElementById(picid).value; 
	
	if(id<10){
		num = (id - 1) + 2;
		$("#photoContainer").append("<label>" + vars['image'] + " " + num +"</label><input id='fileToUpload"+id+"' type='file' name='photo["+id+"]' class='file' /><span class='spacer'></span>");
		id = (id - 1) + 2;
		document.getElementById(picid).value = id;  
	}
}


//JQuery Simple Modal
$(document).ready(function () {
	$('a.contactUserLink').click(function (e) {
		e.preventDefault();
		$('#test').modal({
			onOpen: modalOpen, 
			onClose: modalClose, 
			onShow: modalShow
		});
	});
});

/**
 * When the open event is called, this function will be used to 'open'
 * the overlay, container and data portions of the modal dialog.
 *
 * onOpen callbacks need to handle 'opening' the overlay, container
 * and data.
 */
function modalOpen (dialog) {
	dialog.overlay.fadeIn('slow', function () {
		dialog.container.fadeIn('slow', function () {
			dialog.data.slideDown('slow');
		});
	});
}

/**
 * When the close event is called, this function will be used to 'close'
 * the overlay, container and data portions of the modal dialog.
 *
 * The SimpleModal close function will still perform some actions that
 * don't need to be handled here.
 *
 * onClose callbacks need to handle 'closing' the overlay, container
 * data and iframe.
 */
function modalClose (dialog) {
	dialog.data.fadeOut('slow', function () {
		dialog.container.hide('slow', function () {
			dialog.overlay.slideUp('slow', function () {
				$.modal.close();
			});
		});
	});
}

/**
 * After the dialog is show, this callback will bind some effects
 * to the data when the 'button' button is clicked.
 *
 * This callback is completely user based; SimpleModal does not have
 * a matching function.
 */
function modalShow (dialog) {
	dialog.data.find('input.animate').one('click', function () {
		dialog.data.slideUp('slow', function () {
			dialog.data.slideDown('slow');
		});
	});
}
//~JQuery Simple Modal
