// Virtual-Showroom.com stocklist functions. Copyright virtual-showroom.com 2010
// Written by Andy Bryan

// General Functions

function doNothing(){
	//
}

function setCookie(c_name,value,expires)
{
	var today = new Date();
	today.setTime( today.getTime() );
	
	expires = expires * 1000 * 60 * 60 *24;
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = c_name + "=" + escape(value) +
	((expires_date == null) ? "" : ";expires=" + expires_date.toGMTString() + "; path=/");
}

function deleteCookie(c_name){
	setCookie(c_name, "", -1);
}

function getCookie(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 null;
}

function isEven(value){
	if (value%2 == 0)
		return true;
	else
		return false;
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	pence = num%100;
	num = Math.floor(num/100).toString();
	if(pence<10)
	pence = "0" + pence;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '&#163;' + num);
}

function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

// Stock list listing page functions

function itemLength(x){
	if(x.length < 1){
		return false;
	} else {
		return true;
	}
}

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

function checkEmail(x) { // Email format validation
	email = x.value
	AtPos = email.indexOf("@")
	StopPos = email.lastIndexOf(".")
	errorText = ""
	if (email == "") {
		errorText = "email field does not appear to contain a valid email address"
	}
	if (AtPos == -1 || StopPos == -1) {
		errorText = "email field does not appear to contain a valid email address"
	}
	if (StopPos < AtPos) {
		errorText = "email field does not appear to contain a valid email address"
	}
	if (StopPos - AtPos == 1) {
		errorText = "email field does not appear to contain a valid email address"
	}
}

function checkFormItem(x,y){ // Form validation
	if(itemLength(x.value)){
		if(y == "email"){
			checkEmail(x);
			if(errorText.length > 0){
				x.className = "errorHighlight";
				errorTag = true;
			} else {
				x.className = "";
			}
		 } else {
			x.className = "";
		 }
	} else {
		x.className = "errorHighlight";
		errorTag = true;
	}
}

function listPageFormSubmit(x, y){ // Form validation driver function
	errorTag = false;
	errorText = "";
	var targetForm = y + x;
	
	var formName = document[targetForm].name;
	var formEmail = document[targetForm].from_address;
	
	checkFormItem(formName, 'text');
	checkFormItem(formEmail, 'email');
	
	
	if(y.indexOf("tellAFriendForm") != 0){
		var formPhone = document[targetForm].phone;
		checkFormItem(formPhone, 'text');
	} else {
		var toName = document[targetForm].to_name;
		var toEmail = document[targetForm].to_address;
		
		checkFormItem(toName, 'text');
		checkFormItem(toEmail, 'email'); 

	}
	
	if(errorTag){
		alertTxt = "There appears to be missing information from the submission form.\n\nThese fields are marked in red.\n\n";
		if(errorText.length > 0){
			alertTxt = alertTxt + "Also " + errorText;
		}
		alert(alertTxt);
	} else {
		document[targetForm].submit();
	}
}

function toggle(x){
	$("#" + x).slideToggle("fast");
}


function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

function stateLoggin(x){
	var target = document.getElementById(x);
	var answer = target.style.display;
	if(answer == 'table' || answer == ""){
		setCookie(x, "is set", 1);
	} else {
		setCookie(x, null, -1);
	}
}

function dupListCheck(x, y){ // Duplicate vehicle / cookie check
	if(y == 'saveList'){
		cName = 'saveList';
	} else {
		cName = 'recentList';
	}
	var dup = false;
	for(i=0; i<=7; i++){
		var tempCookie = getCookie(cName + i);
		if (tempCookie != null){
			var tempArray = tempCookie.split("~");
			if (parseFloat(x) == parseFloat(tempArray[0])){
				dup = true;
			}
		}
	}
	return dup;
}

function hideAdjuster(){
	$('#financeAdjuster').html("<p>There is no finance adjustment available for this vehicle.</p><p>Please contact us for further information.</p>");
}
