//'------------------------------------------------------------------------------------------
//' js-core.js (Core)
//' Version 2.0
//'------------------------------------------------------------------------------------------
//' Copyright Surfocracy Ltd, 2010, Contact: info@surfocracy.com
//'------------------------------------------------------------------------------------------
//' Description: Core JavaScript routines which can be used for all dynamic sites.
//               These routines are called from the main site and admin.
//'------------------------------------------------------------------------------------------
// List of functions
// function ConfirmRemove()
// function PopUpWindow(url,name,WinHeight,WinWidth,WinLeft,WinTop,WinResizable,WinScrollbars,WinToolbar,WinMenubar,WinStatus)
// function GetXmlHttpObject()
// function isFloatingPointNumericString(val)
// function isPositiveIntegerString(val)
// function isIntegerString(val)
// function isValidDate(day,month,year)
// function isValidEmail(email)
// function isEmailUnique(Email,IgnoreID)
// function isEmailUnique_Response()
// function isUsernameUnique(Username,IgnoreID)
// function isUsernameUnique_Response()
// 
//'------------------------------------------------------------------------------------------
// Global declarations
var xmlhttp                  // Must be declared here is it needs to be globally visible for AJAX
//--------------------------------------------------------------------------------------
// This function issues a pop up alert seeking confirmation on whether an item should
// be removed. Ok proceeds with removal and Cancel leaves unchanged.
// // onClick="return ConfirmRemove()"
//--------------------------------------------------------------------------------------
function ConfirmRemove() {

   if (!confirm(AlertAboutToRemove + "\n" + 
		        AlertProceedOrLeaveUnchanged))
	  return false;
   else
	  return true
}
//--------------------------------------------------------------------------------------
// This function Pops up a window with the given parameters.
// Example call to this function would look like:
// onClick="PopUpWindow('POPUP-file.htm','name',WinHeight=400,WinWidth=600,WinLeft=100,WinTop=100,
//                       WinResizable='yes',WinScrollbars='yes',WinToolbar='no',WinMenubar='no',WinStatus='no'); return false;"
//--------------------------------------------------------------------------------------
function PopUpWindow(url,name,WinHeight,WinWidth,WinLeft,WinTop,WinResizable,WinScrollbars,WinToolbar,WinMenubar,WinStatus) {
  var newwindow;

  var settings= "height="+WinHeight+
                 ",width="+WinWidth+
  				 ",left="+WinLeft+
				 ",top="+WinTop+
		         ",resizable="+WinResizable+
				 ",scrollbars="+WinScrollbars+			  
                 ",toolbar="+WinToolbar+
				 ",menubar="+WinMenubar+
                 ",status="+WinStatus;
               //  ",location=yes,directories=yes";

  newwindow=window.open(url,'name',settings);
  if (window.focus) {newwindow.focus()}
}
// ------------------------------------------------------------------------
// This JS function creates and returns the XML http request object which
// is necessary for communicating to the server during an AJAX call.
// ------------------------------------------------------------------------
function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	  {
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	  }
	if (window.ActiveXObject)
	  {
	  // code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return null;
}
//'------------------------------------------------------------------------------------------
// This function checks if a string is a floating point number. Will accept negatives and decimals.
//'------------------------------------------------------------------------------------------
function isFloatingPointNumericString (val) {
var RefString="1234567890.-";	
  if (val.length!=0) {
	  for (Count=0; Count < val.length; Count++) {
		   TempChar= val.substring (Count, Count+1);
		   if (RefString.indexOf (TempChar, 0)==-1) {
			  return (false);
		   }
	   }
  } else {
	   return(false)}
  return (true);
}
//'------------------------------------------------------------------------------------------
// This function checks if a string is a positive integer number.
//'------------------------------------------------------------------------------------------
function isPositiveIntegerString (val) {
var RefString="1234567890";	
  if (val.length!=0) {
	  for (Count=0; Count < val.length; Count++) {
		   TempChar= val.substring (Count, Count+1);
		   if (RefString.indexOf (TempChar, 0)==-1) {
			  return (false);
		   }
	   }
  } else {
	   return(false)}
  return (true);
} 
//'------------------------------------------------------------------------------------------
// This function checks if a string is an integer number. Will accept negatives.
//'------------------------------------------------------------------------------------------
function isIntegerString (val) {
var RefString="1234567890-";	
  if (val.length!=0) {
	  for (Count=0; Count < val.length; Count++) {
		   TempChar= val.substring (Count, Count+1);
		   if (RefString.indexOf (TempChar, 0)==-1) {
			  return (false);
		   }
	   }
  } else {
	   return(false)}
   return (true);
} 
//--------------------------------------------------------------------------------------
// This function validates the given date with day, month and year being integer values.
//--------------------------------------------------------------------------------------
function isValidDate(day,month,year){
	var dteDate;
	var jsmonth = month - 1;   // Javascript month start from 0 to 11 hence need to decrement by 1
	
	dteDate=new Date(year,jsmonth,day);
	
	// Javascript Dates are a little too forgiving and will change the date to a reasonable guess if it's invalid. 
	// We'll use this to our advantage by creating the date object and then comparing it to the details we put it. 
	// If the Date object is different, then it must have been an invalid date to start with...
	return ((day==dteDate.getDate()) && (jsmonth==dteDate.getMonth()) && (year==dteDate.getFullYear()));
}
//--------------------------------------------------------------------------------------
// This function validates the email parameter
//--------------------------------------------------------------------------------------
function isValidEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = email;
   if(reg.test(address) == false) {
	  return false;
   } else {
	  return true;
   }
}
// ------------------------------------------------------------------------
// This JS function calls a server side script to check if the Email
// is unique in Core_Accounts.
// Email : the email to check
// IgnoreID : do not compare with this Core_Accounts ID
// ------------------------------------------------------------------------
function isEmailUnique(Email,IgnoreID)
{
  // Check for blank input
  if (Email.length!=0)
	 {  
		// Create the request object
		xmlhttp=GetXmlHttpObject();

		// THIS IS WHERE THE ASP PAGE IS CALLED
		var url="/1-Core/3-components/AJAX-core.asp?command=IsEmailUnique&Email="+Email+"&IgnoreID="+IgnoreID+"&sid="+Math.random();  // Create the URL to the server sideASP file passing in the value and a cache killer
		xmlhttp.onreadystatechange=isEmailUnique_Response;  // Set up the JS function to be called once the server response is returned
		// ##### IMPORTANT : set to ASYNCHRONOUS (true) as we are not interested in the return value.
		xmlhttp.open("GET",url,true);                         // Set up the connection using GET method. Passing the URL and indicate it is asynchronous
		xmlhttp.send(null);                                   // Send request to the server.
	 }
   
}
// ------------------------------------------------------------------------
// This JS function pops up an alert if the given Email is already being
// used. If checks the response from the server side function.
// If in use an alert pops up and the focus is placed on the Email field.
// ------------------------------------------------------------------------
function isEmailUnique_Response()
{
  if (xmlhttp.readyState==4)
  {
	if (xmlhttp.responseText=="notunique")
	{
	 alert ("Email already in use.");
	 document.getElementById('Email').focus();
	 return;
	}
  }
}
// ------------------------------------------------------------------------
// This JS function calls a server side script to check if the Username
// is unique.
// ------------------------------------------------------------------------
function isUsernameUnique(Username,IgnoreID)
{
  // Check for blank input
  if (Username.length!=0)
	{  
		// Create the request object
		xmlhttp=GetXmlHttpObject();

		// THIS IS WHERE THE ASP PAGE IS CALLED
		var url="/1-Core/3-components/AJAX-core.asp?command=IsUsernameUnique&Username="+Username+"&IgnoreID="+IgnoreID+"&sid="+Math.random();  // Create the URL to the server sideASP file passing in the value and a cache killer
		xmlhttp.onreadystatechange=isUsernameUnique_Response;  // Set up the JS function to be called once the server response is returned
		// ##### IMPORTANT : set to ASYNCHRONOUS (true) as we are not interested in the return value.
		xmlhttp.open("GET",url,true);                         // Set up the connection using GET method. Passing the URL and indicate it is asynchronous
		xmlhttp.send(null);                                   // Send request to the server.
   }
}
// ------------------------------------------------------------------------
// This JS function pops up an alert if the given Username is already being
// used. If checks the response from the server side function.
// ------------------------------------------------------------------------
function isUsernameUnique_Response()
{
  if (xmlhttp.readyState==4)
  {
	if (xmlhttp.responseText=="notunique")
	{
	 alert ("Username already in use, please choose another.");
	 document.getElementById('Username').focus();
	 return;
	}
  }
}

