var selected_li_id = new Array();

function make_da_select(select, selectBoxId, isShort) {

	var select_id = select.attr('id');

	if(selectBoxId == undefined) {
		selectBoxId = 'selectBox_id';
	}

	if(isShort) {	
		var selectBoxContainer = $('<div>',{
			width		: select.outerWidth(),
			'class'		: 'da_select_short',
			'id'		: 'da_select_'+selectBoxId,
			html		: '<div class=selectBox_short id='+selectBoxId+'></div>'
		});
	} else {
		var selectBoxContainer = $('<div>',{
			width		: select.outerWidth(),
			'class'		: 'da_select',
			'id'		: 'da_select_'+selectBoxId,
			html		: '<div class=selectBox id='+selectBoxId+'></div>'
		});
	}

	var dropDown = $('<ul>',{'class':'dropDown'});
	dropDown.attr('id', select_id+'_dropdown'); 

	var selectBox = selectBoxContainer.find('#'+selectBoxId);

	populate_da_select_dropdown(select, selectBox, dropDown, isShort);
	
	selectBoxContainer.append(dropDown.hide());
	select.hide().after(selectBoxContainer);

	// Binding custom show and hide events on the dropDown:
	
	dropDown.bind('show',function(){
		
		if(dropDown.is(':animated')){
			return false;
		}
		
		var dropDown_id = dropDown.attr('id');
		da_select_li_highlight(dropDown_id, selected_li_id[select_id]);

		selectBox.addClass('expanded');
		dropDown.slideDown();
		
	}).bind('hide',function(){
		
		if(dropDown.is(':animated')){
			return false;
		}
		
		selectBox.removeClass('expanded');
		dropDown.slideUp();

		
	}).bind('toggle',function(){
		if(selectBox.hasClass('expanded')){
			dropDown.trigger('hide');
		}
		else dropDown.trigger('show');
	});
	
	selectBox.click(function(){
		dropDown.trigger('toggle');
		return false;
	});

	// If we click anywhere on the page, while the
	// dropdown is shown, it is going to be hidden:
	
	$(document).click(function(){
		dropDown.trigger('hide');
	});
}

function populate_da_select (select, selectBoxId, isShort) {

	if(selectBoxId == undefined) {
		selectBoxId = 'selectBox_id';
	}

	var selectBoxContainer = $('#da_select_'+selectBoxId);
	var selectBox = selectBoxContainer.find('#'+selectBoxId);
	var dropDown = selectBoxContainer.find('.dropDown');
	dropDown.html('');
	selectBox.html('');

	populate_da_select_dropdown(select, selectBox, dropDown, isShort);
	
}
	
function populate_da_select_dropdown(select, selectBox, dropDown, isShort) {

	var select_id = select.attr('id');
	var dropDown_id = dropDown.attr('id');

	// Looping though the options of the original select element
	select.find('option').each(function(i){
		var option = $(this);
		i = this.value;
		
		// Creating a dropdown item according to the
		// data-icon and data-html-text HTML5 attributes:

		var li_id = select_id+'_'+i; 		
		if(i==select.val()){
			if(isShort) {
				selectBox.html("<div style='height:18px;width:150px;overflow:hidden'>"+option.text()+"</div>");
			} else {
				selectBox.html("<div style='height:18px;width:300px;overflow:hidden'>"+option.text()+"</div>");
			}
			selected_li_id[select_id]= li_id;

			da_select_li_highlight(dropDown_id, li_id);
		}
		
		// As of jQuery 1.4.3 we can access HTML5 
		// data attributes with the data() method.
		
		if(option.data('skip')){
			return true;
		}
		
		var li = $('<li>',{
			html:	'<span>'+ option.text()+'</span>',
			id:     li_id	
		});
				
		li.click(function(){
			selected_li_id[select_id]= li_id;

			da_select_li_highlight(dropDown_id, li_id);
			
			if(isShort) {
				selectBox.html("<div style='height:18px;width:150px;overflow:hidden'>"+option.text()+"</div>");
			} else {
				selectBox.html("<div style='height:18px;width:300px;overflow:hidden'>"+option.text()+"</div>");
			}
			dropDown.trigger('hide');
			
			// When a click occurs, we are also reflecting
			// the change on the original select element:
			select.val(option.val()).change();
			
			return false;
		});

		li.mouseover(function(){
			da_select_li_highlight(dropDown_id, li_id);
		});

		dropDown.append(li);
	});

}
	

function da_select_li_highlight(dropDown_id, li_id) {

	$('#'+dropDown_id).find('li').each(function(){
		var li = $(this);
		id = li.attr('id');
		if(id==undefined) {
			return false;
		}

		if(id==li_id) {
			li.addClass('da_select_li_selected');
		} else {
			li.removeClass('da_select_li_selected');
		}
	});
}
	

function onmousedown_address_input(id) {
	var elem = document.getElementById(id);
	if(elem.value == "Enter Zip Code") {
		elem.value = "";
	}
}

function onmouseout_address_input(id) {
	var elem = document.getElementById(id);
	if(elem.value == "") {
		elem.value = "Enter Zip Code";
	}
}

function toggle_login_form() {
	var elem = document.getElementById('login_div');
	if(elem.style.display == "block") {
		elem.style.display = "none";
	} else {
		elem.style.display = "block";
	}

}

function onmousedown_dob_input(id) {
	var elem = document.getElementById(id);
	if(elem.value == "MM-DD-YYYY") {
		elem.value = "";
	}
}

function onmouseout_dob_input(dobId, answerDivId) {
	var elem = document.getElementById(dobId);
	var isValid;
	if(elem.value == "") {
		elem.value = "MM-DD-YYYY";
		isValid = 0;
	} else if(elem.value == "MM-DD-YYYY") {
		isValid = 0;
	} else {
		isValid = validate_dob(dobId)
	}

	update_right_wrong_div(answerDivId, isValid);
}

function validate_dob(dobId) {
	var dob = document.getElementById(dobId).value;

	var isPediatrician = 0;
	if(document.getElementById('isPediatrician')) {
		isPediatrician = document.getElementById('isPediatrician').value;
		isPediatrician = parseInt(isPediatrician);
	}

	if(isDate(dob)) {
		if(isAgeOK(dob, isPediatrician)) {
			return(1);
		} else {
			if(isPediatrician) {
				da_alert("Patient's age should be 13 or less to book an appointment of Pediatrician through DocAsap");
			} else {
				da_alert("Patient should be more than 13 years old to book an appointment through DocAsap");
			}
			return(0);
		}
	} else {
		return(0);
	}
}

function onmousedown_phone_input(phoneId) {
	var elem = document.getElementById(phoneId);
	if(elem.value == "555-5555555") {
		elem.value = "";
	}
}

function onmouseout_phone_input(phoneId, answerDivId, userid, validatedUser, userphone) {
	var elem = document.getElementById(phoneId);
	var isValid;
	if(elem.value == "") {
		elem.value = "555-5555555";
		isValid = 0;
	} else if(elem.value == "555-5555555") {
		isValid = 0;
	} else {
		isValid = validate_phone(phoneId)
	}

	if(userid != -1 && !validatedUser && stripCharsInBag(userphone, validWorldPhoneChars)==stripCharsInBag(elem.value, validWorldPhoneChars)) {
		$('#'+answerDivId).html('<span style=color:red>Account has not been validated</span>');
	} else {
		update_right_wrong_div(answerDivId, isValid);
	}
}


function validate_phone(phoneId) {
	var phoneStr = document.getElementById(phoneId).value;
	return(checkInternationalPhone(phoneStr));
}

function onmouseout_name_input(id, answerDivId) {
	var elem = document.getElementById(id);

	var value = elem.value;
	value = value.replace(/^\s+|\s+$/g, '') ;
	elem.value = value;

	var isValid = validateName(value);

	update_right_wrong_div(answerDivId, isValid);
}

function onmouseout_email_input(id, answerDivId) {
	var elem = document.getElementById(id);

	var value = elem.value;
	value = value.replace(/^\s+|\s+$/g, '') ;
	elem.value = value;

	var isValid = echeck(value);

	update_right_wrong_div(answerDivId, isValid);
}

function onmouseout_password_input(passwd_id, confirm_passwd_id, passwd_answerDivId, confirm_passwd_answerDivId) {

	var elem = document.getElementById(passwd_id);
	var passwd = elem.value;
	passwd = passwd.replace(/^\s+|\s+$/g, '') ;
	elem.value = passwd;

	var elem1 = document.getElementById(confirm_passwd_id);
	var c_passwd = elem1.value;
	c_passwd = c_passwd.replace(/^\s+|\s+$/g, '') ;
	elem1.value = c_passwd;

	var isValid = isUserPasswordGood(passwd, 1);
	update_right_wrong_div(passwd_answerDivId, isValid);

	if(!isValid) {
		update_right_wrong_div(confirm_passwd_answerDivId, isValid);
	} else {
		if(passwd != c_passwd) {
			isValid = 0;
		} else {
			isValid = 1;
		}
		update_right_wrong_div(confirm_passwd_answerDivId, isValid);
	}
}

function onmouseout_confirm_password_input(passwd_id, confirm_passwd_id, confirm_passwd_answerDivId) {
	var elem = document.getElementById(passwd_id);

	var passwd = elem.value;
	passwd = passwd.replace(/^\s+|\s+$/g, '') ;
	elem.value = passwd;

	elem = document.getElementById(confirm_passwd_id);

	var c_passwd = elem.value;
	c_passwd = c_passwd.replace(/^\s+|\s+$/g, '') ;
	elem.value = c_passwd;

	var isValid = isUserPasswordGood(passwd, 1);

	if(!isValid) {
		update_right_wrong_div(confirm_passwd_answerDivId, isValid);
	} else {
		if(passwd != c_passwd) {
			isValid = 0;
		} else {
			isValid = 1;
		}
		update_right_wrong_div(confirm_passwd_answerDivId, isValid);
	}
}

function give_input_purpose(answerDivId, purpose) {
	document.getElementById(answerDivId).className = 'answer_div font_style_green font_style_h3';
	$('#'+answerDivId).html(purpose);
	$('#'+answerDivId).addClass('answer_tool_tip_div');
}

function update_right_wrong_div(answerDivId, isValid) {
	$('#'+answerDivId).html('');
	if(isValid) {
		document.getElementById(answerDivId).className = 'answer_div right_answer_div';
	} else {
		document.getElementById(answerDivId).className = 'answer_div wrong_answer_div';
	}
}


