
// Check if INT
function isInt(x) 
{ 
	var y=parseInt(x); 
	if (isNaN(y)) return false; 
	return x==y && x.toString()==y.toString(); 
} 

/* ** LOGIN DETAILS *************************************************** */
function toggleLogin()
{
	obj=document.getElementById('loginContainer');
	objButton=document.getElementById('loginOpenButton');
	if (obj.style.display=='block')
	{
		obj.style.display='none';
		objButton.innerHTML='Show';
	} else {
		obj.style.display='block';
		objButton.innerHTML='Hide';
	}
}

String.prototype.stripHTML = function()
{
	// What a tag looks like
	var matchTag = /<(?:.|\s)*?".">/g;
	// Replace the tag
	return this.replace(matchTag, "");
};

function getValueById(id)
{
	obj=document.getElementById(id);
	return obj.value.stripHTML();
}

function putHtmlById(id,value)
{
	obj=document.getElementById(id);
	obj.innerHTML=value;
}			
		
var loginEditable=false;
function loginDetailsToggle()
{	
	if(loginEditable)
	{
		// Save
		emailIn=getValueById('email');
		password1In=getValueById('password1');
		password2In=getValueById('password2');
		
		// Check vars
		foundErrors=false;
		errorMessage='';
		if(password1In!=password2In)
		{
			foundErrors=true;
			errorMessage=errorMessage+'Passwords not identical';
		} else {
			if (password1In.length<5)
			{
				errorMessage=errorMessage+'Passwords not long enough';
			}
		}
		
		if (!foundErrors)
		{
			loginEditable=false;

			putHtmlById('emailField',emailIn);
			putHtmlById('passwordField','******');
			putHtmlById('loginDetailsButton','Edit');
			putHtmlById('passwordLabel','Password');
			// Save object: 
			saveObject(Array('loginDetails',emailIn,password1In,password2In));
		} else {
			loginEditable=true;
			alert('Error: '+errorMessage+' [1a]');
		}
	 } else {
		//Make editable
		loginEditable=true;
		obj1=document.getElementById('emailField');
		
		putHtmlById('emailField','<input type="text" name="email" id="email" value="'+obj1.innerHTML+'" style="width: 223px; position: relative; left: 16px;">');
		putHtmlById('passwordField','<input type="password" name="password1" id="password1" value="******" style="margin-bottom: 4px; width: 223px; position: relative; left: 16px;"><br><input type="password" name="password2" id="password2" value="******" style="margin-bottom: 4px; width: 223px; position: relative; left: 16px;">');
		putHtmlById('passwordLabel','Password<br><div style="margin-top: 6px;">Retype Password:</div>');
		putHtmlById('loginDetailsButton','Save');
	 }
}

/* ** NAME/ADDRESS *************************************************** */
function toggleNameAddress()
{
	obj=document.getElementById('nameaddressContainer');
	objButton=document.getElementById('nameOpenButton');
	if (obj.style.display=='block')
	{
		obj.style.display='none';
		objButton.innerHTML='Show';
	} else {
		obj.style.display='block';
		objButton.innerHTML='Hide';
	}
}

/* ** BILL ADDRESS *************************************************** */
function toggleBilling()
{
	obj=document.getElementById('billingContainer');
	objButton=document.getElementById('billOpenButton');
	if (obj.style.display=='block')
	{
		obj.style.display='none';
		objButton.innerHTML='Show';
	} else {
		obj.style.display='block';
		objButton.innerHTML='Hide';
	}
}

 var billingEditable=false;
var simpleBillFields=Array('bill_firstName','bill_middleName','bill_lastName','bill_company','bill_street','bill_zipCode','bill_number','bill_city','bill_state','bill_email');

function billingDetailsToggle()
{	
if(billingEditable)
{
	// Save \n
	bill_firstNameIn=getValueById('bill_firstName');
	bill_middleNameIn=getValueById('bill_middleName');
	bill_lastNameIn=getValueById('bill_lastName');
	bill_streetIn=getValueById('bill_street');
	bill_zipCodeIn=getValueById('bill_zipCode');
	bill_numberIn=getValueById('bill_number');
	bill_cityIn=getValueById('bill_city');
	bill_stateIn=getValueById('bill_state');
	bill_emailIn=getValueById('bill_email');
	obj=document.getElementById('noUseBillAddress');
	if (obj.checked)
	{
		noUseBillAddressIn=1;
	} else {
		noUseBillAddressIn=0;
	}
	
	// Check vars
	foundErrors=false;
	errorMessage='';

	//*** Error checking: 
	
	
	if (!foundErrors)
	{
		saveObject(Array('billingDetails',bill_firstNameIn,bill_middleNameIn,bill_lastNameIn,bill_streetIn,bill_zipCodeIn,bill_numberIn,bill_cityIn,bill_stateIn,bill_emailIn,noUseBillAddressIn));
	
		billingEditable=false;
		for ( var i=0, len=simpleBillFields.length; i<len; ++i )
		{
			currentId=simpleBillFields[i];
			putHtmlById(currentId+'Field',getValueById(currentId));
		}
		
		if (noUseBillAddressIn==1)
		{
			putHtmlById('billCheckBoxContainer','[X]');					
		} else {
			putHtmlById('billCheckBoxContainer','[&nbsp;]');					
		}
		putHtmlById('billingButton','Edit');
		
	} else {
		billingEditable=true;
		alert(errorMessage);
	}
} else {
	//Make editable
	billingEditable=true;  
	for ( var i=0, len=simpleBillFields.length; i<len; ++i )
	{
		currentId=simpleBillFields[i];
		obj1=document.getElementById(currentId+'Field');
		value=obj1.innerHTML;
		value=value.replace(/"/,'&quot;');
		putHtmlById(currentId+'Field','<input type="text" name="'+currentId+'" id="'+currentId+'" value="'+value+'" style="width: 223px; position: relative; left: 16px;">');
	}
	checkObj=document.getElementById('billCheckBoxContainer'); 
	checkedHtml=checkObj.innerHTML; 
	
	checked = '';
	if (checkedHtml=='[X]') { checked = 'checked'; } 
	
	putHtmlById('billCheckBoxContainer','<input type="checkbox" id="noUseBillAddress" name="noUseBillAddress" '+checked+'>');					
	putHtmlById('billingButton','Save');
 }
}

/* ** Purchases *************************************************** */
 function togglePurchases()
{
	obj=document.getElementById('purchaseContainer');
	objButton=document.getElementById('purchaseOpenButton');
	if (obj.style.display=='block')
	{
		obj.style.display='none';
		objButton.innerHTML='Show';
	} else {
		obj.style.display='block';
		objButton.innerHTML='Hide';
	}
}


 var profileEditable=false;
			  var simpleProfileFields=Array('profileName','profileWebsite','profileEmail','profileCity');
			  
			  function profileToggle()
			  {	
				if(profileEditable)
				{
					// Save \n
					profileNameIn   =getValueById('profileName');
					profileWebsiteIn=getValueById('profileWebsite');
					profileEmailIn  =getValueById('profileEmail');
					profileCityIn   =getValueById('profileCity');
					profileDescIn   =getValueById('profileDesc');
					
					// Check vars
					foundErrors=false;
					errorMessage='';

					//*** Error checking: 
					
					if (!foundErrors)
					{
						saveObject(Array('profile',profileNameIn,profileWebsiteIn,profileEmailIn,profileCityIn,profileDescIn));
					
						profileEditable=false;
						for ( var i=0, len=simpleProfileFields.length; i<len; ++i )
						{
							currentId=simpleProfileFields[i];
							putHtmlById(currentId+'Field',getValueById(currentId));
						}
						putHtmlById('profileDescField',profileDescIn);
						putHtmlById('profileButton','Edit');
						
					} else {
						profileEditable=true;
						alert(errorMessage);
					}
				} else {
					//Make editable
					profileEditable=true;  
					for ( var i=0, len=simpleProfileFields.length; i<len; ++i )
					{
						currentId=simpleProfileFields[i];
						obj1=document.getElementById(currentId+'Field');
						value=obj1.innerHTML;
						value=value.replace(/"/,'&quot;');
						putHtmlById(currentId+'Field','<input type="text" name="'+currentId+'" id="'+currentId+'" value="'+value+'" >');
					}
					//Textarea:
					obj1=document.getElementById('profileDescField');
					value=obj1.innerHTML;
					putHtmlById('profileDescField','<textarea name="profileDesc" style="width: 311px;" id="profileDesc">'+value+'</textarea>');
				
					putHtmlById('profileButton','Save');
				 }
			  }

 function togglePortfolio()
			   {
					obj=document.getElementById('portfolioContainer');
					objButton=document.getElementById('portfolioOpenButton');
					if (obj.style.display=='block')
					{
						obj.style.display='none';
						objButton.innerHTML='Show';
					} else {
						obj.style.display='block';
						objButton.innerHTML='Hide';
					}
			   }			  
			
