// JavaScript Document

function signout() {
	$.post("ajaxinterface.php", { action:'isProfilePublished' },                 
				function(data) {
					
					if (data == "true") {
						//window.location.href= 'index.php';
						$.post("ajaxinterface.php", { action:'logout' }, 
								function(data) {
									if (data == "true") {
										window.location.href= 'index.php';
									} else {
										// cannot signout
										window.location.href= 'index.php';
									}
						});
						
						
					}
					else {
						
						// profile is not published
						
						ans = confirm("Your profile is not published.\nDo you want to publish your profile?"); 
						
						if (ans) {
							publishProfile();
						}
						else {
							
							// signout 
							
							$.post("ajaxinterface.php", { action:'logout' }, 
								function(data) {
									if (data == "true") {
										window.location.href= 'index.php';
									}
									else {
										// cannot signout 
										
										window.location.href= 'index.php';
									}
						});
							
							
							
							
						}
						
						
					}
		});
	
	
		
		
		return false;
}


function publishProfile() {
	
	
	
	
	$.post("ajaxinterface.php", { action:'publish' }, 
                function(data) {
					
					
					if (data == "true") {
						
						//alert('Your profile has been published.');
						
						ans = confirm("Your Profile has been published successfully.\nDo you want to signout?");
						if (ans) {
							signout();	
						}
						
					}
					else {
						// cannot publish profile
						alert("Your profile couldn't be published.\nPlease go to your personal information page and provide a valid URL.");
					}
					
					
		});
		return false;
}



function showhide (element) {
		
	$('#'+element).toggle('slow');
	
	return false;
}

function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms)
    
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY)

}


function num_days () {
	
	// Store the current date and time
var current_date = new Date()

	// Store the date of the next New Year's Day
	var target_date = new Date()
	target_date.setYear(2010)
	target_date.setMonth(2)
	target_date.setDate(4)
	
	// Call the days_between function
	var days_left = days_between(current_date, target_date)
	
	// Write the result to the page
	if (days_left > 1) {
		return days_left;
	}
	else {
		return days_left;
	}
	
}



function stripspaces(input)
{
    input.value = input.value.replace(/\s/gi,"");
    return true;
}

function checkform(f) {
	var name = f.fullname;
	var email = f.emailaddress;
	var comments = f.comments;
	var achars = " ";
	var span = document.getElementById("msgSpan");
	var error = false;
	
	if( (isBlank(name.value)) || (!isString(name.value, achars)) ) {
		alert("Please type a valid name.");
		span.innerHTML = "Please type a valid name.";
		name.style.background = 'Yellow';	
		name.focus();
		error = true;	
	} else {
		name.style.background = 'White';	
	}// if invalid name...
	
	if( !isEmail(email.value) ) {
		alert("Please type a valid email.");
		span.innerHTML = "Please type a valid email.";
		email.style.background = 'Yellow';	
		email.focus();
		error = true;
	} else {
		email.style.background = 'White';	
	}// if invalid email
	
	if( isBlank(comments.value) ) {
		alert("Please type your query.");
		span.innerHTML = "Please type your query.";
		comments.style.background = 'Yellow';	
		comments.focus();
		error = true;	
	} else {
		comments.style.background = 'White';	
	} // if comments field is blanck

	if(error == true) {
		return false;	
	}
} // checkform(...)

function selectall(frm) {
	if(frm.chkAll.checked) { // if chkAll is checked
		for(c = 0; c < frm.elements.length; c++) {
			if(frm.elements[c].type == "checkbox") {
				frm.elements[c].checked = 1;
			} // if element is checkbox
		} // for c
	} else { // if chkAll is unchecked
		for(c2 = 0; c2 < frm.elements.length; c2++) {
			if(frm.elements[c2].type == "checkbox") {
				frm.elements[c2].checked = 0;
			} // if element is checkbox
		} // for c2
	} // if frm.chkAll...
} // selectall(...)

/////////////////////////////////////////////////////////////////////////////////

function confirmDelete() {
	var confirmMessage = "Do you really want to delete?";
	var n = true;
	frm = document.frmList;
	var a = false;
	
	count = 0;
	while( count < frm.elements.length ) {
		if(frm.elements[count].type == "checkbox") {
			if(frm.elements[count].checked == true) {
				a = confirm(confirmMessage);
				n = false;
				break;
			}
		}
	count++;
	}
	
	if (n) {
		a = false;
		alert("Please, select at least one record to delete!");
		return false;
	} // if(n)
	return a;
} // confirmDelete()

////////////////////////////////////////////////////////////////////////////////

function isEmail(address) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	//var address = document.forms[form_id].elements[email].value;
	if(reg.test(address) == false) {
	  //alert('Invalid Email Address');
	  return false;
	} else {
		return true;   
	}
} // isEmail(address)