function clear_dynamictextbox()
{
	if ( document.getElementById("pGUIDsHolder") )
	{
		document.getElementById("pGUIDsHolder").value = "";
	}
	if ( document.getElementById("PageNumber") )
		document.getElementById("PageNumber").value = "";
	
}

function submitFormByEnter( obj )
{ 	
	//do submit on return
	if(event.keyCode == '13')
	{
		clear_dynamictextbox();
        obj.submit();
    }
} 
/*******************************************/
	FixImagesDone = false;
	FixImages1Done = false;
	function FixLobbyImages_Dyn( w, h, x)
	{
		if( !FixImagesDone )
		{
			FixImagesDone = true;
			colImages0 = document.getElementsByName( x );
			var theImage;
			var theImages = [];
			for( var i = 0; i < colImages0.length; i++ )
			{
				theImages[i] = new Image();
				theImages[i].index = i;
				theImages[i].w = w;
				theImages[i].h = h;
				colImages0[i].style.visibility = "";
				theImages[i].onload = function()
				{
					this.onload = null;
					Resize_Dyn1(colImages0[this.index],this,this.w,this.h);
					//colImages0[this.index].style.visibility = "";
				}
				theImages[i].onerror = function()
				{
					this.onload = null;
					this.onerror = null;
					colImages0[this.index].style.display = "none";
				}
				theImages[i].src = colImages0[i].src;
			}
		}
	}


	function Resize_Dyn1(container,theImage,c_width,c_height)
	{
		try
		{
			var container_width = Number(c_width);
			var container_height = Number(c_height);
			var height = theImage.height;
			var width = theImage.width;
			if( height > container_height || width > container_width )
			{
				var ratio1 = container_height/height;
				var ratio2 = container_width/width;
				var ratio = Math.min(ratio1,ratio2);
				container.width = width*ratio;
				container.height = height*ratio;
			}
			else
			{
				container.width = width;
				container.height = height;
			}
		}
		catch(e){}
	}
/*****************************************/	
function resetFrm( obj )
{
	var fElm=obj.elements;
	for(i=0;i<fElm.length;i++)
	{
	if(fElm[i].type=="text")
		fElm[i].value = "";
	if(fElm[i].type=="hidden")
		fElm[i].value = "";
	if(fElm[i].type=="file")
		fElm[i].value = "";
	if(fElm[i].type=="textarea")
		fElm[i].value = "";
	if(fElm[i].type=="select-one")
		fElm[i].value=-1;
	}
		
}	

function hideShow(x)
{
	if (document.getElementById(x).style.display == '')
	{
		document.getElementById(x).style.display = 'none';
	}else{
		document.getElementById(x).style.display = '';
	}
}
function openWindow(url,w,h,s) {
	if(!s) 
	{
		s = 'yes';
	}
	var wizWin = window.open(url, null, 'width='+w+',height='+h+',status=no,menubar=no,titlebar=no,scrollbars='+s+',resizable=no')
}


var ObjDisplayed = new Object(); 
ObjDisplayed.id=""; 

function disp(id) { 
   if (ObjDisplayed.id!="") {
     ObjDisplayed.style.display="none";
     } 
   
   var Obj = eval("document.all.tr"+id); 
   if (Obj!=ObjDisplayed) { 
      ObjDisplayed=Obj; 
      Obj.style.display="inline"; 
    } else { 
      ObjDisplayed= new Object(); 
	  ObjDisplayed.id=""; 
	}
}

function DoFormSubmit( sourceForm, action, target, params, sourceDescription, index ) 
{
	var sourceElement; 
	if( index == null )
		index = "";
	var tmpForm = "<Form id='tmpForm' method='post' action='" + action + "' target='" + target + "'>"; 
	for( var i = 0; i < params.length; i++ ) 
	{ 
		try
		{ 
			sourceElement = document.getElementById(params[i] + index); 
			if(sourceElement.type == "checkbox")
			{
				if ( sourceElement.checked )
					tmpForm += "<Input type='hidden' value='on' name='" + params[i] + "' />";
				else
					tmpForm += "<Input type='hidden' value='off' name='" + params[i] + "' />";
			}
			else
			{
				// It works now only for text fields. If you need more, add checking of tagname here 
				tmpForm += "<Input type='hidden' value='" + sourceElement.value + "' name='" + params[i] + "' />";
			}
		} 
		catch( Ex ) {} 
	} 
	if( sourceDescription != undefined && sourceDescription != null )
		tmpForm += "<Input type='hidden' value='" + sourceDescription + "' name='sourceDescription' />"
	tmpForm += "</Form>"; 
	document.body.insertAdjacentHTML('beforeEnd', tmpForm); 
	document.forms[document.forms.length-1].submit(); 
}
if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement)
{
	HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode)
	{
		switch (where)
		{
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this)
			break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild);
			break;
		case 'beforeEnd':
			this.appendChild(parsedNode);
			break;
		case 'afterEnd':
			if (this.nextSibling) 
				this.parentNode.insertBefore(parsedNode,this.nextSibling);
			else 
				this.parentNode.appendChild(parsedNode);
			break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr)
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	}


	HTMLElement.prototype.insertAdjacentText = function(where,txtStr)
	{
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	}
}
function SearchIt(){
if(document.searchit.searchbox.value.length>0){
 document.searchit.action="";
 document.searchit.submit(); 
}}

function RegForm(){
if(document.regform.email.value.length>0){
 document.regform.action="";
 document.regform.submit(); 
}}

function ProgramForm(){
if(document.programform.ProgramName.value.length>0){
 document.programform.action="";
 document.programform.submit(); 
}}

function OpenWin( target, url , w, h)
{
	var x =( screen.width - 550 ) / 2 ;
	var y =( screen.height - 200 ) / 2;
	oWin = window.open(url, null , "resizable=yes,scrollbars=1,menubar=0,titlebar=0,location=0,width="+w+",height="+h+",top=" + x + ",left=" + y);
}


//Forms

function SearchIt(){
if(document.searchit.searchbox.value.length>0){
 document.searchit.action="";
 document.searchit.submit(); 
}}

function RegForm(){
if (isNull( document.regform.thetype , "the Type" ))
    return false;
if(!isEmailAddr(document.regform.email )){
    return false; 
}
 document.regform.action="";
 document.regform.submit(); 
}

var alertStr = "";
function ProgramForm(){
	alertStr = "";
	if (isNull( document.programform.fname , "the First name" ))
		return false;
	if (isNull( document.programform.lname , "the Last name" ))
		return false;
	if (isNull( document.programform.fname , "the Last address" ))
		return false;
	if (isNotSelected( document.programform.country, "the Country" ))
		return false;
	if(!isEmailAddr(document.programform.email ))
		return false; 
	if (isNull( document.programform.date_o_b , "the Date of birth" ))
		return false;

 document.programform.action="";
 document.programform.submit(); 
}

function ScholarshipsForm(){
if(document.scholarshipsform.fname.value.length>0){
 document.programform.action="";
 document.programform.submit(); 
}}

function CalcForm(){
if (isNotSelected( document.calcform.country, "the Country" ))
    return false;
if (isNull( document.calcform.y_income , "the Yearly income" ))
    return false;
if (isNull( document.calcform.y_income , "the Yearly income" ))
    return false;
if (isNull( document.calcform.f_m , "the Number of family members" ))
    return false;	
if (isNull( document.calcform.date_o_b , "the Date of birth" ))
    return false;
if (isNotSelected( document.calcform.status, "the Status" ))
    return false;

 document.calcform.action="";
 document.calcform.submit(); 
}
function CregisterForm(){
alertStr = "";
if (isNull( document.cregisterform.fname , "the First name" ))
    return false;
if (isNull( document.cregisterform.lname , "the Last name" ))
    return false;
if (isNull( document.cregisterform.city , "the city name" ))
    return false;
if (isNotSelected( document.cregisterform.CountryId, "the Country" ))
    return false;
if(!isEmailAddr(document.cregisterform.email ))
    return false;
if (isNull( document.cregisterform.phone , "the phone" ))
    return false; 
if (isNotChecked(document.cregisterform.Gender , "the gender" ))
return fakse;
if (isNull( document.cregisterform.school_n , "the schoole name" ))
    return false;     
if (isNull( document.cregisterform.school_n_desc , "the school description" ))
    return false;
if (isNull( document.cregisterform.ym_n , "the Youth movements name" ))
    return false;     
if (isNull( document.cregisterform.ym_n_desc , "the Youth movements description" ))
    return false;
if (isNotSelected( document.cregisterform.religion, "the Religious" ))
    return false;
if (isNull( document.cregisterform.hobbies , "the hobbies" ))
    return false;
if (isNotSelected( document.cregisterform.interest, "the interest" ))
    return false;
if (isValidCVFile(document.cregisterform.AttachCV))
	return false;
 if (isValidPicFile(document.cregisterform.AttachPic))
	return false;

 //document.cregisterform.action="";
 
 
 if ( alertStr.length > 0 )
		{
			alert (alertStr)
			return false;
		}
	else
	document.cregisterform.submit();
}

function stfForm()
{
	alertStr = "";
	if (isNull( document.stfform.from_name , "the sender Name" ))
		return false;
	if(!isEmailAddr(document.stfform.from_email )){
		return false; 
	}
	if (isNull( document.stfform.to_name , "the recipient Name" ))
		return false;
	if(!isEmailAddr(document.stfform.to_email )){
		return false; 
	}
	if ( alertStr.length > 0 )
		{
			alert (alertStr)
			return false;
		}
	else
	{
		document.stfform.action="";
		document.stfform.submit(); 
	}
}

function ContactusForm(){
	alertStr = "";
	if (isNotSelected( document.Form1.contact_to , "the Contact name" ))
		return false;
	if (isNull( document.Form1.subject , "the Subject" ))
		return false;
	if(!isEmailAddr(document.Form1.email ))
		return false; 
	if (isNull( document.Form1.f_name , "the First name" ))
		return false;
	if (isNull( document.Form1.l_name , "the Last name" ))
		return false;
	
	if ( alertStr.length > 0 )
		{
			alert (alertStr)
			return false;
		}
	else
	{
		document.Form1.submit(); 
	}
}

function OrganizerForm(){
	alertStr = "";
	//debugger
	if (isNotSelected( document.Form1.organizerName , "the Organizer" ))
		return false;
	if (isNotSelected( document.Form1.organizerType , "the Organizer type" ))
		return false;
	// 4 addresses
	var addressTotal = document.getElementById("add_duplicate").value;
	for ( x=1;x<=addressTotal;x++ )
	{
		if (isNull( document.getElementById("Street"+x) , "the Street "+x ))
			return false;
		if (isNotSelected( document.getElementById("Orgenaizeraddrs"+x+":CityId") , "the City "+x ))
			return false;			
		if (isNotSelected( document.getElementById("Orgenaizeraddrs"+x+":CountryId") , "the Country "+x ))
			return false;
	}

	// local    
	if(!isEmailAddr(document.Form1.Email ))
		return false; 
	if (isNull( document.Form1.Phone, "the Telephone" ))
		return false;
	if (isNull( document.Form1.institutionManager , "the Director" ))
		return false;
	if (isNotSelected( document.Form1.religion , "the Religious affiliation" ))
		return false;
	if (isNotSelected( document.Form1.religion , "the institution History" ))
		return false;

	if ( alertStr.length > 0 )
		{
			alert (alertStr)
			return false;
		}
	else
	{
		document.Form1.OrganizerNameValue.value = document.Form1.organizerName.options[document.Form1.organizerName.selectedIndex].text;
		document.Form1.submit(); 
	}
}


function ProgramLobbyForm(){
if (isNotSelected( document.programlobbyform.country, "the Country" ))
    return false;
if (isNotSelected( document.programlobbyform.interest_sub, "the Interest/Subject" ))
    return false;
if (isNotSelected( document.programlobbyform.language, "the Language" ))
    return false;
if (isNull( document.programlobbyform.orgaziner , "the orgaziner" ))
    return false;

 document.programlobbyform.action="";
 document.programlobbyform.submit(); 
}



function ProgramParticipant(){

alertStr = "";
if (isNull( document.Form1.ProgramName , "the Program" ))
    return false;
if (isNotSelected( document.Form1.ProgramType, "the Program type" ))
    return false;
if (isNull( document.Form1.ShortDescription , "the Program summary" ))
    return false;
if (isNull( document.Form1.LongDescription , "the Program description" ))
    return false;
if (isNull( document.Form1.SearchSubject, "the Program subject" ))
    return false;
if (isNull( document.Form1.KeyWords , "the Program keywords" ))
    return false;

///////////start checking the dates//////////////
if (isNotSelected( document.Form1.FromDate_Month , "the Start date - month" ))
    return false;
if (isNotSelected( document.Form1.FromDate_Year , "the Start date - year" ))
    return false;
if (isNotSelected( document.Form1.ToDate_Month , "the end date - month" ))
    return false;
if (isNotSelected( document.Form1.ToDate_Year , "the end date - year" ))
    return false;
    
if (checkDateParam(document.Form1.FromDate1_Month,document.Form1.FromDate1_Year, "start date 2"))
	return false;
if (checkDateParam(document.Form1.ToDate1_Month,document.Form1.ToDate1_Year, "end date 2"))
	return false;
	
if (checkDateParam(document.Form1.FromDate2_Month,document.Form1.FromDate2_Year, "start date 3"))
	return false;
if (checkDateParam(document.Form1.ToDate2_Month,document.Form1.ToDate2_Year, "end date 3"))
	return false;
	
if (checkDateParam(document.Form1.FromDate3_Month,document.Form1.FromDate3_Year, "start date 4"))
	return false;
if (checkDateParam(document.Form1.ToDate3_Month,document.Form1.ToDate3_Year, "end date 4"))
	return false;
	
if (checkValideDate(document.Form1.FromDate_Day, document.Form1.FromDate_Month, document.Form1.FromDate_Year,document.Form1.ToDate_Day, document.Form1.ToDate_Month, document.Form1.ToDate_Year) )
	return false;
	
if (isNotSelected( document.Form1.ProgramTime, "the Program length" ))
    return false;
if (isNotSelected( document.Form1.DiplomaType , "the Type of diploma awarded" ))
    return false;
if (isNotSelected( document.Form1.CertType , "the Type of certificate awarded" ))
    return false;
    if (isNotSelected( document.Form1.ProgLocation , "the Program location" ))
    return false;
if (isNotSelected( document.Form1.LocationType , "the Type of Program location" ))
    return false;
if (isNotSelected( document.Form1.ProgramLangauge, "the Language" ))
    return false;
if (isNull( document.Form1.participantNum , "the Number of participants" ))
    return false;
    
if (isNotSelected( document.Form1.FromAge, "the From age" ))
    return false;
if (isNotSelected( document.Form1.ToAge, "the To age" ))
    return false;
if (checkValideAge(document.Form1.FromAge, document.Form1.ToAge))
	return false;
if (document.Form1.theCountry.length > 0)
if (isNull( document.Form1.theCountry[0] , "the Open to participants from" ))
    return false;
  //alert(document.Form1.theCountry.length);
if (isNull( document.Form1.price , "the Program price" ))
    return false;
if (isNotSelected( document.Form1.ParticipantsGender , "theParticipants Gender" ))
    return false;
if (isNotChecked( document.Form1.OutSupport , "the Governmental support" ))
    return false;
if (isNotChecked( document.Form1.participantsupport , "the Institution subsidization" ))
    return false;

	if ( alertStr.length > 0 )
		{
			alert (alertStr)
			return false;
		}
	else
	document.Form1.submit(); 
}


// Open browser window
function MM_openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}








// Validation functions
function onlyNumber()
{
	if((event.keyCode>= 65 && event.keyCode<= 96) || 
			(event.keyCode>= 48 && event.keyCode<= 57)|| 
			 event.keyCode==9 || event.keyCode==46 || event.keyCode==8 ||event.keyCode==37||event.keyCode==17||event.keyCode==16||event.keyCode==39||event.keyCode==46||event.keyCode==35){
		return true;
	} else {
	  alert("Please enter only digits");
	  return false;
        }

}
function alphaNumericCheck()
{
	if((event.keyCode >= 65 && event.keyCode <= 90) || (event.keyCode >= 97 && event.keyCode <= 122)
			|| (event.keyCode >= 48 && event.keyCode <= 57) || event.keyCode == 8)
		return true;
	else
	{
		alert("Please enter only alphanumeric characters.");
		return false;
	}
}

function checkValideDate(FromDate_Day,FromDate_Month, FromDate_Year, ToDate_Day, ToDate_Month, ToDate_Year)
{
	var d1, d2;
	//var arrFrom = FromDate.value.split("/");
	//var d1 = new Date(arrFrom[1] + '/' + arrFrom[0] + '/' + arrFrom[2]);
	if (FromDate_Day.value == "")
		d1 = new Date(FromDate_Month.value + '/1/' + FromDate_Year.value);
	else
		d1 = new Date(FromDate_Month.value +'/' + FromDate_Day.value + '/' + FromDate_Year.value);
	
	//var arrTo = ToDate.value.split("/");
	//var d2 = new Date(arrTo[1] + '/' + arrTo[0] + '/' + arrTo[2]);
	if (ToDate_Day.value == "")
		d2 = new Date(ToDate_Month.value + '/1/' + ToDate_Year.value);
	else
		d2 = new Date(ToDate_Month.value + '/' + ToDate_Day.value + '/' + ToDate_Year.value);
		
	if (d1 > d2)
		alertStr +="Please enter valid date\n";
	return false;
}

function checkValideAge(FromAge, ToAge)
{
	if (FromAge.options[FromAge.selectedIndex].text > ToAge.options[ToAge.selectedIndex].text)
		alertStr +="Please enter valide age\n";
		return false;
}
function isNotSelected(txtName,msg){
   //alert(txtName.value)
   if(txtName.value=="Select" || txtName.value=="" || txtName.value=="-1"){
    alertStr += "Please enter "+msg + "\n";
    //alert("Please select "+msg)
    txtName.focus()
    //txtName.select()
    //return true
   }
   return false
}

function isValidCVFile(txtName)
{
	if (txtName.value != "")
	{
		var end = txtName.value.substr(txtName.value.length-4,4).toLowerCase();
		if (end != ".doc" && end != ".pdf" && end != ".txt")
			alertStr += "Please insert valide cv file \n";
	}
	return false;
}

function isValidPicFile(txtName)
{
	if (txtName.value != "")
	{
		var end = txtName.value.substr(txtName.value.length-4,4).toLowerCase();
		if (end != ".gif" && end != ".jpg")
			alertStr += "Please insert valide picture file\n";
	}
	return false;
}
function isNotChecked(txtName,msg)
{
radio_status = false

for (counter = 0; counter < txtName.length; counter++){
 if (txtName[counter].checked){
  radio_status = true; 
  }
 }
 if (!radio_status){
   //alert("Please select "+msg)
   alertStr += "Please enter "+msg + "\n";
   //txtName.focus()
   //return true
  }
  return false
}
function isNull(txtName,msg)
{
  if(!trim(txtName.value) || txtName.value.length < 1)
  {
	alertStr += "Please enter "+msg + "\n";
    //alert("Please enter "+msg)
	txtName.focus()
    txtName.select()
    //return true	
  }
  return false
}

function checkDateParam(pMonth,pYear, msg)
{
  if(pMonth.value == "" && pYear.value != "")
  {
	alertStr += "Please enter month of " + msg + "\n";	
  }
  if(pMonth.value != "" && pYear.value == "")
  {
	alertStr += "Please enter year of " + msg + "\n";	
  }
  return false
}

function trim(input)
{
  var lre = /^\s*/;
  var rre = /\s*$/;
  input = input.replace(lre, "");
  input = input.replace(rre, "");
  return input;
}
function isEmailAddr(string)
{
	if(!trim(string.value) || string.value.length < 1)
	{
		alertStr += "Please enter Email address \n";
		string.focus()
		string.select()
		return true;
	}
    else 
    {
		if (string.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		{
			return true;
		} 
		else 
		{
			alertStr += "Please enter a valid Email address" + "\n";
			string.focus()
			//return false;
			return true;
		}
	}
}