function trim(nStr){
	return nStr.replace(/(^\s*)|(\s*$)/g, "");
}
	
function showSubMenus(){
	document.getElementById('sub_product_list').style.display = 'block';
	document.getElementById('expand_sub').style.display = 'inline';
	
	document.getElementById('collapse_sub').style.display = 'none';
}

function hideSubMenus( e ){
	var toEl = e.toElement || e.relatedTarget;
	if ( toEl.className == 'product_sublist' ) return;
	var flag = 0;
	for ( i=0; i<3; i++ ){
		toEl = toEl.parentNode;
		if ( toEl.className == 'product_sublist' ){
			flag = 1;
			break;
		}
	}
	
	if ( !flag ){
		document.getElementById('sub_product_list').style.display = 'none';
		document.getElementById('expand_sub').style.display = 'none';
	
		document.getElementById('collapse_sub').style.display = 'block';
	}
}

function ShowBrachTelNo( obj ){
	document.getElementById('branch_tel_no').innerHTML = obj.value;
}

function resetModelDropdown(){	
	while( true ){
		var modelDD = document.getElementById('model_dropdown').childNodes;
		if ( modelDD.length <= 0 )
		break;
		document.getElementById('model_dropdown').removeChild( modelDD[modelDD.length-1] );
	}
}

function getModelDetail( obj ){
	var value = obj.value;
	if ( value != '' ){
		resetModelDropdown();
		document.getElementById('model_dropdown').options[document.getElementById('model_dropdown').options.length] = new Option('Fetching...', '');//return;
		$.ajax({
			   	url:g_site_path+'get_model_detail.php?brand='+value,
				type:'GET',
				dataType : 'xml',
				success:function(xml){
					resetModelDropdown();
					$(xml).find('response').each(function(){
						$(xml).find('optionname').each(function(){
							var optname  = $(this).text();
							var optvalue = $(this).attr('value');												
							document.getElementById('model_dropdown').options[document.getElementById('model_dropdown').options.length] = new Option(optname, optvalue);
						});						
					});
				}
			   })
	}
}

function doSearch(){
	var brand = document.getElementById('brand_dropdown').value;
	var model = document.getElementById('model_dropdown').value;
	if ( brand != '' && model == '' ){
		alert('Please select any model to continue');
		return false;
	}else if ( brand == '' && model != '' ){
		alert('Please select any brand to continue');
		return false;
	}
	if ( brand != '' && model != '' ){
		window.location.href = g_site_path+'product_details.php?pid='+brand+'&mid='+model;
	}
}
var prevTime, nextTime;
function doPrev(){
	if ( typeof prevTime == 'undefined' || prevTime == false ){
		prevTime = true;
		$('#popular'+popularIndex).fadeOut('slow', function(){
																	$('#popular'+(popularIndex-1)).fadeIn();
																	popularIndex--;
																	if ( popularIndex <= 1 ){
																		popularIndex = 1;
																		document.getElementById('popular_prev').style.visibility = 'hidden';
																	}
																	document.getElementById('popular_next').style.visibility = 'visible';
																	$('#popular_brand_name').attr('title', popularBrand[popularIndex-1]);
																	$('#popular_brand_name').html(cutString(popularBrand[popularIndex-1],15));
																	$('#popular_model_name').attr('title',popularModelName[popularIndex-1]);
																	$('#popular_model_name').html('('+cutString(popularModelName[popularIndex-1],10)+')');
																	$('#popular_price').attr('title',popularPrice[popularIndex-1]);
																	$('#popular_price').html(cutString(popularPrice[popularIndex-1],10));
																	prevTime = false;
																});	
	}
}
function doNext(){	
	if ( typeof nextTime == 'undefined' || nextTime == false ){
		nextTime = true;
		$('#popular'+popularIndex).fadeOut('slow', function(){
															$('#popular'+(popularIndex+1)).fadeIn();
															popularIndex++;
															if ( popularIndex >= popularCnt ){
																popularIndex = popularCnt;
																document.getElementById('popular_next').style.visibility = 'hidden';
															}
															document.getElementById('popular_prev').style.visibility = 'visible';
															$('#popular_brand_name').attr('title', popularBrand[popularIndex-1]);
															$('#popular_brand_name').html(cutString(popularBrand[popularIndex-1],15));
															$('#popular_model_name').attr('title',popularModelName[popularIndex-1]);
															$('#popular_model_name').html('('+cutString(popularModelName[popularIndex-1],10)+')');
															$('#popular_price').attr('title',popularPrice[popularIndex-1]);
															$('#popular_price').html(cutString(popularPrice[popularIndex-1],10));
															nextTime = false;
															});	
	}
}
function clearErrorFields( param ){
	var key;
	for ( key in param ){
		if ( document.getElementById( param[key] ) ) document.getElementById( param[key] ).innerHTML = '';
	}
}
function cutString( str, len ){
	var str = trim( str );
	var len = typeof len == 'undefined' ? 15 : len;
	
	if ( str.length > len ){
		return str.substring(0,len)+'...';
	}else{
		return str;
	}
}
function validate_contactus(){
	var email = $('#txtEmailAddress').val();
	var desc  = $('#txtDescription').val();
	var phone = $('#txtPhoneNo').val(); 
	clearErrorFields( ['err_email_address','err_description','err_phone_no'] );
	if ( email == '' ){
		$('#err_email_address').html( 'Please enter your email address' );
		document.getElementById('txtEmailAddress').focus();
		return false;
	}
	if ( phone == '' ){
		$('#err_phone_no').html( 'Please enter your phone number' );
		document.getElementById('txtPhoneNo').focus();
		return false;
	}
	/*if ( !validateEmailAddress( email ) ){
		$('#err_email_address').html( 'Please enter valid email address' );
		return false;
	}*/
	if ( desc == '' ){
		$('#err_description').html( 'Please enter your description' );
		document.getElementById('txtDescription').focus();
		return false;
	}
	return true;
}
function submitContactUs(){
	if ( validate_contactus() ){
		document.getElementById('frm_contactus').submit();
	}
}

function toggleProdBgLeftMenu( obj, status ){
	if ( status ) {
		obj.parentNode.className = 'left_sec_acc_link';
	}else{
		obj.parentNode.className = '';
	}
}

/*Compare*/
function doCompare(){
	var cmp1 = $('#compare_1').val();
	var cmp2 = $('#compare_2').val();
	var cmp3 = $('#compare_3').val();
	if ( (cmp1 == '' && cmp2 == '') || (cmp1 == '' && cmp3 == '') || (cmp2=='' && cmp3=='') ){
		alert('Please select atleast two mobiles to compare');
		return false;
	}else if ( cmp1 == cmp2 || cmp1 == cmp3 || cmp2 == cmp3 ){
		alert('Please select different mobile to compare');
		return false;
	}
	$('#hidModelId1').attr('value', cmp1 );
	$('#hidModelId2').attr('value', cmp2 );
	$('#hidModelId3').attr('value', cmp3 );
	document.getElementById('frmCompare').submit();
}

function makeSelectCompareCheck( sel1, sel2, sel3 ){	
	var len = document.getElementById('compare_1').options.length;
	var flag = 0, flag1= 0, flag2 = 0;
	for(var i = 0; i<len; i++) {
		if (document.getElementById('compare_1').options[i].value == sel1) {
			document.getElementById('compare_1').options[i].selected = true;
			flag = 1;
		}
		if (document.getElementById('compare_2').options[i].value == sel2){
			document.getElementById('compare_2').options[i].selected = true;
			flag1 =1;
		}
		if (document.getElementById('compare_3').options[i].value == sel3){
			document.getElementById('compare_3').options[i].selected = true;
			flag2 =1;
		}
		
		if ( flag == 1 && flag1 == 1 && flag2 == 1 ) 
		break;
	}
}

function showHint( obj, text, mode ) {
	if( !obj ) return;
	if( obj.value == '' ){
		obj.value = text;
		if ( mode )
			obj.className = 'searchTxt'
	}
}
function removeHint( obj, text, mode ) {
	if( !obj ) return;
	if( obj.value == text ){
		obj.value = '';
		if ( mode )
			obj.className = 'searchNormal'
	}
}

function doGlobalSearch(){
	if ( document.getElementById('txtSearchKeyword').value != '' && document.getElementById('txtSearchKeyword').value != '[Search Mobiles, Accessories]' ){
		document.getElementById('frm_search').submit();
		return true;
	}else{
		return false;
	}
	return true;
}
