function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

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);
    }
  }
}

$(function() {
	$(".reviewspage select").each(function() {
		var tmpId3 = $(this).attr('id');
		var v_sb = getCookie(tmpId3);
		$(this).val(v_sb);
	});
	
	$(".reviewspage textarea").each(function() {
		var tmpId8 = $(this).attr('id');
		var v_tab = getCookie(tmpId8);
		$(this).val(v_tab);
	});
	
	$(".reviewspage input[type=text]").each(function() {
		var tmpId7 = $(this).attr('id');
		var v_tb = getCookie(tmpId7);
		$(this).val(v_tb);
	});
		
	$(".reviewspage input[type=checkbox]").each(function() { 
		   var tmpId = $(this).attr('id');
		   var v_cb = getCookie(tmpId);
		   if (v_cb == '1') {
			   $(this).attr('checked', true );
		   }
		} 
	);
	
	$(".reviewspage input[type=radio]").each(function() { 
		   var tmpId5 = $(this).attr('id');
		   var v_rb = getCookie(tmpId5);
		   if (v_rb == '1') {
			   $(this).attr('checked', true );
		   }
		} 
	);
	
	$( "#dialog-modal2" ).dialog({
		autoOpen: false,
		width: 400,
		height: 340,
		modal: true,
		resizable: false,
		position: 'top',
		title: '<div style="font-size:13px; font-weight:bold; text-align:left;" class="greenlink">New User?<br /><a id="link-register" href="#" style="text-decoration:none; font-weight:normal; font-size:12px; color:#5D761D;">Create A New Account</a></div>'
	});
	
	$( "#dialog-modal1" ).dialog({
		autoOpen: false,
		width: 400,
		height: 610,
		modal: true,
		resizable: false,
		position: 'top',
		title: '<div style="font-size:13px; font-weight:bold; text-align:left;" class="greenlink">Already Registered?<br /><a id="link-login" href="#" style="text-decoration:none; font-weight:normal; font-size:12px; color:#5D761D;">Click Here To Sign In</a></div>'
	});
	/*
	$("#submit-btn").click(function() {
		$(".reviewspage select").each(function() {
			var tmpVal1 = $(this).val();
			var tmpId1 = $(this).attr('id');
			setCookie(tmpId1,tmpVal1,2);
		});
		
		$(".reviewspage textarea").each(function() {
			var tmpVal9 = $(this).val();
			var tmpId9 = $(this).attr('id');
			setCookie(tmpId9,tmpVal9,2);
		});
		
		$(".reviewspage input[type=text]").each(function() {
			var tmpVal6 = $(this).val();
			var tmpId6 = $(this).attr('id');
			setCookie(tmpId6,tmpVal6,2);
		});
		
		$(".reviewspage input[type=checkbox]").each(function() { 
			if ($(this).attr('checked') == true) {
				var tmpId2 = $(this).attr('id');
				setCookie(tmpId2,"1",2);
			}
		});
		
		$(".reviewspage input[type=radio]").each(function() { 
			if ($(this).attr('checked') == true) {
				var tmpId4 = $(this).attr('id');
				setCookie(tmpId4,"1",2);
			}
		});
		
	
		
		$( "#dialog-modal2" ).dialog( "close" );
		$( "#frame2" ).hide();
		$( "#dialog-modal1" ).dialog( "open" );
		$( "#frame1" ).show();
		return false;
	}); */
	$("#submit_butt").click(function() {
		var loggedin = false;
		var account_id = "";
		
		var loggedin = getCookie("UserSettings");
		if (loggedin != "" && loggedin != null) {
			var findActId = loggedin.split('account_id');
			var findActIdVal = findActId[1].split('=');
			var findActIdValt = findActIdVal[1].indexOf('&');

			if (findActIdValt != 0) {
				account_id = "true";
			} else {
				account_id = "";
			}
		} else { 
			account_id = "";
		}
		//if logged in, then submit
		if (account_id != null && account_id != "") {
			return true;
		} else {
			$(".reviewspage select").each(function() {
			var tmpVal1 = $(this).val();
			var tmpId1 = $(this).attr('id');
			setCookie(tmpId1,tmpVal1,2);
			});
			
			$(".reviewspage textarea").each(function() {
				var tmpVal9 = $(this).val();
				var tmpId9 = $(this).attr('id');
				setCookie(tmpId9,tmpVal9,2);
			});
			
			$(".reviewspage input[type=text]").each(function() {
				var tmpVal6 = $(this).val();
				var tmpId6 = $(this).attr('id');
				setCookie(tmpId6,tmpVal6,2);
			});
			
			$(".reviewspage input[type=checkbox]").each(function() { 
				if ($(this).attr('checked') == true) {
					var tmpId2 = $(this).attr('id');
					setCookie(tmpId2,"1",2);
				}
			});
			
			$(".reviewspage input[type=radio]").each(function() { 
				if ($(this).attr('checked') == true) {
					var tmpId4 = $(this).attr('id');
					setCookie(tmpId4,"1",2);
				}
			});
			$( "#dialog-modal2" ).dialog( "close" );
			$( "#frame2" ).hide();
			$( "#dialog-modal1" ).dialog( "open" );
			$( "#frame1" ).show();
			return false;
		}
		// else dialog open and return false
	});
	
	$("#link-login").click(function() {
		$( "#dialog-modal1" ).dialog( "close" );
		$( "#frame1" ).hide();
		$( "#dialog-modal2" ).dialog( "open" );
		$( "#frame2" ).show();
		return false;
	});
	
	$("#link-register").click(function() {
		$( "#dialog-modal2" ).dialog( "close" );
		$( "#frame2" ).hide();
		$( "#dialog-modal1" ).dialog( "open" );
		$( "#frame1" ).show();
		return false;
	});
	
	$("#home-reg").click(function() {
		$( "#dialog-modal2" ).dialog( "close" );
		$( "#frame2" ).hide();
		$( "#dialog-modal1" ).dialog( "open" );
		$( "#frame1" ).show();
		return false;
	});
	
	$("#login-btn").live('click', function() {
		$( "#dialog-modal1" ).dialog( "close" );
		$( "#frame1" ).hide();
		$( "#dialog-modal2" ).dialog( "open" );
		$( "#frame2" ).show();
		return false;
	});
});
