﻿_ajaxConfig = {'_cfscriptLocation':'/cfcompantes/BLL/AjaxFavorite.cfc', '_jsscriptFolder':'/js/aj'};

function include(script_filename) {
    document.write('<' + 'script');
    document.write(' language="javascript"');
    document.write(' type="text/javascript"');
    document.write(' src="' + script_filename + '">');
    document.write('</' + 'script' + '>');
}

function include_dom(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}


include(_ajaxConfig._jsscriptFolder + '/engine.js');
include(_ajaxConfig._jsscriptFolder + '/util.js');
//include_once(_jsscriptFolder + '/errorHandler.js');
include(_ajaxConfig._jsscriptFolder + '/wddx.js');

//include(_ajaxConfig._jsscriptFolder + '/ajax.js');
function createFavorite(ProductID,SiteID,urlstring,gourl)	{
	if(ProductID<=0){alert("Item does not exist.");return false;}
	// send request to CF
	//DWRUtil.useLoadingMessage("正在添加收藏……");
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'fCreateFavorite', ProductID,SiteID,urlstring,function(result){returnFavoriteMessages(result, gourl);});
}
function returnFavoriteMessages(result,gourl){
	if(1==result){alert("1 item added to your wish list"); }
	else if(0==result)alert("Oops, failed to add to wish list.");	
	else if(2==result){alert("This item is already in your wish list."); }
	else {var bool = confirm("Sign in first before adding this item to Wish List."); if(bool)window.location = gourl;}
}

function WishListCall(ProductID, WebSiteID, CallBackFun)
{	
	if(ProductID <= 0)
	{
		alert("Item does not exist.");
		return false;
	}
	var Array_Infor = new Array();
	Array_Infor[0] = WebSiteID;
	Array_Infor[1] = ProductID;
	
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'GetFavoriteState', ProductID, WebSiteID, function(ReturnValue){WishListDialogBI(ReturnValue, CallBackFun, Array_Infor);});
}

function WishListAdd(ActionType, ProductID, WebSiteID, CallBackFun)
{
	var Array_Infor = new Array();
	Array_Infor[0] = ActionType;
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'AddToWishList', ProductID, WebSiteID, function(ReturnValue){WishListDialogBI(ReturnValue, CallBackFun, Array_Infor);});
}

function WishListAdd(ProductID, WebSiteID, CallBackFun)
{
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'AddToWishList', ProductID, WebSiteID, function(ReturnValue){WishListDialogBI(ReturnValue, CallBackFun);});
}

function WishListDialogBI(ReturnValue, CallBackFun, Array_Infor)
{
	var Temp = ReturnValue.toLowerCase();
	if(ReturnValue.toLowerCase() == "exists")
	{
		alert("This item is already in your wish list.");
	}	
	else if(ReturnValue.toLowerCase() == "needlogin")
	{
		var bool = confirm("Sign in first before adding this item to Wish List.");
		if(bool)
		{
			window.location = "/UserLogin/";
		}
	}
	else if(ReturnValue.toLowerCase() == "failure")
	{
		alert("Oops, failed to add to wish list.");
	}
	else if(ReturnValue.toLowerCase().split("|o^_^o|")[0] == "ok")
	{	
		Array_Infor[2] = parseInt(ReturnValue.toLowerCase().split("|o^_^o|")[1]) + 1;
		CallBackFun(Array_Infor);
	}
	else if(ReturnValue.toLowerCase().split("|o^_^o|")[0] == "addtowishlist")
	{
		if(parseInt(ReturnValue.toLowerCase().split("|o^_^o|")[1]) > 0)
		{
			CallBackFun();
		}
		else
		{
			CallBackFun();
			alert("Oops, failed to add to wish list.");
		}
	}
	else
	{
		alert("Oops, failed to add to wish list.");
	}
	
}

function DrawImage(ImgD,iwidth,iheight){
		var image = new Image();
		image.src = ImgD.src;
		if(image.width > 0 && image.height >0){
			if(image.width / image.height >= iwidth / iheight){
				if(image.width >= iwidth){
					ImgD.width = iwidth;
					ImgD.height = (image.height * iwidth) / image.width;
					document.getElementById(ImgD.id).style.paddingLeft = 0 + "px";
					document.getElementById(ImgD.id).style.paddingTop = 0+ "px";
					document.getElementById(ImgD.id).style.paddingTop = ( iheight - ((image.height * iwidth) / image.width) ) / 2 + "px";
				}else{					
					document.getElementById(ImgD.id).style.paddingLeft = (iwidth - image.width ) / 2 + "px";
					document.getElementById(ImgD.id).style.paddingTop = (iheight -image.height) / 2 + "px";
					ImgD.width=image.width;  
					ImgD.height=image.height;
				}
			}
			else{
				if(image.height >= iheight){
					ImgD.height = iheight;
					ImgD.width = (image.width*iheight) / image.height;
					document.getElementById(ImgD.id).style.paddingTop = 0+ "px";
				}else{					
					document.getElementById(ImgD.id).style.paddingLeft = (iwidth - image.width ) / 2 + "px";
					document.getElementById(ImgD.id).style.paddingTop = (iheight -image.height) / 2 + "px";
					ImgD.width = image.width;  
					ImgD.height = image.height;
				}
			 }
		}
	}
