// JavaScript Document


function listCat(pdId)
{
	with (window.document.frmModifyProduct) {
		if (cboCategory.selectedIndex == 0) {
			window.location.href = 'index.php';
		} else {
			window.location.href = 'index.php?views=modify&ac=' + cboCategory.options[cboCategory.selectedIndex].value + '&ap=' + pdId;
		}
	}
}

function listCon(catId, pdId)
{
	with (window.document.frmModifyProduct) {
		if (cboCountry.selectedIndex == 0) {
			window.location.href = 'index.php';
		} else {
			window.location.href = 'index.php?views=modify&con=' + cboCountry.options[cboCountry.selectedIndex].value + '&ac=' + catId + '&ap=' + pdId;
		}
	}
}

function listState(conId,catId,pdId)
{
	with (window.document.frmModifyProduct) {
		if (cboState.selectedIndex == 0) {
			window.location.href = 'index.php';
		} else {
			window.location.href = 'index.php?views=modify&cit=' + cboState.options[cboState.selectedIndex].value + '&con=' + conId + '&ac=' + catId + '&ap=' + pdId;
		}
	}
}

function listSub(ctyId,conId,catId,pdId)
{
	with (window.document.frmModifyProduct) {
		if (cboSuburb.selectedIndex == 0) {
			window.location.href = 'index.php';
		} else {
			window.location.href = 'index.php?views=modify&sub=' + cboSuburb.options[cboSuburb.selectedIndex].value + '&cit=' + ctyId + '&con=' + conId + '&ac=' + catId + '&ap=' + pdId;
		}
	}
}


function viewCat()
{
	with (window.document.frmaddAd) {
		if (cboAdListing.selectedIndex == 0) {
			window.location.href = 'index.php';
		} else {
			window.location.href = 'index.php?views=add&ac=' + cboAdListing.options[cboAdListing.selectedIndex].value;
		}
	}
}



function countryListing(catId)
{
	with (window.document.frmaddAd) {
		if (cboCountry.selectedIndex == 0) {
			window.location.href = 'index.php';
		} else {
			window.location.href = 'index.php?views=add&con=' + cboCountry.options[cboCountry.selectedIndex].value + '&ac=' + catId;
		}
	}
}

function stateListing(conId,catId)
{
	with (window.document.frmaddAd) {
		if (cboState.selectedIndex == 0) {
			window.location.href = 'index.php';
		} else {
			window.location.href = 'index.php?views=add&cit=' + cboState.options[cboState.selectedIndex].value + '&con=' + conId + '&ac=' + catId;
		}
	}
}

function subListing(ctyId,conId,catId)
{
	with (window.document.frmaddAd) {
		if (cboSuburb.selectedIndex == 0) {
			window.location.href = 'index.php';
		} else {
			window.location.href = 'index.php?views=add&sub=' + cboSuburb.options[cboSuburb.selectedIndex].value + '&cit=' + ctyId + '&con=' + conId + '&ac=' + catId;
		}
	}
}

function addProduct()
{
	window.location.href = 'index.php?views=list';
}

function modifyProducts()
{
	window.location.href = 'index.php?views=check';
}

function recPass()
{
	window.location.href = 'index.php?views=recover';
}


function check2()
{
    with (window.document.guestform) {
                gName=txtName;
                gSurname=txtSurname;
                gEmail=txtEmail;
               
		// if name is empty alert the visitor
                if(trim(gName.value) == '')
                {
                alert('Please enter your name');
                gName.focus();
                return false;
                }
                else if(trim(gSurname.value) == '')
                {
                alert('Please enter your last name');
                gSurname.focus();
                return false;
                }
                else if( !echeck(trim(gEmail.value)))
                {
                alert('Please enter a valid email address ');
                gEmail.focus();
                return false;
                }
                else
                {
      
                submit();
                }
	}
}

function check3()

{
  
  with (window.document.frmRecPassword) {

              rEmail=txtEmail;
              if( !echeck(trim(rEmail.value)))
                                               {
                                                alert('Please enter a valid email address ');
                                                rEmail.focus();
                                                return false;
                                               }else{
                                                submit();
                                               }   
                                                
   }
}


function check4()

{
  
  with (window.document.frmRecPass) {

              rEmail=txtEmail;
              if( !echeck(trim(rEmail.value)))
                                               {
                                                alert('Please enter a valid email address ');
                                                rEmail.focus();
                                                return false;
                                               }else{
                                                submit();
                                               }   
                                                
   }
}

function check5()
{
    with (window.document.guestform) {
                gUser=txtUser;
                gPassword=txtPassword;
                
               
		// if username is empty alert the visitor
                if(trim(gUser.value) == '')
                {
                alert('Please enter your Username');
                gUser.focus();
                return false;
                }
                else if(trim(gPassword.value) == '')
                {
                alert('Please enter your password');
                gPassword.focus();
                return false;
                }
                else
                {
      
                submit();
                }
	}
}


function checkAddProductForm()
{
	with (window.document.frmaddAd) {
		if (cboCountry.selectedIndex == 0) {
			alert('Choose your Country');
			cboCountry.focus();
			return;
                } else if (cboState.selectedIndex == 0) {
			alert('Choose your City');
			cboState.focus();
			return;
		} else if (isEmpty(txtName, 'Enter Your Ad Title')) {
			return;
                } else if (isEmpty(txtSkill, 'Enter Ad Description')) {
			return;
                } else if (isEmpty(fleImage, 'Please upload an image')) {
			return;
                } else if (isEmpty(txtcontact, 'Enter Your contact phone')) {
			return;
                
                } else if (isEmpty(txtEmail, 'Enter Your Email')) {
			return;
                } else if (isEmpty(txtUname, 'Select a user name')) {
			return;
                } else if (isEmpty(txtPassword, 'Select a password')) {
			return;
		} else {
			submit();
		}
	}
}

function checkModifyPdForm()
{
	with (window.document.frmModifyProduct) {
		if (cboCategory.selectedIndex == 0) {
			alert('Choose your product category');
			cboCategory.focus();
			return;
                } else if (isEmpty(txtName, 'Enter Your Name')) {
			return;
                } else if (isEmpty(txtEmail, 'Enter Your Email')) {
			return;
                } else if (cboCountry.selectedIndex == 0) {
			alert('Choose your country');
			cboCountry.focus();
                        return;
                } else if (cboState.selectedIndex == 0) {
			alert('Choose your city');
			cboState.focus();
                        return;
                } else if (isEmpty(txtDes, 'Enter product description')) {
			return;
                
		} else {
			submit();
		}
	}
}

function logincheck()
{
	with (window.document.loginform) {
		
	        if (isEmpty(txtUName, 'Enter Your username')) {
			return;
                } else if (isEmpty(txtPassword, 'Enter Your password')) {
			return;
                
		} else {
			submit();
		}
	}
}

function deleteImage(pdId)
{
	if (confirm('Delete this image')) {
		window.location.href = 'processPd.php?action=deleteImage&pdId=' + pdId;
	}
}


function deleteChildImg(pdId,tnId)
{
	if (confirm('Delete this image')) {
		window.location.href = 'processPd.php?action=deleteChildImg&tnId=' + tnId + '&pdId=' + pdId;
	}
}

function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail Address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail Address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

 		 return true					
	}


function checkSendEmailForm2() 
{
	with (window.document.frmSendEmail) {
		if (isEmpty(txtSEmail, 'Enter Sender Email Address')) {
			txtSEmail.focus();
			return;
                } else if (isEmpty(txtSubject, 'Enter Email Subject')) {
                        txtSubject.focus();
			return;
                } else if (isEmpty(mtxMessage, 'Enter Email Message')) {
                        mtxMessage.focus();
			return;
		} else {
			submit();
		}
	}
}






