function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

$(document).ready(function() {
	if( $('#opc-cf-invoice').length > 0 ) {
		$("input[name='custom_field\[1\]']").css({position:'absolute', margin:'-2000px 0px 0px -20000px'});
		$("input[name='custom_field\[6\]']").val(getCookie("domain"));
		$("input[name='custom_field\[7\]']").val(getCookie("full_url"));
		$("input[name='custom_field\[8\]']").val(unescape(getCookie("arg")));
	
				
		$.post("/kak/getdate.php",
			function(data) {
				if( data != "error" ) {				
					aData	= data.split("|");
					U_date	= aData[0];
					S_date	= aData[1];
					E_date	= aData[2];		
					D_date	= aData[3];		
					
					$('#opc-cf-invoice .field div.clearfix label').attr('style', 'float:left;display:block;width:100%;');
		
					$('#opc-cf-invoice .field div.clearfix:first').append('<input id="date1" class="date-pick dp-applied" name="ddate" value="'+D_date+'" />');
					$("input[name='custom_field\[1\]']").val(D_date);
					
					$(function()
						{
							$('.date-pick')
								.datePicker(
									{
										createButton:true,
										startDate: S_date,
										endDate: E_date,
										renderCallback:function($td, thisDate, month, year)
										{
											if (thisDate.isWeekend()) {
												$td.addClass('weekend');
												$td.addClass('disabled');
											}
											
											aUdate	= U_date.split(",");
											var compareDate = new Date(thisDate);
											
											for( i = 0; i < aUdate.length; i++ ) {	
											//alert(compareDate.getDate() + "/" + (compareDate.getMonth() + 1)  + "/" + compareDate.getFullYear() + "======" + aUdate[i]);											
												if( compareDate.getDate() + "/" + (compareDate.getMonth() + 1)  + "/" + compareDate.getFullYear() == aUdate[i] ) {
											//alert(compareDate.getDate() + "/" + (compareDate.getMonth() + 1)  + "/" + compareDate.getFullYear() + "======" + aUdate[i]);		
													$td.addClass('disabled');
												}
											}
											
										}
									}
								) 
								.bind('click',
									function()
									{
										$(this).dpDisplay();
										this.blur();
										return false;
									}
								)
								.bind('dateSelected',
									function(e, selectedDate, $td)
									{
										var myDate = new Date(selectedDate);
										$("input[name='ddate']").val(myDate.getDate() + "/" + (myDate.getMonth() + 1)  + "/" + myDate.getFullYear());
										$("input[name='custom_field\[1\]']").val(myDate.getDate() + "/" + (myDate.getMonth() + 1)  + "/" + myDate.getFullYear());
									}
								);
						});
				}
			}
		);
	}
});

function getDomain(url) {
return url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/)[2];
}

function gup( name, url )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( url );
  if( results == null )
    return "";
  else
    return results[1];
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}



// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( getCookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
	
function parseURL(url)
{

dm = getCookie("domain");

if (dm == null) 
  {
  var domain = '';

  var base_url = Array();
	
  base_url = url.split('.');


  var qarg = gup('q',url);

  var parg = gup('p',url);


  param = unescape(qarg)+unescape(parg);

  Set_Cookie( 'version', 'e', 30, '/', '', '' );

  // construct domain
  domain = getDomain(url);

  // set the domain
  
  Set_Cookie( 'domain', domain, 30, '/', '', '' );

  // set the full url
   Set_Cookie( 'full_url', url, 30, '/', '', '' );

  // set the parameter if available
  if(param != null)
    {
    
	 Set_Cookie( 'arg', param, 30, '/', '', '' );
    } 
  }
}

if (document.referrer != '')
{
	parseURL(document.referrer);
}


if (gup('p',document.location) == 'completed')
	{
		utma =getCookie("store_utma");
		utmb =getCookie("store_utmb");
		utmz =getCookie("store_utmz");

		if (utma.length > 0 )
			{
	
			Delete_Cookie('__utmz','/','.keevilandkeevil.co.uk'); 

			Set_Cookie( '__utma', utma, 30, '/', '.keevilandkeevil.co.uk', '' );
			Set_Cookie( '__utmb', utmb, 30, '/', '.keevilandkeevil.co.uk', '' );

			//Set_Cookie( '__utmz', 't:'+utmz, 30, '/', '.keevilandkeevil.co.uk', '' );
			Set_Cookie( '__utmtest', 't:'+utmz, 30, '/', '.keevilandkeevil.co.uk', '' );

			}

	}
else
	{

		utma =getCookie("__utma");
		utmb =getCookie("__utmb");
		utmz =getCookie("__utmz");

		if (utma != "")
			Set_Cookie( 'store_utma', utma, 30, '/', '', '' );
		if (utmb != "")
			Set_Cookie( 'store_utmb', utmb, 30, '/', '', '' );
		if (utmz != "")
			Set_Cookie( 'store_utmz', utmz, 30, '/', '', '' );

	}


	
