/*Coding to show dialog form*/
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;
}
function close_dialog(divid)
{
	document.getElementById(divid).style.display= 'none';
}

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='';
}
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);
}

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';
		}
	}
}
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");
	}

}

////To fetch data for quick find printer search

var http_dropDown = createRequestObject();

function fill_drop_down(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
		{
			http_dropDown.open('get', 'quick_pri_supply.php?selectID='+select_id+'&action='+action_value+'&brand='+brand_id_value);
		
			/////Response function end here 
			http_dropDown.onreadystatechange = function()
			{
				if(http_dropDown.readyState == 4){
				var response_brand = http_dropDown.responseText;
				////To show message in case of error

				var both_drop_down = new Array();
				try
				{
					if(response_brand.indexOf('|' != -1)) {
						both_drop_down = response_brand.split('|');
						if(id == 'pbrand')
						{
							for(i = 0, j = 1; i < both_drop_down.length; i++, j++)
							{
								option = new Option(both_drop_down[i],both_drop_down[i]);
								document.getElementById('ptype').options[j] = option;
								document.getElementById('ptype').disabled = false;
							}
						}
						else if(id == 'ptype')
						{
							for(i = 0, j = 1; i < both_drop_down.length; i++, j++)
							{
								option = new Option(both_drop_down[i],both_drop_down[i]);
								document.getElementById('pmodel').options[j] = option;				
								document.getElementById('pmodel').disabled = false;   
							}
						}
					}
				}////try end
				catch(ex)
				{
					alert(ex);  
				}
			}
		}
		/////Response function end here
		http_dropDown.send(null);
	}
}

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);  
		}
    }
 
}


