
// -------------------------------------------------------------------
// Madgex Limited
// Copyright (c) 2007 Madgex Limited. All Rights Reserved.
// Common, version 1.0
// Glenn Jones
// 26 June 2007
// CSSClass
// -------------------------------------------------------------------

//JScript for the contact us form
function CheckValidEmail( sText )
{
		
	// Assumes sText is not empty
	var d = sText.split('@')

	if ( d.length != 2 )
	{
		return false;
	}
	else
	{
		var dom = d[1].split('.') ;
		
		if ( dom.length < 2 )
			return false;
	}
	
	return true;
}

function IsBlank( sIn )
{
	
	var bBlank = true ;
	var s = '' + sIn + '';
	var c;
	if ( s != null )
	{
		for ( var i = 0; i < s.length; i++ )
		{
			c = s.charAt(i);
			if ( !c.IsWhiteSpace() ) 
			{
				bBlank = false;
			}
		}
	}
	return bBlank;
}

String.prototype.IsWhiteSpace = function()
{
	return this == ' ' || this == '\t';
}

function ValidateCallback( aForm, bSomething )
{	
	var sMsg='';
	var bHighLightElement = false;
	var bgColor = 'red';
	var validates = true ;

			
	//if( arguments.length > 1)
	//{
	//	bHighLightElement = arguments[1] ;
	//}

	if ( IsBlank( aForm.first_name.value ) ) 
	{
		sMsg += '\nYou must supply your first name.';
		validates = false;
		
		
		
		
		if ( bHighLightElement )
		{
			aForm.first_name.style.backgroundColor = bgColor
		}
	}
	
	if ( IsBlank( aForm.last_name.value ) ) 
	{
		sMsg += '\nYou must supply your last name.';
		validates = false;
		
		if ( bHighLightElement )
		{
			aForm.last_name.style.backgroundColor = bgColor
		}
	}
	
	if ( IsBlank( aForm.company.value ) ) 
	{
		sMsg += '\nYou must supply your company name.';
		validates = false;
		
		if ( bHighLightElement )
		{
			aForm.company.style.backgroundColor = bgColor
		}
	}
	
	if ( IsBlank( aForm.title.value ) ) 
	{
		sMsg += '\nYou must supply your job title.';
		validates = false;
		
		if ( bHighLightElement )
		{
			aForm.JobTitle.style.backgroundColor = bgColor
		}
	}
					
	if ( IsBlank( aForm.email.value ) || !( CheckValidEmail(aForm.email.value))  ) 
	{
		sMsg += '\nYou must supply a valid email address.';
		validates = false;
		
		if ( bHighLightElement )
		{
			aForm.email.style.backgroundColor = bgColor
		}
	}
	
	if ( IsBlank( aForm.phone.value ) ) 
	{
		sMsg += '\nYou must supply a telephone number.';
		validates = false;
		
		if ( bHighLightElement )
		{
			aForm.phone.style.backgroundColor = bgColor
		}
	}
	
	
	if( IsBlank( aForm.elements["city"].value ) )
	{
		sMsg += '\nYou must supply a city.';
		if ( bHighLightElement )
		{
			aForm.elements["city"].style.backgroundColor = bgColor	
		}
	}


	
	if( IsBlank( aForm.elements["country"].value ) )
	{
		sMsg += '\nYou must supply a country.';
		if ( bHighLightElement )
		{
			aForm.elements["country"].style.backgroundColor = bgColor	
		}
	}	

	if ( !validates )
	{
		alert(sMsg);
		validates = true;
		sMsg='';
		return false;
	}
	else
	{
		return true;
	}
	
}
//End of JScript for contact us form


	/*
	    Written by Jonathan Snook, http://www.snook.ca/jonathan
	    Add-ons by Robert Nyman, http://www.robertnyman.com
	*/
	function getElementsByClassName(oElm, strTagName, strClassName){
	    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	    var arrReturnElements = new Array();
	    strClassName = strClassName.replace(/\-/g, "\\-");
	    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	    var oElement;
	    for(var i=0; i<arrElements.length; i++){
	        oElement = arrElements[i];      
	        if(oRegExp.test(oElement.className)){
	            arrReturnElements.push(oElement);
	        }   
	    }
	    return (arrReturnElements)
	}
	
	
	function addEvent( obj, type, fn )
	{
		try
		{
		    if (obj.addEventListener)
			    obj.addEventListener( type, fn, false );
		    else if (obj.attachEvent)
		    {
			    obj["e"+type+fn] = fn;
			    obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
			    obj.attachEvent( "on"+type, obj[type+fn] );
		    }
		}
		catch(err)
		{
		    //alert(err); //just in for testing!
		}
	}



 /**
     * CSSClass.js: utilities for manipulating the CSS class of an HTML element.
     * 
     * This module defines a single global symbol named CSSClass.  This object
     * contains utility functions for working with the class attribute (className
     * property) of HTML elements.  All functions take two arguments: the element
     * e being tested or manipulated and the CSS class c that is to be tested,
     * added, or removed.  If element e is a string, it is taken as an element
     * id and passed to document.getElementById().
     */
    var CSSClass = {};  // Create our namespace object

    // Return true if element e is a member of the class c; false otherwise
    CSSClass.has = function(e, c) {
        if (typeof e == "string") e = document.getElementById(e); // element id

        // Before doing a regexp search, optimize for a couple of common cases.
        var classes = e.className;
        if (!classes) return false;    // Not a member of any classes
        if (classes == c) return true; // Member of just this one class

        // Otherwise, use a regular expression to search for c as a word by itself
        // \b in a regular expression requires a match at a word boundary.
        return e.className.search("\\b" + c + "\\b") != -1;
    };

    // Add class c to the className of element e if it is not already there.
    CSSClass.add = function(e, c) {
        if (typeof e == "string") e = document.getElementById(e); // element id
        if (CSSClass.has(e, c)) return; // If already a member, do nothing
        if (e.className) c = " " + c;  // Whitespace separator, if needed
        e.className += c;              // Append the new class to the end
    };

    // Remove all occurrences (if any) of class c from the className of element e
    CSSClass.remove = function(e, c) {
        if (typeof e == "string") e = document.getElementById(e); // element id
        // Search the className for all occurrences of c and replace with "".
        // \s* matches any number of whitespace characters.
        // "g" makes the regular expression match any number of occurrences
        
        e.className = e.className.replace(new RegExp("\\b"+ c+"\\b\\s*", "g"), "");   
    };

