//TCP Javascsripts
function clearThisField(field)
	{
		if(field.value==field.defaultValue)
			field.value="";
	}

function verify() 
	{
		var themessage = "You are required to complete the following fields: ";
		if (document.ContactForm.name.value=="")
			{
				themessage = themessage + " - Name";
			}
		if (document.ContactForm.address.value=="") 
			{
				themessage = themessage + " -  Address";
			}
		if (document.ContactForm.city.value=="")
			{
				themessage = themessage + " -  City";
			}
		if (document.ContactForm.state.value=="")
			{
				themessage = themessage + " -  State";
			}
		if (document.ContactForm.postal.value=="")
			{
				themessage = themessage + " -  Postal Code";
			}
		if (document.ContactForm.email.value=="")
			{
				themessage = themessage + " -  Email";
			}
		var emailA = document.ContactForm.email;
		if (emailA.value != ""||emailA.value !="")
		{
			if(emailCheck(emailA.value)==false)
			{
				//alert("Invalid Email Address");
				emailA.value="";
				emailA.focus();
				return false;
			}

		}
		//alert if fields are empty and cancel form submit
		if (themessage == "You are required to complete the following fields: ")
			{
				document.ContactForm.submit();
			}
			else {
					alert(themessage);
					return false;
					}
	}

function emailCheck(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 Address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail Address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail Address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

 		 return true					
	}


var popupStatus = 0;
function loadPopup()
	{
		if(popupStatus == 0)
			{
				$("#backgroundPopup").css({
										  "opacity": "0.7"
										  });
				$("#backgroundPopup").fadeIn("slow");
				$("#popupContact").fadeIn("slow");
				popupStatus = 1;
			}
	}


function disablePopup()
	{
		if(popupStatus==1)
			{
				$("#backgroundPopup").fadeOut("slow");
				$("#popupContact").fadeOut("slow");
				popupStatus = 0;
			}
	}

function centerPopup()
	{
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = $("#popupContact").height();
		var popupWidth = $("#popupContact").width();
		
		$("#popupContact").css({
							   "position": "absolute",
							   "top": windowHeight/2-popupHeight/2,
							   "left": windowWidth/2-popupWidth/2
							   });
		$("#backgroundPopup").css({
								  "height": windowHeight
								  });
	}

$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});

	//AJAX AD LOGIN By KPS 9-16-2009
	$("#loginForm").submit(function()
	{
		$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
		$.post("includes/authlogin.php",{ user_name:$('#username').val(),password:$('#pass').val(),rand:$('#leadKey').val() } ,function(data)
        {
		  if(data=='followup')
		  {
		  	$("#msgbox").fadeTo(200,0.1,function()
			{ 
			   $(this).html('Successfully Validated').addClass('messageboxok').fadeTo(900,1,
              function()
				  { 
					 //document.location='http://www.twocpack.com/';
				  });
			  });
		  }
		  //If account is locked
		  if(data=='badcredent') //if correct login detail
		  {
		  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Invalid Username/Password!!').addClass('messageboxerror').fadeTo(900,1);
			  
			});
		  }
		  if(data=='dberror') //if correct login detail
		  {
		  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('There was DB error IT has been notified!').addClass('messageboxerror').fadeTo(900,1);
			  
			});
		  }
		  else 
		  {
		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Contact IT Dept').addClass('messageboxerror').fadeTo(900,1);
			});		
          }
		});
 		return false; //not to post the  form physically
	});

	//End AJAX
	
	
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});

function verify2() 
	{
		var themessage = "You are required to complete the following fields: ";
		if (document.loginForm.pass.value=="")
			{
				themessage = themessage + " - Password";
			}
		if (document.loginForm.username.value=="")
			{
				themessage = themessage + " -  Username";
			}
		var emailA = document.loginForm.username;
		if (emailA.value != ""||emailA.value !="")
		{
			if(emailCheck(emailA.value)==false)
			{
				//alert("Invalid Email Address");
				emailA.value="";
				emailA.focus();
				return false;
			}

		}
		//alert if fields are empty and cancel form submit
		if (themessage == "You are required to complete the following fields: ")
			{
				document.loginForm.submit();
			}
			else {
					alert(themessage);
					return false;
					}
	}

