//var site_admin = '/blinds/testing/admin/';
////// ********  CREATING THE SACK (AJAX) VARIABLE  ********** ///////
var Ajax = new sack();

////// ******* FUNCTION DEFINITION TO FIND THE POSITION OF THE OBJECT ****** /////
function findPos(obj)
{
    var curleft = curtop = 0;
    if (obj.offsetParent)
    {
        curleft = obj.offsetLeft;
        curtop = obj.offsetTop;
        while (obj = obj.offsetParent) 
        {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
    }
    return [curleft,curtop];
}
function descriptionDiv(anchor_id,name,img,desc,typ)
{
	anchor_id = decodeURIComponent(anchor_id);
	name = decodeURIComponent(name);
	img = decodeURIComponent(img);
	desc = decodeURIComponent(desc);
	var site_admin = '/';
	var oDiv = document.getElementById('ttip');
	if (oDiv.className == 'hideTip') {
		if(document.getElementById(anchor_id) != null)
		{
			if (typ == 'shape')
				var imgFolder = 'prod_shape_images';
			else if (typ == 'attach')
				var imgFolder = 'prod_shape_att_imgs';
			else if (typ == 'extra')
				var imgFolder = 'prod_extra_images';
			else if (typ == 'panel')
				var imgFolder = 'prod_panel_images';
			else if (typ == 'panattach')
				var imgFolder = 'prod_panel_att_imgs';
			else
				var imgFolder = 'prod_option_images';
			//alert(img);
			imgsrc = document.getElementById(img).value;
			//alert(imgsrc);
			var txt = '<div class="box"><p class="blue-text"><img src="'+site_url+'images/delete.gif" class="float-right" alt="close" title="close" onclick="descriptionDiv(0,0,0,0,0)" />'+name+'</p><div class="textt"><img src="'+imgsrc+'" alt="image" title="image" />'+desc+'</div></div>';
			oDiv.innerHTML = txt;
			var oAnchor = document.getElementById(anchor_id);
			var pos = findPos(oAnchor);
			oDiv.className = 'showTip';
			oDiv.style.left = pos[0]-5+'px';
			oDiv.style.top = pos[1]+10+'px';
			return false;
		}
	}
	else {
		oDiv.className = 'hideTip';
	}
}

// FUNCTION TO VALIDATE MM PRODUCT QUOTE
function validateMmQuote(prodActionId,prodcolImgId,opts,extSubOpts)
{
	frm=document.form_mmproducts;
	
	if(document.getElementById('sel_slatsize').value == 0)
	{
		alert(document.getElementById('hdn_slatalert').value);
		document.getElementById('sel_slatsize').focus();
		return false;	
	}
	else if(document.getElementById('txt_width').value=='Enter width in cm')
	{
		alert('Please enter width');	
		document.getElementById('txt_width').focus();
		return false;
	}
	else if(document.getElementById('txt_drop').value=='Enter drop in cm')
	{
		alert('Please enter drop');	
		document.getElementById('txt_drop').focus();
		return false;	
	}
	else
	{
		
		if(opts != 0)//To get selected option ids
		{
			var optIds = ''
			for(var i=1; i< opts ; i++)	
			{
				if(optIds == '')
				{
					if(document.getElementById('sel_options_'+i).value != 0)	
						optIds += document.getElementById('sel_options_'+i).value;
				}
				else
				{
					if(document.getElementById('sel_options_'+i).value != 0)	
						optIds += ','+document.getElementById('sel_options_'+i).value;	
				}
			}
		}
		if(extSubOpts != 0)//TO get selected extra option ids
		{
			var extSubOptIds = ''
			for(var i=1; i< extSubOpts ; i++)	
			{
				if(extSubOptIds == '')
				{
					if(document.getElementById('sel_extsubopts_'+i).value != 0)	
						extSubOptIds += document.getElementById('sel_extsubopts_'+i).value;
				}
				else
				{
					if(document.getElementById('sel_extsubopts_'+i).value != 0)	
						extSubOptIds += ','+document.getElementById('sel_extsubopts_'+i).value;	
				}
			}
		}
		// To validate attached shape
		if(document.getElementById('sel_shapes') != null)
		{
			if(document.getElementById('sel_shapes').value != 0)
			{
				var ittr = document.getElementById('hdn_totattOpts').value;
				var flag = 0;
				for(var i=1; i<ittr; i++)
				{
					if(document.getElementById('rad_shapeattopt_'+i).checked == true)
					{
						flag = 1;	
					}
				}
				if(flag == 0)
				{
					alert('Please select shape attached options');	
					document.getElementById('rad_shapeattopt_1').focus();
					return false;
				}
			}
		}
		var shapeOPtId = 0;
		if(document.getElementById('sel_shapes') != null)
		{
			shapeOPtId = document.getElementById('sel_shapes').value;
		}
		// To validate attached panels
		if(document.getElementById('sel_panels') != null)
		{
			if(document.getElementById('sel_panels').value != 0)
			{
				var ittr = document.getElementById('hdn_totattPanOpts').value;
				var flag = 0;
				for(var i=1; i<ittr; i++)
				{
					if(document.getElementById('rad_panattopt_'+i).checked == true)
					{
						flag = 1;	
					}
				}
				if(flag == 0)
				{
					alert('Please select panel attached options');	
					document.getElementById('rad_panattopt_1').focus();
					return false;
				}
			}
		}
		var panelOPtId = 0;
		var bead_depth = 0;
		var frame_colour = '';
		if(document.getElementById('sel_panels') != null)
		{
			panelOPtId = document.getElementById('sel_panels').value;
		}
		if(document.getElementById('sel_bead') != null)
		{
			bead_depth = document.getElementById('sel_bead').value;
		}
		if(document.getElementById('sel_frame') != null)
		{
			frame_colour = document.getElementById('sel_frame').value;
		}
		document.getElementById('hdn_options').value = optIds; 
		document.getElementById('hdn_extra_opts').value = extSubOptIds;
		var slat = document.getElementById('sel_slatsize').value;
		var width = document.getElementById('txt_width').value;
		var drop = document.getElementById('txt_drop').value;
		document.getElementById('sale_price').innerHTML = '<img src="'+site_url+'images/loader.gif" alt"Loading Image" width="150" /><p>Getting price, please wait.....</p>';
		
		Ajax.requestFile = site_url+'controllers/ajax-controller.php?slat='+slat+'&width='+width+'&prodActionId='+prodActionId+'&drop='+drop+'&prodcolImgId='+prodcolImgId+'&optIds='+optIds+'&extSubOptIds='+extSubOptIds+'&shapeOPtId='+shapeOPtId+'&panelOPtId='+panelOPtId+'&bead_depth='+bead_depth+'&frame_colour='+frame_colour+'&act=getMmQuote';
		Ajax.onCompletion = displayInfo1;
		Ajax.runAJAX();		
	}
}
////// ******* FUNCTION TO EVAL AJAX RESPONSE ******** //////////
function displayInfo1()
{
	//alert(Ajax.response);
	eval(Ajax.response);
}
// FUNCTION TO SAVE SHAPE ATTACHED OPTION ID 
function saveAttachOptId(attOptId)
{
	document.getElementById('hdn_shape_opts').value=attOptId;		
}
// FUNCTION TO SAVE PANEL ATTACHED OPTION ID 
function saveAttachPanOptId(attOptId)
{
	document.getElementById('hdn_panel_opts').value=attOptId;		
}
// FUNCTION TO GET EXTRA SUB OPTIONS 
function getExtSubOptions(extOptId,id)
{
	Ajax.requestFile = site_url+'controllers/ajax-controller.php?extOptId='+extOptId+'&id='+id+'&act=getExtSubOpts';
	Ajax.onCompletion = displayInfo1;
	Ajax.runAJAX();		
}
// FUNCTION TO GET SHAPE OPTIONS 
function getShapeOptions(shapeId)
{
	Ajax.requestFile = site_url+'controllers/ajax-controller.php?shapeId='+shapeId+'&act=getShapeOptions';
	Ajax.onCompletion = displayInfo1;
	Ajax.runAJAX();	
}
// FUNCTION TO GET PANEL OPTIONS 
function getPanelOptions(panelId)
{
	Ajax.requestFile = site_url+'controllers/ajax-controller.php?panelId='+panelId+'&act=getPanelOptions';
	Ajax.onCompletion = displayInfo1;
	Ajax.runAJAX();	
}
// FUNCTION TO GET ATTACHED SHAPES 
function getAttachedShapes(shapeOPtId)
{
	Ajax.requestFile = site_url+'controllers/ajax-controller.php?shapeId='+shapeOPtId+'&act=getShapeAttach';
	Ajax.onCompletion = displayInfo1;
	Ajax.runAJAX();	
}
// FUNCTION TO GET ATTACHED PANELS 
function getAttachedPanels(panelOPtId,id)
{
	Ajax.requestFile = site_url+'controllers/ajax-controller.php?panelOPtId='+panelOPtId+'&act=getPanelAttach';
	Ajax.onCompletion = displayInfo1;
	Ajax.runAJAX();	
}
// FUNCTION TO CLOSE ALERT BOX
function closeAlertBox(){
	document.getElementById('alertMessage').style.display="none";
	document.getElementById('fade').style.display="none";	
}
// FUNCTION TO CHECK WIDTH
function checkWidth(prodActionId,prodcolImgId,width){
	frm=document.form_mmproducts;
	if(SelectValidation(frm.sel_slatsize,'Slat size') == 0){
		return false;	
	}
	else if(isNaN(width)){
		alert('Please enter only numaric data');
		document.getElementById('txt_width').value='';
		document.getElementById('txt_width').focus();
		return false;
	}
	else{
		var slat = document.getElementById('sel_slatsize').value;
		Ajax.requestFile = site_url+'controllers/ajax-controller.php?slat='+slat+'&width='+width+'&prodActionId='+prodActionId+'&prodcolImgId='+prodcolImgId+'&act=checkMeasures';
		Ajax.onCompletion = displayInfo1;
		Ajax.runAJAX();	
	}
}
// FUNCTION TO CHECK DROP
function checkDrop(prodActionId,prodcolImgId,drop){
	frm=document.form_mmproducts;
	if(SelectValidation(frm.sel_slatsize,'Slat size') == 0){
		return false;	
	}
	else if(isNaN(drop)){
		alert('Please enter only numaric data');
		document.getElementById('txt_drop').value='';
		document.getElementById('txt_drop').focus();
		return false;
	}
	else{
		var slat = document.getElementById('sel_slatsize').value;
		Ajax.requestFile = site_url+'controllers/ajax-controller.php?slat='+slat+'&drop='+drop+'&prodActionId='+prodActionId+'&prodcolImgId='+prodcolImgId+'&act=checkMeasures';
		Ajax.onCompletion = displayInfo1;
		Ajax.runAJAX();	
	}
}

//// ****** FUNCTION TO ADD PRODUCT TO BASKET ****** ////////
function addToBasket(productName) {
	

	
	if (document.getElementById('hdn_product_price').value != '') {
		document.getElementById('hdn_prod_name').value = productName;
		document.getElementById('hdn_txt_width').value = document.getElementById('txt_width').value;
		document.getElementById('hdn_txt_drop').value = document.getElementById('txt_drop').value;
		
		document.getElementById('txt_width').setAttribute('disabled','disabled');
		document.getElementById('txt_drop').setAttribute('disabled','disabled');
		if (SelectValidation(frm.sel_slatsize,'slat size') == 0) {
			return false;
		}
		 else if(checkFitting()==0)
		{
			alert('Please select blind fitting type');
			document.getElementById('rad_recess').focus();	
			return false;
		}
		else if(checkRaise()==0)
		{
			alert('Please Select raise/lower From the list');
			document.getElementById('sel_raise').focus();
			return false;
		}
		else if(checkTilt()==0)
		{
			alert('Please Select tilt From the list');
			document.getElementById('sel_tilt').focus();
			return false;
		}
		else {
				document.form_mmproducts.submit();
		}
	}
	else {
		alert('Price is not defined for this product.\n Please get quote for this product.');
	} 
}
//// FUNCTION TO CHECK FITTING ////////
function checkFitting()
{
	if(document.getElementById('rad_recess') && document.getElementById('rad_exact'))
	{
		if(document.getElementById('rad_recess').checked==false && document.getElementById('rad_exact').checked==false)
		{
			return 0;
		}
		else
			return 1;
	}
	else
		return 1;
}
//// FUNCTION TO CHECK RAISE ////////
function checkRaise()
{
	if(document.getElementById('sel_raise'))
	{
		if (document.getElementById('sel_raise').value == 0) {
			return 0;
		}
	}
	else
		return 1
}
//// FUNCTION TO CHECK TILT ////////
function checkTilt()
{
	if(document.getElementById('sel_tilt'))
	{
		if (document.getElementById('sel_tilt').value == 0) {
			return 0;
		}
		else
		return 1
	}
	else
		return 1
}

//// ****** FUNCTION TO ORDER FREE SAMPLE **** /////////
function orderSample(imgName) {
	document.getElementById('hdn_img_name').value=imgName;
	document.getElementById('hdn_order_sample').value = 1;
	document.form_mmproducts.submit();
}


////////******** FUNCTION TO SHOW ENLAGED IMAGE ********/////////////////////
function viewEnlargedImage(image)
{
	document.getElementById('alert_id').innerHTML = 	'<img src="'+image+'">';
	document.getElementById('alertMessage').style.display='block';
	document.getElementById('fade').style.display='block';
}
