// 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;

function switchImage(imgName, imgSrc)
{
	if (document.images)
	{
		if (imgSrc != "none")
		{
			document.images[imgName].src = imgSrc;
		}
	}
}

function validateField(field,alerttxt)
{
	var flag = false;
	
	with (field)
	{
		if (value==null||value=="")
			alert(alerttxt);
		else 
			flag = true;
	}
	return flag;
}

function formValidation(form) 
{
	var flag = true;
	
	with (form)
	{
		if (validateField(txtFirstname,"You are missing a mandatory field. Please write your first name")==false)
  		{	
			txtFirstname.focus();
			flag = false;
		} 
		else if (validateField(txtLastname,"You are missing a mandatory field. Please write your last name")==false)
		{
			txtLastname.focus();
			flag = false;
		}
		else if (validateField(txtOrgname,"You are missing a mandatory field. Please write your company name")==false)
		{
			txtOrgname.focus();
			flag = false;
		}
		else if (validateField(txtEmail,"You are missing a mandatory field. Please write your email address")==false)
		{
			txtEmail.focus();
			flag = false;
		}
		else if (validateEmail(txtEmail.value)==false){
			txtEmail.value="";
			txtEmail.focus();
			flag = false;
		}
		else if (validateField(txtPhone,"You are missing a mandatory field. Please write your phone number")==false)
		{
			txtPhone.focus();
			flag = false;
		}
		else if (validatePhoneNumber(txtPhone.value)==false){
			txtPhone.value=""
			txtPhone.focus();
			flag = false;
		}
		
		// Final response
		if (flag == true)
		{
			alert("Your data is going to be send. Thank you for letting us know about yourself");
			form.submit();
		}
	}
}

/**
 * DHTML phone number | email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 * Modified by Víctor Chacón | (LuxRed.com)
 */
 
function validateEmail(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	var flag = true;
	if (str.indexOf(at)==-1) flag = false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)  flag = false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)  flag = false;
	if (str.indexOf(at,(lat+1))!=-1)  flag = false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) flag = false;
	if (str.indexOf(dot,(lat+2))==-1)  flag = false;
	if (str.indexOf(" ")!=-1)  flag = false;
	
	// Check the result
	if (flag == false)
	{
		alert("The email address you wrote is incorrect. Please write your email address in the correct format");
	} 
	else 
	{
		flag = true;
	}					
	
	return flag;
}

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;
}

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;
}

function validatePhoneNumber(strPhone)
{
	var flag = true;
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	
	if ((isInteger(s) && s.length >= minDigitsInIPhoneNumber) == false)
	{
		alert("The phone number you wrote is incorrect. Please write your phone number in the correct format");
		flag = false;
	}
	return flag;
}