$(document).ready(function() {
	$(':input').tipsy({opacity: 0.9, html: true, trigger: 'focus', gravity: 'w'});
	$('.hint').tipsy({opacity: 0.9, html: true, gravity: 'w'});
	/*$('img').bind('error', function(){
		this.src	= conf['public_url']+'/image/image.gif';
	});*/
});

$(document).ready(function(){

	jQuery("#keywordone").blur(function(){
		keywordone = jQuery('#keywordone').attr('value');
		if(trim(keywordone) != ""){
			var postData = {};
			postData['keyword'] = trim(keywordone);
			$.ajax(
			{
				url: conf['base_url']+'/index.php/buyingleads_controller/checkforstopword',
				type: 'POST',
				data:{'request_data': JSON.stringify(postData)},
				dataType : 'json',
				success: function(data) 
				{
					stopWordSuccess(data, "keywordone");
				},
				error: function(xhr, ajaxOptions, thrownError)
				{
					//alert('Error' + thrownError);
				}
			});
		}
	});

	jQuery("#keywordtwo").blur(function(){
		keywordtwo = jQuery('#keywordtwo').attr('value');
		if(trim(keywordtwo) != ""){
			var postData = {};
			postData['keyword'] = trim(keywordtwo);
			$.ajax(
			{
				url: conf['base_url']+'/index.php/buyingleads_controller/checkforstopword',
				type: 'POST',
				data:{'request_data': JSON.stringify(postData)},
				dataType : 'json',
				success: function(data) 
				{
					stopWordSuccess(data, "keywordtwo");
				},
				error: function(xhr, ajaxOptions, thrownError)
				{
					//alert('Error' + thrownError);
				}
			});
		}
	});

	jQuery("#keywordthree").blur(function(){
		keywordthree = jQuery('#keywordthree').attr('value');
		if(trim(keywordthree) != ""){
			var postData = {};
			postData['keyword'] = trim(keywordthree);
			$.ajax(
			{
				url: conf['base_url']+'/index.php/buyingleads_controller/checkforstopword',
				type: 'POST',
				data:{'request_data': JSON.stringify(postData)},
				dataType : 'json',
				success: function(data) 
				{
					stopWordSuccess(data, "keywordthree");
				},
				error: function(xhr, ajaxOptions, thrownError)
				{
					//alert('Error' + thrownError);
				}
			});
		}
	});
		
});	

function stopWordSuccess(data, fieldName)
{
	if(data['status'] == 10){
		jQuery("#"+fieldName+"Span").html("Please use some other keyword!");
		$('#'+fieldName).focus();
	}
	else{
		jQuery("#"+fieldName+"Span").html("");
	}
}	

function fnSelectTab(sType)
{
	var oForm		= document.frmSearch;
	var oHdnType	= oForm.searchType;
	var oSearch		= document.getElementById('searchbox');
	if(sType == '1')
	{
		oHdnType.value	= 'products';
		oSearch.value	= lang['search.area.search.products'];
	}
	else if(sType == '2')
	{
		oHdnType.value	= 'buyers';
		oSearch.value	= lang['search.area.search.buyers'];
	}
	else if(sType == '3')
	{
		oHdnType.value	= 'suppliers';
		oSearch.value	= lang['search.area.search.suppliers'];
	}
	else if(sType == '4')
	{
		oHdnType.value	= 'companies';
		oSearch.value	= lang['search.area.search.companies'];
	}
}

function fnFocusSelect(oInput)
{
	if(oInput.value == lang['search.area.search.products'] || oInput.value == lang['search.area.search.buyers'] || oInput.value == lang['search.area.search.suppliers'] || oInput.value == lang['search.area.search.companies'])
		oInput.value	= '';
}

function fnSelectCountry(oSelect)
{
	var oForm		= oSelect.form;
	
	try
	{
		var oTCode		= oForm.telephone_country;
		if(oSelect.value)
			oTCode.value	= oCode[oSelect.value];
		else
			oTCode.value	= '';	
	} catch(e) {/* do nothing */}
	
	try
	{
		var oFCode		= oForm.fax_country;
		if(oSelect.value)
			oFCode.value	= oCode[oSelect.value];
		else
			oFCode.value	= '';	
	} catch(e) {/* do nothing */}	
	
	try
	{
		var oMCode		= oForm.mobile_country;
		if(oSelect.value)
			oMCode.value	= oCode[oSelect.value];
		else
			oMCode.value	= '';	
	} catch(e) {/* do nothing */}
	//alert(oSelect.value)	
}

function fnSelectCode(oSelect)
{
	var oForm		= oSelect.form;
	
	try
	{
		var oCountry	= oForm.country_abbr;
		oCountry.value	= oAbbr[oSelect.value];
		fnSelectCountry(oCountry);
	} catch(e) {/* do nothing */}	
	
//	try
//	{
//		var oTCode		= oForm.telephone_country;
//		oTCode.value	= oAbbr[oSelect.value];
//	} catch(e) {/* do nothing */}	
//	
//	try
//	{
//		var oFCode		= oForm.fax_country;
//		oFCode.value	= oAbbr[oSelect.value];
//	} catch(e) {/* do nothing */}	
//	
//	try
//	{
//		var oMCode		= oForm.mobile_country;
//		oMCode.value	= oAbbr[oSelect.value];
//	} catch(e) {/* do nothing */}	
}

function fnSelectMemberType(oForm)
{
	var oBuy	= window.document.getElementById('pBuy');
	var oSell	= window.document.getElementById('pSell');
	var oRadio	= oForm.user_type_id;
	for(var counter = 0; counter < oRadio.length; counter++)
	{
		if(oRadio[counter].checked)
		{
			if(oRadio[counter].value == '1')
			{
				oBuy.style.display	= 'block';
				oSell.style.display	= 'none';
			}
			else if(oRadio[counter].value == '2')
			{
				oBuy.style.display	= 'none';
				oSell.style.display	= 'block';
			}
			else
			{
				oBuy.style.display	= 'block';
				oSell.style.display	= 'block';
			}
		}	
	}
}

function fncheckMail(oEmail)
{
	var sEmailFilter	=	/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
	var sReturnVal		=	sEmailFilter.test(oEmail.value);
	return sReturnVal;
}

function fncheckMailValue(sVal)
{
	var sEmailFilter	=	/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
	var sReturnVal		=	sEmailFilter.test(sVal);
	return sReturnVal;
}

function trim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) 
{
	return stringToTrim.replace(/\s+$/,"");
}

function isCharsInBag (s, bag)
{
	var i;
	// Search through string's characters one by one.
	// If character is in bag, append to returnString.
	for (i = 0; i < s.length; i++)
	{
		// Check that current character is not whitespace.
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1)
			return false;
	}
	return true;
}

function fnRemoveAllOptions(oSelect)
{
	for(var counter = oSelect.options.length-1 ; counter >= 0; counter--)
		oSelect.remove(counter);
}

function fnRemoveAllOptionsExceptOne(oSelect)
{
	for(var counter = oSelect.options.length-1 ; counter > 0; counter--)
		oSelect.remove(counter);
}

function fnRemoveOption(oSelect)
{
	var i;
	for(var counter = oSelect.options.length-1 ; counter >= 0; counter--)
	{
		if(oSelect.options[counter].selected)
			oSelect.remove(counter);
	}
}

function fnAddOption(oSelect, text, value)
{
	var oOption		= window.document.createElement("OPTION");
	oOption.text	= text;
	oOption.value	= value;
	oSelect.options.add(oOption);
	return oOption;
}

function fnCheckAll(oCheckbox)
{
	var oForm		= oCheckbox.form;
	var oElements	= oForm.elements;
	for(var counter = 0; counter < oElements.length; counter++)
	{
		if(oElements[counter].type == 'checkbox')
		{
			if(oCheckbox.checked == true)
				oElements[counter].checked	= true;
			else
				oElements[counter].checked	= false;	
		}
	}
}

function fnLimitText(limitField, spnID, limitNum) 
{
	var oSpan	= document.getElementById(spnID);
	if (limitField.value.length > limitNum) 
	{
		limitField.value	= limitField.value.substring(0, limitNum);
	} else 
	{
		oSpan.innerHTML		= limitNum - limitField.value.length;
	}
}

function fnSelectSupplier(sId)
{
	var oForm	= document.frm_sellinglead;	
	var oComp	= oForm.companies;
	var oElem	= oForm.elements;
	try{
		var oUrl	= oForm.url;
		var oProd	= oForm.products;
		var oIdProductBinding = oForm.id_product_binding;
	}catch(e){
		//Nothing
	}
	
	if(sId)
	{
		var oCheckbox		= document.getElementById(sId);
		oCheckbox.checked	= true;
	}
	
	oComp.value	= '';
	try{
		oProd.value	= '';
		oUrl.value	= '';
		oIdProductBinding.value = '';
	}catch(e){
		//Nothing
	}
	for(var counter = 0; counter < oElem.length; counter++)
	{
		value	= oElem[counter].value + ',';
		try{
			prod	= oElem[counter].getAttribute('product_name') + ',';
			url		= oElem[counter].getAttribute('product_url') + ',';
		}catch(e){
			//Nothing
		}
		if(oElem[counter].type == 'checkbox' && oElem[counter].checked && oElem[counter].id)
		{
			oComp.value	+= value;
			try{
				oProd.value	+= prod;
				oUrl.value	+= url;
				oIdProductBinding.value += value+prod+url + ';';
			}catch(e){
				//Nothing
			}
		}	
	}
	if(sId)
		fnContactSelectedSuppliers();
}

function fnContactSelectedSuppliers()
{
	var oForm	= document.frm_sellinglead;
	var oElem	= oForm.elements;
	var oCond	= true;
	
	for(var counter = 0; counter < oElem.length; counter++)
	{
		if(oElem[counter].type == 'checkbox' && oElem[counter].checked)
			oCond	= false;
	}
	
	if(oCond)
	{
		alert(lang['no.item.selected.alert']);
		return;
	}
	
	oForm.action	= conf['base_url'] +'/'+ conf['SEF_CONTACT_SUPPLIER'];
	oForm.submit();
}

function fnContactSupplier(){
	var oForm = document.frmProduct;
	oForm.action = conf['base_url'] +'/'+ conf['SEF_CONTACT_SUPPLIER'];
	oForm.submit();	
}

function fnSelectBuyer(sId, buyingLeadName, buyingLeadUrl)
{
	var oForm		= document.frm_buyinglead;
	var oComp		= oForm.companies;
	var oElem		= oForm.elements;
	try{
		var oProduct	= oForm.products;
		var oUrl		= oForm.url;
	}catch(e){
		//Nothing
	}
	
	oComp.value		= sId;
	try{
		oProduct.value = buyingLeadName;
		oUrl.value = buyingLeadUrl;
	}catch(e){
		//Nothing
	}
	oForm.action	= conf['base_url'] +'/'+ conf['SEF_CONTACT_BUYER'];
	oForm.submit();
}

function fnCallLogin()
{
	var oForm		= document.frmLogin;
	oForm.submit();
}

function submitContactSupplierForm()
{
	var aForm = document.frmContact;	
	aForm.submit();
}
function fnShowAlerts(sType)
{
	var oBSpn = document.getElementById('spnBuyer');
	var oSSpn = document.getElementById('spnSupplier');
	var oBLSpn = document.getElementById('spnLinkBuyer');
	var oSLSpn = document.getElementById('spnLinkSupplier');
	if(sType == 'buyer')
	{
		oBSpn.style.fontWeight = 'bold';
		oSSpn.style.fontWeight = 'normal';
		oBLSpn.style.display = 'inline';
		oSLSpn.style.display = 'none';
	}
	else
	{
		oBSpn.style.fontWeight = 'normal';
		oSSpn.style.fontWeight = 'bold';
		oBLSpn.style.display = 'none';
		oSLSpn.style.display = 'inline';
	}
}
function fnToogleCategory(sType)
{
	var oLIs	= window.document.getElementsByTagName('li');
	var oLI	= document.getElementById('showMore');
	if(!sType)
		oLI.style.display	= 'none';
	else
		oLI.style.display	= 'block';	
	
	for(var counter = 0; counter < oLIs.length; counter++)
	{
		if(oLIs[counter].id.indexOf('menuitem_') != -1)
		{
			if(sType)
			{
				oLIs[counter].style.display	= 'none';
				//$(oLIs[counter]).hide('slow');
			}	
			else
			{
				oLIs[counter].style.display	= 'block';
				//$(oLIs[counter]).show('slow');
			}	
		}
	}
	buildsubmenus();
}

function fnBookmarkThisPage()
{
	var sTitle = top.document.title;
	var sURL = top.location.href;
	
	if (window.sidebar)
	{ 
		window.sidebar.addPanel(sTitle, sURL, ""); 
	}
	else if( document.all )
	{
		window.external.AddFavorite(sURL, sTitle);
	}
	else if( window.opera && window.print )
	{
		return true;
	}
}

function fnPrintThisPage()
{
	var sText	= document.getElementById('TA_print').innerHTML
	
	var el = document.createElement("iframe");
	el.setAttribute('id', 'ifrm');
	el.setAttribute('width', '1');
	el.setAttribute('height', '1');
	document.body.appendChild(el);
	//el.setAttribute('src', 'http://www.google.com');
	var ifrm = document.getElementById('ifrm');
	//ifrm.style.display = 'none';
	ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;
	ifrm.document.open();
	ifrm.document.write("<html><head><link rel='stylesheet' type='text/css' href='"+ conf['public_url'] +"/css/main.css' /></head><body style='font-size:11px' onload='window.print()'>");
	ifrm.document.write(sText);
	ifrm.document.write("</body></html>");
	ifrm.document.close();
}

var hWnd
function PopupWindow( passurl , height, width) {
        
		//var x = screen.availWidth/2 - 200;
		//var y = 150;
		
		var x = 0;
		var y = 0;
		
		
        enterCalled = true
        window.name = "parent"
        var url = passurl
        if( !hWnd ) {
        //if no help window is open then open a new help window and give it focus
        hWnd = window.open(url,"popup","width=" + width + ",height=" + height +", left=" + x + ", top=" + y + ", resizable=yes,scrollbars=yes")
        hWnd.focus()
        } else if ( hWnd.closed ) {
        //if no help window is open then open a new help window and give it focus
        hWnd = window.open(url,"popup","width=" + width + ",height=" + height +", left=" + x + ", top=" + y + ", resizable=yes,scrollbars=yes")
        hWnd.focus()
        } else {
        //if the help window is already open then update it and give it focus
        hWnd.location=url
        hWnd.focus()
        }
        // handle Navigator 2, which doesn't have an opener property
        if (!hWnd.opener) { hWnd.opener = window}
}

/*Top Menu*/
// Copyright 2006-2007 javascript-array.com

var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';

}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose;

$.fn.serializeObject = function()
{
	var o = {};
	var a = this.serializeArray();
	$.each(a, function() {
		if (o[this.name]) {
			if (!o[this.name].push) {
				o[this.name] = [o[this.name]];
			}
			o[this.name].push(this.value || '');
		} else {
			o[this.name] = this.value || '';
		}
	});
	return o;
};

function fnDoesUploadPhoto(oChk)
{
	var oForm	= oChk.form;
	var oDiv	= document.getElementById('divUploadPhoto');
	var oImg	= document.getElementById('imgLoader');
	if(oChk.checked)
	{
		oDiv.style.display	= 'none';
		oImg.src	= conf['public_url']+'/image/product_image.gif';
	}	
	else
	{
		oDiv.style.display	= 'inline';	
		try
		{
			if(oForm.UploadedProdcutPhotoName.value != '')
				oImg.src	= conf['base_url'] + '/' + conf['TMP_PHOTO_PATH'] + oForm.UploadedProdcutPhotoName.value;
			else
				oImg.src	= oForm.ShowUploadedPhoto.value;
		}
		catch(e)
		{
			if(oForm.uploadedProdcutPhotoName.value != '')
				oImg.src	= conf['base_url'] + '/' + conf['TMP_PHOTO_PATH'] + oForm.uploadedProdcutPhotoName.value;
			else
				oImg.src	= oForm.ShowUploadedPhoto.value;
		}
	}	
}

function fnShowErrorImage(oImg)
{
	/*do nothing*/
}
