﻿


//-------------------------------------------------- Get Browser Data -------------------------------------------------------------------------------------
var detect = navigator.userAgent.toLowerCase();

function GetBrowserData()
{
	var browser;
	
	if(checkIt('msie')) 
	{
		browser = "IE";
	}
	else
	{
		browser = "";
	}
	return browser;
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}




//--------------- Definition of "outerHTML" for not IE browsers -------------------------
if(GetBrowserData() != "IE")
{
	var _emptyTags = {
	"IMG":   true,
	"BR":    true,
	"INPUT": true,
	"META":  true,
	"LINK":  true,
	"PARAM": true,
	"HR":    true
	};

	HTMLElement.prototype.__defineGetter__("outerHTML", function () {
	var attrs = this.attributes;
	var str = "<" + this.tagName;
	for (var i = 0; i < attrs.length; i++)
		str += " " + attrs[i].name + "=\"" + attrs[i].value + "\"";

	if (_emptyTags[this.tagName])
		return str + ">";

	return str + ">" + this.innerHTML + "<" + this.tagName + ">";
	});

	HTMLElement.prototype.__defineSetter__("outerHTML", function () {
	var attrs = this.attributes;
	var str = "<" + this.tagName;
	for (var i = 0; i < attrs.length; i++)
		str += " " + attrs[i].name + "=\"" + attrs[i].value + "\"";

	if (_emptyTags[this.tagName])
		return str + ">";

	return str + ">" + this.innerHTML + "<" + this.tagName + ">";
	});

	HTMLElement.prototype.__defineSetter__("outerHTML", function (sHTML) {
	var r = this.ownerDocument.createRange();
	r.setStartBefore(this);
	var df = r.createContextualFragment(sHTML);
	this.parentNode.replaceChild(df, this);
	});
}





//------------------------- AJAX -----------------------------------------------------
function GetXmlResponse(DataToSend,URL,binText)
{
	var xmlhttp;
    if(GetBrowserData() != "IE") 
	{
        xmlhttp = new XMLHttpRequest();
    } 
	else if(GetBrowserData() == "IE")  
	{
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
	else
	{
		xmlhttp = false;
	}
	
	if(xmlhttp)
	{
   	 	xmlhttp.open("POST",URL + '?rand='+ Math.random() ,false);
   		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   	 	xmlhttp.send(DataToSend);
    	return(binText?xmlhttp.responseText:xmlhttp.responseXML);
	}
}


//-------------------------- Validation functions ------------------------------------

//Triming string from both sides.
function Trim(sString)
{
	while (sString.substring(0,1) == " ")
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == " ")
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}


//Validates email.
function ValidEmail(m)
{
	m = Trim(m);
	if(m.indexOf("@")<1||m.indexOf(".")==-1||m.lastIndexOf(".")==(m.length-1)||m.indexOf("@")-m.indexOf(".")==1||m.lastIndexOf(".")-m.indexOf("@")==1||m.lastIndexOf(".")-m.indexOf("@")==1||m.indexOf("@")!=m.lastIndexOf("@"))
		return false;		
	return true;
}


//Checks if string is an numeric.
function IsNumeric(sText)
{
	var ValidChars = "0123456789";
	var IsNumber = true;
	var Char;
	
	sText = Trim(sText);
	
	if(sText.length == 0)
		return false;

	for (var i=0; i<sText.length && IsNumber == true; i++) 
	{ 
	    Char = sText.charAt(i); 
	    if (ValidChars.indexOf(Char) == -1) 
	    {
	        IsNumber = false;
	    }
	}
	return IsNumber;
}

//Validates cell phone.
function IsLegalCell(cell)
{
	if(cell.length != 11)
	{
		return false;
	}
	if(cell.substr(0,1) != "0")
	{
		return false;
	}
	if(!IsInteger(cell.substr(1,2)))
	{
		return false;
	}
	if(cell.substr(3,1) != "-")
	{
		return false;
	}
	if(!IsInteger(cell.substr(2,1)))
	{
		return false;
	}
	if(!IsInteger(cell.substr(4,7)))
	{
		return false;
	}
	return true;
}

//Validates phone.
function IsLegalPhone(phone)
{
	if(phone.length != 10)
	{
		return false;
	}
	if(phone.substr(0,1) != "0")
	{
		return false;
	}
	if(phone.substr(2,1) != "-")
	{
		return false;
	}
	if(!IsInteger(phone.substr(1,1)))
	{
		return false;
	}
	if(!IsInteger(phone.substr(3,7)))
	{
		return false;
	}
	return true;
}

//Custom Phone field.
function CustomPhone(phone)
{
	phone = Trim(phone);
	if(phone.length >= 2 && phone.indexOf("-") != 2)
	{
		phone = phone.substr(0,2) + "-" +  phone.substr(2);
	}
	return phone;
}

//Custom Cell field.
function CustomCell(cell)
{
	cell = Trim(cell);
	if(cell.length >= 3 && cell.indexOf("-") != 3)
	{
		cell = cell.substr(0,3) + "-" +  cell.substr(3);
	}
	return cell;
}

//Checks if file is of type given.
function CheckFileType(assignedFile,fileType)
{
	assignedFile = assignedFile.substr(assignedFile.lastIndexOf('\\'));
	var assignedFileExt = assignedFile.substr(assignedFile.lastIndexOf('.')).toLowerCase();
	if(assignedFileExt != ('.' + fileType.toLowerCase()))
	{
		return false;
	}
	else
	{
		return true;
	}
}

//Show Dialog.
function ShowDialog(href,title,width,height)
{
	try
	{
		NewWindow = window.open(href,title,"resizable=0,location=0,top=100,left=200,directories=0,status=0,menubar=0,scrollbars=0,toolbar=0,width=" + width + ",height=" + height);
		NewWindow.document.close();
	}
	catch(ex){}	
}

//Home page.
function SetSiteAsHomePage(obj)
{
	if(GetBrowserData() == "IE")
	{
		obj.style.behavior='url(#default#homepage)';
		obj.setHomePage('http://www.focusnet.co.il');
	}
	else
	{
		alert(".הדפדפן שלך אינו תומך בפעולה זאת");
	}
}

//Reverse string.
function reverse(inp) 
{
  var outp = "";

  for (var ir = 0; ir <= inp.length; ir++) 
  {
    outp = inp.charAt(ir) + outp;
  }

  return outp;
}

//Run on all elements in page where id 
//starts with peefix and ends with suffix do cmd.
function RunOnAllElements(element,prefix,suffix,cmd)
{
	var a = document.getElementsByTagName(element), i = a.length;
	var tmp = "";
	while(i--)
	{
		if( a[i].id && a[i].id.indexOf(prefix)==0 && cutId(a[i].id) == suffix)
		{
			eval (cmd.replace("{e}", "a[" + i + "]"));
		}
	}
}

function showPic(id, img){
	showModalDialog("pics.asp?id=" + id + "&img=" + img, "", "dialogWidth: 670px; dialogHeight: 670px;")
	//alert(window.avilable)
	//window.open("pics.asp?id=" + id + "&img=" + img,"","resizable=0,location=0,top=100,left=200,directories=0,status=0,menubar=0,scrollbars=1,toolbar=0,width=600,height=600");
}

function AddToCart(assetID)
{
	var DataToSend = "assetID=" + assetID;
	var URL = "AddAssetToCart.asp"
	var res = GetXmlResponse(DataToSend,URL,true);

	var AddAssetToCartID ="AddAssetToCart_"+assetID
	document.getElementById("AssetCart").innerText = res;
	//document.getElementById("AssetCart1").innerText = "סל נכסים (" + res + ")";
	//document.getElementById("AssetCart1").style.display = "";
	//document.getElementById("AssetCart").style.display = "";
	existImg = document.getElementById(AddAssetToCartID).src.replace('addBtn', 'existBtn')
	document.getElementById(AddAssetToCartID).src=existImg;
	document.getElementById(AddAssetToCartID).onclick = "";
	document.getElementById(AddAssetToCartID).style.cursor = 'default';
	document.getElementById(AddAssetToCartID).alt = "נמצא בסל";
}

function showpicture(iid)
{
	var picObj = document.getElementById("BigAssetPic"+iid)
	if (picObj.style.display=='none')
	{
		picObj.style.display='';
	}
	else
	{
		picObj.style.display='none';		
	}
	
}
function printAssets()
{
	var printMe = (document.getElementById("printMe")).innerHTML;
	document.frmPrint.printText.value = printMe;
	document.frmPrint.submit();	
}

