// Open a window with a parsed object, width and height.
function OpenWindow(obj,thisWidth,thisHeight)
{
	window.open(obj,'newWindow','width='+thisWidth+',height='+thisHeight+',scrollbars=no,resizable=yes,left=10,top=20');
}

//--------------------------------------------------------------------------------------------------
// Open a sizable window with a parsed image name.
function OpenImageSizableWindow(thisImage)
{		
	var thisWindowWidth = 620;
	var thisWindowHeight = 500;
	
	newWindow=window.open('','newWindow','width='+thisWindowWidth+',height='+thisWindowHeight+',scrollbars=yes,resizable,left=20,top=20');
	newWindow.document.open();
	newWindow.document.write("<html>");
	newWindow.document.write("<head>");
	newWindow.document.write("<title>"+thisWindowTitle+"</title>");
	newWindow.document.write('<LINK rel=stylesheet type="text/css" href="/stylesheets/main.css" title="style">');
	newWindow.document.write("</head>");
	newWindow.document.write("<body topmargin='10' leftmargin='10' rightmargin='10'>");
	newWindow.document.write("<center><img src='"+thisImage+"'><center><br>");
	newWindow.document.write('<center><a href="#" onclick="JavaScript:window.print();">[Print Photo]</a> &nbsp;&nbsp; <a href="#" onclick="JavaScript:window.close()">[Close Photo]</a></center>');
	newWindow.document.write("</body>");
	newWindow.document.write("</html>");
	newWindow.document.close();
	newWindow.focus();
}

//--------------------------------------------------------------------------------------------------
// Open a pop-up window with the provided URL, width and height of the window.
function OpenPopWindow(thisURL,thisWidth,thisHeight)
{
	var thisWindowWidth = thisWidth + 30;
	var thisWindowHeight = thisHeight + 10;

	window.open(""+thisURL+"","OrderWindow","width="+thisWindowWidth+",height="+thisWindowHeight+",left=5,top=5,status=Yes,toolbar=no,scrollbars=yes,resizable");
}
	
//--------------------------------------------------------------------------------------------------
// Open a pop-up frame type window with the provided URL, width and height of the window.
function OpenSmallPopWindowFrame(thisURL,thisWidth,thisHeight)
{
	window.open(""+thisURL+"","PopWindow","width="+thisWidth+",height="+thisHeight+",left=50,top=100,toolbar=no,scrollbars");
}

//--------------------------------------------------------------------------------------------------

// Open a non-sizeable window with a parsed image name.
function OpenShopPopWindow(thisWindowTitle, thisImage, thisWindowWidth, thisWindowHeight)
{		
	thisWindowWidth = thisWindowWidth + 40;
	thisWindowHeight = thisWindowHeight + 60;
	
	newWindow=window.open('','newWindow','width='+thisWindowWidth+',height='+thisWindowHeight+',scrollbars=no,left=20,top=20');
	newWindow.document.open();
	newWindow.document.write("<html>");
	newWindow.document.write("<head>");
	newWindow.document.write("<title>"+thisWindowTitle+"</title>");
	newWindow.document.write('<LINK rel=stylesheet type="text/css" href="/stylesheets/clientstyles.css" title="style">');
	newWindow.document.write("</head>");
	newWindow.document.write("<body topmargin='20' leftmargin='10' rightmargin='10'>");
	newWindow.document.write("<br><center><img src='"+thisImage+"'><center><br>");
	newWindow.document.write('<center><a href="#" onclick="JavaScript:window.print();">[Print]</a> &nbsp;&nbsp; <a href="#" onclick="JavaScript:window.close()">[Close]</a></center>');
	newWindow.document.write("</body>");
	newWindow.document.write("</html>");
	newWindow.document.close();
	newWindow.focus();
}

//--------------------------------------------------------------------------------------------------

// Define a list of acceptable characters for use in the CheckValidCharacters function.
var identifierCharList = new RegExp(/[^a-zA-Z0-9()\_\-:.\ ]/);
var nameCharList = new RegExp(/[^a-zA-Z0-9()\_\-:.&\ \"\'\[\]\|\@]/); 
var numericCharList = new RegExp(/[^0-9.]/);
var alphaNumericCharList =  new RegExp(/[^a-zA-Z0-9]/);
var descriptionCharList = new RegExp(/[^a-zA-Z0-9~!@$%&*()\_\+\-\=\[\]\\{};:\./?,\"\'\|\ \r\n]/);


function CheckValidCharacters(thisField, thisFieldName, thisCharList)
{
RemoveXSWhiteSpace(thisField);

//Check the acceptable character list.
if (thisField.value.match(thisCharList))
	{
	alert("Data  Error:\n\nInvalid characters in the " +thisFieldName+ " field.\n\nPlease remove any non-standard characters (eg. `~^<>#)\n\nAdditionally, for any non-alphanumeric characters, try retyping them.");
	thisField.focus();
	return false;
	}
else
	{
	return true;
	}
}

//--------------------------------------------------------------------------------------------------

function RemoveXSWhiteSpace(item)
{ //Remove excess white space (ie. trailing/leading)
  var tmp = "";
  var itemLength = item.value.length;
  var itemLengthMinus1 = item.value.length - 1;
  for (index = 0; index < itemLength; index++)
  {
    if (item.value.charAt(index) != ' ')
    {
      tmp += item.value.charAt(index);
    }
    else
    {
      if (tmp.length > 0)
      {
        if (item.value.charAt(index+1) != ' ' && index != itemLengthMinus1)
        {
          tmp += item.value.charAt(index);
        }
      }
    }
  }
  item.value = tmp;
}

//--------------------------------------------------------------------------------------------------
function CheckSecurity(thisWMSAddress)
{ // Alert the user before going to In-Control.
  if (confirm("                           - SECURITY WARNING -\n\nYou are about to be transferred to In-Control's secure login facility.\n\n  Please click 'CANCEL' if you are not authorised to utilise this facility.\n\n                  Unauthorised access is prohibited."))
    document.location.href = thisWMSAddress;
}

//--------------------------------------------------------------------------------------------------
// Check for a valid email address.
function IsEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

//--------------------------------------------------------------------------------------------------
// Check all required fields have been entered on the Contact Us Form.
function ValidateContact() 
{
    var formOk = true;
    var errormsg = "Please complete/check the following required fields:\n";	

    if (theContactForm("First_Name").value == "")
       {
       errormsg += "\n- First Name";
       formOk = false;
       }

    if (theContactForm("Last_Name").value == "")
       {
       errormsg += "\n- Last Name";
       formOk = false;
       }

    if (theContactForm("Email_Address").value == "")
       {
       errormsg += "\n- Email Address";
       formOk = false;
       }
		
    if ((theContactForm("Email_Address").value != "") && (!IsEmailAddr(theContactForm("Email_Address").value)))
       {
       errormsg += "\n- Email Address is invalid";
       formOk = false;
       }

    if (theContactForm.Comments.value == "")
       {
       errormsg += "\n- Comments";
       formOk = false;
       }

    if (!formOk)
       {
       alert(errormsg);
       return false;
       }
    else
       {
       alert("Thank you - your request will now be submitted.");
       return (true);
       }
}  

//--------------------------------------------------------------------------------------------------
// Check all required fields have been entered on the Contact Us Form.
function ValidateDistributor() 
{
    var formOk = true;
    var errormsg = "Please complete/check the following required fields:\n";	

    if (theContactForm("First_Name").value == "")
       {
       errormsg += "\n- First Name";
       formOk = false;
       }

    if (theContactForm("Last_Name").value == "")
       {
       errormsg += "\n- Last Name";
       formOk = false;
       }

    if (theContactForm("Email_Address").value == "")
       {
       errormsg += "\n- Email Address";
       formOk = false;
       }
		
    if ((theContactForm("Email_Address").value != "") && (!IsEmailAddr(theContactForm("Email_Address").value)))
       {
       errormsg += "\n- Email Address is invalid";
       formOk = false;
       }

    if (!formOk)
       {
       alert(errormsg);
       return false;
       }
    else
       {
       alert("Thank you - your request will now be submitted.");
       return (true);
       }
}  

//--------------------------------------------------------------------------------------------------
// Check all required fields have been entered on the respective form.
function ValidateMaterialRequest() 
{
    var formOk = true;
    var errormsg = "Please complete/check the following required fields:\n";	

    if (theContactForm("First_Name").value == "")
       {
       errormsg += "\n- First Name";
       formOk = false;
       }

    if (theContactForm("Last_Name").value == "")
       {
       errormsg += "\n- Last Name";
       formOk = false;
       }

    if (theContactForm("Email_Address").value == "")
       {
       errormsg += "\n- Email Address";
       formOk = false;
       }
		
    if ((theContactForm("Email_Address").value != "") && (!IsEmailAddr(theContactForm("Email_Address").value)))
       {
       errormsg += "\n- Email Address is invalid";
       formOk = false;
       }

    if (!formOk)
       {
       alert(errormsg);
       return false;
       }
    else
       {
       alert("Thank you - your request will now be submitted.");
       return (true);
       }
}  

//--------------------------------------------------------------------------------------------------
// Check all required fields have been entered on the Ask An Expert Form.
function ValidateAskAnExpert() 
{
    var formOk = true;
    var errormsg = "Please complete/check the following required fields:\n";	

    if (theContactForm("First_Name").value == "")
       {
       errormsg += "\n- First Name";
       formOk = false;
       }

    if (theContactForm("Last_Name").value == "")
       {
       errormsg += "\n- Last Name";
       formOk = false;
       }

    if (theContactForm("Email_Address").value == "")
       {
       errormsg += "\n- Email Address";
       formOk = false;
       }
		
    if ((theContactForm("Email_Address").value != "") && (!IsEmailAddr(theContactForm("Email_Address").value)))
       {
       errormsg += "\n- Email Address is invalid";
       formOk = false;
       }

    if (theContactForm("Postal_Address").value == "")
       {
       errormsg += "\n- Postal Address";
       formOk = false;
       }

    if (theContactForm("State").value == "")
       {
       errormsg += "\n- State";
       formOk = false;
       }

    if (theContactForm("Post_Code").value == "")
       {
       errormsg += "\n- Post Code";
       formOk = false;
       }

    if (theContactForm("Question").value == "")
       {
       errormsg += "\n- Question";
       formOk = false;
       }

    if (!formOk)
       {
       alert(errormsg);
       return false;
       }
    else
       {
       alert("Thank you - your request will now be submitted.");
       return (true);
       }
}  


//--------------------------------------------------------------------------------------------------
// Check all required fields have been entered on the Shareholder Signup Form.
function ValidateShareholderSignup() 
{
	  if (ShareholderSignupForm("Full_Name").value == "")
	    {
	    alert("Please enter your Full Name");
	    ShareholderSignupForm("Full_Name").focus();
	    return (false);
	    }

	  if (ShareholderSignupForm("Email_Address").value == "")
	    {
	    alert("Please enter your Email Address");
	    ShareholderSignupForm("Email_Address").focus();
	    return (false);
	    }
		
	  if (!IsEmailAddr(ShareholderSignupForm("Email_Address").value))
	    {
	    alert("Please enter a complete Email Address i.e. yourname@yourdomain.com");
	    ShareholderSignupForm("Email_Address").focus();
	    return (false);
	    }

           else
             {
             alert("Thank you - your request will now be submitted.");
             return (true);
             }
}  

//--------------------------------------------------------------------------------------------------
// Function to create a cookie with the specified name and value. 

function SetCookie(sName, sValue)
{ 
    document.cookie = sName + "=" + escape(sValue) + "; expires=Mon, 31 Dec 2010 23:59:59 UTC;"; 
} 
  
//--------------------------------------------------------------------------------------------------
// Function to delete a cookie with the specified name. 
function DeleteCookie(sName, sValue)
{
    if (GetCookie(sName))
      {
      document.cookie = sName + "=" + escape(sValue) + "; expires=0;"; 
      }
}

//--------------------------------------------------------------------------------------------------
// Function to retrieve the value of the cookie with the specified name. 

function GetCookie(sName)
{
   var docCookie = document.cookie;

   if(docCookie.length > 0)
      { 
      var begin = docCookie.indexOf(sName+'=') 
      if(begin != -1)
        { 
        end = docCookie.indexOf(';', begin) 
        if(end == -1)
          end = docCookie.length
        return unescape(docCookie.substring(begin+sName.length+1, end)) 
        } 
      } 
   //a cookie with the requested name does not exist 
   return null 
}

//--------------------------------------------------------------------------------------------------
// Function to resize and reposition a window with parsed parameters.
function ResizeWindow(thisWidth,thisHeight,thisTop,thisLeft)
{
	resizeTo(thisWidth, thisHeight);  // Resize.
	moveTo(thisLeft,thisTop);  // Then re-position
}

//--------------------------------------------------------------------------------------------------