$(document).ready(function() {

$("#nav ul").hide();
$("#nav li:has(ul)").each(function() {
	$(this).children().slideUp(1);
});

$("li.p1:has(ul)").click(function(event){
if (this == event.target) {
	var current = this;
	$("#nav li:has(ul)").each(function() {
	if (this != current) $(this).children().slideUp(1);
	});
	$("ul:first", $(this)).slideToggle(1);
}
});
$("li.p1:not(:has(ul))").click(function(event){
if (this == event.target) {
	var current = this;
	$("#nav li:has(ul)").each(function() {
	if (this != current) $(this).children().slideUp(1);
	});
	$("ul:first", $(this)).slideToggle(1);
}
});

$("li.p2:has(ul)").click(function(event){
if (this == event.target) {
	var current = this;
	$("li.p2:has(ul)").each(function() {
	if (this != current) $(this).children().slideUp(1);
	});
	$("li.p3:has(ul)").each(function() {
	if (this != current) $(this).children().slideUp(1);
	});
	$("ul:first", $(this)).slideToggle(1);
}
});

$("li.p3:has(ul)").click(function(event){
if (this == event.target) {
var current = this;
$("li.p3:has(ul)").each(function() {
if (this != current) $(this).children().slideUp(1);
});
$("ul:first", $(this)).slideToggle(1);
}
});

});

/*To check the user status if he is logged in or not*/
function check_status(status,obj,dialogID,topPos)
{
	var status = typeof(status) != 'undefined'?status:0;
	if(dialogID == 'email_form')
	{
		var randomkeys=makecaptcha();
		document.getElementById('captcha_code_value').value = randomkeys;
		document.getElementById('captcha').innerHTML = randomkeys;
	}

	if(status)
	{
		window.location = "account_history.php";
	}
	else
	{
		opendialog(obj,dialogID,topPos);
	}
	return false;
}

///To close dialog for login and email
function close_dialog(divid)
{
	document.getElementById(divid).style.display= 'none';
}

///To open dialog for login and email
function opendialog(obj,dialogID,topPos)
{
	var curleft = curtop = 0;
	var nVer = navigator.appVersion;
	var nAgt = navigator.userAgent;
	var browserVersion  = getBrowserVersion(); 

	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	if(dialogID == 'email_form')
	{
		curleft = curleft-100
	}
	else
	{
		curleft = curleft-5
	}
	var int_top=parseInt(curtop);
	if(browserVersion == '7.0')
	{
		topPos = topPos - 50;
	}
	var new_pos=int_top-topPos;
	document.getElementById(dialogID).style.left=curleft+"px";
	document.getElementById(dialogID).style.top=new_pos+"px";
	document.getElementById(dialogID).style.display='';
}

///Get browser Version///
function getBrowserVersion()
{
	var nVer = navigator.appVersion;
	var nAgt = navigator.userAgent;
	var browserName  = navigator.appName;
	var fullVersion  = ''+parseFloat(navigator.appVersion); 
	var majorVersion = parseInt(navigator.appVersion,10);
	var nameOffset,verOffset,ix;

	// In MSIE, the true version is after "MSIE" in userAgent
	if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
		browserName = "Microsoft Internet Explorer";
		fullVersion = nAgt.substring(verOffset+5);
	}
	// In Opera, the true version is after "Opera" 
	else if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
		browserName = "Opera";
		fullVersion = nAgt.substring(verOffset+6);
	}
	// In Chrome, the true version is after "Chrome" 
	else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
		browserName = "Chrome";
		fullVersion = nAgt.substring(verOffset+7);
	}
	// In Safari, the true version is after "Safari" 
	else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
		browserName = "Safari";
		fullVersion = nAgt.substring(verOffset+7);
	}
	// In Firefox, the true version is after "Firefox" 
	else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
		browserName = "Firefox";
		fullVersion = nAgt.substring(verOffset+8);
	}
	// In most other browsers, "name/version" is at the end of userAgent 
	else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) ) 
	{
		browserName = nAgt.substring(nameOffset,verOffset);
		fullVersion = nAgt.substring(verOffset+1);
		if (browserName.toLowerCase()==browserName.toUpperCase()) {
			browserName = navigator.appName;
		}
	}
	// trim the fullVersion string at semicolon/space if present
	if ((ix=fullVersion.indexOf(";"))!=-1) fullVersion=fullVersion.substring(0,ix);
	if ((ix=fullVersion.indexOf(" "))!=-1) fullVersion=fullVersion.substring(0,ix);

	majorVersion = parseInt(''+fullVersion,10);
	if (isNaN(majorVersion)) {
		fullVersion  = ''+parseFloat(navigator.appVersion); 
		majorVersion = parseInt(navigator.appVersion,10);
	}
	return fullVersion;
}
/*Coding to send email to friend*/

function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
}

var http = createRequestObject();

function sendemail() {
	var msg = document.contactform.message.value;
	var name = document.contactform.your_name.value;
	var email = document.contactform.friend_email.value;
	var captchavalue = document.contactform.captcha_code_value.value; 
	var code = document.contactform.code.value;
	var pageURL = window.location.href;  
	document.contactform.submit_form.disabled=true; 
	document.contactform.submit_form.value='Sending....';

	http.open('get', 'contact.php?msg='+msg+'&name='+name+'&email='+email+'&action=send'+'&pageurl='+pageURL+'&captchaValue='+captchavalue+'&captcha_code='+code);
	http.onreadystatechange = handleResponse;
	http.send(null);
}

////To handle the response from email sending script
function handleResponse() {
	if(http.readyState == 4){
		var response = http.responseText;
		////To show message in case of error
		var update = new Array();
		if(response.indexOf('|' != -1)) {
			update = response.split('|');
			var message_str = '';
			for(i = 0; i < update.length; i++)
			{
				if(update[i] == 'success')
				{
					
				}
				else
				{
					message_str += 	update[i]+'\n';
				}
			}
			alert(message_str);
			if(update[0] == 'success')
			{
				document.getElementById('captcha_code_value').value = randomkeys;
				document.getElementById('captcha').innerHTML = randomkeys;
				document.contactform.code.value = '';
				document.contactform.submit_form.disabled=false; 
				document.contactform.submit_form.value='Send Message';
				document.contactform.reset();
				close_dialog('email_form');
			}
			var randomkeys=makecaptcha();
			document.getElementById('captcha_code_value').value = randomkeys;
			document.getElementById('captcha').innerHTML = randomkeys;
			document.contactform.code.value = '';
			document.contactform.submit_form.disabled=false; 
			document.contactform.submit_form.value='Send Message';
		}
	}
}

/////To create captch on the email dialog box///
function makecaptcha()
{
	var text = "";
	var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

	for( var i=0; i < 5; i++ )
		text += possible.charAt(Math.floor(Math.random() * possible.length));

	return text;
}
	
function addToFavourite()
{
	var description = 'Its my favourite';
	var fullurl = document.URL;

	if (navigator.appName=='Microsoft Internet Explorer')
	{
		window.external.AddFavorite(fullurl, description);
	}
	else if (navigator.appName=='Netscape')
	{
		window.sidebar.addPanel(description, fullurl,"");
	}
	else
	{
		alert("Can't add to favorite");
	}
}

///////Coding to fill dropdown
function check(id)
{
	var select_obj = document.getElementById(id);
	var select_id = select_obj.options[select_obj.selectedIndex].value;
	
	var model_obj = document.getElementById('pmodel');
	
	var action_value = (id == 'pbrand') ? 'brand' : 'type';
	
	var brand_id_value;
	
	if(id == 'pbrand')
	{
		action_value = 'brand';
		brand_id_value = select_id;	
	}
	else if(id == 'ptype')
	{
		action_value = 'type';	
		var pbrand_obj = document.getElementById('pbrand');
		brand_id_value = pbrand_obj.options[pbrand_obj.selectedIndex].value;	
	}
	
	var type_obj = document.getElementById('ptype');
	
	if((model_obj != '') && ((type_obj != '')) && (id == 'pbrand'))
	{
		for(var j = model_obj.options.length-1; j >0 ; j--)
		{
			model_obj.options[j] = null;
		}
		for(var j = type_obj.options.length-1; j >0 ; j--)
		{
			type_obj.options[j] = null;
		}
		model_obj.disabled = true;
		type_obj.disabled = true; 
	}
	else if((model_obj != '') && (id == 'ptype'))
	{ 
		for(var j = model_obj.options.length-1; j >0 ; j--)
		{
			model_obj.options[j] = null;
		}
		model_obj.disabled = true;
	}

	if((select_id == '') && (id == 'pbrand'))
	{
		document.getElementById('div_type').innerHTML = '<select name="ptype" class="Select_type" id="ptype" disabled="disabled"><option value="">Select model</option></select>';	
		document.getElementById('div_model').innerHTML = '<select name="pmodel" class="Select_type" id="pmodel" disabled="disabled"><option value="">Select type</option></select>';
	}
	else if((select_id == '') && (id == 'ptype'))
	{
		document.getElementById('div_model').innerHTML = '<select name="pmodel" class="Select_type" id="pmodel" disabled="disabled"><option value="">Select type</option></select>';	
	}	
	else
	{
		try
		{
			if(id == 'pbrand')
			{
				var products_type = get_products_type(select_id);
				for(i = 0, j = 1; i < products_type.length; i++, j++)
				{
					option = new Option(products_type[i],products_type[i]);
					document.getElementById('ptype').options[j] = option;
					document.getElementById('ptype').disabled = false;
				}
			}
			else if(id == 'ptype')
			{
			var products_models = products_model(select_id, brand_id_value);
				for(i = 0, j = 1; i < products_models.length; i++, j++)
				{
					option = new Option(products_models[i],products_models[i]);
					document.getElementById('pmodel').options[j] = option;				
					document.getElementById('pmodel').disabled = false;   
				}
			}
		}////try end
		catch(ex)
		{
			alert(ex);  
		}
	}
}


function check_form()
{
	if(document.frmFind.p_code.value == "Product Code")
	{
		if(document.frmFind.pbrand.value == "")
		{
			alert("Please select a product brand or enter a product code.");
			document.frmFind.pbrand.focus();
			return false;
		}
		else if(document.frmFind.ptype.value == "")
		{
			alert("Please select a product type.");
			document.frmFind.ptype.focus();
			return false;
		}
		else if(document.frmFind.pmodel.value == "")
		{
			alert("Please select a product model.");
			document.frmFind.pmodel.focus();
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}


