/*------------------------------------------------------------------
 File:			functions.js
 Use:			Collection of clients functions.
 
 Updated by Assaf at 19/02/2003.
 function DoSearch():
 Added p_CategoryId that indicate the CategoryId to be include in the search
 
Added new function: function IsDate() by assaf cohen on 03/11/03
Added new function: function IsTime() by assaf cohen on 03/11/03
Added new function: function IsPhoneNumber() by assaf cohen on 03/11/03
-------------------------------------------------------------------*/

//-----------------------------------------------------------------------
// Function:	IsDate
// Use:			Return true if value is date formated as dd/mm/yyyy
//				and 2030-12-31 > value > 1990-1-1 
//
// Details	
// Parameters: 
//	Name				Description
// -------------------------------------------------------------------
//	p_value				String to be search in
//
// Author:    Assaf
// Date:      
//----------------------------------------------------------------------

function IsDate(p_value) {
	var d = new String(p_value);
	var arTmp = new Array();
	arTmp = d.split("/");
	
	//alert(!IsNumber(arTmp[0]));
	
	if (arTmp.length != 3)
		return false;
	if (/\d+/.test(arTmp[2]) && IsNumber(arTmp[2])) {
		if (arTmp[2] < 1900 || arTmp[2] > 2030)
			return false;
	} else {
		return false;
	}
	if (/\d+/.test(arTmp[1]) && IsNumber(arTmp[1])) {
		if (arTmp[1] < 1 || arTmp[1] > 12)
			return false;
	} else {
		return false;
	}
	
	if (/\d+/.test(arTmp[0]) && IsNumber(arTmp[0])) {
		if (arTmp[0] < 1 || arTmp[0] > 31)
			return false;
	} else {
		return false;
	}
	return true;
}

//-----------------------------------------------------------------------
// Function:	IsNumber
// Use:			Return true if value is a number
//
// Details	
// Parameters: 
//	Name				Description
// -------------------------------------------------------------------
//	p_value				String to be search in
//
// Author:    Assaf
// Date:      
//----------------------------------------------------------------------
function IsNumber(p_value){
	if (p_value!=""){
		for(i=0;i<p_value.length;i++){
			if((p_value.charAt(i)<"0")||(p_value.charAt(i)>"9")){ 
				return false;
			}
		}
		return true;
	}
	return false;
}

//-----------------------------------------------------------------------
// Function:	IsTime
// Use:			Return true if value is time formated as hh:mm
//
// Details	
// Parameters: 
//	Name				Description
// -------------------------------------------------------------------
//	p_value				String to be search in
//
// Author:    Assaf
// Date:      
//----------------------------------------------------------------------
function IsTime(p_value) {
	var d = new String(p_value);
	var arTmp = new Array();
	arTmp = d.split(":");
	if (arTmp.length != 2)
		return false;
	if (/\d+/.test(arTmp[0])) {
		if (arTmp[0] < 0 || arTmp[2] > 23)
			return false;
	} else {
		return false;
	}
	if (/\d+/.test(arTmp[1])) {
		if (arTmp[1] < 0 || arTmp[1] > 59)
			return false;
	} else {
		return false;
	}
	return true;
}

//-----------------------------------------------------------------------
// Function:	IsPhoneNumber
// Use:			Return true if value is a Phone number
//
// Details	
// Parameters: 
//	Name				Description
// -------------------------------------------------------------------
//	p_value				String to be search in
//
// Author:    Assaf
// Date:      
//----------------------------------------------------------------------
function IsPhoneNumber(p_value, p_StrLen){
	if (p_value!=""){
		if((p_value.length != p_StrLen) && (p_value.length != (p_StrLen-1)) && (p_value.length != (p_StrLen+1))){
			return false;
		}
		for(i=0;i<p_value.length;i++){
			if((p_value.charAt(i)<"0")||(p_value.charAt(i)>"9")){ 
				if(p_value.charAt(i)!="-"){
					return false;
				} 
			}
		}
		return true;
	}
}
/*-----------------------------------------------------------------
	Validate a form's elements according to varius attributes.
	Parameters: 
    Name				Description
    -------------------------------------------------------------------
    form				form object
    title				alert title to be displayed
    submit				True or false : To submit the form or not.
    validclass	
    invalidclass		Initialize the variable.		
------------------------------------------------------------------*/
function validateForm(form, title, submit, validclass, invalidclass)
{
	var i, j, u, sum;
	var input;
	var valid;
	var sErrorMsg = "";

	if (invalidclass)
	{
		for (i=0; i<document.styleSheets(0).rules.length; i++)
			if (document.styleSheets(0).rules.item(i).selectorText.toLowerCase() == "." + invalidclass.toLowerCase())
			{
				invalidclass = document.styleSheets(0).rules.item(i).style;
				break;
			}
	}

	for (i=0; i<form.elements.length; i++)
	{
		input = form.elements[i];
		// skip input when it's not rendered (ie. parent display:none)
		if (input.offsetHeight == 0) continue;
		valid = true;

		// Validate value according to element type and validation type
		switch (input.type.toLowerCase())
		{
		case "text":
		case "password":
		case "textarea":
		case "file":
			if (!input.validation) continue;
			if (input.mandatory)
			{
				if (input.mandatory.toLowerCase() == "false" && input.value.length == 0) continue;
			}
			else
			{
				continue;
			}
			switch (input.validation.toLowerCase())
			{
			case "string":
				if (input.value.length == 0)
				{
					valid = false;
				}
				break;
			case "password":
				if (input.value.length == 0)
				{
					valid = false;
				}
				break;
			case "integer":
				if (!/\d+/.test(input.value))
				{
					valid = false;
				}
				break;
			case "email":
				if (!/^[\w\.\-]+@[\w\-]+(\.\w+)+$/.test(input.value.replace(/-/g,"")))
				{
					valid = false;
				}
				break;
			case "phone":
				if (!IsPhoneNumber(input.value, 10))
				{
					valid = false;
				}
				break;
				
			case "phonewithoutprefix":
				if (!IsPhoneNumber(input.value, 7))
				{
					valid = false;
				}
				break;	
				
			case "date":
				if (!IsDate(input.value))
				{
					valid = false;
				}
				break;
			case "time":
				if (!IsTime(input.value))
				{
					valid = false;
				}
				break;
			case "id":
				j = input.value.toString();
				input.value = j.replace(/\D/g, "");
				if (/\d+/.test(input.value))
				{
					sum = 0;
					for (j=0; j<input.value.length; j++)
					{
						u = (j % 2 ? 2 : 1) * parseInt(input.value.charAt(input.value.length - j - 1));
						sum += u > 9 ? Math.floor(u / 10) + u % 10 : u;
					}
					if (sum % 10) valid = false;
				}
				else
				{
					valid = false;
				}
				break;
			case "compare":
				// Check the compareInput attribute
				if (input.compareInput)
					if (input.value != form.elements[input.compareInput].value)
						valid = false;
			}

			// Validate max and min according to validation type
			if (valid == true)
			{
				switch (input.validation.toLowerCase())
				{
				case "integer":
					if (input.validmax)
					{
						if (parseInt(input.value) > parseInt(input.validmax))
							valid = false;
					}
					if (input.validmin)
					{
						if (parseInt(input.value) < parseInt(input.validmin))
							valid = false;
					}
					break;
				default:
					if (input.validmax)
					{
						if (input.value.length > input.validmax) valid = false;
					}
					if (input.validmin)
					{
						if (input.value.length < input.validmin) valid = false;
					}
					break;
				}
			}
			break;
		case "select-one":
			if (input.mandatory)
			{
				if (input.selectedIndex == 0) valid = false;
			}
			break;
		case "select-multiple":
			sum = 0;
			for (j=0; j<input.options.length; j++)
			{
				if (input.options[j].selected) sum++;
			}
			if ((!input.validmax) && (!input.validmin))
			{
				if (sum == 0) valid = false;
			}
			else
			{
				if (input.validmax)
				{
					if (sum > input.validmax) valid = false;
				}
				if (input.validmin)
				{
					if (sum < input.validmin) valid = false;
				}
			}
			break;
		case "checkbox":
			if (input.mandatory)
			{
				if (!input.checked) valid = false;
			}
			break;
		}

		if (!valid)
		{
			if (input.validationError)
			{
				sErrorMsg += input.validationError + "\n";
			}
			else
			{
				sErrorMsg += "Error in field " + input.name + "\n";
			}
			if (invalidclass)
			{
				if (typeof(input.originalColor) == "undefined") input.originalColor = input.style.color;
				if (typeof(input.originalBackgoundColor) == "undefined") input.originalBackgoundColor = input.style.backgroundColor;
				input.style.color = invalidclass.color;
				input.style.backgroundColor = invalidclass.backgroundColor;
			}
		}
		else
		{
			if (typeof(input.originalColor) != "undefined") input.style.color = input.originalColor;
			if (typeof(input.originalBackgoundColor) != "undefined") input.style.backgroundColor = input.originalBackgoundColor;
		}
	}
	if (sErrorMsg.length)
	{
		alert(title + "\n" + sErrorMsg);
		return(false);
	}
	else
	{
		if (submit)
		{
			form.submit();
		}
		return(true);
	}
}

/*-----------------------------------------------------------------
	Redirect function.
	Updated by Assaf at 19/02/2003.
	Added p_CategoryId that indicate the CategoryId to be include in the search
------------------------------------------------------------------*/
function DoSearch(p_CategoryId)
{
	// go to the page doing search
	document.location="search.asp?strSearch=" + document.all.strSearch.value + "&lngCategoryID=" + p_CategoryId
	
	return false;
}

/*-----------------------------------------------------------------
	Return the Previous Link from the links box.
	If the link that shown is the first then 
	the function return the last link
------------------------------------------------------------------*/
function ShowPreviousLink()
{
	if (g_nLinkCounter == 0)
	{
		g_nLinkCounter = g_nAmountOfLinks - 1
	}
	else
	{
		g_nLinkCounter--;
	}
	
	document.all.Links.innerHTML = "<A href=\"javascript:void(0);\" onclick=\"clickMenu(this, g_LinksID[g_nLinkCounter]); return(false)\" class=\"NewsMessages\" TargetRef=\"" + g_LinksTargetRef[g_nLinkCounter] + "\" TypeRef=\"" + g_LinksTypeRef[g_nLinkCounter] + "\" Link=\"" + g_LinksUrl[g_nLinkCounter] + "\">" + g_Links[g_nLinkCounter] + "</A>"
	
}

/*-----------------------------------------------------------------
	Return the next Link from the links box.
	If the link that shown is the last then 
	the function return the first link
------------------------------------------------------------------*/
function ShowNextLink()
{
	
	if (g_nLinkCounter == g_nAmountOfLinks - 1)
	{
		g_nLinkCounter = 0
	}
	else
	{
		g_nLinkCounter++;
	}
	
	document.all.Links.innerHTML = "<A href=\"javascript:void(0);\" onclick=\"clickMenu(this, g_LinksID[g_nLinkCounter]); return(false)\" class=\"NewsMessages\" TargetRef=\"" + g_LinksTargetRef[g_nLinkCounter] + "\" TypeRef=\"" + g_LinksTypeRef[g_nLinkCounter] + "\" Link=\"" + g_LinksUrl[g_nLinkCounter] + "\">" + g_Links[g_nLinkCounter] + "</A>"
	
}

function loadCategoryBySize(p_Id){
	//var w;
	//w = window.open("DisplayOneContentBySize.asp?lngCategoryID=" + p_Id,"loadCategoryBySize" ,"height=1,width=1,top=0,left=0,resizable=yes,scrollbars=yes,status=no,location=no,toolbar=no");
	//w.focus();
	loadImageBPpath(p_Id);
}

var g_DisplayImageWindowName = "ImageWindow";
document.write("<iframe scrolling='no' frameborder=0 id='" + g_DisplayImageWindowName + "' style='position:absolute;top:0px;left:0px;width:1px;height:1px;display:none;z-index:100'></iframe>");
function loadImageBPpath(p_Image){
	var l_ObjIframe = document.getElementById(g_DisplayImageWindowName);
	
	var l_ImagePath;
	var l_lngCategoryID;
	if (IsNumber(p_Image)){
		l_ImagePath = "";
		l_lngCategoryID = p_Image;
	}else{
		l_ImagePath = p_Image;
		l_lngCategoryID = "";
	}
	//alert(window.self.name);
	if(window.self.name == g_DisplayImageWindowName){
		window.location.href = "DisplayImageBySize.asp?ImagePath=" + l_ImagePath + "&lngCategoryID=" + l_lngCategoryID + "&IsNew=1";
	}else{
		l_ObjIframe.src = "DisplayImageBySize.asp?ImagePath=" + l_ImagePath + "&lngCategoryID=" + l_lngCategoryID;
		l_ObjIframe.style.display = 'block';
	}
	
	//var Ww;
	//Ww = window.open("DisplayImageBySize.asp?ImagePath=" + l_ImagePath + "&lngCategoryID=" + l_lngCategoryID,"EggedImages" ,"height=1,width=1,top=0,left=0,resizable=yes,scrollbars=yes,status=no,location=no,toolbar=no");
	//Ww.focus();
}

function loadImageBPpath2(p_Image){
	var l_ImagePath;
	var l_lngCategoryID;
	if (IsNumber(p_Image)){
		l_ImagePath = "";
		l_lngCategoryID = p_Image;
	}else{
		l_ImagePath = p_Image;
		l_lngCategoryID = "";
	}
	var Ww;
	Ww = window.open("DisplayImageBySize.asp?ImagePath=" + l_ImagePath + "&lngCategoryID=" + l_lngCategoryID,"EggedImages" ,"height=1,width=1,top=0,left=0,resizable=yes,scrollbars=yes,status=no,location=no,toolbar=no");
	Ww.focus();
}

function LoadWebsite(p_Site){
	if(p_Site != "")
		window.location.href = p_Site;
}



/*-----------------------------------------------------------------

            Open Window

            Parameters: 

    Name                                                Description

    -------------------------------------------------------------------

    p_Url                         Url to Open
    p_Width        The                   Width of the window
    p_Height                   The Height of the window
    p_Resizable              yes or no
    p_Scrollbars  yes or no
    p_Status                   yes or no
    p_Addressbar            yes or no
    p_Toolbar                  yes or no
------------------------------------------------------------------*/

function OpenNewWindow(p_Url, p_Width, p_Height, p_Resizable, p_Scrollbars, p_Status, p_Addressbar, p_Toolbar)
{
	var Wwind;
	Wwind = window.open(p_Url,"" ,"height=" + p_Height + ",width=" + p_Width + ",top=100,left=100,resizable=" + p_Resizable + ",scrollbars=" + p_Scrollbars + ",status=" + p_Status + ",location=" + p_Addressbar + ",toolbar=" + p_Toolbar);
	Wwind.focus();
}

/*-----------------------------------------------------------------

            Open Window

            Parameters: 

    Name                                                Description

    -------------------------------------------------------------------

    p_Url                         Url to Open
    p_Width        The                   Width of the window
    p_Height                   The Height of the window
    p_Resizable              yes or no
    p_Scrollbars  yes or no
    p_Status                   yes or no
    p_Addressbar            yes or no
    p_Toolbar                  yes or no
    p_WinName	Name of the window
------------------------------------------------------------------*/

function OpenNewWindowWithPosition(p_Url, p_Width, p_Height, p_Resizable, p_Scrollbars, p_Status, p_Addressbar, p_Toolbar, p_Left, p_Top, p_WinName)
{
	if (p_WinName == null)
		p_WinName = "";
	var Wwind2;
	Wwind2 = window.open(p_Url, p_WinName,"height=" + p_Height + ",width=" + p_Width + ",top=" + p_Top + ",left=" + p_Left + ",resizable=" + p_Resizable + ",scrollbars=" + p_Scrollbars + ",status=" + p_Status + ",location=" + p_Addressbar + ",toolbar=" + p_Toolbar);
	Wwind2.focus();
}

