$(document).ready(function () 
{
	$(document).pngFix();
	hoverCreditInfo();
	hoverFormRow();
	hoverIndex();
	faqTopLink();
	buyCredits();
	PanelLayout();
	
	manageIMEI();
	
});

function forceBlurScreen()
{
		var tb_pathToImage = "images/loadingAnimation.gif";
		imgLoader = new Image();// preload image
		imgLoader.src = tb_pathToImage;
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				//$("#TB_overlay").click(tb_remove);
			}
		}else{//all others
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				//$("#TB_overlay").click(tb_remove);
			}
		}
		
		if(tb_detectMacXFF()){
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
		}

		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$('#TB_load').show();//show loader

}
function clearBlurScreen() {
 	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}



gCredits = "";
gTTime = "";
gTotalIMEIs = 0;
gValidIMEIs = 0;
gPrompt1 = "";
gPrompt2 = "";
gLink1 = "";
gLink2 = "";
gMsg = "";

function hoverCreditInfo()
{
	if ( $('.creditInfo').length )
	{
		$(".creditInfo").hover(
			function () {
				$(this).css("background-color","#333333")
				$(this).css("border","1px #FFFFFF solid")
				$(this).css("color","#FFCC00")
			}, 
			function () {
				$(this).css("background-color","#111111")
				$(this).css("border","1px #666666 solid")
				$(this).css("color","#FFFFFF")
			}
		);
	}
}
function hoverFormRow()
{
	if ( $('.divClearForm').length )
	{
		$(".divClearForm").hover(
			function () {
				$(".divClearForm").css("background-color","")
				$(".divClearForm").css("border","1px #0B0B0B solid")


				$(this).css("background-color","#111111")
				$(this).css("border","1px #333333 solid")
			}, 
			function () {
				$(this).css("background-color","")
				$(this).css("border","1px #0B0B0B solid")
			}
		);
	}
}
function hoverIndex()
{
	if ( $('.BigIcons').length )
	{
		$(".BigIcons").hover(
			function () {
				$(this).animate({opacity: "0.4"},"slow" );
			}, 
			function () {
				$(this).animate({opacity: "1.0"},"fast" );
			}
		);
	}
	
	
	$("#running").hover(
		function () {
			$(this).animate({marginLeft: "+=30"},"slow" );
		}, 
		function () {
			$(this).animate({marginLeft: "-=30"},"slow" );
		}
	);
	$(".specialDownloadsHandle").click(
		function () {
			if($(this).attr("id") == "specialDownloadsHandle2")
			{
				$("#specialDownloads").animate({top:"+=68"},"slow" );
			}
			else
			{
				$("#specialDownloads").animate({top:"-=68"},"fast" );
			}
			$(".specialDownloadsHandle").toggle();
		}
	);
}

function faqTopLink()
{
	$('.faq .question').append('&nbsp; <a href="#index"><img src="./images/back_to_top.gif" height="16" width="16" alt="Back to top" /></a>');
}
function buyCredits()
{
	$(".creditRadio").parent().css("cursor","pointer");
	$(".creditRadio").click(
		function () {
			$(".creditRadio").parent().css("color","#FFFFFF");
			$(".creditRadio").parent().css("font-size","11px");
			$(".creditRadio").parent().css("font-family","tahoma");
			$(".creditRadio").parent().css("margin-top","0px");
			
			//$(".div_credits").css("opacity","0.2");
			$(this).parent().animate({"opacity": "1"}, "slow");
			
			$(this).parent().css("color","#FFFF00");
			$(this).parent().css("font-size","16px");
			$(this).parent().css("font-family","arial");
			
			
			if($(this).attr("value") > 200)
			{
				$("#divPay1").css("display","none");
				$("#divPay4").css("display","none");
			}
			else
			{
				$("#divPay1").css("display","block");
				$("#divPay4").css("display","block");
			}
		}
	);
	
	
	//$(".div_payment").css("opacity","0.2");
	$(".paymentRadio").parent().css("cursor","pointer");
	$(".paymentRadio").click(
		function () {
			//$(".div_payment").animate({"opacity": "0.2"}, 10);
			//$(".div_payment").css("opacity","0.2");
			//$(this).parent().animate({"opacity": "1"}, "slow");
		}
	);
}

function PanelLayout()
{
	if ( $('.bigIcon > li').length )
	{
		$(".bigIcon > li").hover(
			function () {
				$(this).css("background-color","#101018")
				$(this).css("border","1px #FFFFFF solid")
				$(this).css("color","#FFCC00")
			}, 
			function () {
				$(this).css("background-color","")
				$(this).css("border","1px #333333 solid")
				$(this).css("color","#FFFFFF")
			}
		);
	}
	
	if ( $('.NaviLeftHeader').length )
	{
		$(".NaviLeftHeader").hover(
			function () {
				$(this).css("border","1px #AAAAAA solid")
				$(this).css("background-position","0px -46px")
				$(this).css("color","#FFCC00")
			}, 
			function () {
				$(this).css("border","1px #333333 solid")
				$(this).css("background-position","0px 0px")
				$(this).css("color","#FFFFFF")
			}
		);
		$(".NaviLeftHeader").click(
			function () {
				$(this).next().slideToggle("slow");
			}
		);
		$(".NaviLeftBody > ul > li").hover(
			function () {
				$(this).css("background-color","#101018");
				$(this).css("color","#FFCC00");
			}, 
			function () {
				$(this).css("background-color","");
				$(this).css("color","#FFFFFF");
			}
		);
	}
	
	if ( $('#brand').length )
	{
		//Brand Change Events
		$("#brand").change(
			function () {
				loadBrandDetails();
		})
		.change();
		$("#brand").keydown(
			function (event) {
				loadBrandDetails();
		});
	}
	if ( $('#operator').length )
	{
		//Operator Change Events
		var toExe = true;
		$("#operator").change(
			function () {
				setDetails();
				toExe = false;
		})
		.change();
		$("#operator").keydown(
			function (event) {
				if(toExe == true)
				{
					setDetails();
				}
				toExe = true;
		});
	}
	if ( $('.searchHideShow').length )
	{
		
		$(".searchHideShow").click(
		function () {
			if($("#searchForm").is(":hidden"))
			{
				$("#searchForm").slideDown("slow");
				$(".searchHideShow").html("Hide Search");
			}
			else
			{
				$("#searchForm").slideUp("slow");
				$(".searchHideShow").html("Show Search");
			}
		}
		);
	}
	
}

function loadBrandDetails()
{
	$("#operatorDivs").prepend('<img src="./images/progress.gif" /><br />');
	randNum = Math.random()*100;
	$.ajax({
	   type: "GET",
	   url: "_brand_details.php",
	   data: "id=" + randNum + '&brand=' + $("#brand").attr("value"),
	   success: function(msg){
		   $("#operatorDivs").html(msg);
	   }
	 });
}

//End Load Brand Details
function setDetails()
{
	var val = $("#operator").attr("value");
	var strArr = val.split("~#$Aftg567%~");
	gCredits = strArr[1];
	gTTime = strArr[2];
	gPrompt1 = strArr[3];
	gPrompt2 = strArr[4];
	gLink1 = strArr[5];
	gLink2 = strArr[6];
	gMsg = strArr[7];
	$("#divTurnaroundTime").html(gTTime);
	$("#divOperatorMsg").html(gMsg);
	calc();
}
function calc()
{
	val = $("#imeiNumber").attr("value");
	this.gTotalIMEIs = 0;
	this.gValidIMEIs = 0;
	if(val != "")
	{
		val = val.replace(new RegExp( "\\n", "g" ),",");
		val = val.replace(";",",");
		valArr = val.split(",");
		strTemp = "";
		count = valArr.length;
		for(counter=0;counter<count;counter++)
		{
			strTemp = valArr[counter];
			if(strTemp != "")
			{
				this.gTotalIMEIs = this.gTotalIMEIs + 1;
				if(IsNumeric(strTemp) && strTemp.length==15)
				{
					this.gValidIMEIs = this.gValidIMEIs + 1;
				}
			}
		}
	}else{
		count = 0;
	}
	if(gTotalIMEIs == gValidIMEIs)
	{
		$("#submitIMEIButton").show();
	}
	else
	{
		$("#submitIMEIButton").hide();
	}
	if(this.gPrompt1 != ""){
		$("#prompt_1_caption").html(this.gPrompt1);
		$("#prompt_1").show();
	}
	else{
		$("#prompt_1").hide();
	}
	if(this.gLink1 != ""){
		$("#link_1").html(' <a href="' + this.gLink1 + '" target="_blank">More Details</a>');
	}
	else{
		$("#link_1").html("");
	}
	if(this.gPrompt2 != ""){
		$("#prompt_2_caption").html(this.gPrompt2);
		$("#prompt_2").html("");
	}
	else{
		$("#prompt_2").html("none");
	}
	if(this.gLink2 != ""){
		$("#link_2").html(' <a href="' + this.gLink2 + '" target="_blank">More Details</a>');
	}
	else{
		$("#link_2").html("");
	}
	
	$("#divValidIMEIs").attr("value","Total: " + gTotalIMEIs + "  Valid:" + gValidIMEIs);
	$("#divCredits").html((this.gCredits) + " x " + gTotalIMEIs + " = " + (this.gCredits * gTotalIMEIs));
}

function IsNumeric(sText)
{
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}


function showButtons(proId)
{
	$("#hiddenFileds" + proId).show();
	$("#addToCartButton" + proId).hide();
	
}
function hideButtons(proId)
{
	$("#hiddenFileds" + proId).hide();
	$("#addToCartButton" + proId).show();
	
}

function silentAddToCart(id,qty,divName)
{
	$("#" + divName).html('<br /><br /><br /><img src="./images/progress.gif" />');
	randNum = Math.random()*100;
	$.ajax({
	   type: "GET",
	   url: "addToCart.php",
	   data: "id=" + randNum + "&id=" + id + "&qty=" + qty,
	   success: function(msg){
		   $("#" + divName).html('<br /><br /><br />' + msg);
	   }
	 });
}

function getUserName(newUsername)
{
	if(newUsername != "")
	{
		$("#usernameIcon").show();
		$("#usernameIcon").html('<img src="./images/progress.gif" />');
		randNum = Math.random()*100;
		$.ajax({
		   type: "GET",
		   url: "customer_add_new_check_username.php",
		   data: "id=" + randNum + "&id=" + randNum + "&newUsername=" + newUsername,
		   success: function(msg){
				if(msg == "YES")
				{
					$("#usernameIcon").html('<img src="./newicons/icon_log_unavail_small.png" />');
				}
				else
				{
					$("#usernameIcon").html('<img src="./newicons/icon_log_avail_small.png" />');
				}
		   }
		 });
	}
}

function manageIMEI()
{
	$(".speicalButtonCancelIMEI").click(
		function(){
			if(confirm("Are you sure you want to cancel the selected IMEI?"))
			{
				var objThis = $(this);
				var randNum = Math.random()*100;
				forceBlurScreen();
				$.ajax({
					   type: "POST",
					   url: "_ajax_cancel_imei.php",
					   data: "imei=" + objThis.find("div:first").html() + "&randNum=" + randNum,
					   error: function(){
						 alert("Can't cancel your IMEI at present! There is some technical problem.");
						 clearBlurScreen();
					 	 },
					   success: function(msg){
						 //objThis.parent().parent().hide("Slow");
						 if(msg == "YES")
						 {
							objThis.parent().parent().hide("Slow");
						 }
						 else
						 {
							alert("Can't cancel your IMEI!");
						 }
						 clearBlurScreen();
					   }
				});
			}
		});
	$(".speicalButtonArchiveIMEI").click(
		function(){
			if(confirm("Are you sure you want to move your IMEI to Archive List?"))
			{
				var objThis = $(this);
				var randNum = Math.random()*100;
				forceBlurScreen();
				$.ajax({
					   type: "POST",
					   url: "_ajax_archive_imei.php",
					   data: "imei=" + objThis.find("div:first").html() + "&randNum=" + randNum,
					   error: function(){
						 alert("Can't make your IMEI archive at present! There is some technical problem.");
						 clearBlurScreen();
					 	 },
					   success: function(msg){
						 //objThis.parent().parent().hide("Slow");
						 alert(msg);
						 if(msg == "YES")
						 {
							objThis.parent().parent().hide("Slow");
						 }
						 else
						 {
							alert("Can't send you IMEI to archive list!");
						 }
						 clearBlurScreen();
					   }
				});
			}
		});
	$(".speicalButtonVerifyIMEI").click(
		function(){
			if(confirm("Are you sure you want to verify your IMEI?"))
			{
				var objThis = $(this);
				var randNum = Math.random()*100;
				forceBlurScreen();
				$.ajax({
					   type: "POST",
					   url: "_ajax_verify_imei.php",
					   data: "imei=" + objThis.find("div:first").text() + "&randNum=" + randNum,
					   error: function(){
						 alert("Can't send your IMEI for verification at present! There is some technical problem.");
						 clearBlurScreen();
					 	 },
					   success: function(msg){
						 //objThis.parent().parent().hide("Slow");
						 if(msg == "YES")
						 {
							alert("IMEI Sent for verification!");
						 }
						 else
						 {
							alert("Can't send your IMEI for verification!");
						 }
						 clearBlurScreen();
					   }
				});
			}
		});
}