var firstFlag=false;
var currentTab = document.getElementById("t1");
document.getElementById("t1Contents").style.display = 'block';
document.getElementById("t2Contents").style.display = 'none';
document.getElementById("t3Contents").style.display = 'none';
document.getElementById("t4Contents").style.display = 'none';
document.getElementById("t5Contents").style.display = 'none';

function TabFirstInit()
{
  if(firstFlag == true)
  {
		currentTab = t1;
		firstFlag = false;
  }
}

function changeTabs(tab)
{
	var currentDiv = document.getElementById(currentTab.id+'Contents');
	//Hide Old Tab and Contents
	currentDiv.style.display = 'none';
	currentTab.className = "Tab";
	AdjustTabSpacers(currentTab, "off");
	//Mode to New tab and Display Contents
  currentTab= tab;
  currentTab.className = "SelectedTab";
  AdjustTabSpacers(currentTab, "on");
  currentDiv = document.getElementById(tab.id+'Contents');
  currentDiv.style.display = 'block';
}

function bob(tab)
{
	//TabFirstInit();
  if(tab.className == "TabOver")
  {
		CommitTabCache();
		currentTab.className = "Tab";
		AdjustTabSpacers(currentTab, "off");

		// Move to New Tab
		currentTab = tab;
		currentTab.className = "SelectedTab";
		AdjustTabSpacers(currentTab, "on");

		// Change the new contents
		tabContentID = currentTab.id + "Contents";
		tabContent = document.all(tabContentID);
	
		tabContents.innerHTML = tabContent.innerHTML;
		//alert("hell0 " + tabContents.innerHTML)
  }
}


function check()
{
	CommitTabCache();
	//alert(document.getElementById("bob").value)
	
	var emailID = document.getElementById("email")
	var name = document.getElementById("name")
	var title = document.getElementById("title")
	var Mileage = document.getElementById("Mileage")
	var cellcode = document.getElementById("cellcode")
	var cellno = document.getElementById("cellno")
	var workcode = document.getElementById("workcode")
	var workno = document.getElementById("workno")
	var homecode = document.getElementById("HomeCode")
	var homeno = document.getElementById("homeno")
	var bobv = document.getElementById("bob")
			
	var errorMessage= ""

	if (((trim(cellcode.value)=="")&&(trim(cellno.value)==""))&&((trim(workcode.value)=="")&&(trim(workno.value)==""))&&((trim(homecode.value)=="")&&(trim(homeno.value)=="")))
	{
			errorMessage = errorMessage  + "At least one contact number is required.\n";
	}
	
	if ((trim(cellcode.value)=="") &&  (trim(cellno.value)!=""))
	{
		errorMessage = errorMessage  + "Cell Code Required .\n " 
	}
	
	if ((trim(cellcode.value)!="")  &&  (trim(cellno.value)==""))
	{
		errorMessage = errorMessage  + "Cell Number Required .\n " 
	}
	
	if ((trim(cellcode.value)!="") && ((trim(cellcode.value).length < 3) ||  (trim(cellcode.value).length > 6 )))
	{
		errorMessage = errorMessage  + "Cell Code must be between 3 and 6 characters .\n " 
	}
	
	if ((trim(cellno.value)!="") && (trim(cellno.value).length != 7))
	{
		errorMessage = errorMessage  + "Cell no must be 7 characters in length .\n " 
	}
	
	if ((trim(workcode.value)=="") &&  (trim(workno.value)!=""))
	{
		errorMessage = errorMessage  + "work Code Required .\n " 
	}
	
	if ((trim(workcode.value)!="")  &&  (trim(workno.value)==""))
	{
		errorMessage = errorMessage  + "work Number Required .\n " 
	}
	
	if ((trim(workcode.value)!="") && ((trim(workcode.value).length < 3) ||  (trim(workcode.value).length > 6 )))
	{
		errorMessage = errorMessage  + "work Code must be between 3 and 6 characters .\n " 
	}
	
	if ((trim(workno.value)!="") && (trim(workno.value).length != 7))
	{
		errorMessage = errorMessage  + "work no must be 7 characters in length .\n " 
	}
	
	if ((trim(homecode.value)=="") &&  (trim(homeno.value)!=""))
	{
		errorMessage = errorMessage  + "home Code Required .\n " 
	}
	
	if ((trim(homecode.value)!="")  &&  (trim(homeno.value)==""))
	{
		errorMessage = errorMessage  + "home Number Required .\n " 
	}
	
	if ((trim(homecode.value)!="") && ((trim(homecode.value).length < 3) ||  (trim(homecode.value).length > 6 )))
	{
		errorMessage = errorMessage  + "home Code must be between 3 and 6 characters .\n " 
	}
	
	if ((trim(homeno.value)!="") && (trim(homeno.value).length != 7))
	{
		errorMessage = errorMessage  + "home no must be 7 characters in length .\n " 
	}
	
	if (title.value=="0000")
	{
		errorMessage = errorMessage  + "Title Required .\n";
	}
	
	if (name.value=="")
	{
		errorMessage = errorMessage  + "FirstName Required .\n";
	}

	if (emailID.value=="")
	{
	    errorMessage = errorMessage  + "Email Required .\n";
	}

	if (emailID.value!="")
	{
		if (echeck(emailID.value)==false)
		{
				errorMessage =  errorMessage  + "Email Invalid .\n";
				emailID.value=""
		}
	}
	
	if ((Mileage.value!="") && ((parseInt(Mileage.value) < 0)||(parseInt(Mileage.value) > 1000000)))
	{
		errorMessage = errorMessage  + "Mileage must be between 0 and 1000000 .\n";
	}
	
	if   (errorMessage != "")
	{
			alert("Invalid Information .\n\n"   +   errorMessage);		
			return false
	}
	
	//document.frmSample.submit()
	//return false
}


function checkBrochure()
{
	CommitTabCache();
	//alert(document.getElementById("bob").value)
	var FromAddress = document.getElementById("youremail")
	var ToAddress = document.getElementById("recemail")
	var errorMessage= ""	
	if (FromAddress.value=="")
	{
	    errorMessage = errorMessage  + "Your email address is required .\n";
	}

	if (FromAddress.value!="")
	{
		if (echeck(FromAddress.value)==false)
		{
				errorMessage =  errorMessage  + "Your Email Address is Invalid .\n";
				FromAddress.value=""
		}
	}
	
	if (ToAddress.value=="")
	{
	    errorMessage = errorMessage  + "Recipients email address is required .\n";
	}

	if (ToAddress.value!="")
	{
		if (echeck(ToAddress.value)==false)
		{
				errorMessage =  errorMessage  + "Recipients Email Address is Invalid .\n";
				ToAddress.value=""
		}
	}
	
	if   (errorMessage != "")
	{
			alert("Invalid Information .\n\n"   +   errorMessage);		
			return false
	}
}

function echeck(str) 
{

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1)
		{
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		{
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		{
		    //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1)
		 {
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		 {
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1)
		 {
		    //alert("Invalid E-mail ID")
		    return false
		 }
		 if (str.indexOf(" ")!=-1)
		 {
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}
	
 function LTrim( value ) 
 {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function TabClass(tab)
{
  if(!tab)
	return;

  return tab.className;
}

function AdjustTabSpacers(tab, TabStyle)
{
  var TabNum;
  var TabStart;
  var TabStop;

  TabNum = parseInt(tab.id.substr(1,tab.id.length));
  TabStart = document.all("TabSpacer" + TabNum);
  TabStop = document.all("TabSpacer" + (TabNum+1));

  if(TabStart)
  {
    if(TabClass(document.all("t" + (TabNum-1))) == "SelectedTab")
    {
	TabStart.src = "Layout/TabControl/menu.on." + TabStyle + ".gif";
    }
    else
    {
	if(document.all("t" + (TabNum-1)))
	  TabStart.src = "Layout/TabControl/menu.off." + TabStyle + ".gif";
	else
	  TabStart.src = "Layout/TabControl/menu.start." + TabStyle + ".gif";
    }
  }

  if(TabStop) 
  {
    if(TabClass(document.all("t" + (TabNum+1))) == "SelectedTab")
    {
	TabStop.src = "Layout/TabControl/menu." + TabStyle + ".on.gif";
    }
    else
    {
    if(document.all("t" + (TabNum + 1))) 
	TabStop.src = "Layout/TabControl/menu." + TabStyle + ".off.gif";
    else
	TabStop.src = "Layout/TabControl/menu." + TabStyle + ".end.gif";
    }
  }


}

function TabOver(tab)
{

  if(tab.className == "Tab")
  {
	tab.className = "TabOver";
	AdjustTabSpacers(tab, "mouseover");
  }
  else if(tab.className == "TabOver")
  {
	tab.className = "Tab";
	AdjustTabSpacers(tab, "off");
  }

}

function CommitTabCache()
{
  TabFirstInit();

  tabContentID = currentTab.id + "Contents";
  tabContent = document.all(tabContentID);
  tabContent.innerHTML = tabContents.innerHTML;
}

