var flexApp;
var myTimeout;
/*entry with link*/
function processVisit(value)
{
	flexApp = getFlexApp("xss");
	//compare current cookie with passed value...determine if fresh start needed
	var temp = getMyCookie("performance_id");
	if(temp != value)
	{
		setMyCookie("performance_id",value,365);
		setMyCookie("session",sid);
		getTrackInfo();
	}
	else
	{
		var sess = getMyCookie("session");
		if(sid != sess)
		{
			setMyCookie("session",sid,1);
			insertPerformanceData(getMyCookie("performance_id"),getMyCookie("auth_code"),getMyCookie("dealership_name"),getMyCookie("customer_name"),getMyCookie("customer_title"),getMyCookie("office_phone"),getMyCookie("cell_phone"),getMyCookie("email"));
		}	
		else
		{
			logAction(getMyCookie("visit_id"),"nav : "+document.URL);
		}	
	}
}
/*entry without link*/
function loadJSVars()
{
	flexApp = getFlexApp("xss");
	findSubDomain();
	var sess = getMyCookie("session");
	if(sid != sess)
	{
		setMyCookie("session",sid,1);
		//test for cookie
		var temp = getMyCookie("visit_id");
		if(temp!="")
		{
			insertPerformanceData(getMyCookie("performance_id"),getMyCookie("auth_code"),getMyCookie("dealership_name"),getMyCookie("customer_name"),getMyCookie("customer_title"),getMyCookie("office_phone"),getMyCookie("cell_phone"),getMyCookie("email"));
		}
		else
		{
			insertPerformanceData("","","","","","","","");
		}
	}
	else
	{
		logAction(getMyCookie("visit_id"),"nav : "+document.URL);
	}
}
function displayDialog1()
{	
	//load form from cookie
	var current = document.getElementById("theDealerForm");
	current.dealership.value = getMyCookie("dealership_name");
    current.name.value = getMyCookie("customer_name");
    current.title.value = getMyCookie("customer_title");
    current.office.value = getMyCookie("office_phone");
    current.cell.value = getMyCookie("cell_phone");
    current.email.value = getMyCookie("email");	
    YAHOO.example.container.dialog1.show();
}
function saveDealerInformation()
{
	if(validateForm())
	{
		//set cookies to capture any changes
		var current = document.getElementById("theDealerForm");
		setMyCookie("dealership_name",current.dealership.value,365);
		setMyCookie("customer_name",current.name.value,365);
		setMyCookie("customer_title",current.title.value,365);
		setMyCookie("office_phone",current.office.value,365);
		setMyCookie("cell_phone",current.cell.value,365);
		setMyCookie("email",current.email.value,365);
    	//dispatch save
    	flexApp.fUpdateVisitData(getMyCookie("visit_id"),getMyCookie("dealership_name"),getMyCookie("customer_name"),getMyCookie("customer_title"),getMyCookie("office_phone"),getMyCookie("cell_phone"),getMyCookie("email"),"N","updateComplete");
		//fUpdateVisitData(getMyCookie("visit_id"),getMyCookie("dealership_name"),getMyCookie("customer_name"),getMyCookie("customer_title"),getMyCookie("office_phone"),getMyCookie("cell_phone"),getMyCookie("email"),"N",updateComplete);
		return false;
	}
	try
	{
		YAHOO.example.container.dialog1.show();    
	}
	catch(e)
	{
		return false;
	}
}	
function updateComplete(value)
{
	try
	{
		YAHOO.example.container.dialog1.hide(); 
	}
	catch (e)
	{}
	/*
	if(req.readyState == 4)
	{
		if(req.status == 200)
		{
			if(req.responseText == "true")
			{
				alert("Thank you for registering your information. \nWe will contact you soon.");
			}
			else
			{
				alert("Error saving information.  Please contact technical support.");
			}
		}
		else
		{
			alert("Error: " + req.statusText);
		}
	}
	 */
if(value == "true")
    {
    	alert("Thank you for registering your information. \nWe will contact you soon.");
    }
    else
    {
    	alert("Error saving information.  Please contact technical support.");
    }	 
}
function cancelInfoSubmit()
{
	YAHOO.example.container.dialog1.hide();		
}
function insertPerformanceData(performance_id,auth_code,dealership_name,customer_name,customer_title,office_phone,cell_phone,email)
{
	try
	{		
		clearTimeout(myTimeout);
		flexApp.fInsertVisitData("akamata",sid,ip_address,performance_id,auth_code,dealership_name,customer_name,customer_title,office_phone,cell_phone,email,"N",getMyCookie("affiliate"),"insertPerformanceComplete");
		//fInsertVisitData("akamata",sid,ip_address,performance_id,auth_code,dealership_name,customer_name,customer_title,office_phone,cell_phone,email,"N",getMyCookie("affiliate"),insertPerformanceComplete);
	}
	catch(e)
	{
		flexApp = getFlexApp("xss");
		myTimeout = setTimeout("insertPerformanceData('"+performance_id+"','"+auth_code+"','"+dealership_name+"','"+customer_name+"','"+customer_title+"','"+office_phone+"','"+cell_phone+"','"+email+"','insertPerformanceComplete')", 100);
	}
}
function insertPerformanceComplete(value)
{
	if(value != "Error")
	{
		setMyCookie("visit_id",value,365);
		logAction(getMyCookie("visit_id"),"login");
		logAction(getMyCookie("visit_id"),"nav : "+document.URL);
	}
	/*
	if(req.readyState == 4)
	{
		if(req.status == 200)
		{
			setMyCookie("visit_id", req.responseText, 365);
			logAction(getMyCookie("visit_id"),"login");
			logAction(getMyCookie("visit_id"),"nav : "+document.URL);
		}
		else
		{
			alert("Error: " + req.statusText);
		}
	}
	*/
}
/*  
	!!!!!!!!!!!  DB Functions    !!!!!!!!!!!!!!!
*/	
function logAction(visit_id, action)
{
	try
	{
		clearTimeout(myTimeout);
		var temp = getMyCookie("auth_code");
		flexApp.fLogNavigation(temp, visit_id, action, ip_address, sid,"akamata","");
		//fLogNavigation(temp, visit_id, action, ip_address, sid,"akamata");
	}
	catch(e)
	{
		if(flexApp==null){
			flexApp = getFlexApp("xss");
			myTimeout = setTimeout("delayAction('"+visit_id+"','"+action+"')", 1000);	
		}
		else
			myTimeout = setTimeout("logAction('"+visit_id+"','"+action+"')", 100);		
	}
}
function delayAction(visit_id,action)
{
	clearTimeout(myTimeout);
	logAction(visit_id,action);
}
function getTrackInfo()
{
	try
	{
		flexApp.fGetTrackInformation(performance_id, ip_address, sid,"receiveDealerData","akamata");
		//fGetTrackInformation(performance_id, ip_address, sid,"akamata",receiveDealerData);
	}
	catch(e)
	{
		if(flexApp==null){
			flexApp = getFlexApp("xss");
			myTimeout = setTimeout("getTrackInfo();", 1000);	
		}
		else
			myTimeout = setTimeout("getTrackInfo();", 100);
	}
}
function delayTrack()
{
	clearTimeout(myTimeout);
	getTrackInfo();
}

function receiveDealerData(obj)
{
	/*
	if(req.readyState == 4)
	{
		if(req.status == 200)
		{
			alert(req.responseText);
			eval(req.responseText);
			visit_id = obj.visit_id;
			setMyCookie("auth_code", obj.auth_id,365);
			setMyCookie("visit_id", obj.visit_id,365);
			setMyCookie("dealership_name",obj.dealership_name,365);
			setMyCookie("customer_name",obj.customer_name,365);
			setMyCookie("customer_title",obj.customer_title,365);
			setMyCookie("office_phone",obj.office_phone,365);
			setMyCookie("cell_phone",obj.cell_phone,365);
			setMyCookie("email",obj.email,365);	
		}
		else
		{
			alert("Error: " + req.statusText);
		}
	}
	*/
	if(obj.status != "error" && obj.status != "invalid")
	{
		visit_id = obj.visit_id;
		setMyCookie("auth_code", obj.auth_id,365);
		setMyCookie("visit_id", obj.visit_id,365);
	    setMyCookie("dealership_name",obj.dealership_name,365);
	    setMyCookie("customer_name",obj.customer_name,365);
	    setMyCookie("customer_title",obj.customer_title,365);
	    setMyCookie("office_phone",obj.office_phone,365);
	    setMyCookie("cell_phone",obj.cell_phone,365);
	    setMyCookie("email",obj.email,365);	
	}
	else
	{
		//handle error
	}
	
}
function goToDocs()
{
	logAction(getMyCookie("visit_id"),"nav : docs");
	myTimeout = setTimeout("delayDocs();", 500);
}
function delayDocs()
{
	clearTimeout(myTimeout);
	window.location.href = './docs/info.pdf';
}
/*
	!!!!!!!!!!!  Functions for Form Validation   !!!!!!!!!!!				
*/
function validateForm() 
{
    var isValid = true;
    var message = "";
    var current = document.getElementById("theDealerForm");
    if(current.dealership.value == "" || current.dealership.value == "Dealership"){
        isValid = false; 		
        message += "Dealership must be filled out.  ";
    }
    if(current.name.value == "" || current.name.value == "Name"){
        isValid = false;
        message += "Name must be filled out.  ";
    }	
    if(current.office.value == "")
    {
    	isValid = false;
        message += "Office phone must be filled out.  ";
    }	
    if(!validatePhone(current.office.value))
    {
        isValid = false;
        message += "Office Phone is not valid.  ";	
    }		
    if(!validatePhone(current.cell.value))
    {
        isValid = false;
        message += "Cell Phone is not valid.  ";	
    }	
    if(!validateEmail(current.email.value))
    {
        isValid = false;
        message += "Email is not valid.  ";				
    }
    if(!isValid)
        alert(message);
    return isValid;
} 
function validatePhone(fld) 
{
	if(fld=="")
		return true;
    var isValid = true;
    var val = fld;
    var stripped = val.replace(/\D/g, '');     

    if (isNaN(parseInt(stripped))) {
        isValid = false;
    } else if (!(stripped.length == 10)) {
        isValid = false;
    } 
    return isValid;
}
function validateEmail(fld) 
{
    var isValid = true;
    var tfld = trim(fld);                       
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld == "") {
        return true;
    } else if (!emailFilter.test(tfld)) {        
        isValid = false;
    } else if (fld.match(illegalChars)) {
        isValid = false;
    }
    return isValid;
}
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 
/*  
	!!!!!!!!!!!  Helper Functions    !!!!!!!!!!!!!!!
*/	
function getFlexApp(appName) 
{
    if (navigator.appName.indexOf ("Microsoft") !=-1) 
    {
        return window[appName];
    } 
    else 
    {
        return document[appName];
    }
}
function findSubDomain()
{
	var sub = document.URL;
	sub = sub.replace("http://www.","");
	sub = sub.replace("http://","");		
	var subArr = sub.split("/");
	subArr = subArr[0].split(".");
	if(subArr.length>2)
	{
		setMyCookie("affiliate",subArr[0],1);
	}
	else
	{
		setMyCookie("affiliate","",1);
	}
}
/*
	!!!!!!!!!!!  Functions for Cookie Management    !!!!!!!!!!!				
*/
function setMyCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getMyCookie(c_name)
{
	if (document.cookie.length>0)
  	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
    	{ 
    		c_start=c_start + c_name.length+1; 
    		c_end=document.cookie.indexOf(";",c_start);
    		if (c_end==-1) c_end=document.cookie.length;
    		return unescape(document.cookie.substring(c_start,c_end));
    	} 
  	}
	return "";
}
