var NORMAL_STATE = 4;
var LOGIN_PREFIX_AVAIL = 'check_avail.php?';
var PRODUCT_NAME_AVAIL = 'CheckProName.php?';
var UPDATE_PRODUCT_PRICE	=	'UpdateProPrice.php?'
var UPDATE_CART		=	'updateCart.php?';
var Add_SINGLE_SHIP_ADDRESS	=	'addShipAddress.php?';
var REGISTER_ADDRESS_TYPE	=	'registerAddType.php?';
var UPDATE_SHIP_METHOD	=	'updateShipMethod.php?';
var GIFT_REMINDER_RECORD	=	'reminderList.php?';
var GIFT_CERTIFICATE_RECORD		=	'checkValidGiftCertificate.php?';
var DISCOUNT_COUPON	=	'checkValidDiscountCoupon.php?';
var DEL_FROM_CART	=	'delProductCart.php?';
var CART             ='cart.php?';

function getHTTPObject() {
 var request_o; //declare the variable to hold the object.

 // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        request_o = new XMLHttpRequest();
    // branch for IE/Windows ActiveX version
    }
 else if (window.ActiveXObject) {
        request_o = new ActiveXObject("Microsoft.XMLHTTP");
    }

 return request_o; //return the object
}

var http = getHTTPObject();

function TrimString(sInString) {
	sInString = sInString.replace( /^\s+/g, "" );
	return sInString.replace( /\s+$/g, "" );
}



function CheckAvailability(imgType,imgName)
{
	//alert(LOGIN_PREFIX_AVAIL + 'imgN=' + imgName + '&imgType=' + imgType );
	if ( imgName!='')
	{
	
	http.open('GET', LOGIN_PREFIX_AVAIL + 'imgN=' + imgName + '&imgType=' + imgType , true);
	if(imgType==1)
		http.onreadystatechange = handleHttpCheckAvailability;
	if(imgType==2)
		http.onreadystatechange = handleHttpCheckAvailability2;
	http.send(null);
	}
}

function handleHttpCheckAvailability()
{
	if (http.readyState == NORMAL_STATE) {
		if(http.responseText > 0){
			alert('The image name is already exists. Please enter the different name.');
			document.product_management_frm.smallImgNameVal.focus();
			document.product_management_frm.smallImgNameVal.select();
		}
	}
}
function handleHttpCheckAvailability2()
{
	if (http.readyState == NORMAL_STATE) {
		if(http.responseText > 0){
			alert('The image name is already exists. Please enter the different name.');
			document.product_management_frm.bigImgNameVal.focus();
			document.product_management_frm.bigImgNameVal.select();
		}
	}
}

function CheckAvailabilityName(proName)
{
//	alert(PRODUCT_NAME_AVAIL + 'name=' + proName );
	http.open('GET', PRODUCT_NAME_AVAIL + 'name=' + proName, true);
	http.onreadystatechange = handleHttpCheckName;
	http.send(null);
}
function handleHttpCheckName()
{
	if (http.readyState == NORMAL_STATE) {
		if(http.responseText > 0){
			alert('The product name is already exists. Please enter the different name.');
			document.product_management_frm.nameVal.focus();
			document.product_management_frm.nameVal.select();
			
		}
	}
}



// to update the price on detail page

function updatePrice(pid,discount,potType,yearOfOrchids,salePrice)
{
	//alert(UPDATE_PRODUCT_PRICE  + 'pid=' + pid + '&discount=' + discount + '&potType=' + potType + '&yearOfOrchids=' +yearOfOrchids + '&salePrice=' +salePrice);

	http.open('GET', UPDATE_PRODUCT_PRICE + 'pid=' + pid + '&discount=' + discount + '&potType=' + potType + '&yearOfOrchids=' +yearOfOrchids + '&salePrice=' +salePrice, true);
	
	http.onreadystatechange = handleHttpUpdatePrice;
	http.send(null);
}

function handleHttpUpdatePrice()
{
	if (http.readyState == NORMAL_STATE) {
		if(http.responseText){
			ttt=http.responseText;
			

//			var mytool_array = ttt.split(",");
//			if(trim(mytool_array[0])==0){
//				document.getElementById("potPriceId").value="Free";
//			}
//			else
//			document.getElementById("potPriceId").value="$ " + trim(mytool_array[0]);

//			document.getElementById("saleprice_fld").value=trim(mytool_array[1]);
			document.getElementById("saleprice_fld").value=ttt;
//			return ttt;

		}
	}
}
function savegiftaddresssession(giftaddress)
{
	
	//alert('Cart1.php?giftadress=' + giftaddress);return false;

	http.open('GET', 'Cart1.php?giftadress=' + giftaddress, true);
	http.onreadystatechange = handleHttpUpdateCart1;
	http.send(null);
}

function handleHttpUpdateCart1()
{
	//alert(http.responseText);return false;
	// alert(http.responseText);
	if (http.readyState == NORMAL_STATE) 
		{
		 
		if(http.responseText )
			{
			//alert(http.responseText);return false;
			//document.getElementById("giftaddress").value=http.responseText;
			window.location.reload();
		}
		
		}
	
}


function updateCart(pid,qty,shipID,ucId)
{
	
	//alert(UPDATE_CART  + 'pid=' + pid + '&qty=' + qty + '&shipID=' + shipID);

	http.open('GET', 'updateCart.php?pid=' + pid + '&qty=' + qty + '&shipID=' + shipID+ '&ucid=' + ucId, true);
	http.onreadystatechange = handleHttpUpdateCart;
	http.send(null);
}

function handleHttpUpdateCart()
{
	//alert(http.responseText);return false;
	if (http.readyState == NORMAL_STATE) 
		{
		 
		if(http.responseText > 0)
			{
			 
			window.location.reload();
		}
		var mytool_array = http.responseText.split(",");
		if(mytool_array[0]==0){
			alert("Such quantity is not available in stock.");
			var pidVal	=trim(mytool_array[2]);
			var strVal	= "qtyId"+pidVal;
			document.getElementById(strVal).value=trim(mytool_array[1]);
		}
	}
}
/*updateShipMethod*/
function updateShipMethod(pid,qty,shipID)
{
	//alert(UPDATE_SHIP_METHOD  + 'pid=' + pid + '&qty=' + qty + '&shipID=' + shipID );
	http.open('GET', UPDATE_SHIP_METHOD  + 'pid=' + pid + '&qty=' + qty + '&shipID=' + shipID, true);
	http.onreadystatechange = handleHttpUpdateShipMethod;
	http.send(null);
}
function handleHttpUpdateShipMethod()
{
		if (http.readyState == NORMAL_STATE) {
		if(http.responseText){
		var mytool_array = http.responseText.split(",");
		//alert(mytool_array[0]+" "+mytool_array[1]+ " " +mytool_array[2]);
		var pidVal	=	trim(mytool_array[2]);
		var strVal	="shipPrice" + pidVal;
		document.getElementById("gTotal").value=trim(mytool_array[0]);
		document.getElementById(strVal).value=mytool_array[1];

		}
	}
}
/*to trim the values */
// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

/* ends */

/* to add the single shipping address for cart */
function saveSingleShipAddress(ship_name, address,  city, state, zip, phone)
{
    // alert(phone);return false;
	//alert(Add_SINGLE_SHIP_ADDRESS  + 'ship_name=' + ship_name + '&address=' + address + '&city=' + city + '&state=' + state + '&zip=' + zip + '&phone=' + phone );return false;

	http.open('GET', 'addShipAddress.php?ship_name=' + ship_name + '&address=' + address +  '&city=' + city + '&state=' + state + '&zip=' + zip + '&phone=' + phone, true);
	http.onreadystatechange = handleHttpSaveSingShipAdd;
	http.send(null);
}
function handleHttpSaveSingShipAdd()
{
	if (http.readyState == NORMAL_STATE) {
		if(http.responseText > 0){
		//	alert("ok");
		//alert(http.responseText);
		//document.getElementById("total_cost_fld").value=http.responseText;
		}
	}
}
/* to add the multiple shipping addresses for cart */
function saveMultiShipAddress(pid,ship_name, address,  city, state, zip, phone,ucid)
{

    http.open('GET', Add_SINGLE_SHIP_ADDRESS  + 'pid=' + pid + '&ship_name=' + ship_name + '&address=' + address +'&city=' + city + '&state=' + state + '&zip=' + zip + '&phone=' + phone + '&ucid=' + ucid, true);
	http.onreadystatechange = handleHttpSaveMultiShipAdd;
	http.send(null);
}

function handleHttpSaveMultiShipAdd()
{  
	if (http.readyState == NORMAL_STATE) {
		if(http.responseText.length > 0){
		//alert(http.responseText);
		}
	}
}
/*to register the address type as single or multiple */
function registerSession(addressType)
{
	//alert(REGISTER_ADDRESS_TYPE  + 'addressType=' + addressType);
	http.open('GET', REGISTER_ADDRESS_TYPE  + 'addressType=' + addressType, true);
	http.onreadystatechange = handleHttpRegisterAddressType;
	http.send(null);
}
function handleHttpRegisterAddressType()
{
	if (http.readyState == NORMAL_STATE) {
		if(http.responseText > 0){
		//document.getElementById("total_cost_fld").value=http.responseText;
		}
	}
}

function giftReminderrecord(flag)
{
	//alert(GIFT_REMINDER_RECORD  + 'flag=' + flag);
	http.open('GET', GIFT_REMINDER_RECORD  + 'flag=' + flag, true);
	http.onreadystatechange = handleHttpGiftReminderRecordTPL;
	http.send(null);
}

function handleHttpGiftReminderRecordTPL()
{
	if(http.readyState == NORMAL_STATE){
		if(http.responseText){
			document.getElementById("gift_name2").innerHTML=http.responseText;
			
		}
		
	}
}

function chkGiftCertificate(giftVal)
{
	if(giftVal!='Gift Certificate Code'){
	http.open('GET', GIFT_CERTIFICATE_RECORD  + 'giftVal=' + giftVal , true);
	http.onreadystatechange = handleHttpGiftMessage;
	http.send(null);
	}
}

function handleHttpGiftMessage()
{
	if(http.readyState == NORMAL_STATE){
		if(http.responseText){
			if(http.responseText ==0){
				alert("This is not a valid gift certificate");
				return false;
			}
			//document.getElementById("gift_name2").innerHTML=http.responseText;
		}
	}
}

function chkDiscountCoupon(couponVal)
{
	//alert(DISCOUNT_COUPON  + 'couponVal=' + couponVal);
	if(couponVal!='Discount Coupon'){
	http.open('GET', DISCOUNT_COUPON  + 'couponVal=' + couponVal , true);
	http.onreadystatechange = handleHttpDiscCoupon;
	http.send(null);
	}
}

function handleHttpDiscCoupon()
{	
	if(http.readyState == NORMAL_STATE){
		if(http.responseText){
			if(http.responseText ==0){
				alert("This is not a valid discount coupan");
			//	document.cart_frm.discount_coupon.focus();
			}
		}
	}
}

function delFromCart(pmId)
{
	//alert(DEL_FROM_CART  + 'pmId=' + pmId);
	http.open('GET', DEL_FROM_CART  + 'pmId=' + pmId , true);
	http.onreadystatechange = handleHttpDelProductCart;
	http.send(null);
}

function handleHttpDelProductCart()
{
		if (http.readyState == NORMAL_STATE) {
		if(http.responseText > 0){
		window.location.reload();
		//document.getElementById("total_cost_fld").value=http.responseText;
		}
	}
}


/* to validate the product adding in the cart which are out of stock*/
function CheckQuantity(qty)
{
	//alert(CHECK_QUANTITY  + 'qty=' + qty);
	http.open('GET', CHECK_QUANTITY  + 'qty=' + qty , true);
	http.onreadystatechange = handleHttpCheckProductQuantity;
	http.send(null);
}
function handleHttpCheckProductQuantity()
{
	if(http.readyState == NORMAL_STATE){
		if(http.responseText > 0){
			alert("Product is not available in the stock");
			return false;
		}
	}
}

