function ProcessAjax(type, v)
{
	var ajax = GetObjects();
	var url = "/Common/XmlHttpRequest.aspx?type=" + type + "&checkvalue=" + v;
	ajax.open("POST", url, false);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	ajax.send(v);
	
	return ajax.responseText;
}

function WishListDeleteGroup()
{
	var defaultid = $Obj('ctl00_CPH_hidDefaultListID').value;
	var curid = $("ctl00_CPH_hidListID").value;
	var v = curid + "|" + defaultid;
	v = escape(v);
	var result = ProcessAjax("WishListDeleteGroup", v);
	if(result == "true")
	{
		if(defaultid != curid)
		    window.location.href = "/WishList/?id=" + defaultid;
		else
			window.location.href = "/WishList/";
	}
	else if(result == "timeout")
	{
		CloseWindow();
		$("spanmsg").innerHTML = "Sorry, session timeout, please <a href='/MyPaintingMania/SignIn.aspx' class='blue'>Sign In</a> again.";
		ShowWindow(350, 130, 'msgdone');
	}
	else
	{
		window.location.href = "/Common/Message.aspx";
	}
}

function WishListMakePublicFG()
{
	var v = $("ctl00_CPH_hidListID").value;
	v = escape(v);
	var result = ProcessAjax("WishListMakePublicFG", v);
	if(result == "true")
	{
	    window.location.href = "/WishList/?id=" + v;
	}
	else if(result == "timeout")
	{
	    $("spanmsg").innerHTML = "Sorry, session timeout, please <a href='/MyPaintingMania/SignIn.aspx' class='blue'>Sign In</a> again.";
		ShowWindow(350, 130, 'msgdone');
	}
	else
	{
		window.location.href = "/Common/Message.aspx";
	}
}

function WishListMakeDefaultList()
{
	var v = $("ctl00_CPH_hidListID").value;
	v = escape(v);
	var result = ProcessAjax("WishListMakeDefaultList", v);
	if(result == "true")
	{
		window.location.href = "/WishList/?id=" + v;
	}
	else if(result == "timeout")
	{
	    $("spanmsg").innerHTML = "Sorry, session timeout, please <a href='/MyPaintingMania/SignIn.aspx' class='blue'>Sign In</a> again.";
		ShowWindow(350, 130, 'msgdone');
	}
	else
	{
		window.location.href = "/Common/Message.aspx";
	}
}

function WishListUpdateInfo()
{
	var id = $("ctl00_CPH_hidListID").value;
	var listname = $("ctl00_CPH_hidListName").value; 
	var listdesc = $("ctl00_CPH_hidListDesc").value;
	
	var v = escape(id + "^" + listname + "^" + listdesc);
	
	var result = ProcessAjax("WishListUpdateInfo", v);
	
	if(result == "true")
	{
		window.location.href = "/WishList/?id=" + id;
	}
	else if(result == "timeout")
	{
		CloseWindow();
		$("spanmsg").innerHTML = "Sorry, session timeout, please <a href='/MyPaintingMania/SignIn.aspx' class='blue'>Sign In</a> again.";
		ShowWindow(350, 130, 'msgdone');
	}
	else
	{
		window.location.href = "/Common/Message.aspx";
	}
}

function SavaItem(id) {
	var strid = id.substring(0, id.lastIndexOf('_'));
	var Code = $(strid + "_hidCode").value;
	var Desired = $(strid + "_txtDesired").value;
	var Received = $(strid + "_txtReceived").value;
	var Priority = $(strid + "_ddlPriority").value;
	var Comment = $(strid + "_txtComment").value;
	
	var v = Code + "^" + Desired + "^" + Received + "^" + Priority + "^" + escape(Comment);
	v = escape(v);
	var result = ProcessAjax("WishListSaveItem", v);
	
	if(result == "true")
	{
		ShowWindow(350, 130, 'msgdone');
	}
	else if(result == "timeout")
	{
	    $("spanmsg").innerHTML = "Sorry, session timeout, please <a href='/MyPaintingMania/SignIn.aspx' class='blue'>Sign In</a> again.";
		ShowWindow(350, 130, 'msgdone');
	}
	else
	{
		window.location.href = "/Common/Message.aspx";
	}
}

function ConfirmDeleteItem(id)
{
	$Obj('hidCode2').value = id;
	var strid = id.substring(0, id.lastIndexOf('_'));
	$Obj('spanProductName').innerHTML = '"' + $(strid + "_hidProductName").value + '"';
	ShowWindow(350, 130, 'msgdeleteitem');
}

function ShowMoveCopyWindow(id)
{
	$Obj('hidCode2').value = id;
	var strid = id.substring(0, id.lastIndexOf('_'));
	$Obj('spanProductName2').innerHTML = '"' + $(strid + "_hidProductName").value + '"';
	var catid = $(strid + "_hidCatID").value;
	var pid = $(strid + "_hidProductID").value;
	if($("hidNewFG").value == "")
	{
		$("ctl00_CPH_ddlListGroup").options.add(new Option("new Wish List...", "new")); 
		$("hidNewFG").value = "1";
	}

	$("hidListIDForDDL").value = $("ctl00_CPH_ddlListGroup").value;
	//$Obj('ctl00_CPH_hidListNameForDDL').value = $("ctl00_CPH_ddlListGroup").options[$("ctl00_CPH_ddlListGroup").selectedIndex].text;

	ShowWindow(350, 170, 'msgmovecopyitem');
}

function SaveMoveCopy()
{
    var ListID = $("hidListIDForDDL").value;
	var id = $("hidCode2").value;
	var strid = id.substring(0, id.lastIndexOf('_'));
	var code = $(strid + "_hidCode").value;
	var catid = $(strid + "_hidCatID").value;
	var pid = $(strid + "_hidProductID").value;
	var v = ListID + "|" + catid + "|" + pid;
	
	if(ListID == "new")
	{
		window.location.href = "Create.aspx?create=true&cat=" + catid + "&pid=" + pid;
		return;
	}
	
	var result;
	var type = $Obj('hidMoveCopy').value;

	if(type == "Move")
	{
		v = code + "|"  + v;
		v = escape(v);
		result = ProcessAjax("WishListMoveItem", v);
	}
	else if(type == "Copy")
	{
		v = escape(v);
		result = ProcessAjax("WishListCopyItem", v);
	}
	
	if(result == "true")
	{
		if(type == "Copy")
		{
			CloseWindow();
			$("spanmsg").innerHTML = "Data Successfully Copied!";
			ShowWindow(350, 130, 'msgdone');
		}
		else
		{
			window.location.href = "/WishList/?id=" + ListID;
			return;
		}
	}
	else if(result == "taken")
	{
		$("spanmsg").innerHTML = "This item already exists in this list";
		CloseWindow();
		ShowWindow(350, 130, 'msgdone');
	}
	else if(result == "failed")
	{
		window.location.href = "/Common/Message.aspx";
	}
}

function ShowAddListWindow()
{
    if ($("ctl00_CPH_hidSignInFG").value == "")
	{
	    $("spanmsg").innerHTML = "Please <a href='/MyPaintingMania/SignIn.aspx' class='blue'>Sign In</a> to Add this list to your Wish List.";
		ShowWindow(350, 130, 'msgdone');
		return;
	}
	
	$("hidAddItemFG").value = "list";
	
	if($("hidNewFG").value == "")
	{
		$("ctl00_CPH_ddlListGroup").options.add(new Option("new Wish List...", "new")); 
		$("hidNewFG").value = "1";
	}

	$("hidListIDForDDL").value = $("ctl00_CPH_ddlListGroup").value;
	$Obj('spanProductName').innerHTML = '"' + $Obj('ctl00_CPH_lblDisplayName').innerHTML + "' " + $Obj('ctl00_CPH_lblListName').innerHTML + '"';
	ShowWindow(400, 150, 'msgaddtowishlist');
}

function ShowAddItemtoWishListWindow(id)
{
    if ($("ctl00_CPH_hidSignInFG").value == "")
	{
	    $("spanmsg").innerHTML = "Please <a href='/MyPaintingMania/SignIn.aspx' class='blue'>Sign In</a> to Add this item to your Wish List.";
		ShowWindow(350, 130, 'msgdone');
		return;
	}
	
	$("hidAddItemFG").value = "item";
	
	$Obj('hidCode2').value = id;
	var strid = id.substring(0, id.lastIndexOf('_'));
	$Obj('spanProductName').innerHTML = '"' + $(strid + "_hidProductName").value + '"';
	var catid = $(strid + "_hidCatID").value;
	var pid = $(strid + "_hidProductID").value;
	if($("hidNewFG").value == "")
	{
		$("ctl00_CPH_ddlListGroup").options.add(new Option("new Wish List...", "new")); 
		$("hidNewFG").value = "1";
	}

	$("hidListIDForDDL").value = $("ctl00_CPH_ddlListGroup").value;
	ShowWindow(400, 150, 'msgaddtowishlist');
}

function SaveAddtoWishList()
{
	var v;
	var result;
	
	if($("hidAddItemFG").value == "item")
	{
	    var ListID = $("hidListIDForDDL").value;
		var id = $("hidCode2").value;
		var strid = id.substring(0, id.lastIndexOf('_'));
		var catid = $(strid + "_hidCatID").value;
		var pid = $(strid + "_hidProductID").value;
		v = ListID + "|" + catid + "|" + pid;
		
		if(ListID == "new")
		{
			window.location.href = "Create.aspx?create=true&cat=" + catid + "&pid=" + pid;
			return;
		}
		
		v = escape(v);
		result = ProcessAjax("WishListCopyItem", v);
	}
	else
	{
	    v = $("ctl00_CPH_hidListID").value + "," + $("hidListIDForDDL").value;
	    if ($("hidListIDForDDL").value == "new")
		{
			window.location.href = "Create.aspx?create=true&lid=" + $("ctl00_CPH_hidListID").value;
			return;
		}
		v = escape(v);
		result = ProcessAjax("WishListAddList", v);
	}

	if ($("hidAddItemFG").value == "item")
	{
		if(result == "true")
		{
			CloseWindow();
			$("spanmsg").innerHTML = "Data Successfully Added to your Wish List!";
			ShowWindow(350, 130, 'msgdone');
		}
		else if(result == "taken")
		{
			$("spanmsg").innerHTML = "This item already exists in you Wish List";
			CloseWindow();
			ShowWindow(350, 130, 'msgdone');
		}
		else if(result == "failed")
		{
			window.location.href = "/Common/Message.aspx";
			return;
		}
	}
	else
	{
		if(result == "")
		{
			CloseWindow();
			$("spanmsg").innerHTML = "Data Successfully Added to your Wish List!";
			ShowWindow(350, 130, 'msgdone');
		}
		else
		{
			CloseWindow();
			var str = result.split('|');
			var row = str[0];
			var msg = str[1];
			
			$("spanmsg").innerHTML = msg;
			var h = eval(parseInt(row) * 24 + 124);
			ShowWindow(350, h, 'msgdone');
		}
	}
}

function DeleteItem()
{
	var ListID = $("ctl00_CPH_hidListID").value;
	var id = $("hidCode2").value;
	var strid = id.substring(0, id.lastIndexOf('_'));
	var Code = $(strid + "_hidCode").value;
	var v = ListID + "^" + Code;
	v = escape(v);
	var result = ProcessAjax("WishListDeleteItem", v);

	if(result == "true")
	{
		window.location.href = "/WishList/?id=" + ListID;
	}
	else if(result == "timeout")
	{
		CloseWindow();
		$("spanmsg").innerHTML = "Sorry, session timeout, please <a href='/MyPaintingMania/SignIn.aspx' class='blue'>Sign In</a> again.";
		ShowWindow(350, 130, 'msgdone');
	}
	else
	{
		window.location.href = "/Common/Message.aspx";
	}
}

function SearchWishList()
{
	var SearchText = $("ctl00_CPH_hidListID").value + "|" + $("ctl00_CPH_ddlReveal").value + "|" + $("ctl00_CPH_ddlCategory").value + "|" + $("ctl00_CPH_ddlSort").value;
	window.location.href = "/WishList/?q=" + SearchText;
}

function SearchWishListByFind()
{
	var SearchText = $("ctl00_CPH_hidListID").value + "|" + $("ctl00_CPH_ddlReveal").value + "|" + $("ctl00_CPH_ddlCategory").value + "|" + $("ctl00_CPH_ddlSort").value;
	window.location.href = "Entry.aspx?ref=" + $("hidRefName").value + "&q=" + SearchText;
}

function FindList(v)
{
	if(Trim(v) == "")
	{
		alert("Please input name or email.");
		return;
	}
	window.location.href = "Search.aspx?ref=" + escape(v);
}

function checkEmailInfo() {
    if (Trim(document.getElementById("ctl00_CPH_txtFriendEmail").value) == "") {
        document.getElementById("ctl00_CPH_txtFriendEmail").focus();
        alert("Email of recipient(s) can not be empty.")
        return false;
    }
    else if (Trim(document.getElementById("ctl00_CPH_txtName").value) == "") {
        document.getElementById("ctl00_CPH_txtName").focus();
        alert("Please input your name.")
        return false;
    }
    else if (Trim(document.getElementById("ctl00_CPH_txtYourEmail").value) == "") {
        document.getElementById("ctl00_CPH_txtYourEmail").focus();
        alert("Please input your email.")
        return false;
    }

    var emailPat = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
    var matchArray = Trim(document.getElementById("ctl00_CPH_txtYourEmail").value).match(emailPat);
    if (matchArray == null) {
        document.getElementById("ctl00_CPH_txtYourEmail").focus();
        alert("Please input valid email.")
        return false;
    }
    return true;
}