/**************************************************************
 * This code was created by and is copyright of ICEDPLASMA.   *
 * This code is licensed for use with the Race4Real website   *
 * designed and created by ICEDPLASMA only. This code may not *
 * be modified or redistributed in any way without express    *
 * permission of ICEDPLASMA. Any unauthorised use of this     *
 * code is expressly forbidden.                               *
 * ***********************************************************/ 

// To get selectSingleNode to work in all browsers
if (!window.ActiveXObject) 
{

  try 
  {
    Element.prototype.selectNodes = function(sXPath) 
                                    {
                                      var oEvaluator = new XPathEvaluator();
                                      var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);                                
                                      var aNodes = new Array();
                                      
                                      if (oResult != null)
                                      {                                  
                                        var oElement = oResult.iterateNext();
                                      
                                        while(oElement) 
                                        {
                                          aNodes.push(oElement);
                                      
                                          oElement = oResult.iterateNext();
                                      
                                        }
                                      
                                      }
  
                                      return aNodes;
                                    }

  }
  catch(e)
  { 
    alert(e);
  }

  try 
  {  
    Element.prototype.selectSingleNode = function(sXPath) 
    {
      var oEvaluator = new XPathEvaluator();
    
      // FIRST_ORDERED_NODE_TYPE returns the first match to the xpath.
      var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
    
      if (oResult != null) 
      {
        return oResult.singleNodeValue;
      } 
      else 
      {
        return null;
      }              
    }
  }
  catch(e)
  { 
    alert(e);
  }
}

// Custom Window.Open Function
function openwindow(url, name, winwidth, winheight, params)
{ 
  var optionstring = 'scrollbars=yes,menubar=no';
  
  if (winheight) 
  { optionstring += ',height=' + winheight + ',';
  }
  else
  {
    optionstring += ',height=' + screen.height + ',';
  }
  
  if (winwidth)
  {
    optionstring += ',width=' + winwidth + ',';
  }
  else
  {
    optionstring += ',width=' + screen.width + ',';
  }
  
  if (params)
  {
    optionstring += "," + params;
  }
  
  var load = window.open(url,name,optionstring);
}

// Post XML Dom to another page
function postToServer(URL, Data)
{
  var xmlhttp=false;
  var xmlDom;
    
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  // JScript gives us Conditional compilation, we can cope with old IE versions.
  // and security blocked creation of the objects.
  
  // Internet Explorer - create xmlhttp request
  try 
  {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } 
  catch (e) 
  {
    try 
    {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    catch (e) 
    {
      xmlhttp = false;
    }
  }
  @end @*/

  // Firefox, Opera etc - create xmlhttp request
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
  {
    try 
    {
		  xmlhttp = new XMLHttpRequest();
    } 
    catch (e) 
    {
		  xmlhttp=false;
    }
  }
  
  // IceBrowser  - create xmlhttp request
  if (!xmlhttp && window.createRequest) 
  {
    try 
    {
		  xmlhttp = window.createRequest();
    } 
    catch (e) 
    {
		  xmlhttp=false;
    }
  }

  // Post Data
  if (!xmlhttp || xmlhttp!=null)
  {
    xmlhttp.open("POST",URL,false);
    
    try
    {
      xmlhttp.send(Data);
      
      // Prepare Return XMLDom
      try //Internet Explorer
      {
        xmlDom=new ActiveXObject("Microsoft.XMLDOM");
        xmlDom.async="false";
        xmlDom.loadXML(xmlhttp.responseText);
      }
      catch(e)
      {
        try //Firefox, Mozilla, Opera, etc.
        {
          parser=new DOMParser();
          xmlDom=parser.parseFromString(xmlhttp.responseText,"text/xml");
      
        }
        catch(e)
        {
          alert(e);
        }
      }
    }
    catch(e)
    {
      alert(e);
    }
    	
  }
  else
  {
  alert("Your browser does not support XMLHTTP.");
  }
  
  return xmlDom;

}

// Checks if string is integer
function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

// Removes all whitespace from a string
function removeWhitespace(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}

// Search through string's characters one by one.
// If character is not in bag, append to returnString.
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

// Check if phone number is valid including international format
function checkInternationalPhone(strPhone)
{
  // Declaring required variables
  var digits = "0123456789";
  // non-digit characters which are allowed in phone numbers
  var phoneNumberDelimiters = "()- ";
  // characters which are allowed in international phone numbers
  // (a leading + is OK)
  var validWorldPhoneChars = phoneNumberDelimiters + "+";
  // Minimum no of digits in an international phone no.
  var minDigitsInIPhoneNumber = 10;

  var bracket=3
  strPhone=removeWhitespace(strPhone)
  if(strPhone.indexOf("+")>1) return false
  if(strPhone.indexOf("-")!=-1)bracket=bracket+1
  if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
  var brchr=strPhone.indexOf("(")
  if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
  if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
  s=stripCharsInBag(strPhone,validWorldPhoneChars);
  return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

// Phone Number Validation
function validatePhoneNumber(strPhoneNumber)
{ 
	if ((strPhoneNumber.length<1)||(strPhoneNumber==null)||(strPhoneNumber==""))
  {
		return false
	}
	else if (checkInternationalPhone(strPhoneNumber)==false)
  {
		return false
	}
	else
	{	
    return true
	}
}

function validateEmail(strEmail)
{
  if ((strEmail.length < 6) || (strEmail==null) || (strEmail==""))
  {
		return false;
	}
	else 
	{
	  apos=strEmail.indexOf("@");
    dotpos=strEmail.lastIndexOf(".");
    if (apos < 1 || dotpos - apos < 2) 
    {
       return false;
    }
    else 
    {
      return true;
    }
  }
}